Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce update latest audit #316

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ export interface DataAccess {
removeAuditsForSite: (
siteId: string,
) => Promise<void>;
updateLatestAudit: (
audit: Audit,
) => Promise<Audit>;
getSites: () => Promise<Site[]>;
getSitesByDeliveryType: (
deliveryType: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,47 @@ async function removeAudits(
await Promise.all(removeAuditPromises);
}

/**
* Updates existing latest audit.
* This can be used for adding suggestions for example.
* @param dynamoClient - The DynamoDB client.
* @param config - The data access config.
* @param log - The logger.
* @param auditData - The audit data.
* @returns {Promise<Readonly<Audit>>}
*/
export const updateLatestAudit = async (
dynamoClient,
config,
log,
auditData,
) => {
const newAudit = createAudit(auditData);
const existingAudit = await getLatestAuditForSite(
dynamoClient,
config,
log,
newAudit.getSiteId(),
newAudit.getAuditType(),
);

if (!isObject(existingAudit)) {
throw new Error('Audit to update not found');
}

await dynamoClient.putItem(
config.tableNameLatestAudits,
AuditDto.toDynamoItem(newAudit, true),
);

await dynamoClient.putItem(
config.tableNameAudits,
AuditDto.toDynamoItem(newAudit),
);

return newAudit;
};

/**
* Removes all audits for a specified site and the latest audit entry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getLatestAudits,
getLatestAuditsForSite,
removeAuditsForSite,
updateLatestAudit,
} from './accessPatterns.js';

export const auditFunctions = (dynamoClient, config, log) => ({
Expand Down Expand Up @@ -69,4 +70,10 @@ export const auditFunctions = (dynamoClient, config, log) => ({
log,
siteId,
),
updateLatestAudit: (auditData) => updateLatestAudit(
dynamoClient,
config,
log,
auditData,
),
});
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,58 @@ describe('Audit Access Pattern Tests', () => {

await expect(exportedFunctions.removeAuditsForSite('some-id')).to.be.rejectedWith(errorMessage);
});

it('successfully updates the latest audit', async () => {
const mockAuditData = {
siteId: 'siteId',
auditType: 'broken-backlinks',
auditedAt: new Date().toISOString(),
fullAuditRef: 'https://someurl.com',
auditResult: {
brokenBacklinks: [
{
title: 'Broken Link 1',
url_to: 'https://brokenlink1.com',
url_from: 'https://site1.com',
traffic_domain: 123,
},
{
title: 'Broken Link 2',
url_to: 'https://brokenlink2.com',
url_from: 'https://site2.com',
traffic_domain: 456,
},
],
},
};
const updatedAuditData = {
...mockAuditData,
auditResult: {
...mockAuditData.auditResult,
brokenBacklinks: mockAuditData.auditResult.brokenBacklinks.map((backlink) => ({
...backlink,
urls_suggested: ['https://suggested.com', 'https://suggested2.com', 'https://suggested3.com'],
})),
},
};

mockDynamoClient.getItem.resolves(mockAuditData);

await exportedFunctions.updateLatestAudit(updatedAuditData);

expect(mockDynamoClient.putItem.calledTwice).to.be.true;
});

it('should throw an error if the latest audit was not found', async () => {
const audit = {
siteId: 'siteId',
auditType: 'broken-backlinks',
auditedAt: new Date().toISOString(),
auditResult: {},
fullAuditRef: 'https://someurl.com',
};

await expect(exportedFunctions.updateLatestAudit(audit)).to.be.rejectedWith('Audit to update not found');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Data Access Object Tests', () => {
'getLatestAuditForSite',
'getLatestAuditsForSite',
'removeAuditsForSite',
'updateLatestAudit',
];
const siteFunctions = [
'addSite',
Expand Down
Loading