diff --git a/packages/spacecat-shared-data-access/src/service/audits/accessPatterns.js b/packages/spacecat-shared-data-access/src/service/audits/accessPatterns.js index 7c23dd31..8be13776 100644 --- a/packages/spacecat-shared-data-access/src/service/audits/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/audits/accessPatterns.js @@ -151,17 +151,12 @@ export const getLatestAuditForSite = async ( siteId, auditType, ) => { - const latestAudit = await dynamoClient.query({ - TableName: config.tableNameLatestAudits, - KeyConditionExpression: 'siteId = :siteId AND auditType = :auditType', - ExpressionAttributeValues: { - ':siteId': siteId, - ':auditType': `${auditType}`, - }, - Limit: 1, + const latestAudit = await dynamoClient.getItem(config.tableNameLatestAudits, { + siteId, + auditType, }); - return latestAudit.length > 0 ? AuditDto.fromDynamoItem(latestAudit[0]) : null; + return latestAudit ? AuditDto.fromDynamoItem(latestAudit) : null; }; /** diff --git a/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js index 5257c6d4..ac32dbf6 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/audits/index.test.js @@ -73,6 +73,7 @@ describe('Audit Access Pattern Tests', () => { beforeEach(() => { mockDynamoClient = { query: sinon.stub().returns(Promise.resolve([])), + getItem: sinon.stub().resolves(), removeItem: sinon.stub().resolves(), }; mockLog = { @@ -89,16 +90,16 @@ describe('Audit Access Pattern Tests', () => { expect(mockDynamoClient.query.called).to.be.true; }); - it('calls getLatestAudits and return an array', async () => { + it('calls getLatestAudits and returns an array', async () => { const result = await exportedFunctions.getLatestAudits('auditType', true); expect(result).to.be.an('array'); expect(mockDynamoClient.query.called).to.be.true; }); - it('calls getLatestAuditForSite and return an array', async () => { + it('calls getLatestAuditForSite and returns null', async () => { const result = await exportedFunctions.getLatestAuditForSite('siteId', 'auditType'); expect(result).to.be.null; - expect(mockDynamoClient.query.called).to.be.true; + expect(mockDynamoClient.getItem.called).to.be.true; }); }); 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 51e51e54..e442daa3 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 @@ -177,7 +177,7 @@ describe('Site Access Pattern Tests', () => { baseURL: 'https://example.com', }]; - const mockLatestAuditData = [{ + const mockLatestAuditData = { siteId: 'site1', auditType: 'lhs-mobile', auditedAt: new Date().toISOString(), @@ -190,10 +190,10 @@ describe('Site Access Pattern Tests', () => { }, }, fullAuditRef: 'https://example.com', - }]; + }; mockDynamoClient.query.onFirstCall().resolves(mockSiteData); - mockDynamoClient.query.onSecondCall().resolves(mockLatestAuditData); + mockDynamoClient.getItem.onFirstCall().resolves(mockLatestAuditData); const result = await exportedFunctions.getSiteByBaseURLWithAuditInfo('https://example.com', 'lhs-mobile', true); const audits = result.getAudits(); @@ -201,11 +201,11 @@ describe('Site Access Pattern Tests', () => { const audit = audits[0]; expect(audit.getId()).to.be.a('string').that.is.not.empty; - expect(audit.getSiteId()).to.equal(mockLatestAuditData[0].siteId); - expect(audit.getAuditType()).to.equal(mockLatestAuditData[0].auditType); - expect(audit.getAuditedAt()).to.equal(mockLatestAuditData[0].auditedAt); - expect(audit.getAuditResult()).to.deep.equal(mockLatestAuditData[0].auditResult); - expect(audit.getFullAuditRef()).to.equal(mockLatestAuditData[0].fullAuditRef); + expect(audit.getSiteId()).to.equal(mockLatestAuditData.siteId); + expect(audit.getAuditType()).to.equal(mockLatestAuditData.auditType); + expect(audit.getAuditedAt()).to.equal(mockLatestAuditData.auditedAt); + expect(audit.getAuditResult()).to.deep.equal(mockLatestAuditData.auditResult); + expect(audit.getFullAuditRef()).to.equal(mockLatestAuditData.fullAuditRef); }); it('calls getSiteByBaseURLWithAuditInfo and assigns all audits when latestOnly is false', async () => {