Skip to content

Commit

Permalink
feat: Add update queries for import-job and import-url entites (#254)
Browse files Browse the repository at this point in the history
## 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 <[email protected]>
  • Loading branch information
swetabar and blefebvre authored Jun 7, 2024
1 parent 7107024 commit d1cba93
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,18 @@ export interface DataAccess {
createNewImportJob: (
importJobData: object,
) => Promise<ImportJob>;
updateImportJob: (
importJob: ImportJob,
) => Promise<ImportJob>;
getImportUrlByID: (
id: string,
) => Promise<ImportUrl | null>;
createNewImportUrl: (
importUrlData: object,
) => Promise<ImportUrl>;
updateImportUrl: (
importUrl: ImportUrl,
) => Promise<ImportUrl>;

// site candidate functions
getSiteCandidateByBaseURL: (baseURL: string) => Promise<SiteCandidate>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createNewImportJob,
getImportJobByID,
getImportJobsByStatus,
updateImportJob,
} from './accessPatterns.js';

export const importJobFunctions = (dynamoClient, config, log) => ({
Expand All @@ -35,4 +36,10 @@ export const importJobFunctions = (dynamoClient, config, log) => ({
log,
importJobData,
),
updateImportJob: (importJobData) => updateImportJob(
dynamoClient,
config,
log,
importJobData,
),
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -25,4 +29,10 @@ export const importUrlFunctions = (dynamoClient, config, log) => ({
log,
importUrlData,
),
updateImportUrl: (importUrl) => updateImportUrl(
dynamoClient,
config,
log,
importUrl,
),
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
};
Expand Down Expand Up @@ -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');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ describe('Data Access Object Tests', () => {
'getImportJobByID',
'getImportJobsByStatus',
'createNewImportJob',
'updateImportJob',
];

const importUrlFunctions = [
'getImportUrlById',
'createNewImportUrl',
'updateImportUrl',
];

let dao;
Expand Down

0 comments on commit d1cba93

Please sign in to comment.