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 ecfc898ec..34bfb45c9 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js @@ -55,7 +55,10 @@ export const getSitesToAudit = async (dynamoClient, config) => { }; /** - * Retrieves sites with their latest audit of a specified type. + * Retrieves sites with their latest audit of a specified type. If there is + * no audit of the specified type for a site, the site will be returned with + * an empty audits array. + * The audits are sorted ascending or descending by scores. * * @param {DynamoDbClient} dynamoClient - The DynamoDB client. * @param {DataAccessConfig} config - The data access config. @@ -78,16 +81,17 @@ export const getSitesWithLatestAudit = async ( getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending), ]); - const sitesMap = new Map(sites.map((site) => [site.getId(), site])); + const auditsMap = new Map(latestAudits.map((audit) => [audit.getSiteId(), audit])); - return latestAudits.reduce((result, audit) => { - const site = sitesMap.get(audit.getSiteId()); - if (site) { + return sites.map((site) => { + const audit = auditsMap.get(site.getId()); + if (audit) { site.setAudits([audit]); - result.push(site); + } else { + site.setAudits([]); } - return result; - }, []); + return site; + }); }; /** 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 ed7fdacc1..32252e8c7 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -113,12 +113,17 @@ describe('DynamoDB Integration Test', async () => { it('gets sites with latest audit', async () => { const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE); - // Every tenth site will not have any audits - expect(sites.length).to.equal(NUMBER_OF_SITES - 1); + expect(sites.length).to.equal(NUMBER_OF_SITES); - sites.forEach((site) => { + sites.forEach((site, index) => { checkSite(site); - expect(site.getAudits()).to.be.an('array').that.has.lengthOf(1); + expect(site.getAudits()).to.be.an('array'); + + // Every tenth site will not have any audits + if (index % 10 === 0) { + expect(site.getAudits()).to.be.an('array').that.is.empty; + } + site.getAudits().forEach((audit) => { expect(audit.getAuditType()).to.equal(AUDIT_TYPE_LHS_MOBILE); expect(Object.keys(audit.getScores())).to.have.members( 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 7e8d671ad..275df6015 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 @@ -149,7 +149,8 @@ describe('Site Access Pattern Tests', () => { mockDynamoClient.query.onSecondCall().resolves(mockAuditData); const result = await exportedFunctions.getSitesWithLatestAudit('auditType'); - expect(result).to.be.an('array').that.is.empty; + expect(result).to.be.an('array').that.has.lengthOf(1); + expect(result[0].getAudits()).to.be.an('array').that.is.empty; }); it('calls getSiteByBaseURL and returns null', async () => {