From 72895da79384bea6b93de00dbf9eec80fcde6d96 Mon Sep 17 00:00:00 2001 From: alinarublea Date: Mon, 29 Jan 2024 11:31:52 +0100 Subject: [PATCH] feat: add getSitesByOrganizationIDWithLatestAudits query --- package-lock.json | 4 +- .../src/service/sites/accessPatterns.js | 45 +++++++++++++++++++ .../src/service/sites/index.js | 14 +++++- .../test/it/db.test.js | 20 +++++++++ .../test/unit/service/index.test.js | 1 + .../test/unit/service/sites/index.test.js | 33 ++++++++++++++ 6 files changed, 114 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index bb56dba7..5df7acec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12762,7 +12762,7 @@ }, "packages/spacecat-shared-data-access": { "name": "@adobe/spacecat-shared-data-access", - "version": "1.10.4", + "version": "1.10.5", "license": "Apache-2.0", "dependencies": { "@adobe/spacecat-shared-dynamo": "1.2.5", @@ -13267,7 +13267,7 @@ }, "packages/spacecat-shared-dynamo": { "name": "@adobe/spacecat-shared-dynamo", - "version": "1.3.5", + "version": "1.3.6", "license": "Apache-2.0", "dependencies": { "@adobe/spacecat-shared-utils": "1.1.0", diff --git a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js index 1b3464c6..1753fcf5 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js @@ -266,6 +266,51 @@ export const getSitesByOrganizationID = async ( return dynamoItems.map((dynamoItem) => SiteDto.fromDynamoItem(dynamoItem)); }; +/** + * Retrieves sites by organizationId with their latest audit. Sites without a latest + * audit will not be included in the result. + * The sites are sorted by their latest audit scores in ascending order by default. + * The sortAuditsAscending parameter can be used to change the sort order. + * @param {DynamoDbClient} dynamoClient - The DynamoDB client. + * @param {DataAccessConfig} config - The data access config. + * @param {Logger} log - The logger. + * @param {string} auditType - The type of audits to retrieve for the sites. + * @param {string} organizationId - The organizationId to retrieve the sites. + * @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in + * ascending order. + * to retrieve. + * @return {Promise[]>} A promise that resolves to an array of sites with their + * latest audit. + */ +export const getSitesByOrganizationIDWithLatestAudits = async ( + dynamoClient, + config, + log, + organizationId, + auditType, + sortAuditsAscending = true, +) => { + const [sites, latestAudits] = await Promise.all([ + getSitesByOrganizationID(dynamoClient, config, organizationId), + getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending), + ]); + + const sitesMap = new Map(sites.map((site) => [site.getId(), site])); + const orderedSites = []; + + // First, append sites with a latest audit in the sorted order + latestAudits.forEach((audit) => { + const site = sitesMap.get(audit.getSiteId()); + if (site) { + site.setAudits([audit]); + orderedSites.push(site); + sitesMap.delete(site.getId()); // Remove the site from the map to avoid adding it again + } + }); + + return orderedSites; +}; + /** * Retrieves a site by its ID. * diff --git a/packages/spacecat-shared-data-access/src/service/sites/index.js b/packages/spacecat-shared-data-access/src/service/sites/index.js index 231088a0..07e0b471 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/index.js +++ b/packages/spacecat-shared-data-access/src/service/sites/index.js @@ -19,7 +19,7 @@ import { getSiteByID, getSites, getSitesByDeliveryType, - getSitesByOrganizationID, + getSitesByOrganizationID, getSitesByOrganizationIDWithLatestAudits, getSitesToAudit, getSitesWithLatestAudit, removeSite, @@ -58,6 +58,18 @@ export const siteFunctions = (dynamoClient, config, log) => ({ sortAuditsAscending, deliveryType, ), + getSitesByOrganizationIDWithLatestAudits: ( + organizationId, + auditType, + sortAuditsAscending, + ) => getSitesByOrganizationIDWithLatestAudits( + dynamoClient, + config, + log, + organizationId, + auditType, + sortAuditsAscending, + ), getSiteByBaseURL: (baseUrl) => getSiteByBaseURL( dynamoClient, config, diff --git a/packages/spacecat-shared-data-access/test/it/db.test.js b/packages/spacecat-shared-data-access/test/it/db.test.js index e82fc456..f36d7d39 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -250,6 +250,26 @@ describe('DynamoDB Integration Test', async () => { }); }); + it('gets sites by organization ID with latest audit', async () => { + const organizations = await dataAccess.getOrganizations(); + const sites = await dataAccess.getSitesByOrganizationIDWithLatestAudits( + organizations[0].getId(), + AUDIT_TYPE_LHS_MOBILE, + ); + + sites.forEach((site) => { + checkSite(site); + expect(site.getAudits()).to.be.an('array'); + + site.getAudits().forEach((audit) => { + expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE); + expect(Object.keys(audit.getScores())).to.have.members( + ['performance', 'seo', 'accessibility', 'best-practices'], + ); + }); + }); + }); + it('gets sites with latest audit of delivery type', async () => { const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE, true, 'aem_cs'); diff --git a/packages/spacecat-shared-data-access/test/unit/service/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/index.test.js index 6af72b10..472a2578 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/index.test.js @@ -35,6 +35,7 @@ describe('Data Access Object Tests', () => { 'getSitesByOrganizationID', 'getSitesToAudit', 'getSitesWithLatestAudit', + 'getSitesByOrganizationIDWithLatestAudits', 'getSiteByBaseURL', 'getSiteByBaseURLWithAuditInfo', 'getSiteByBaseURLWithAudits', diff --git a/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js index bd1dfc8b..2ad03c2b 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/sites/index.test.js @@ -67,6 +67,11 @@ describe('Site Access Pattern Tests', () => { expect(exportedFunctions.getSitesWithLatestAudit).to.be.a('function'); }); + it('exports getSitesByOrganizationIDWithLatestAudits function', () => { + expect(exportedFunctions).to.have.property('getSitesByOrganizationIDWithLatestAudits'); + expect(exportedFunctions.getSitesByOrganizationIDWithLatestAudits).to.be.a('function'); + }); + it('exports getSiteByBaseURL function', () => { expect(exportedFunctions).to.have.property('getSiteByBaseURL'); expect(exportedFunctions.getSiteByBaseURL).to.be.a('function'); @@ -167,6 +172,34 @@ describe('Site Access Pattern Tests', () => { expect(result).to.be.an('array').that.has.lengthOf(1); }); + it('calls getSitesByOrganizationIDWithLatestAudit and handles latestAudits', async () => { + const mockSiteData = [{ + id: 'site1', + organizationId: 'org1', + baseURL: 'https://example.com', + }]; + + const mockAuditData = [{ + siteId: 'site1', + auditType: '404', + auditedAt: new Date().toISOString(), + auditResult: { + result: { + views: 1000, + source: 'https://abc.com', + target: 'https://abc.com/pay', + }, + }, + fullAuditRef: 'https://example.com', + }]; + + mockDynamoClient.query.onFirstCall().resolves(mockSiteData); + mockDynamoClient.query.onSecondCall().resolves(mockAuditData); + + const result = await exportedFunctions.getSitesByOrganizationIDWithLatestAudits('404'); + expect(result).to.be.an('array').that.has.lengthOf(1); + }); + it('calls getSitesWithLatestAudit and handles empty latestAudits', async () => { const mockSiteData = [{ id: 'site1',