From 2485f70238678a2279ddbeef78f01e68a3e4f099 Mon Sep 17 00:00:00 2001 From: sggerard Date: Wed, 26 Jun 2024 16:01:48 -0700 Subject: [PATCH 1/3] Add support for creating permits from multiple permit codes --- api/src/integrations/core/datasource.js | 90 +++++++++++++++-------- api/src/integrations/core/permit-utils.js | 2 +- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/api/src/integrations/core/datasource.js b/api/src/integrations/core/datasource.js index 38a8f1fd3..75a65eba7 100644 --- a/api/src/integrations/core/datasource.js +++ b/api/src/integrations/core/datasource.js @@ -250,8 +250,7 @@ class CoreDataSource { } /** - * Gets valid permit for a Core mine. A valid permit must meet the following criteria: - * - Must not be exploratory + * Gets all valid permits for a Core mine. A valid permit must meet the following criteria: * - Must not be historical * . * @@ -259,33 +258,57 @@ class CoreDataSource { * @returns {object} Valid permit. * @memberof CoreDataSource */ - async getMinePermit(nrptiRecord) { + async getMinePermits(nrptiRecord) { if (!nrptiRecord) { - throw Error('getMinePermit - required nrptiRecord is null.'); + throw Error('getMinePermits - required nrptiRecord is null.'); } // Get permits with detailed information. const url = getIntegrationUrl(CORE_API_HOST, `/api/mines/${nrptiRecord._sourceRefId}/permits`); const { records: permits } = await integrationUtils.getRecords(url, getAuthHeader(this.client_token)); - // First, any mines with 'X' as their second character are considered exploratory. Remove them. - // const nonExploratoryPermits = permits.filter(permit => permit.permit_no[1].toLowerCase() !== 'x'); + return permits.filter(p => this.isValidPermit(p,nrptiRecord)); + } + + isValidPermit(permit,nrptiRecord){ + //Mine must not be historical which is indicated by an authorized year of '9999' on the latest amendment. + if((permit.permit_amendments.length && !permit.permit_amendments[0].authorization_end_date) + || permit.permit_status_code === 'O'){ + + // Do not use 'G-4-352' for Lumby + // https://bcmines.atlassian.net/browse/NRPT-684 + if (nrptiRecord.name === 'Lumby Mine' && permit.permit_no === 'G-4-352') { + return false; + } + return true; + } + return false; + } + + /** + * Gets valid permit for a Core mine. A valid permit must meet the following criteria: + * - Must not be exploratory + * - Must not be historical + * . + * + * @param {object} nrptiRecord NRPTI record + * @returns {object} Valid permit. + * @memberof CoreDataSource + */ + getValidPermit(permits) { + // First, any mines with 'X' as their second character are considered exploratory. Remove them unless they are the only valid permits + let nonExploratoryPermits = permits.filter(permit => permit.permit_no[1].toLowerCase() !== 'x'); + if(nonExploratoryPermits.length === 0){ + nonExploratoryPermits = permits; + } + // Second, mine must not be historical which is indicated by an authorized year of '9999' on the latest amendment. let validPermit; - for (const permit of permits) { + for (const permit of nonExploratoryPermits) { // Confirm that the most recent amendment is not historical, which is always the first index. // If 'null' then it is considered valid. // Otherwise, if the status is O, it's valid. (date 9999 check removed) - if ( - (permit.permit_amendments.length && !permit.permit_amendments[0].authorization_end_date) || - permit.permit_status_code === 'O' - ) { - // Do not use 'G-4-352' for Lumby - // https://bcmines.atlassian.net/browse/NRPT-684 - if (nrptiRecord.name === 'Lumby Mine' && permit.permit_no === 'G-4-352') { - continue; - } // There should only be a single record. If there is more then we need to identify the most // recent permit as the official valid permit @@ -309,7 +332,6 @@ class CoreDataSource { } else { validPermit = permit; } - } } return validPermit; @@ -332,14 +354,18 @@ class CoreDataSource { throw new Error('createMinePermit - nrptiRecord is required'); } - const permit = await this.getMinePermit(nrptiRecord); + const permits = await this.getMinePermits(nrptiRecord); + const validPermit = this.getValidPermit(permits); - if (!permit) { + if (!validPermit) { throw new Error('createMinePermit - Cannot find valid permit'); } // Transform the permit and amendments into single permits. Each document in an amendment will create a single permit. - const transformedPermits = await permitUtils.transformRecord(permit, nrptiRecord); + let transformedPermits = []; + for (const permit of permits) { + transformedPermits = transformedPermits.concat(await permitUtils.transformRecord(permit, nrptiRecord)); + } // To trigger flavour for this import. const preparedPermits = transformedPermits.map(amendment => ({ ...amendment, PermitBCMI: {} })); @@ -348,9 +374,9 @@ class CoreDataSource { await Promise.all(promises); return { - permitNumber: permit.permit_no, - permittee: permit.current_permittee, - permit + permitNumber: validPermit.permit_no, + permittee: validPermit.current_permittee, + validPermit }; } @@ -364,17 +390,21 @@ class CoreDataSource { */ async updateMinePermit(permitUtils, mineRecord) { // Get the updated Core permit. - const permit = await this.getMinePermit(mineRecord); + const permits = await this.getMinePermits(mineRecord); + const validPermit = this.getValidPermit(permits); - if (!permit || !permit.permit_amendments || !permit.permit_amendments.length) { + if (!validPermit || !validPermit.permit_amendments || !validPermit.permit_amendments.length) { throw new Error('updateMinePermit - Cannot find valid permit'); } // Get the current permits for the mine. - const currentPermits = await permitUtils.getMinePermits(mineRecord._sourceRefId); + const currentPermits = await permitUtils.getCurrentMinePermits(mineRecord._sourceRefId); // Transform into permits. - const transformedPermits = permitUtils.transformRecord(permit, mineRecord); + let transformedPermits = []; + for (const permit of permits) { + transformedPermits = transformedPermits.concat(await permitUtils.transformRecord(permit, mineRecord)); + } // Find the new permits that need to be created, otherwise update the permits if needed const newPermits = []; @@ -416,9 +446,9 @@ class CoreDataSource { await Promise.all(promises); return { - permitNumber: permit.permit_no, - permittee: permit.current_permittee, - permit + permitNumber: validPermit.permit_no, + permittee: validPermit.current_permittee, + validPermit }; } diff --git a/api/src/integrations/core/permit-utils.js b/api/src/integrations/core/permit-utils.js index 789ff4658..1c5c5e093 100644 --- a/api/src/integrations/core/permit-utils.js +++ b/api/src/integrations/core/permit-utils.js @@ -103,7 +103,7 @@ class Permits extends BaseRecordUtils { return await super.updateRecord(newPermit, existingPermit); } - async getMinePermits(mineId) { + async getCurrentMinePermits(mineId) { const Permit = mongoose.model('Permit'); const permits = await Permit.find({ _schemaName: 'Permit', mineGuid: mineId }); From 58c7c38abc0bd853341dc9cb908e0c51e1e01399 Mon Sep 17 00:00:00 2001 From: sggerard Date: Thu, 27 Jun 2024 15:03:48 -0700 Subject: [PATCH 2/3] Fix test method calls --- api/src/integrations/core/datasource.test.js | 6 +++--- api/src/integrations/core/permit-utils.test.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/src/integrations/core/datasource.test.js b/api/src/integrations/core/datasource.test.js index c1f5a0705..f5e927dc7 100644 --- a/api/src/integrations/core/datasource.test.js +++ b/api/src/integrations/core/datasource.test.js @@ -147,14 +147,14 @@ describe('Core DataSource', () => { }); }); - describe('getMinePermit', () => { + describe('getMinePermits', () => { - it('throws an error when no permit is found in getMinePermit method', async () => { + it('throws an error when no permit is found in getMinePermits method', async () => { const nrptiRecord = null; const dataSource = new DataSource(null); - await expect(dataSource.getMinePermit(nrptiRecord)).rejects.toThrow('getMinePermit - required nrptiRecord is null.'); + await expect(dataSource.getMinePermits(nrptiRecord)).rejects.toThrow('getMinePermits - required nrptiRecord is null.'); }); }); diff --git a/api/src/integrations/core/permit-utils.test.js b/api/src/integrations/core/permit-utils.test.js index 61b5f8667..d3684ac94 100644 --- a/api/src/integrations/core/permit-utils.test.js +++ b/api/src/integrations/core/permit-utils.test.js @@ -231,13 +231,13 @@ describe('Permits class', () => { }); }); - describe('getMinePermits method', () => { + describe('getCurrentMinePermits method', () => { it('returns null if no existing record found', async () => { mongoose.model = jest.fn(() => ({ find: jest.fn(() => null), })); - const result = await permitsInstance.getMinePermits('123'); + const result = await permitsInstance.getCurrentMinePermits('123'); expect(result).toBeNull(); }); @@ -247,7 +247,7 @@ describe('Permits class', () => { find: jest.fn(() => existingRecord), })); - const result = await permitsInstance.getMinePermits('123'); + const result = await permitsInstance.getCurrentMinePermits('123'); expect(result).toEqual(existingRecord); }); }); From 532a40a7ceaf351c20e8713c0bd5a89c7d34641c Mon Sep 17 00:00:00 2001 From: sggerard Date: Thu, 27 Jun 2024 16:18:37 -0700 Subject: [PATCH 3/3] Fix whitespace and replace if-then-else with single return statement --- api/src/integrations/core/datasource.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/api/src/integrations/core/datasource.js b/api/src/integrations/core/datasource.js index 75a65eba7..c79af8625 100644 --- a/api/src/integrations/core/datasource.js +++ b/api/src/integrations/core/datasource.js @@ -270,17 +270,14 @@ class CoreDataSource { return permits.filter(p => this.isValidPermit(p,nrptiRecord)); } - isValidPermit(permit,nrptiRecord){ + isValidPermit(permit,nrptiRecord) { //Mine must not be historical which is indicated by an authorized year of '9999' on the latest amendment. - if((permit.permit_amendments.length && !permit.permit_amendments[0].authorization_end_date) - || permit.permit_status_code === 'O'){ + if ((permit.permit_amendments.length && !permit.permit_amendments[0].authorization_end_date) + || permit.permit_status_code === 'O') { // Do not use 'G-4-352' for Lumby // https://bcmines.atlassian.net/browse/NRPT-684 - if (nrptiRecord.name === 'Lumby Mine' && permit.permit_no === 'G-4-352') { - return false; - } - return true; + return !nrptiRecord.name === 'Lumby Mine' && permit.permit_no === 'G-4-352'; } return false; } @@ -298,7 +295,7 @@ class CoreDataSource { getValidPermit(permits) { // First, any mines with 'X' as their second character are considered exploratory. Remove them unless they are the only valid permits let nonExploratoryPermits = permits.filter(permit => permit.permit_no[1].toLowerCase() !== 'x'); - if(nonExploratoryPermits.length === 0){ + if (nonExploratoryPermits.length === 0) { nonExploratoryPermits = permits; }