Skip to content

Commit

Permalink
Merge branch 'master' into 1242-add-support-for-mx-permits
Browse files Browse the repository at this point in the history
  • Loading branch information
sggerard committed Jun 28, 2024
2 parents f78d650 + cf61de1 commit 04adc12
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 92 deletions.
74 changes: 0 additions & 74 deletions api/migrations/20240619192233-updateBCMICollectionTypeField.js

This file was deleted.

57 changes: 49 additions & 8 deletions api/src/integrations/core-documents/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CoreDocumentsDataSource {

try {
// Get Core API access token.
this.client_token = await getCoreAccessToken(CORE_CLIENT_ID, CORE_CLIENT_SECRET, CORE_GRANT_TYPE);
await this.setClientToken();

// Run main process.
await this.updateRecords();
Expand Down Expand Up @@ -68,17 +68,17 @@ class CoreDocumentsDataSource {
const jobCount = process.env.PARALLEL_IMPORT_LIMIT ? parseInt(process.env.PARALLEL_IMPORT_LIMIT) : 1;

const permits = await this.getPermits();

this.status.itemTotal = permits.length;
const numPermits = permits.length;
this.status.itemTotal = numPermits;
await this.taskAuditRecord.updateTaskRecord({ itemTotal: this.status.itemTotal });

const permitUtils = new PermitUtils(this.auth_payload, RECORD_TYPE.Permit);

// Push records to proccess into a Promise array to be processed in parallel.
for (let i = 0; i < permits.length; i += jobCount) {
for (let i = 0; i < numPermits; i += jobCount) {
const promises = [];
for (let j = 0; j < jobCount && i + j < permits.length; j++) {
defaultLog.info(`Processing permit ${i + j + 1} out of ${permits.length}`);
for (let j = 0; j < jobCount && i + j < numPermits; j++) {
defaultLog.info(`Processing permit ${i + j + 1} out of ${numPermits}`);
promises.push(this.processRecord(permits[i + j], permitUtils));
}

Expand Down Expand Up @@ -161,7 +161,9 @@ class CoreDocumentsDataSource {
if (!documentId) {
throw new Error('getDownloadToken - param documentId must not be null');
}

if (this.isAPITokenExpired(this.apiAccessExpiry)) {
this.setClientToken();
}
try {
const url = getIntegrationUrl(CORE_API_HOST, `/api/download-token/${documentId}`);
const { token_guid } = await integrationUtils.getRecords(url, getAuthHeader(this.client_token));
Expand Down Expand Up @@ -269,12 +271,51 @@ class CoreDocumentsDataSource {
const transformedAmendment = permitUtils.transformRecord(permit);
const result = await permitUtils.updateRecord(transformedAmendment, permit);

if(result.length && result[0].status && result[0].status === 'failure')
if (result.length && result[0].status && result[0].status === 'failure')
throw Error(`permitUtils.updateRecord failed: ${result[0].errorMessage}`);
} catch (error) {
throw new Error(`updateAmendment - unexpected error: ${error.message}`);
}
}

/**
* Sets the CORE API token and marks when the token will expire
*
* @memberof CoreDocumentsDataSource
*/
async setClientToken() {
console.log('Updating Client Token...');
const apiAccess = await getCoreAccessToken(CORE_CLIENT_ID, CORE_CLIENT_SECRET, CORE_GRANT_TYPE);
this.apiAccessExpiry = this.getExpiryTime(apiAccess.expires_in);
this.client_token = apiAccess.access_token;
console.log('Client Token updated.');
}

/**
* Gives a time for when the given duration will pass with a buffer
*
* @param {int} tokenDuration the number of seconds that the token is valid for.
* @returns {int} the epoch time when the token is expected to expire ( - the buffer ) = current time + token duration - buffer
*
* @memberof CoreDocumentsDataSource
*/
getExpiryTime(tokenDuration) {
const TIME_BUFFER = 30000;
const SECONDS_TO_MILLISECONDS_MULTIPLIER = 1000;
return Date.now() + ( tokenDuration * SECONDS_TO_MILLISECONDS_MULTIPLIER ) - TIME_BUFFER;
}

/**
* checks if the given time has passed
*
* @param {int} expiryTime the epoch time we are checking for
* @returns {boolean} true if the provided time is in the past
*
* @memberof CoreDocumentsDataSource
*/
isAPITokenExpired(expiryTime) {
return Date.now() >= expiryTime;
}
}

module.exports = CoreDocumentsDataSource;
3 changes: 2 additions & 1 deletion api/src/integrations/core/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class CoreDataSource {

try {
// Get a new API access token.
this.client_token = await getCoreAccessToken(CORE_CLIENT_ID, CORE_CLIENT_SECRET, CORE_GRANT_TYPE);
const apiAccess = await getCoreAccessToken(CORE_CLIENT_ID, CORE_CLIENT_SECRET, CORE_GRANT_TYPE);
this.client_token = apiAccess.access_token;

// Run main process.
await this.updateRecords();
Expand Down
16 changes: 8 additions & 8 deletions api/src/integrations/integration-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ exports.getRecords = async function(url, options = undefined) {
* @param {string} clientId Core client ID.
* @param {string} clientSecret Core client secret.
* @param {string} grantType Core SSO grant type.
* @returns {string} Core API access token.
* @returns {Object?} payload - the res.data obj which includes the access_token as well as its time to live.
* @memberof CoreDataSource
*/
exports.getCoreAccessToken = async function(clientId, clientSecret, grantType) {
Expand All @@ -73,9 +73,9 @@ exports.getRecords = async function(url, options = undefined) {
}

const requestBody = {
client_id: clientId,
client_secret: clientSecret,
grant_type: grantType
client_id: clientId,
client_secret: clientSecret,
grant_type: grantType
};

const config = {
Expand All @@ -92,12 +92,12 @@ exports.getRecords = async function(url, options = undefined) {
throw new Error('coreLogin - unable to log in to Core API.');
}

return payload.access_token;
}
return payload;
};

/**
* Creates the authentication header for protected requests.
*
*
* @param {string} token Bearer token.
* @param {object} additionalOptions Additional HTTP options.
* @returns {object} Axios header with the bearer token set
Expand All @@ -110,4 +110,4 @@ exports.getRecords = async function(url, options = undefined) {
},
...additionalOptions
};
}
};
2 changes: 1 addition & 1 deletion tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Moving the **App** from `dev` (`:latest`) to `test`
```sh
oc project f00029-tools
oc tag nrpti:test nrpti:test-backup
oc tag nrpti:lastest nrpti:test
oc tag nrpti:latest nrpti:test
```

Moving **NRCED** from `dev` (`:latest`) to `test`
Expand Down

0 comments on commit 04adc12

Please sign in to comment.