-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NRPT-117: Adds Permit and Amendment import with flavours (#467)
* NRPT-117: Adds Permit and Amendment import with flavours
- Loading branch information
1 parent
a3a0483
commit 3ea4972
Showing
23 changed files
with
886 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.