Skip to content

Commit 5d0b00f

Browse files
committed
fix: removeSite, add integration test
1 parent 5c24998 commit 5d0b00f

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/spacecat-shared-data-access/src/service/audits/accessPatterns.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ export const addAudit = async (dynamoClient, log, auditData) => {
189189
return audit;
190190
};
191191

192-
async function removeAudits(dynamoClient, audits) {
192+
async function removeAudits(dynamoClient, audits, latest = false) {
193+
const tableName = latest ? TABLE_NAME_LATEST_AUDITS : TABLE_NAME_AUDITS;
193194
// TODO: use batch-remove (needs dynamo client update)
194-
const removeAuditPromises = audits.map((audit) => dynamoClient.removeItem({
195-
TableName: TABLE_NAME_AUDITS,
196-
Key: {
197-
siteId: audit.getSiteId(),
198-
auditedAt: audit.getAuditedAt(),
199-
},
195+
const removeAuditPromises = audits.map((audit) => dynamoClient.removeItem(tableName, {
196+
siteId: audit.getSiteId(),
197+
SK: `${audit.getAuditType()}#${audit.getAuditedAt()}`,
200198
}));
201199

202200
await Promise.all(removeAuditPromises);
@@ -216,7 +214,7 @@ export const removeAuditsForSite = async (dynamoClient, log, siteId) => {
216214
const latestAudits = await getLatestAuditsForSite(dynamoClient, log, siteId);
217215

218216
await removeAudits(dynamoClient, audits);
219-
await removeAudits(dynamoClient, latestAudits);
217+
await removeAudits(dynamoClient, latestAudits, true);
220218
} catch (error) {
221219
log.error(`Error removing audits for site ${siteId}: ${error.message}`);
222220
throw error;

packages/spacecat-shared-data-access/src/service/sites/accessPatterns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export const removeSite = async (dynamoClient, log, siteId) => {
254254
try {
255255
await removeAuditsForSite(dynamoClient, log, siteId);
256256

257-
await dynamoClient.removeItem(TABLE_NAME_SITES, { siteId });
257+
await dynamoClient.removeItem(TABLE_NAME_SITES, { id: siteId });
258258
} catch (error) {
259259
log.error(`Error removing site: ${error.message}`);
260260
throw error;

packages/spacecat-shared-data-access/test-it/db.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,20 @@ describe('DynamoDB Integration Test', async () => {
330330
// Try to add the same audit again
331331
await expect(dataAccess.addAudit(auditData)).to.be.rejectedWith('Audit already exists');
332332
});
333+
334+
it('successfully removes a site and its related audits', async () => {
335+
const siteToRemove = await dataAccess.getSiteByBaseURL('https://example1.com');
336+
const siteId = siteToRemove.getId();
337+
338+
await expect(dataAccess.removeSite(siteId)).to.eventually.be.fulfilled;
339+
340+
const siteAfterRemoval = await dataAccess.getSiteByBaseURL('https://example1.com');
341+
expect(siteAfterRemoval).to.be.null;
342+
343+
const auditsAfterRemoval = await dataAccess.getAuditsForSite(siteId);
344+
expect(auditsAfterRemoval).to.be.an('array').that.is.empty;
345+
346+
const latestAuditAfterRemoval = await dataAccess.getLatestAuditForSite(siteId, AUDIT_TYPE_LHS);
347+
expect(latestAuditAfterRemoval).to.be.null;
348+
});
333349
});

0 commit comments

Comments
 (0)