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 opportunity query by siteId, type and status #463

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class OpportunityCollection extends BaseCollection {

return this.findByIndexKeys({ siteId, status });
}

async allBySiteIdAndTypeAndStatus(siteId, type, status) {
if (!hasText(siteId)) {
throw new Error('SiteId is required');
}

if (!hasText(type)) {
throw new Error('Type is required');
}

if (!hasText(status)) {
throw new Error('Status is required');
}

return this.findByIndexKeys({ siteId, type, status });
}
}

export default OpportunityCollection;
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ const OpportunitySchema = {
composite: ['updatedAt'],
},
},
bySiteIdAndTypeAndStatus: {
index: 'spacecat-data-opportunity-by-site-and-type-and-status',
pk: {
field: 'gsi3pk',
composite: ['siteId', 'type', 'status'],
},
sk: {
field: 'gsi3sk',
composite: ['updatedAt'],
},
},
},
};

Expand Down
6 changes: 6 additions & 0 deletions packages/spacecat-shared-data-access/test/it/v2/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const removeElectroProperties = (record) => { /* eslint-disable no-underscore-da
delete cleanedRecord.gsi1sk;
delete cleanedRecord.gsi2pk;
delete cleanedRecord.gsi2sk;
delete cleanedRecord.gsi3pk;
delete cleanedRecord.gsi3sk;
delete cleanedRecord.__edb_e__;
delete cleanedRecord.__edb_v__;

Expand Down Expand Up @@ -309,6 +311,8 @@ describe('Opportunity & Suggestion IT', function () {
delete record.gsi1sk;
delete record.gsi2pk;
delete record.gsi2sk;
delete record.gsi3pk;
delete record.gsi3sk;
// eslint-disable-next-line no-underscore-dangle
delete record.__edb_e__;
// eslint-disable-next-line no-underscore-dangle
Expand Down Expand Up @@ -498,6 +502,8 @@ describe('Opportunity & Suggestion IT', function () {
delete record.gsi1sk;
delete record.gsi2pk;
delete record.gsi2sk;
delete record.gsi3pk;
delete record.gsi3sk;
// eslint-disable-next-line no-underscore-dangle
delete record.__edb_e__;
// eslint-disable-next-line no-underscore-dangle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const mockElectroService = {
query: {
bySiteId: stub(),
bySiteIdAndStatus: stub(),
bySiteIdAndTypeAndStatus: stub(),
},
put: stub(),
},
Expand Down Expand Up @@ -151,4 +152,42 @@ describe('OpportunityCollection', () => {
.to.be.rejectedWith('Status is required');
});
});

describe('allBySiteIdAndTypeAndStatus', () => {
it('returns an array of Opportunity instances when opportunities exist', async () => {
const mockFindResults = { data: [mockRecord] };
mockElectroService.entities.opportunity.query.bySiteIdAndTypeAndStatus.returns(
{ go: () => Promise.resolve(mockFindResults) },
);

const results = await opportunityCollectionInstance.allBySiteIdAndTypeAndStatus('site67890', 'TYPE', 'IN_PROGRESS');
expect(results).to.be.an('array').that.has.length(1);
expect(results[0]).to.be.instanceOf(Opportunity);
expect(results[0].record).to.deep.include(mockOpportunityModel.record);
});

it('returns an empty array if no opportunities exist for the given site ID, type and status', async () => {
mockElectroService.entities.opportunity.query.bySiteIdAndTypeAndStatus.returns(
{ go: () => Promise.resolve([]) },
);

const results = await opportunityCollectionInstance.allBySiteIdAndTypeAndStatus('site67890', 'TYPE', 'IN_PROGRESS');
expect(results).to.be.an('array').that.is.empty;
});

it('throws an error if siteId is not provided', async () => {
await expect(opportunityCollectionInstance.allBySiteIdAndTypeAndStatus('', 'TYPE', 'IN_PROGRESS'))
.to.be.rejectedWith('SiteId is required');
});

it('throws an error if type is not provided', async () => {
await expect(opportunityCollectionInstance.allBySiteIdAndTypeAndStatus('site67890', '', 'IN_PROGRESS'))
.to.be.rejectedWith('Type is required');
});

it('throws an error if status is not provided', async () => {
await expect(opportunityCollectionInstance.allBySiteIdAndTypeAndStatus('site67890', 'TYPE', ''))
.to.be.rejectedWith('Status is required');
});
});
});
Loading