From d1cba93c2ce1073426dc8333142a8d8fd81017ea Mon Sep 17 00:00:00 2001 From: Sweta Barman Date: Fri, 7 Jun 2024 10:11:04 -0400 Subject: [PATCH] feat: Add update queries for import-job and import-url entites (#254) ## Related Issues [SITES-22578](https://jira.corp.adobe.com/browse/SITES-22578) Add the following queries to import-url and import-job entities: - updateImportUrl - updateImportJob --------- Co-authored-by: Bruce Lefebvre --- .../src/index.d.ts | 6 +++ .../src/service/import-job/accessPatterns.js | 20 +++++++ .../src/service/import-job/index.js | 7 +++ .../src/service/import-url/accessPatterns.js | 26 ++++++++++ .../src/service/import-url/index.js | 12 ++++- .../unit/service/import-job/index.test.js | 52 +++++++++++++++++-- .../unit/service/import-url/index.test.js | 36 ++++++++++++- .../test/unit/service/index.test.js | 2 + 8 files changed, 155 insertions(+), 6 deletions(-) diff --git a/packages/spacecat-shared-data-access/src/index.d.ts b/packages/spacecat-shared-data-access/src/index.d.ts index dd2e38ff..0c373edf 100644 --- a/packages/spacecat-shared-data-access/src/index.d.ts +++ b/packages/spacecat-shared-data-access/src/index.d.ts @@ -610,12 +610,18 @@ export interface DataAccess { createNewImportJob: ( importJobData: object, ) => Promise; + updateImportJob: ( + importJob: ImportJob, + ) => Promise; getImportUrlByID: ( id: string, ) => Promise; createNewImportUrl: ( importUrlData: object, ) => Promise; + updateImportUrl: ( + importUrl: ImportUrl, + ) => Promise; // site candidate functions getSiteCandidateByBaseURL: (baseURL: string) => Promise; diff --git a/packages/spacecat-shared-data-access/src/service/import-job/accessPatterns.js b/packages/spacecat-shared-data-access/src/service/import-job/accessPatterns.js index f3b69bdf..491615fa 100644 --- a/packages/spacecat-shared-data-access/src/service/import-job/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/import-job/accessPatterns.js @@ -10,6 +10,7 @@ * governing permissions and limitations under the License. */ +import { isObject } from '@adobe/spacecat-shared-utils'; import { ImportJobDto } from '../../dto/import-job.js'; import { createImportJob } from '../../models/importer/import-job.js'; @@ -63,3 +64,22 @@ export const createNewImportJob = async (dynamoClient, config, log, importJobDat await dynamoClient.putItem(config.tableNameImportJobs, ImportJobDto.toDynamoItem(importJob)); return importJob; }; + +/** + * Updates an Import Job + * @param {DynamoClient} dynamoClient + * @param {Object} config + * @param {Logger} log + * @param {ImportJobDto} importJob + */ +export const updateImportJob = async (dynamoClient, config, log, importJob) => { + const existingImportJob = await getImportJobByID(dynamoClient, config, log, importJob.getId()); + + if (!isObject(existingImportJob)) { + throw new Error(`Import Job with id: ${importJob.getId()} does not exist`); + } + + await dynamoClient.putItem(config.tableNameImportJobs, ImportJobDto.toDynamoItem(importJob)); + + return importJob; +}; diff --git a/packages/spacecat-shared-data-access/src/service/import-job/index.js b/packages/spacecat-shared-data-access/src/service/import-job/index.js index 7bf4b180..5a386d2a 100644 --- a/packages/spacecat-shared-data-access/src/service/import-job/index.js +++ b/packages/spacecat-shared-data-access/src/service/import-job/index.js @@ -14,6 +14,7 @@ import { createNewImportJob, getImportJobByID, getImportJobsByStatus, + updateImportJob, } from './accessPatterns.js'; export const importJobFunctions = (dynamoClient, config, log) => ({ @@ -35,4 +36,10 @@ export const importJobFunctions = (dynamoClient, config, log) => ({ log, importJobData, ), + updateImportJob: (importJobData) => updateImportJob( + dynamoClient, + config, + log, + importJobData, + ), }); diff --git a/packages/spacecat-shared-data-access/src/service/import-url/accessPatterns.js b/packages/spacecat-shared-data-access/src/service/import-url/accessPatterns.js index dc263941..5f4b24d4 100644 --- a/packages/spacecat-shared-data-access/src/service/import-url/accessPatterns.js +++ b/packages/spacecat-shared-data-access/src/service/import-url/accessPatterns.js @@ -10,6 +10,7 @@ * governing permissions and limitations under the License. */ +import { isObject } from '@adobe/spacecat-shared-utils'; import { ImportUrlDto } from '../../dto/import-url.js'; import { createImportUrl } from '../../models/importer/import-url.js'; @@ -45,3 +46,28 @@ export const createNewImportUrl = async (dynamoClient, config, log, importUrlDat ); return importUrl; }; + +/** + * Update an existing Import Url + * @param {DynamoClient} dynamoClient + * @param {Object} config + * @param {Logger} log + * @param {Object} importUrl + * @returns {ImportUrlDto} + */ +export const updateImportUrl = async (dynamoClient, config, log, importUrl) => { + const existingImportUrl = await getImportUrlById( + dynamoClient, + config, + log, + importUrl.getId(), + ); + + if (!isObject(existingImportUrl)) { + throw new Error(`Import Url with ID:${importUrl.getId()} does not exist`); + } + + await dynamoClient.putItem(config.tableNameImportUrls, ImportUrlDto.toDynamoItem(importUrl)); + + return importUrl; +}; diff --git a/packages/spacecat-shared-data-access/src/service/import-url/index.js b/packages/spacecat-shared-data-access/src/service/import-url/index.js index 1db073a1..c4f137bd 100644 --- a/packages/spacecat-shared-data-access/src/service/import-url/index.js +++ b/packages/spacecat-shared-data-access/src/service/import-url/index.js @@ -10,7 +10,11 @@ * governing permissions and limitations under the License. */ -import { getImportUrlById, createNewImportUrl } from './accessPatterns.js'; +import { + getImportUrlById, + createNewImportUrl, + updateImportUrl, +} from './accessPatterns.js'; export const importUrlFunctions = (dynamoClient, config, log) => ({ getImportUrlById: (id) => getImportUrlById( @@ -25,4 +29,10 @@ export const importUrlFunctions = (dynamoClient, config, log) => ({ log, importUrlData, ), + updateImportUrl: (importUrl) => updateImportUrl( + dynamoClient, + config, + log, + importUrl, + ), }); diff --git a/packages/spacecat-shared-data-access/test/unit/service/import-job/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/import-job/index.test.js index 2561a220..ee515afb 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/import-job/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/import-job/index.test.js @@ -17,6 +17,7 @@ import chaiAsPromised from 'chai-as-promised'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; import { importJobFunctions } from '../../../../src/service/import-job/index.js'; +import { createImportJob } from '../../../../src/models/importer/import-job.js'; chai.use(sinonChai); chai.use(chaiAsPromised); @@ -37,14 +38,18 @@ describe('Import Job Tests', () => { beforeEach(() => { mockDynamoClient = { - getItem: sinon.stub().resolves([]), + getItem: sinon.stub().resolves(), query: sinon.stub().resolves(null), putItem: sinon.stub().resolves(), }; mockLog = { - log: sinon.stub(), + log: console, }; - exportedFunctions = importJobFunctions(mockDynamoClient, TEST_DA_CONFIG, mockLog); + exportedFunctions = importJobFunctions( + mockDynamoClient, + TEST_DA_CONFIG, + mockLog, + ); }); describe('getImportJobByID', () => { @@ -110,11 +115,50 @@ describe('Import Job Tests', () => { apiKey: 'test-api-key', importQueueId: 'test-import-queue-id', }; - const result = await exportedFunctions.createNewImportJob(mockImportJobData); + const result = await exportedFunctions.createNewImportJob( + mockImportJobData, + ); expect(result).to.be.not.null; expect(mockDynamoClient.putItem.calledOnce).to.be.true; }); }); + + describe('updateImportJob', () => { + it('should update an existing ImportJob', async () => { + const mockImportJobData = { + id: 'test-id', + status: 'RUNNING', + options: {}, + baseURL: 'https://www.test.com', + apiKey: 'test-api-key', + importQueueId: 'test-import-queue-id', + }; + mockDynamoClient.getItem.resolves(mockImportJobData); + + const importJob = await exportedFunctions.getImportJobByID('test-id'); + importJob.updateStatus('COMPLETE'); + const result = await exportedFunctions.updateImportJob( + importJob, + ); + + expect(result).to.be.not.null; + expect(mockDynamoClient.putItem).to.have.been.calledOnce; + expect(result.getStatus()).to.equal('COMPLETE'); + }); + + it('should throw an error if the ImportJob does not exist', async () => { + const mockImportJobData = { + id: 'test-id', + status: 'RUNNING', + apiKey: 'test-api-key', + options: {}, + baseURL: 'https://www.test.com', + }; + const importJob = createImportJob(mockImportJobData); + const result = exportedFunctions.updateImportJob(importJob); + expect(result).to.be.rejectedWith('Import Job with id:test-id does not exist'); + }); + }); }); }); diff --git a/packages/spacecat-shared-data-access/test/unit/service/import-url/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/import-url/index.test.js index ed8df6c4..da097c4b 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/import-url/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/import-url/index.test.js @@ -15,8 +15,11 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import sinon from 'sinon'; +import sinonChai from 'sinon-chai'; import { importUrlFunctions } from '../../../../src/service/import-url/index.js'; +import { createImportUrl } from '../../../../src/models/importer/import-url.js'; +chai.use(sinonChai); chai.use(chaiAsPromised); const { expect } = chai; @@ -33,7 +36,7 @@ describe('Import Url Tests', () => { beforeEach(() => { mockDynamoClient = { - getItem: sinon.stub().resolves([]), + getItem: sinon.stub().resolves(), query: sinon.stub().resolves(null), putItem: sinon.stub().resolves(), }; @@ -74,5 +77,36 @@ describe('Import Url Tests', () => { expect(mockDynamoClient.putItem.calledOnce).to.be.true; }); }); + + describe('updateImportUrl', () => { + it('should update an existing importUrl with the correct status', async () => { + const mockImportUrl = { + id: 'test-id', + status: 'RUNNING', + url: 'https://www.test.com', + }; + mockDynamoClient.getItem.resolves(mockImportUrl); + + const importUrl = await exportedFunctions.getImportUrlById('test-id'); + importUrl.updateStatus('COMPLETE'); + const result = await exportedFunctions.updateImportUrl(importUrl); + + expect(result).to.be.not.null; + expect(mockDynamoClient.putItem).to.have.been.calledOnce; + expect(result.getStatus()).to.equal('COMPLETE'); + }); + + it('should throw an error when the importUrl does not exist', async () => { + const mockImportUrl = { + id: 'test-id', + status: 'RUNNING', + url: 'https://www.test.com', + }; + + const importUrl = createImportUrl(mockImportUrl); + const result = exportedFunctions.updateImportUrl(importUrl); + await expect(result).to.be.rejectedWith('Import Url with ID:test-id does not exist'); + }); + }); }); }); diff --git a/packages/spacecat-shared-data-access/test/unit/service/index.test.js b/packages/spacecat-shared-data-access/test/unit/service/index.test.js index fa948cae..9ff9bcae 100644 --- a/packages/spacecat-shared-data-access/test/unit/service/index.test.js +++ b/packages/spacecat-shared-data-access/test/unit/service/index.test.js @@ -82,11 +82,13 @@ describe('Data Access Object Tests', () => { 'getImportJobByID', 'getImportJobsByStatus', 'createNewImportJob', + 'updateImportJob', ]; const importUrlFunctions = [ 'getImportUrlById', 'createNewImportUrl', + 'updateImportUrl', ]; let dao;