Skip to content

Commit

Permalink
[BUG][NRPTI-1242] Add support for creating & updating permits from mu…
Browse files Browse the repository at this point in the history
…ltiple permit codes (#1248)

* Add support for creating permits from multiple permit codes

* Fix test method calls

* Fix whitespace and replace if-then-else with single return statement
  • Loading branch information
sggerard authored Jun 27, 2024
1 parent 02a49b2 commit b97bc99
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
87 changes: 57 additions & 30 deletions api/src/integrations/core/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,42 +250,62 @@ 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
* .
*
* @param {object} nrptiRecord NRPTI record
* @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
return !nrptiRecord.name === 'Lumby Mine' && permit.permit_no === 'G-4-352';
}
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
Expand All @@ -309,7 +329,6 @@ class CoreDataSource {
} else {
validPermit = permit;
}
}
}

return validPermit;
Expand All @@ -332,14 +351,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: {} }));
Expand All @@ -348,9 +371,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
};
}

Expand All @@ -364,17 +387,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 = [];
Expand Down Expand Up @@ -416,9 +443,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
};
}

Expand Down
6 changes: 3 additions & 3 deletions api/src/integrations/core/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
});

Expand Down
2 changes: 1 addition & 1 deletion api/src/integrations/core/permit-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
6 changes: 3 additions & 3 deletions api/src/integrations/core/permit-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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);
});
});
Expand Down

0 comments on commit b97bc99

Please sign in to comment.