Skip to content

Commit

Permalink
NRPT-117: Adds Permit and Amendment import with flavours (#467)
Browse files Browse the repository at this point in the history
* NRPT-117: Adds Permit and Amendment import with flavours
  • Loading branch information
KitArmstrong authored Jul 3, 2020
1 parent a3a0483 commit 3ea4972
Show file tree
Hide file tree
Showing 23 changed files with 886 additions and 130 deletions.
1 change: 1 addition & 0 deletions api/src/controllers/post/mine-bcmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports.createMaster = function (args, res, next, incomingObj, flavourIds) {
// set data
incomingObj.name && (mine.name = incomingObj.name);
incomingObj.permitNumber && (mine.permitNumber = incomingObj.permitNumber);
incomingObj.permit && (mine.permit = incomingObj.permit);
incomingObj.status && (mine.status = incomingObj.status);
incomingObj.commodities.length && (mine.commodities = incomingObj.commodities);
incomingObj.region && (mine.region = incomingObj.region);
Expand Down
122 changes: 122 additions & 0 deletions api/src/controllers/post/permit-amendment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const mongoose = require('mongoose');
const ObjectId = require('mongoose').Types.ObjectId;

const postUtils = require('../../utils/post-utils');
const { userHasValidRoles } = require('../../utils/auth-utils');
const { ROLES } = require('../../utils/constants/misc');

/**
* Create a new Mine Permit record.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj Mine Permit record to create
* @returns {MinePermit} new Mine Permit record
*/
exports.createRecord = async function (args, res, next, incomingObj) {
const flavourFunctions = {
PermitAmendmentBCMI: this.createBCMI,
};
return await postUtils.createRecordWithFlavours(args, res, next, incomingObj, this.createMaster, flavourFunctions);
};

/**
* Performs all operations necessary to create a master Mine Permit record.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj Mine Permit record to create
* @param {*} flavourIds array of flavour record _ids
* @returns {MinePermit} created master Mine Permit record
*/
exports.createMaster = function (args, res, next, incomingObj, flavourIds) {
let PermitAmendment = mongoose.model('PermitAmendment');
let permitAmendment = new PermitAmendment();

permitAmendment._schemaName = 'PermitAmendment';

// set forward references
if (flavourIds && flavourIds.length) {
flavourIds.forEach(id => {
if (ObjectId.isValid(id)) {
permitAmendment._flavourRecords.push(new ObjectId(id));
}
});
}

// Set permissions
permitAmendment.read = ROLES.ADMIN_ROLES;
permitAmendment.write = ROLES.ADMIN_ROLES;

// Set data
incomingObj.statusCode && (permitAmendment.statusCode = incomingObj.statusCode);
incomingObj.typeCode && (permitAmendment.typeCode = incomingObj.typeCode);
incomingObj.receivedDate && (permitAmendment.receivedDate = incomingObj.receivedDate);
incomingObj.issueDate && (permitAmendment.issueDate = incomingObj.issueDate);
incomingObj.authorizedEndDate && (permitAmendment.authorizedEndDate = incomingObj.authorizedEndDate);
incomingObj.description && (permitAmendment.description = incomingObj.description);
(incomingObj.documents && incomingObj.documents.length) && (permitAmendment.documents = incomingObj.documents);

// Set meta.
permitAmendment.addedBy = args && args.swagger.params.auth_payload.displayName || incomingObj.addedBy;
permitAmendment.updatedBy = args && args.swagger.params.auth_payload.displayName || incomingObj.updatedBy;
permitAmendment.dateAdded = new Date();
permitAmendment.dateUpdated = new Date();

// Set data source reference
incomingObj.sourceSystemRef && (permitAmendment.sourceSystemRef = incomingObj.sourceSystemRef);
incomingObj._sourceRefId && (permitAmendment._sourceRefId = incomingObj._sourceRefId);

return permitAmendment;
};

/**
* Creates the BCMI flavour of a permit amendment.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj
* @returns created BCMI permit amendment
*/
exports.createBCMI = function (args, res, next, incomingObj) {
// Confirm user has correct role for this type of record.
if (!userHasValidRoles([ROLES.SYSADMIN, ROLES.BCMIADMIN], args.swagger.params.auth_payload.realm_access.roles)) {
throw new Error('Missing valid user role.');
}

let PermitAmendmentBCMI = mongoose.model('PermitAmendmentBCMI');
let permitAmendmentBCMI = new PermitAmendmentBCMI();

permitAmendmentBCMI._schemaName = 'PermitAmendmentBCMI';

// set integration references
incomingObj._sourceRefId && (permitAmendmentBCMI._sourceRefId = incomingObj._sourceRefId);

// set permissions and meta
permitAmendmentBCMI.read = ROLES.ADMIN_ROLES;
permitAmendmentBCMI.write = [ROLES.SYSADMIN, ROLES.BCMIADMIN];

// Set data
incomingObj.statusCode && (permitAmendmentBCMI.mineGuid = incomingObj.statusCode);
incomingObj.typeCode && (permitAmendmentBCMI.typeCode = incomingObj.typeCode);
incomingObj.receivedDate && (permitAmendmentBCMI.receivedDate = incomingObj.receivedDate);
incomingObj.issueDate && (permitAmendmentBCMI.issueDate = incomingObj.issueDate);
incomingObj.authorizedEndDate && (permitAmendmentBCMI.authorizedEndDate = incomingObj.authorizedEndDate);
incomingObj.description && (permitAmendmentBCMI.description = incomingObj.description);
(incomingObj.documents && incomingObj.documents.length) && (permitAmendmentBCMI.documents = incomingObj.documents);

// Set meta.
permitAmendmentBCMI.addedBy = args && args.swagger.params.auth_payload.displayName || incomingObj.addedBy;
permitAmendmentBCMI.updatedBy = args && args.swagger.params.auth_payload.displayName || incomingObj.updatedBy;
permitAmendmentBCMI.dateAdded = new Date();
permitAmendmentBCMI.dateUpdated = new Date();

// Set data source reference
incomingObj.sourceSystemRef && (permitAmendmentBCMI.sourceSystemRef = incomingObj.sourceSystemRef);
incomingObj._sourceRefId && (permitAmendmentBCMI._sourceRefId = incomingObj._sourceRefId);

return permitAmendmentBCMI;
};
86 changes: 83 additions & 3 deletions api/src/controllers/post/permit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const { ROLES } = require('../../utils/constants/misc');
exports.createRecord = async function (args, res, next, incomingObj) {
const flavourFunctions = {
PermitLNG: this.createLNG,
PermitBCMI: this.createBCMI,
}
return await postUtils.createRecordWithFlavours(args, res, next, incomingObj, this.createMaster, flavourFunctions);
};
Expand Down Expand Up @@ -70,12 +71,10 @@ exports.createMaster = function (args, res, next, incomingObj, flavourIds) {
incomingObj._epicProjectId &&
ObjectId.isValid(incomingObj._epicProjectId) &&
(permit._epicProjectId = new ObjectId(incomingObj._epicProjectId));
incomingObj._sourceRefId &&
ObjectId.isValid(incomingObj._sourceRefId) &&
(permit._sourceRefId = new ObjectId(incomingObj._sourceRefId));
incomingObj._epicMilestoneId &&
ObjectId.isValid(incomingObj._epicMilestoneId) &&
(permit._epicMilestoneId = new ObjectId(incomingObj._epicMilestoneId));
incomingObj._sourceRefId && (permit._sourceRefId = incomingObj._sourceRefId);

// set permissions
permit.read = ROLES.ADMIN_ROLES;
Expand Down Expand Up @@ -115,6 +114,11 @@ exports.createMaster = function (args, res, next, incomingObj, flavourIds) {
incomingObj.centroid && (permit.centroid = incomingObj.centroid);
incomingObj.documents && (permit.documents = incomingObj.documents);

incomingObj.mineGuid && (permit.mineGuid = incomingObj.mineGuid);
incomingObj.permitNumber && (permit.permitNumber = incomingObj.permitNumber);
incomingObj.status && (permit.status = incomingObj.status);
incomingObj.permitAmendments && (permit.permitAmendments = incomingObj.permitAmendments);

// set meta
permit.addedBy = args.swagger.params.auth_payload.displayName;
permit.dateAdded = new Date();
Expand Down Expand Up @@ -223,3 +227,79 @@ exports.createLNG = function (args, res, next, incomingObj) {

return permitLNG;
};

/**
* Creates the BCMI flavour of a permit.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj
* @returns created BCMI permit
*/
exports.createBCMI = function (args, res, next, incomingObj) {
// Confirm user has correct role for this type of record.
if (!userHasValidRoles([ROLES.SYSADMIN, ROLES.BCMIADMIN], args.swagger.params.auth_payload.realm_access.roles)) {
throw new Error('Missing valid user role.');
}

let PermitBCMI = mongoose.model('PermitBCMI');
let permitBCMI = new PermitBCMI();

permitBCMI._schemaName = 'PermitBCMI';

// set integration references
incomingObj._sourceRefId && (permitBCMI._sourceRefId = incomingObj._sourceRefId);

// set permissions and meta
permitBCMI.read = ROLES.ADMIN_ROLES;
permitBCMI.write = [ROLES.SYSADMIN, ROLES.BCMIADMIN];

// If incoming object has addRole: 'public' then read will look like ['sysadmin', 'public']
if (incomingObj.addRole && incomingObj.addRole === 'public') {
permitBCMI.read.push('public');
permitBCMI.datePublished = new Date();
permitBCMI.publishedBy = args.swagger.params.auth_payload.displayName;
}

permitBCMI.addedBy = args.swagger.params.auth_payload.displayName;
permitBCMI.dateAdded = new Date();

// set master data
incomingObj.recordName && (permitBCMI.recordName = incomingObj.recordName);
permitBCMI.recordType = 'Permit';
incomingObj.recordSubtype && (permitBCMI.recordSubtype = incomingObj.recordSubtype);
incomingObj.dateIssued && (permitBCMI.dateIssued = incomingObj.dateIssued);
incomingObj.issuingAgency && (permitBCMI.issuingAgency = incomingObj.issuingAgency);
incomingObj.legislation && incomingObj.legislation.act && (permitBCMI.legislation.act = incomingObj.legislation.act);
incomingObj.legislation &&
incomingObj.legislation.regulation &&
(permitBCMI.legislation.regulation = incomingObj.legislation.regulation);
incomingObj.legislation &&
incomingObj.legislation.section &&
(permitBCMI.legislation.section = incomingObj.legislation.section);
incomingObj.legislation &&
incomingObj.legislation.subSection &&
(permitBCMI.legislation.subSection = incomingObj.legislation.subSection);
incomingObj.legislation &&
incomingObj.legislation.paragraph &&
(permitBCMI.legislation.paragraph = incomingObj.legislation.paragraph);
incomingObj.legislationDescription && (permitBCMI.legislationDescription = incomingObj.legislationDescription);
incomingObj.projectName && (permitBCMI.projectName = incomingObj.projectName);
incomingObj.location && (permitBCMI.location = incomingObj.location);
incomingObj.centroid && (permitBCMI.centroid = incomingObj.centroid);
incomingObj.documents && (permitBCMI.documents = incomingObj.documents);

// set flavour data
incomingObj.mineGuid && (permitBCMI.mineGuid = incomingObj.mineGuid);
incomingObj.permitNumber && (permitBCMI.permitNumber = incomingObj.permitNumber);
incomingObj.status && (permitBCMI.status = incomingObj.status);
incomingObj.permitAmendments && (permitBCMI.permitAmendments = incomingObj.permitAmendments);

// set data source references
incomingObj.sourceDateAdded && (permitBCMI.sourceDateAdded = incomingObj.sourceDateAdded);
incomingObj.sourceDateUpdated && (permitBCMI.sourceDateUpdated = incomingObj.sourceDateUpdated);
incomingObj.sourceSystemRef && (permitBCMI.sourceSystemRef = incomingObj.sourceSystemRef);

return permitBCMI;
};
101 changes: 101 additions & 0 deletions api/src/controllers/put/permit-amendment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const mongoose = require('mongoose');
const ObjectID = require('mongodb').ObjectID;
const PutUtils = require('../../utils/put-utils');
const PermitPost = require('../post/permit');

/**
* Performs all operations necessary to edit a master Permit Amendment record and its associated flavour records.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj see example
* @returns object containing the operation's status and created records
*/
exports.editRecord = async function (args, res, next, incomingObj) {
const flavourFunctions = {
PermitAmendmentBCMI: this.editBCMI
}
return await PutUtils.editRecordWithFlavours(args, res, next, incomingObj, this.editMaster, PermitPost, 'PermitAmendment', flavourFunctions);
};


/**
* Performs all operations necessary to edit a master Permit Amendment record.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj see example
* @returns edited master permit amendment record
*/
exports.editMaster = function (args, res, next, incomingObj, flavourIds) {
delete incomingObj._id;

// Reject any changes to master permissions
delete incomingObj.read;
delete incomingObj.write;

const Permit = mongoose.model('PermitAmendment');

const sanitizedObj = PutUtils.validateObjectAgainstModel(Permit, incomingObj);

if (!sanitizedObj || sanitizedObj === {}) {
// skip, as there are no changes to master record
return;
}

sanitizedObj.dateUpdated = new Date();
sanitizedObj.updatedBy = args.swagger.params.auth_payload.displayName;

const dotNotatedObj = PutUtils.getDotNotation(sanitizedObj);

const updateObj = { $set: dotNotatedObj };

if (flavourIds && flavourIds.length) {
updateObj.$addToSet = { _flavourRecords: flavourIds.map(id => new ObjectID(id)) };
}

return updateObj;
};

/**
* Performs all operations necessary to edit a lng Permit record.
*
* @param {*} args
* @param {*} res
* @param {*} next
* @param {*} incomingObj see example
* @returns edited BCMI permit amendment record
*/
exports.editBCMI = function (args, res, next, incomingObj) {
delete incomingObj._id;

// Reject any changes to permissions
// Publishing must be done via addRole or removeRole
delete incomingObj.read;
delete incomingObj.write;

let PermitLNG = mongoose.model('PermitAmendmentLNG');

const sanitizedObj = PutUtils.validateObjectAgainstModel(PermitLNG, incomingObj);

sanitizedObj.dateUpdated = new Date();

const dotNotatedObj = PutUtils.getDotNotation(sanitizedObj);

// If incoming object has addRole: 'public' then read will look like ['sysadmin', 'public']
const updateObj = { $set: dotNotatedObj, $addToSet: {}, $pull: {} };

if (incomingObj.addRole && incomingObj.addRole === 'public') {
updateObj.$addToSet['read'] = 'public';
updateObj.$set['datePublished'] = new Date();
updateObj.$set['publishedBy'] = args.swagger.params.auth_payload.displayName;
} else if (incomingObj.removeRole && incomingObj.removeRole === 'public') {
updateObj.$pull['read'] = 'public';
updateObj.$set['datePublished'] = null;
updateObj.$set['publishedBy'] = '';
}

return updateObj;
};
Loading

0 comments on commit 3ea4972

Please sign in to comment.