Skip to content

Commit

Permalink
feat(data-access): get site candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Dec 11, 2024
1 parent 54b10c6 commit c579357
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 628 deletions.
720 changes: 93 additions & 627 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"access": "public"
},
"dependencies": {
"@adobe/spacecat-shared-dynamo": "1.3.50",
"@adobe/spacecat-shared-dynamo": "1.4.0",
"@adobe/spacecat-shared-utils": "1.23.1",
"@aws-sdk/client-dynamodb": "3.705.0",
"@aws-sdk/lib-dynamodb": "3.705.0",
Expand Down
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 @@ -872,6 +872,7 @@ export interface DataAccess {
) => Promise<ApiKey | null>;

// site candidate functions
getSiteCandidates: () => Promise<SiteCandidate[]>;
getSiteCandidateByBaseURL: (baseURL: string) => Promise<SiteCandidate>;
upsertSiteCandidate: (siteCandidateDate: object) => Promise<SiteCandidate>;
siteCandidateExists: (baseURL: string) => Promise<boolean>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ import { isObject } from '@adobe/spacecat-shared-utils';
import { createSiteCandidate } from '../../models/site-candidate.js';
import { SiteCandidateDto } from '../../dto/site-candidate.js';

/**
* Retrieves all site candidates.
*
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
* @param {DataAccessConfig} config - The data access config.
* @returns {Promise<Readonly<Site>[]>} A promise that resolves to an array of all site candidates.
*/
export const getSiteCandidates = async (dynamoClient, config) => {
const dynamoItems = await dynamoClient.scan({
TableName: config.tableNameSites,
});

return dynamoItems.map((dynamoItem) => SiteCandidateDto.fromDynamoItem(dynamoItem));
};

/**
* Checks if a site candidate exists in site candidates table using base url
* @param {DynamoDbClient} dynamoClient - The DynamoDB client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
*/

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

export const siteCandidateFunctions = (dynamoClient, config, log) => ({
getSiteCandidates: () => getSiteCandidates(
dynamoClient,
config,
),
getSiteCandidateByBaseURL: (baseURL) => getSiteCandidateByBaseURL(
dynamoClient,
config,
Expand Down
5 changes: 5 additions & 0 deletions packages/spacecat-shared-data-access/test/it/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,11 @@ describe('Legacy Data Model IT', function () {
expect(latestAuditAfterRemoval).to.be.null;
});

it('get all site candidates', async () => {
const siteCandidates = await dataAccess.getSiteCandidates();
expect(siteCandidates.length).to.equal(10);
});

it('verify a previously added site candidate exists', async () => {
const exists = await dataAccess.siteCandidateExists('https://example0.com');
expect(exists).to.be.true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Data Access Object Tests', () => {
];

const siteCandidateFunctions = [
'getSiteCandidates',
'getSiteCandidateByBaseURL',
'upsertSiteCandidate',
'siteCandidateExists',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe('Site Candidate Access Pattern Tests', () => {

const exportedFunctions = siteCandidateFunctions(mockDynamoClient, TEST_DA_CONFIG, mockLog);

it('exports upsertSiteCandidate function', () => {
expect(exportedFunctions).to.have.property('getSiteCandidates');
expect(exportedFunctions.getSiteCandidates).to.be.a('function');
});

it('exports upsertSiteCandidate function', () => {
expect(exportedFunctions).to.have.property('upsertSiteCandidate');
expect(exportedFunctions.upsertSiteCandidate).to.be.a('function');
Expand All @@ -63,11 +68,13 @@ describe('Site Candidate Access Pattern Tests', () => {
let mockDynamoClient;
let mockLog = {};
let exportedFunctions;
const siteCandidates = [{ baseURL: 'https://site1.com' }, { baseURL: 'https://site2.com' }];

beforeEach(() => {
mockDynamoClient = {
getItem: sinon.stub().returns(Promise.resolve(null)),
putItem: sinon.stub().returns(Promise.resolve()),
scan: sinon.stub().resolves(siteCandidates),
};

mockLog = {
Expand All @@ -77,6 +84,13 @@ describe('Site Candidate Access Pattern Tests', () => {
exportedFunctions = siteCandidateFunctions(mockDynamoClient, TEST_DA_CONFIG, mockLog);
});

it('returns all sitecandidates', async () => {
const result = await exportedFunctions.getSiteCandidates();

expect(result[0].getBaseURL()).to.equal(siteCandidates[0].baseURL);
expect(result[1].getBaseURL()).to.equal(siteCandidates[1].baseURL);
});

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));
Expand Down

0 comments on commit c579357

Please sign in to comment.