Skip to content

Commit

Permalink
feat: add getSitesByOrganizationIDWithLatestAudits query
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Jan 29, 2024
1 parent c9fc3d1 commit 72895da
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Readonly<Site>[]>} 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.
*
Expand Down
14 changes: 13 additions & 1 deletion packages/spacecat-shared-data-access/src/service/sites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getSiteByID,
getSites,
getSitesByDeliveryType,
getSitesByOrganizationID,
getSitesByOrganizationID, getSitesByOrganizationIDWithLatestAudits,
getSitesToAudit,
getSitesWithLatestAudit,
removeSite,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Data Access Object Tests', () => {
'getSitesByOrganizationID',
'getSitesToAudit',
'getSitesWithLatestAudit',
'getSitesByOrganizationIDWithLatestAudits',
'getSiteByBaseURL',
'getSiteByBaseURLWithAuditInfo',
'getSiteByBaseURLWithAudits',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 72895da

Please sign in to comment.