Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRPT-57: Allows multiple NRIS imports #537

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/migrations/20200807181534-fixMineGuidDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.setup = function(options, seedLink) {
seed = seedLink;
};

exports.up = function(db) {
exports.up = async function(db) {
const mClient = await db.connection.connect(db.connectionString, { native_parser: true })
try {
console.log('Starting fix for mineGUID.');
Expand Down
71 changes: 71 additions & 0 deletions api/migrations/20200810162152-fixNrisSourceRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

let dbm;
let type;
let seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = async function(db) {
const mClient = await db.connection.connect(db.connectionString, { native_parser: true });

try {
console.log('Starting fix for NRIS records...');

const nrpti = await mClient.collection('nrpti');

// Get all records that have been created from an NRIS import.
const records = await nrpti.find({ sourceSystemRef: 'nris-epd' }).toArray();

for (const record of records) {
if (!record.recordName){
console.log(`Record ${record._id} is missing recordName`);
continue;
}

// The record name contains the assessment ID at the end.
// It is in the format 'Inspection - {record name} - {assessment ID}'
const lastDash = record.recordName.lastIndexOf('-');

if (!lastDash){
console.log(`Record ${record._id} has a different recordName format`);
continue;
}

// There is always a space after the last dash.
const assessmentId = record.recordName.substring(lastDash + 2);

if (!assessmentId){
console.log(`Cannot find assessment ID for record ${record._id}`);
continue;
}

await nrpti.update({ _id: new ObjectId(record._id) }, { $set: { _sourceRefNrisId: parseInt(assessmentId) }});
}

} catch (err) {
console.log('Error:', err);
}

console.log('Done.');
mClient.close()
};

exports.down = function(db) {
return null;
};

exports._meta = {
"version": 1
};
2 changes: 2 additions & 0 deletions api/src/controllers/post/inspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ exports.createMaster = function(args, res, next, incomingObj, flavourIds) {
(inspection._sourceRefOgcInspectionId = incomingObj._sourceRefOgcInspectionId);
incomingObj._sourceRefOgcDeficiencyId &&
(inspection._sourceRefOgcDeficiencyId = incomingObj._sourceRefOgcDeficiencyId);
incomingObj._sourceRefNrisId &&
(inspection._sourceRefNrisId = incomingObj._sourceRefNrisId);

// set permissions
inspection.read = ROLES.ADMIN_ROLES;
Expand Down
63 changes: 58 additions & 5 deletions api/src/integrations/nris-epd/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,16 @@ class NrisDataSource {
moment().diff(moment(records[i].completionDate), 'days') > 7
) {
const newRecord = await this.transformRecord(records[i]);
await this.createRecord(newRecord);

const existingRecord = await this.findExistingRecord(newRecord);

if (existingRecord) {
await this.updateRecord(newRecord, existingRecord);
} else {
await this.createRecordAttachments(records[i], newRecord);
await this.createRecord(newRecord);
}

// Assuming we didn't get thrown an error, update the items successfully processed.
processingObject.itemsProcessed++;
} else {
Expand Down Expand Up @@ -176,7 +185,7 @@ class NrisDataSource {
newRecord.recordName = `Inspection - ${record.requirementSource} - ${record.assessmentId}`;
newRecord.legislationDescription = 'Inspection to verify compliance with regulatory requirement.';
newRecord.recordType = 'Inspection';
newRecord._sourceRefNrisId = record.assessmentId;
newRecord._sourceRefNrisId = null
newRecord.dateIssued = record.assessmentDate;
newRecord.issuingAgency = this.stringTransformEPOtoEPD(record.resourceAgency);
newRecord.author = 'Environmental Protection Division';
Expand Down Expand Up @@ -233,6 +242,12 @@ class NrisDataSource {
newRecord.outcomeDescription += ' - ' + record.inspection.inspctResponse;
}

defaultLog.info('Processed:', record.assessmentId);
return newRecord;
}

// Gets the files and saves them for a record's attachment.
async createRecordAttachments(record, newRecord) {
for (let i = 0; i < record.attachment.length; i++) {
if (record.attachment[i].fileType === 'Final Report') {
const tempFileData = await this.getFileFromNRIS(record.assessmentId, record.attachment[i].attachmentId);
Expand All @@ -244,9 +259,6 @@ class NrisDataSource {
}
}
}

defaultLog.info('Processed:', record.assessmentId);
return newRecord;
}

// Grabs a file from NRIS datasource
Expand Down Expand Up @@ -350,6 +362,47 @@ class NrisDataSource {
defaultLog.error(`Failed to create Inspection record: ${error.message}`);
}
}

// Checks to see if a record already exists.
async findExistingRecord(transformedRecord) {
const masterRecordModel = mongoose.model(RECORD_TYPE.Inspection._schemaName);

return await masterRecordModel
.findOne({
_schemaName: RECORD_TYPE.Inspection._schemaName,
_sourceRefNrisId: transformedRecord._sourceRefNrisId
})
.populate('_flavourRecords');
}

// Updates an existing record with new data pulled from the API.
async updateRecord(newRecord, existingRecord) {
if (!newRecord) {
throw Error('updateRecord - required newRecord must be non-null.');
}

if (!existingRecord) {
throw Error('updateRecord - required existingRecord must be non-null.');
}

try {
// build update Obj, which needs to include the flavour record ids
const updateObj = { ...newRecord, _id: existingRecord._id };
existingRecord._flavourRecords.forEach(flavourRecord => {
updateObj[flavourRecord._schemaName] = { _id: flavourRecord._id, addRole: 'public' };
});

return await RecordController.processPutRequest(
{ swagger: { params: { auth_payload: this.auth_payload } } },
null,
null,
RECORD_TYPE.Inspection.recordControllerName,
[updateObj]
);
} catch (error) {
defaultLog.error(`Failed to save ${RECORD_TYPE.Inspection._schemaName} record: ${error.message}`);
}
}
}

module.exports = NrisDataSource;
8 changes: 2 additions & 6 deletions api/src/models/bcmi/permit-bcmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ module.exports = require('../../utils/model-schema-generator')(
},
authorizedEndDate: { type: Date, default: null },
description: { type: String, default: '' },
// Document ref to meta record and physical document on the object store
amendmentDocument: {
_sourceRefId: { type: String, default: null },
documentName: { type: String, defualt: '' },
documentId: { type: 'ObjectId', default: null }
},
documents: [{ type: 'ObjectId', default: [], index: true }],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to match the master.


// final Record boilerplate
dateAdded: { type: Date, default: Date.now() },
dateUpdated: { type: Date, default: null },
Expand Down