diff --git a/packages/spacecat-shared-data-access/src/index.d.ts b/packages/spacecat-shared-data-access/src/index.d.ts index 45b74db61..79533fbac 100644 --- a/packages/spacecat-shared-data-access/src/index.d.ts +++ b/packages/spacecat-shared-data-access/src/index.d.ts @@ -249,6 +249,7 @@ export interface DataAccess { getSitesWithLatestAudit: ( auditType: string, sortAuditsAscending?: boolean, + deliveryType?: string, ) => Promise; getSiteByBaseURL: ( baseUrl: string, 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 6e4beecfb..e10a72870 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js @@ -50,6 +50,10 @@ export const getSites = async (dynamoClient, config) => { * specified delivery type. */ export const getSitesByDeliveryType = async (dynamoClient, config, deliveryType) => { + if (deliveryType === 'all') { + return getSites(dynamoClient, config); + } + const dynamoItems = await dynamoClient.query({ TableName: config.tableNameSites, IndexName: config.indexNameAllSitesByDeliveryType, @@ -87,6 +91,9 @@ export const getSitesToAudit = async (dynamoClient, config) => { * @param {Logger} log - The logger. * @param {string} auditType - The type of audits to retrieve for the sites. * @param {boolean} [sortAuditsAscending=true] - Determines if the audits should be sorted in + * ascending order. + * @param {string} [deliveryType=DEFAULT_DELIVERY_TYPE] - The delivery type of the sites + * to retrieve. * @return {Promise[]>} A promise that resolves to an array of sites with their * latest audit. */ @@ -96,9 +103,10 @@ export const getSitesWithLatestAudit = async ( log, auditType, sortAuditsAscending = true, + deliveryType = 'all', ) => { const [sites, latestAudits] = await Promise.all([ - getSites(dynamoClient, config), + getSitesByDeliveryType(dynamoClient, config, deliveryType), getLatestAudits(dynamoClient, config, log, auditType, sortAuditsAscending), ]); 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 bb859dc6c..7b621e906 100644 --- a/packages/spacecat-shared-data-access/src/service/sites/index.js +++ b/packages/spacecat-shared-data-access/src/service/sites/index.js @@ -37,12 +37,17 @@ export const siteFunctions = (dynamoClient, config, log) => ({ dynamoClient, config, ), - getSitesWithLatestAudit: (auditType, sortAuditsAscending) => getSitesWithLatestAudit( + getSitesWithLatestAudit: ( + auditType, + sortAuditsAscending, + deliveryType, + ) => getSitesWithLatestAudit( dynamoClient, config, log, auditType, sortAuditsAscending, + deliveryType, ), getSiteByBaseURL: (baseUrl) => getSiteByBaseURL( dynamoClient, 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 0e8c2885b..b525890c3 100644 --- a/packages/spacecat-shared-data-access/test/it/db.test.js +++ b/packages/spacecat-shared-data-access/test/it/db.test.js @@ -150,6 +150,25 @@ describe('DynamoDB Integration Test', async () => { }); }); + it('gets sites with latest audit of delivery type', async () => { + const sites = await dataAccess.getSitesWithLatestAudit(AUDIT_TYPE_LHS_MOBILE, true, 'aem_cs'); + + expect(sites.length).to.equal(NUMBER_OF_SITES / 2); + + sites.forEach((site) => { + checkSite(site); + expect(site.getDeliveryType()).to.equal('aem_cs'); + 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 site by baseURL', async () => { const site = await dataAccess.getSiteByBaseURL('https://example1.com'); 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 1a61626b5..3347844be 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 @@ -121,6 +121,12 @@ describe('Site Access Pattern Tests', () => { expect(mockDynamoClient.query.called).to.be.true; }); + it('calls getSitesWithLatestAudit of delivery type and returns an array', async () => { + const result = await exportedFunctions.getSitesWithLatestAudit('lhs-mobile', true, 'aem_edge'); + expect(result).to.be.an('array'); + expect(mockDynamoClient.query.called).to.be.true; + }); + it('calls getSitesWithLatestAudit and handles latestAudits', async () => { const mockSiteData = [{ id: 'site1',