Skip to content

Commit

Permalink
fix: retrieve site candidate by base url (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney authored Feb 7, 2024
1 parent de7b077 commit b37cb0b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/spacecat-shared-data-access/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The module provides two main DAOs:
- `removeSite`

### Site Candidate Functions
- `getSiteCandidateByBaseURL`
- `upsertSiteCandidate`
- `siteCandidateExists`
- `updateSiteCandidate`
Expand Down
21 changes: 21 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/site-candidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { createSiteCandidate } from '../models/site-candidate.js';

/**
* Data transfer object for Site Candidate.
*/
Expand All @@ -28,4 +30,23 @@ export const SiteCandidateDto = {
updatedAt: siteCandidate.getUpdatedAt(),
updatedBy: siteCandidate.getUpdatedBy(),
}),

/**
* Converts a DynamoDB item into Site Candidate object;
* @param {object } dynamoItem - DynamoDB item.
* @returns {Readonly<SiteCandidate>} SiteCandidate object.
*/
fromDynamoItem: (dynamoItem) => {
const siteCandidateData = {
baseURL: dynamoItem.baseURL,
siteId: dynamoItem.siteId,
source: dynamoItem.source,
status: dynamoItem.status,
createdAt: dynamoItem.createdAt,
updatedAt: dynamoItem.updatedAt,
updatedBy: dynamoItem.updatedBy,
};

return createSiteCandidate(siteCandidateData);
},
};
1 change: 1 addition & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ export interface DataAccess {
) => Promise<void>;

// site candidate functions
getSiteCandidateByBaseURL: (baseURL: string) => Promise<SiteCandidate>;
upsertSiteCandidate: (siteCandidateDate: object) => Promise<SiteCandidate>;
siteCandidateExists: (baseURL: string) => Promise<boolean>;
updateSiteCandidate: (siteCandidate: SiteCandidate) => Promise<SiteCandidate>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export const exists = async (dynamoClient, config, baseURL) => {
return isObject(dynamoItem);
};

export const getSiteCandidateByBaseURL = async (
dynamoClient,
config,
log,
baseURL,
) => {
const dynamoItem = await dynamoClient.getItem(config.tableNameSiteCandidates, { baseURL });

return isObject(dynamoItem) ? SiteCandidateDto.fromDynamoItem(dynamoItem) : null;
};

/**
* Upserts a site candidate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
*/

import {
getSiteCandidateByBaseURL,
upsertSiteCandidate,
exists,
updateSiteCandidate,
} from './accessPatterns.js';

export const siteCandidateFunctions = (dynamoClient, config, log) => ({
getSiteCandidateByBaseURL: (baseURL) => getSiteCandidateByBaseURL(
dynamoClient,
config,
log,
baseURL,
),
upsertSiteCandidate: (siteCandidateData) => upsertSiteCandidate(
dynamoClient,
config,
Expand Down
9 changes: 9 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 @@ -656,4 +656,13 @@ describe('DynamoDB Integration Test', async () => {
expect(updatedSiteCandidate.getSource()).to.equal(siteCandidateData.source);
expect(updatedSiteCandidate.getStatus()).to.equal(siteCandidateData.status);
});

it('verify the get site candidate by base url flow', async () => {
const siteCandidate = await dataAccess.getSiteCandidateByBaseURL('https://example2.com');

expect(siteCandidate.getBaseURL()).to.equal('https://example2.com');
expect(siteCandidate.getStatus()).to.equal(SITE_CANDIDATE_STATUS.PENDING);
expect(siteCandidate.getSource()).to.be.undefined;
expect(siteCandidate.getSiteId()).to.be.undefined;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Data Access Object Tests', () => {
];

const siteCandidateFunctions = [
'getSiteCandidateByBaseURL',
'upsertSiteCandidate',
'siteCandidateExists',
'updateSiteCandidate',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ describe('Site Candidate Access Pattern Tests', () => {
exportedFunctions = siteCandidateFunctions(mockDynamoClient, TEST_DA_CONFIG, mockLog);
});

it('returns the site candidate by base url', async () => {
const siteCandidateData = { baseURL: 'https://existingsite.com', status: SITE_CANDIDATE_STATUS.PENDING };
mockDynamoClient.getItem.returns(Promise.resolve(siteCandidateData));

const result = await exportedFunctions.getSiteCandidateByBaseURL('test-url');

expect(result.getBaseURL()).to.equal(siteCandidateData.baseURL);
expect(result.getStatus()).to.equal(siteCandidateData.status);
});

it('returns null if the site candidate does not exist', async () => {
mockDynamoClient.getItem.returns(Promise.resolve(undefined));

const result = await exportedFunctions.getSiteCandidateByBaseURL('test-url');

expect(result).to.equal(null);
});

it('siteCandidateExists returns false when no site candidate exists', async () => {
const result = await exportedFunctions.siteCandidateExists('test-url');

Expand Down

0 comments on commit b37cb0b

Please sign in to comment.