Skip to content

Commit

Permalink
[FEATURE][NRPTI-1262] redirect link on bcmi that directs to the corre…
Browse files Browse the repository at this point in the history
…sponding search on nrced (#1267)

* Add paramaters to ncred, update importers

* Fix linting, tests

* fix linting

* Remove todo
  • Loading branch information
sggerard authored Aug 15, 2024
1 parent 42f8772 commit 165fba4
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ export class RecordsListComponent implements OnInit, OnDestroy {
activityType: new FormControl((this.queryParams && this.queryParams.activityType) || ''),
agency: new FormControl((this.queryParams && this.queryParams.agency) || ''),
act: new FormControl((this.queryParams && this.queryParams.act) || ''),
regulation: new FormControl((this.queryParams && this.queryParams.regulation) || '')
regulation: new FormControl((this.queryParams && this.queryParams.regulation) || ''),
companyName: new FormControl((this.queryParams && this.queryParams.companyName) || ''),
project: new FormControl((this.queryParams && this.queryParams.project) || '')
});
}

Expand Down Expand Up @@ -340,6 +342,10 @@ export class RecordsListComponent implements OnInit, OnDestroy {
delete this.queryParams['regulation'];
}

delete this.queryParams['companyName'];

delete this.queryParams['project'];

delete this.queryParams['autofocus'];

this.router.navigate(['/records', this.queryParams]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export class RecordUtils {
filterParams['legislation.regulation'] = params.regulation;
}

if (params.companyName) {
filterParams['issuedTo.companyName'] = params.companyName;
}

if (params.project) {
filterParams['projectName'] = params.project;
}

return filterParams;
}

Expand Down
10 changes: 6 additions & 4 deletions api/src/integrations/core-documents/datasource.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const mongoose = require('mongoose');
const fs = require('fs');
const axios = require('axios');

const defaultLog = require('../../utils/logger')('core-datasource');
const DocumentController = require('../../controllers/document-controller');
const PermitUtils = require('./permit-utils');
const RECORD_TYPE = require('../../utils/constants/record-type-enum');
const { getIntegrationUrl } = require('../integration-utils');
const { getIntegrationUrl, getAuthHeader } = require('../integration-utils');
const CoreUtil = require('../core-util');

const CORE_API_HOST = process.env.CORE_API_HOST || 'https://minesdigitalservices.pathfinder.gov.bc.ca';
Expand Down Expand Up @@ -188,14 +189,15 @@ class CoreDocumentsDataSource {
// Get a download token.
const downloadToken = await this.getDownloadToken(documentId);

const url = getIntegrationUrl(CORE_DOC_MANAGER_HOST,'/documents',{"token": downloadToken });
const url = `${CORE_DOC_MANAGER_HOST}/documents?token=${downloadToken}`;

const res = await this.coreUtil.getRecords(url, { responseType: 'stream' });
await this.coreUtil.checkTokenExpiry();
const res = await axios.get(url, getAuthHeader(this.coreUtil.client_token, { responseType: 'stream' }));

const tempFilePath = `${uploadDir}/${documentName}`;
// Attempt to save locally.
await new Promise((resolve, reject) => {
res
res.data
.pipe(fs.createWriteStream(tempFilePath))
.on('finish', resolve)
.on('error', error => reject(error));
Expand Down
8 changes: 6 additions & 2 deletions api/src/integrations/core-documents/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const axios = require('axios');
const { Readable } = require('stream');
const DocumentController = require('../../controllers/document-controller');
const permitUtils = require('./permit-utils');
const coreUtil = require('../core-util');
const moment = require('moment-timezone');

jest.mock('../../controllers/document-controller');
jest.mock('axios');
Expand Down Expand Up @@ -93,7 +95,7 @@ describe('CoreDocumentsDataSource', () => {
const dataSource = new DataSource();

jest.spyOn(integrationUtils, 'getIntegrationUrl').mockReturnValue('/test/');
jest.spyOn(integrationUtils, 'getRecords').mockReturnValue(Promise.resolve({ token_guid: 'testing' }));
jest.spyOn(coreUtil.prototype, 'getRecords').mockReturnValue(Promise.resolve({ token_guid: 'testing' }));

const token = await dataSource.getDownloadToken('testing');

Expand Down Expand Up @@ -123,11 +125,13 @@ describe('CoreDocumentsDataSource', () => {
const mockResponseStream = new Readable();
mockResponseStream.push('file content');
mockResponseStream.push(null);
const mockResponse = { data: mockResponseStream };
const mockResponse = { data: mockResponseStream, status: 200 };

axios.get.mockResolvedValue(mockResponse);

const dataSource = new DataSource();
dataSource.coreUtil.apiAccessExpiry = new moment(new Date()).add(2, 'w');
dataSource.coreUtil.client_token = 'test_token';

dataSource.getDownloadToken = jest.fn().mockResolvedValue(mockDownloadToken);

Expand Down
13 changes: 10 additions & 3 deletions api/src/integrations/core-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ class CoreUtil {
}

/**
* Wrapper for integration-utils.getRecords with additional logic to check if the token is expired
*
* Checks if the current token is valid and requests a new token if necessasary
*/
async getRecords(url, additionalOptions = {}) {
async checkTokenExpiry(){
if (this.client_token == null || Date.now() >= this.apiAccessExpiry) {
await this.getToken();
}
}

/**
* Wrapper for integration-utils.getRecords with additional logic to check if the token is expired
*
*/
async getRecords(url, additionalOptions = {}) {
await this.checkTokenExpiry();
return await integrationUtils.getRecords(url, getAuthHeader(this.client_token, additionalOptions));
}
}
Expand Down
14 changes: 9 additions & 5 deletions api/src/integrations/core/datasource.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const DataSource = require('./datasource');
const defaultLogger = require('../../utils/logger')('core-datasource');
const integrationUtils = require('../integration-utils');
const coreUtil = require('../core-util');
const moment = require('moment-timezone');

const mockedTaskAuditRecord = { updateTaskRecord: jest.fn() };
const mockedAuthPayload = 'auth_payload';
Expand Down Expand Up @@ -64,6 +66,8 @@ describe('Core DataSource', () => {
describe('getAllRecordData', () => {
it('should handle unexpected error during data retrieval', async () => {
const dataSource = new DataSource(mockedTaskAuditRecord, mockedAuthPayload);
dataSource.coreUtil.apiAccessExpiry = new moment(new Date()).add(2, 'w');
dataSource.coreUtil.client_token = 'test_token';
integrationUtils.getRecords = jest.fn(() => { throw new Error('Test error') });

await expect(dataSource.getAllRecordData()).rejects.toThrow('Test error');
Expand All @@ -72,9 +76,8 @@ describe('Core DataSource', () => {

describe('processRecords', () => {
it('calls `processRecord` on all records', async () => {
integrationUtils.getRecords = jest.fn(() => ({
records: []
}));

jest.spyOn(coreUtil.prototype, 'getRecords').mockReturnValue(Promise.resolve({ records: [] }));

const mockGetIntegrationUrl = jest.fn(() => {
return 'test/path';
Expand Down Expand Up @@ -198,9 +201,10 @@ describe('Core DataSource', () => {

it('should call getRecords in the getVerifiedMines call', async () => {
const dataSource = new DataSource(null);
dataSource.client_token = 'testToken';

const spy = jest.spyOn(integrationUtils, 'getRecords');
dataSource.coreUtil.apiAccessExpiry = new moment(new Date()).add(2, 'w');
dataSource.coreUtil.client_token = 'test_token';
const spy = jest.spyOn(coreUtil.prototype, 'getRecords');

await dataSource.getVerifiedMines();

Expand Down
1 change: 1 addition & 0 deletions api/src/integrations/core/mine-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Mines extends BaseRecordUtils {
...super.transformRecord(mineRecord),
_sourceRefId: mineRecord.mine_guid,
name: mineRecord.mine_name,
mineNo: mineRecord.mine_no,
status: this.getLatestStatus(mineRecord),
commodities: this.getCommodities(mineRecord, commodityTypes),
tailingsImpoundments: mineRecord.mine_tailings_storage_facilities.length,
Expand Down
2 changes: 1 addition & 1 deletion api/src/integrations/integration-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('IntegrationUtils', () => {

const token = await IntegrationUtils.getCoreAccessToken('123', 'abc', 'client_auth');

expect(token).toEqual('testToken');
expect(token.access_token).toEqual('testToken');
expect(mockAxios).toHaveBeenCalled();
});
});
Expand Down
18 changes: 17 additions & 1 deletion api/src/integrations/nris-emli/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,23 @@ class NrisDataSource {
}

newRecord.recordName = `EMLI Inspection - ${record.assessmentId}`;
newRecord.projectName = record.location.locationName;

let parentMine;
if (record.location.locationId) {
const MineBCMIModel = mongoose.model(RECORD_TYPE.MineBCMI._schemaName);
parentMine = await MineBCMIModel
.findOne({
_schemaName: RECORD_TYPE.MineBCMI._schemaName,
mineNo: record.location.locationId
})
}

if (parentMine != null) {
newRecord.projectName = parentMine.name;
} else {
newRecord.projectName = record.location.locationName;
}

const permitNo =
record.authorization && record.authorization.sourceId ? `; Permit No.: ${record.authorization.sourceId}` : '';
newRecord.description = `Inspection No.: ${record.assessmentId}; Inspection Type: ${record.inspection.inspectionType[0]}${permitNo}`;
Expand Down
25 changes: 3 additions & 22 deletions api/src/integrations/nris-emli/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,6 @@ describe('NrisDataSource', () => {
expect(result).toEqual(true);
});

it('should not process audit record', async () => {
const dataSource = new NrisDataSource();

const record = {
assessmentSubType: 'Inspection - Site Visit',
assessmentSubStatus: 'Closed',
inspection: {
inspectionType: ['Audit'],
inspctReportSentDate: moment()
.subtract(43, 'days')
.format(),
inspectionSubType: 'Mine Inspection'
}
};

const result = dataSource.shouldProcessRecord(record);
expect(result).toEqual(false);
});

it('should not process non Mine Inspection record', async () => {
const dataSource = new NrisDataSource();

Expand All @@ -309,10 +290,10 @@ describe('NrisDataSource', () => {
expect(result).toEqual(false);
});

it('should process Compliance Review OR Inspection assessmentSubType records', async () => {
it('should process inspection assessmentSubType records', async () => {
const dataSource = new NrisDataSource();

const assessmentSubTypeList = ['Compliance Review', 'Inspection - Site Visit', 'Inspection - Desktop'];
const assessmentSubTypeList = ['Inspection - Site Visit', 'Inspection - Desktop'];

for (const assessmentSubType of assessmentSubTypeList) {
const record = {
Expand Down Expand Up @@ -471,7 +452,7 @@ describe('NrisDataSource', () => {
assessmentId: 'sampleAssessmentId',
attachment: [
{ attachmentId: 'attachmentId1', fileType: 'Other' },
{ attachmentId: 'attachmentId2', fileType: 'Final Report' },
{ attachmentId: 'attachmentId2', fileType: 'Final Report', attachmentMediaType: 'application/pdf'},
//{ attachmentId: 'attachmentId3', fileType: 'Report', attachmentComment: 'Inspection Report'},
//{ attachmentId: 'attachmentId3', fileType: 'Report', attachmentComment: 'Inspection Report', attachmentDate: "2020-01-10 11:50"},
//{ attachmentId: 'attachmentId4', fileType: 'Report', attachmentComment: 'Inspection Report', attachmentDate: "2024-06-06" },
Expand Down
20 changes: 18 additions & 2 deletions api/src/integrations/nris-epd/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ class NrisDataSource {
case 'Corporation':
newRecord.issuedTo = {
type: 'Company',
companyName: record.client[0].orgName || '',
fullName: record.client[0].orgName || ''
companyName: record.client[0].orgName.trim() || '',
fullName: record.client[0].orgName.trim() || ''
};
break;
case 'P':
Expand Down Expand Up @@ -296,6 +296,22 @@ class NrisDataSource {
newRecord.centroid = [Number(record.location.longitude), Number(record.location.latitude)];
}

if (newRecord.issuedTo.type === 'Company' && newRecord.issuedTo.companyName !== '') {
const MineBCMIModel = mongoose.model(RECORD_TYPE.MineBCMI._schemaName);
let parentMines = await MineBCMIModel
.find({
_schemaName: RECORD_TYPE.MineBCMI._schemaName,
permittee: {'$regex': newRecord.issuedTo.companyName, $options: 'i'}
})

if (parentMines.length > 0) {
let mine = parentMines.find( parent => newRecord.location.toLowerCase().includes(parent.name.toLowerCase()));
if (mine != null) {
newRecord.projectName = mine.name;
}
}
}

if (record.complianceStatus) {
newRecord.outcomeDescription = record.complianceStatus;
}
Expand Down
1 change: 1 addition & 0 deletions api/src/models/bcmi/mine-bcmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = require('../../utils/model-schema-generator')(
write: [{ type: String, trim: true, default: 'sysadmin' }],

name: { type: String, default: '' },
mineNo: {type: String, default: ''},
permitNumber: { type: String, default: '' },
showPermitNumber: { type: Boolean, default: true },
status: { type: String, default: '' },
Expand Down

0 comments on commit 165fba4

Please sign in to comment.