Skip to content

Commit

Permalink
fix: SITES-25634 [Importer] Expose hasCustomImportJs and hasCustomHea…
Browse files Browse the repository at this point in the history
…ders as two new fields on a import job (#381)

Co-authored-by: Ben Helleman <[email protected]>
  • Loading branch information
bhellema and Ben Helleman authored Sep 24, 2024
1 parent 62f880c commit b90a7c5
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 157 deletions.
8 changes: 8 additions & 0 deletions packages/spacecat-shared-data-access/docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,14 @@
{
"AttributeName": "initiatedBy",
"AttributeType": "M"
},
{
"AttributeName": "hasCustomHeaders",
"AttributeType": "B"
},
{
"AttributeName": "hasCustomImportJs",
"AttributeType": "B"
}
],
"GlobalSecondaryIndexes": [
Expand Down
4 changes: 4 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/import-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const ImportJobDto = {
redirectCount: importJob.getRedirectCount(),
importQueueId: importJob.getImportQueueId(),
initiatedBy: importJob.getInitiatedBy(),
hasCustomHeaders: importJob.hasCustomHeaders(),
hasCustomImportJs: importJob.hasCustomImportJs(),
GSI1PK: 'ALL_IMPORT_JOBS',
}),

Expand All @@ -64,6 +66,8 @@ export const ImportJobDto = {
redirectCount: dynamoItem.redirectCount,
importQueueId: dynamoItem.importQueueId,
initiatedBy: dynamoItem.initiatedBy,
hasCustomHeaders: dynamoItem.hasCustomHeaders,
hasCustomImportJs: dynamoItem.hasCustomImportJs,
};

return createImportJob(importJobData);
Expand Down
10 changes: 10 additions & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ export interface ImportJob {
* Retrieves the initiatedBy metadata (name, imsOrgId, imsUserId, userAgent) of the import job.
*/
getInitiatedBy: () => object;

/**
* Indicates if the import job has custom headers.
*/
hasCustomHeaders: () => boolean;

/**
* Indicates if the import job has custom import js.
*/
hasCustomImportJs: () => boolean;
}

export interface ImportUrl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {
hasText, isIsoDate, isValidUrl, isObject, isString, isNumber, isInteger,
hasText, isIsoDate, isValidUrl, isObject, isString, isNumber, isInteger, isBoolean,
} from '@adobe/spacecat-shared-utils';
import { Base } from '../base.js';
import { ImportJobStatus, ImportOptions } from './import-constants.js';
Expand Down Expand Up @@ -39,6 +39,8 @@ const ImportJob = (data) => {
self.getRedirectCount = () => self.state.redirectCount;
self.getImportQueueId = () => self.state.importQueueId;
self.getInitiatedBy = () => self.state.initiatedBy;
self.hasCustomHeaders = () => self.state.hasCustomHeaders || false;
self.hasCustomImportJs = () => self.state.hasCustomImportJs || false;

/**
* Updates the state of the ImportJob.
Expand Down Expand Up @@ -147,6 +149,29 @@ const ImportJob = (data) => {
},
);

/**
* Update the hasCustomHeaders value to true if the ImportJob has custom headers, false otherwise.
* @param {boolean} hasCustomHeaders - The new value for hasCustomHeaders.
* @return {ImportJob} The updated ImportJob object.
*/
self.updateHasCustomHeaders = (hasCustomHeaders) => updateState('hasCustomHeaders', hasCustomHeaders, (value) => {
if (!isBoolean(value)) {
throw new Error(`Invalid hasCustomHeaders value: ${value}`);
}
});

/**
* Update the hasCustomImportJs value to true if the ImportJob has custom import js, false
* otherwise.
* @param {boolean} hasCustomImportJs - The new value for hasCustomImportJs.
* @return {ImportJob} The updated ImportJob object.
*/
self.updateHasCustomImportJs = (hasCustomImportJs) => updateState('hasCustomImportJs', hasCustomImportJs, (value) => {
if (!isBoolean(value)) {
throw new Error(`Invalid hasCustomImportJs value: ${value}`);
}
});

return Object.freeze(self);
};

Expand Down Expand Up @@ -215,5 +240,13 @@ export const createImportJob = (data) => {
});
}

if (newState.hasCustomImportJs && !isBoolean(newState.hasCustomImportJs)) {
throw new Error(`Invalid hasCustomImportJs value: ${newState.hasCustomImportJs}`);
}

if (newState.hasCustomHeaders && !isBoolean(newState.hasCustomHeaders)) {
throw new Error(`Invalid hasCustomHeaders value: ${newState.hasCustomHeaders}`);
}

return ImportJob(newState);
};
6 changes: 6 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 @@ -1022,6 +1022,8 @@ describe('DynamoDB Integration Test', async () => {
options: {
[ImportOptions.ENABLE_JAVASCRIPT]: true,
},
hasCustomImportJs: true,
hasCustomHeaders: false,
});

// helper
Expand All @@ -1036,6 +1038,8 @@ describe('DynamoDB Integration Test', async () => {
const job = await createNewImportJob();
expect(job.getId()).to.be.a('string');
expect(job.getCreatedAt()).to.be.a('string');
expect(job.hasCustomHeaders()).to.be.false;
expect(job.hasCustomImportJs()).to.be.true;
});

it('Verify updateImportJob', async () => {
Expand All @@ -1047,6 +1051,7 @@ describe('DynamoDB Integration Test', async () => {
newJob.updateDuration(1234);
newJob.updateUrlCount(100);
newJob.updateImportQueueId('Q-456');
newJob.updateHasCustomHeaders(true);

const updatedJob = await dataAccess.updateImportJob(newJob);

Expand All @@ -1058,6 +1063,7 @@ describe('DynamoDB Integration Test', async () => {
expect(updatedJob.getOptions()).to.deep.equal({
[ImportOptions.ENABLE_JAVASCRIPT]: true,
});
expect(updatedJob.hasCustomHeaders()).to.be.true;
});

it('Verify getImportJobsByStatus', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ describe('ImportJob Model tests', () => {
const importJob = createImportJob({ ...validImportJob, startTime: '' });
expect(importJob.getStartTime()).to.match(/^20/);
});

it('create an import job with custom fields', () => {
let headersJob = createImportJob({ ...validImportJob, hasCustomHeaders: true });
expect(headersJob.hasCustomHeaders()).to.be.true;
headersJob = createImportJob({ ...validImportJob, hasCustomHeaders: false });
expect(headersJob.hasCustomHeaders()).to.be.false;
headersJob = createImportJob({ ...validImportJob });
expect(headersJob.hasCustomHeaders()).to.be.false;
expect(() => createImportJob({ ...validImportJob, hasCustomHeaders: 'x' })).to.throw('Invalid hasCustomHeaders value: x');

let importJsJob = createImportJob({ ...validImportJob, hasCustomImportJs: true });
expect(importJsJob.hasCustomImportJs()).to.be.true;
importJsJob = createImportJob({ ...validImportJob, hasCustomImportJs: false });
expect(importJsJob.hasCustomImportJs()).to.be.false;
importJsJob = createImportJob({ ...validImportJob });
expect(importJsJob.hasCustomImportJs()).to.be.false;
expect(() => createImportJob({ ...validImportJob, hasCustomImportJs: 'x' })).to.throw('Invalid hasCustomImportJs value: x');
});
});

describe('Import Job Functionality Tests', () => {
Expand Down Expand Up @@ -171,6 +189,26 @@ describe('ImportJob Model tests', () => {
expect(() => importJob.updateRedirectCount(-1)).to.throw('Invalid redirect count during update: -1');
});

it('update hasCustomHeaders of import job', () => {
importJob.updateHasCustomHeaders(true);
expect(importJob.hasCustomHeaders()).to.equal(true);

importJob.updateHasCustomHeaders(false);
expect(importJob.hasCustomHeaders()).to.equal(false);

expect(() => importJob.updateHasCustomHeaders(undefined)).to.throw('Invalid hasCustomHeaders value: undefined');
});

it('update hasCustomImportJs of import job', () => {
importJob.updateHasCustomImportJs(true);
expect(importJob.hasCustomImportJs()).to.equal(true);

importJob.updateHasCustomImportJs(false);
expect(importJob.hasCustomImportJs()).to.equal(false);

expect(() => importJob.updateHasCustomImportJs(undefined)).to.throw('Invalid hasCustomImportJs value: undefined');
});

it('updates import queue id of import job', () => {
importJob.updateImportQueueId('123');
expect(importJob.getImportQueueId()).to.equal('123');
Expand Down
Loading

0 comments on commit b90a7c5

Please sign in to comment.