From 3e60735b746661fe87d511fee27fbd9b07f2bd1b Mon Sep 17 00:00:00 2001 From: Samuel Gerard Date: Thu, 20 Jun 2024 15:48:32 -0700 Subject: [PATCH] [BUG][NRPTI-1233] Amalgamated permits not showing in BCMI (#1243) * unrelated minor fixes * Add getPermitType method to PermitUtils * Add support for Amalgamated Permit code * Revert datasource.js * Fix typo * created migration that updates the type field based on the typeCode of the associate records * linting fixes * Replace ternary operators with function calls * Replace ternary operator with function call in add component * moved permitName to utils and refactored to support * removed minePermitNames from individual files as they are now in recordUtils * added minePermitNames to recordUtils * Fix mine permit bugs due to typos * linting fix 3: The Revenge of Lint --------- Co-authored-by: acatchpole Co-authored-by: acatchpole <113044739+acatchpole@users.noreply.github.com> --- .github/ISSUE_TEMPLATE/bug.md | 2 +- .../mines-record-add.component.html | 4 +- .../mines-record-add.component.ts | 1 - .../mines-records-detail.component.html | 8 +- .../mines-records-detail.component.ts | 16 + .../mines-records-edit.component.html | 4 +- .../mines-records-edit.component.ts | 1 - .../src/app/records/utils/record-utils.ts | 436 +++++++++--------- .../common/src/app/utils/record-constants.ts | 1 + ...619192233-updateBCMICollectionTypeField.js | 74 +++ api/src/controllers/mine-controller.js | 2 +- api/src/integrations/core/datasource.js | 12 +- api/src/integrations/core/permit-utils.js | 11 + api/src/models/bcmi/permit-bcmi.js | 2 +- api/src/models/master/permit.js | 2 +- 15 files changed, 343 insertions(+), 233 deletions(-) create mode 100644 api/migrations/20240619192233-updateBCMICollectionTypeField.js diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md index e40de7e7b..301382c07 100644 --- a/.github/ISSUE_TEMPLATE/bug.md +++ b/.github/ISSUE_TEMPLATE/bug.md @@ -2,7 +2,7 @@ name: Bug about: An undesirable behaviour that needs correction title: '' -labels: Spike +labels: Bug assignees: '' --- diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.html b/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.html index aa2ea8547..732705dbd 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.html +++ b/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.html @@ -45,8 +45,8 @@ * diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.ts b/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.ts index 095dbd5a1..6c8f1bf0d 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.ts +++ b/angular/projects/admin-nrpti/src/app/mines/mines-record-add/mines-record-add.component.ts @@ -43,7 +43,6 @@ export class MinesRecordAddComponent implements OnInit, OnDestroy { return item.displayName; }) .sort(); - public permitTypes = ['OGP', 'AMD']; // Documents public documents = []; diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.html b/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.html index 59aec394d..37391309f 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.html +++ b/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.html @@ -37,13 +37,7 @@

Record Information

- {{ - record && record.typeCode && record.typeCode === 'OGP' - ? 'Original Permit' - : record && record.typeCode && record.typeCode === 'AMD' - ? 'Permit Amendment' - : '-' - }} + {{ this.permitType(record) }}
diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.ts b/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.ts index 5c6941646..dc46b3004 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.ts +++ b/angular/projects/admin-nrpti/src/app/mines/mines-records-detail/mines-records-detail.component.ts @@ -74,6 +74,22 @@ export class MinesRecordDetailComponent implements OnInit, OnDestroy { return agencyDataService.displayNameFull(agency); } + permitType(record) { + if (record && record.typeCode) { + switch (record.typeCode) { + case 'OGP': + return 'Original Permit'; + case 'AMD': + return 'Permit Amendment'; + case 'ALG': + return 'Amalgamated Permit'; + default: + return '-'; + } + } + return '-'; + } + ngOnDestroy() { this.ngUnsubscribe.next(); this.ngUnsubscribe.complete(); diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.html b/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.html index 478d4d490..73e32b99a 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.html +++ b/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.html @@ -67,8 +67,8 @@

Edit record

diff --git a/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.ts b/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.ts index e9a3e1dd5..e7a885e6a 100644 --- a/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.ts +++ b/angular/projects/admin-nrpti/src/app/mines/mines-records-edit/mines-records-edit.component.ts @@ -49,7 +49,6 @@ export class MinesRecordsEditComponent implements OnInit { return item.displayName; }) .sort(); - public permitTypes = ['OGP', 'AMD']; public recordAgencies = Picklists.collectionAgencyCodePicklist; diff --git a/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts b/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts index 3ec13b205..24d576dd8 100644 --- a/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts +++ b/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts @@ -77,214 +77,228 @@ import { RecordComponent } from './record-component'; import { ActDataServiceNRPTI } from '../../../../../global/src/lib/utils/act-data-service-nrpti'; export class RecordUtils { - /** - * Given a single record object, find the matching model based on the records _schemaName, and return a new instance. - * Returns null if no matching model found. - * - * @static - * @param {*} data - * @returns {object} new instance of record model, or null. - * @memberof RecordUtils - */ - static getRecordModelInstance(data: any): object { - if (!data || !data._schemaName) { - return null; - } - - // We can find and create the model instance via reflection - // This only works when our Schema Name is IDENTICAL to the class name - // So when creating new models/flavours for records, always ensure the class - // name and schema name match, case sensitive. - // If you're getting errors here, you've likely named your model class - // something different, so make sure you fix the name so it matches your Schema - return this.getReflectiveInstance(models, data._schemaName, data); - } - - /** - * Given a record type, return the matching detail component type, or null if no matching component found. - * Returns null if no matching detail component found. - * - * @static - * @param {string} recordType - * @returns {Type} the record detail component, or null. - * @memberof RecordUtils - */ - static getRecordDetailComponent(recordType: string): Type { - if (!recordType) { - return null; - } - - // we can't use a reflective visitor with these because - // importing from RecordModule would cause circular dependencies. - switch (recordType) { - case 'OrderNRCED': - return OrderNRCEDDetailComponent; - case 'OrderLNG': - return OrderLNGDetailComponent; - case 'InspectionNRCED': - return InspectionNRCEDDetailComponent; - case 'InspectionLNG': - return InspectionLNGDetailComponent; - case 'CertificateLNG': - return CertificateLNGDetailComponent; - case 'PermitLNG': - return PermitLNGDetailComponent; - case 'AgreementLNG': - return AgreementLNGDetailComponent; - case 'SelfReportLNG': - return SelfReportLNGDetailComponent; - case 'RestorativeJusticeNRCED': - return RestorativeJusticeNRCEDDetailComponent; - case 'RestorativeJusticeLNG': - return RestorativeJusticeLNGDetailComponent; - case 'TicketLNG': - return TicketLNGDetailComponent; - case 'TicketNRCED': - return TicketNRCEDDetailComponent; - case 'AdministrativePenaltyLNG': - return AdministrativePenaltyLNGDetailComponent; - case 'AdministrativePenaltyNRCED': - return AdministrativePenaltyNRCEDDetailComponent; - case 'AdministrativePenaltyBCMI': - return AdministrativePenaltyBCMIDetailComponent; - case 'AdministrativeSanctionLNG': - return AdministrativeSanctionLNGDetailComponent; - case 'AdministrativeSanctionNRCED': - return AdministrativeSanctionNRCEDDetailComponent; - case 'WarningLNG': - return WarningLNGDetailComponent; - case 'WarningNRCED': - return WarningNRCEDDetailComponent; - case 'ConstructionPlanLNG': - return ConstructionPlanLNGDetailComponent; - case 'ManagementPlanLNG': - return ManagementPlanLNGDetailComponent; - case 'CourtConvictionBCMI': - return CourtConvictionBCMIDetailComponent; - case 'CourtConvictionLNG': - return CourtConvictionLNGDetailComponent; - case 'CourtConvictionNRCED': - return CourtConvictionNRCEDDetailComponent; - case 'CertificateAmendmentLNG': - return CertificateAmendmentLNGDetailComponent; - case 'CertificateAmendmentBCMI': - return CertificateAmendmentBCMIDetailComponent; - case 'CorrespondenceNRCED': - return CorrespondenceNRCEDDetailComponent; - case 'CorrespondenceBCMI': - return CorrespondenceBCMIDetailComponent; - case 'ReportNRCED': - return ReportNRCEDDetailComponent; - case 'ReportBCMI': - return ReportBCMIDetailComponent; - case 'DamSafetyInspectionNRCED': - return DamSafetyInspectionNRCEDDetailComponent; - case 'DamSafetyInspectionBCMI': - return DamSafetyInspectionBCMIDetailComponent; - case 'AnnualReportBCMI': - return AnnualReportBCMIDetailComponent; - default: - return null; - } - } - - // links is an array of objects that contain documents without upfile and with url already populated. - // documents is an array of objects that contain documents with upfile and without url. - // documentsToDelete is an array of objectIds whicfh map to existing documents in the database. - async handleDocumentChanges(links = [], documents = [], documentsToDelete = [], recordId, factoryService) { - const promises = []; - - // Handle adding links - links.forEach(async link => { - const formData = new FormData(); - formData.append('fileName', link.fileName); - formData.append('url', link.url); - promises.push(factoryService.createDocument(formData, recordId)); - }); - - // Handle adding S3 Docs - documents.forEach(async doc => { - const formData = new FormData(); - formData.append('fileName', doc.fileName); - formData.append('upfile', doc.upfile); - promises.push(factoryService.createDocument(formData, recordId)); - }); - - // Handle deleting documents - documentsToDelete.forEach(async docId => { - promises.push(factoryService.deleteDocument(docId, recordId)); - }); - - // Execute - return Promise.all(promises).catch(e => { - alert('Server Error: ' + e.error); - }); - } - - parseResForErrors(res) { - if (!res || !res.length || !res[0] || !res[0].length || !res[0][0]) { - alert('Failed to save record.'); - } - - if (res[0][0].status === 'failure') { - alert('Failed to save master record.'); - } - - if (res[0][0].flavours) { - let flavourFailure = false; - res[0][0].flavours.forEach(flavour => { - if (flavour.status === 'failure') { - flavourFailure = true; - } - }); - if (flavourFailure) { - alert('Failed to save one or more flavour records'); - } - } - } - - static getReflectiveInstance(context: any, name: string, ...args: any[]) { - const instance = Object.create(context[name].prototype); - - if (args) { - instance.constructor.apply(instance, args); - } - - return instance; - } - - /** - * Replaces the 'act' value in the given record object with a corresponding act code. - * @param {Object} record - The object containing legislation information. - * @param {ServiceFactory} factoryService - The service factory used to create data service instances. - * @returns {void} Modifies the record object in place. - */ - replaceActTitleWithCode(record, factoryService) { - if (!record || !record.legislation || !record.legislation[0] || !record.legislation[0].act) { - return; - } - - const actTitle = record.legislation[0].act; - const dataservice = new ActDataServiceNRPTI(factoryService); - const actCode = dataservice.getCodeFromTitle(actTitle); - if (!actCode) { - return; - } - record.legislation[0].act = actCode; - } - - /** - * Replaces the 'act' value in the given record object with a corresponding act code. - * @param {string} actCode - an intermediate code mapped to a title - * @param {ServiceFactory} factoryService - The service factory used to create data service instances. - * @returns {string} The title associated with the act code - */ - replaceActCodeWithTitle(actCode, factoryService) { - if (!actCode) { - return actCode; - } - const dataservice = new ActDataServiceNRPTI(factoryService); - const actTitle = dataservice.displayActTitleFull(actCode); - return actTitle; - } -} + /** + * Given a single record object, find the matching model based on the + * records _schemaName, and return a new instance. + * Returns null if no matching model found. + * + * @static + * @param {*} data + * @returns {object} new instance of record model, or null. + * @memberof RecordUtils + */ + static getRecordModelInstance(data: any): object { + if (!data || !data._schemaName) { + return null; + } + + // We can find and create the model instance via reflection + // This only works when our Schema Name is IDENTICAL to the class name + // So when creating new models/flavours for records, always ensure the class + // name and schema name match, case sensitive. + // If you're getting errors here, you've likely named your model class + // something different, so make sure you fix the name so it matches your Schema + return this.getReflectiveInstance(models, data._schemaName, data); + } + + /** + * Given a record type, return the matching detail component type, or null if no matching component found. + * Returns null if no matching detail component found. + * + * @static + * @param {string} recordType + * @returns {Type} the record detail component, or null. + * @memberof RecordUtils + */ + static getRecordDetailComponent(recordType: string): Type { + if (!recordType) { + return null; + } + + // we can't use a reflective visitor with these because + // importing from RecordModule would cause circular dependencies. + switch (recordType) { + case 'OrderNRCED': + return OrderNRCEDDetailComponent; + case 'OrderLNG': + return OrderLNGDetailComponent; + case 'InspectionNRCED': + return InspectionNRCEDDetailComponent; + case 'InspectionLNG': + return InspectionLNGDetailComponent; + case 'CertificateLNG': + return CertificateLNGDetailComponent; + case 'PermitLNG': + return PermitLNGDetailComponent; + case 'AgreementLNG': + return AgreementLNGDetailComponent; + case 'SelfReportLNG': + return SelfReportLNGDetailComponent; + case 'RestorativeJusticeNRCED': + return RestorativeJusticeNRCEDDetailComponent; + case 'RestorativeJusticeLNG': + return RestorativeJusticeLNGDetailComponent; + case 'TicketLNG': + return TicketLNGDetailComponent; + case 'TicketNRCED': + return TicketNRCEDDetailComponent; + case 'AdministrativePenaltyLNG': + return AdministrativePenaltyLNGDetailComponent; + case 'AdministrativePenaltyNRCED': + return AdministrativePenaltyNRCEDDetailComponent; + case 'AdministrativePenaltyBCMI': + return AdministrativePenaltyBCMIDetailComponent; + case 'AdministrativeSanctionLNG': + return AdministrativeSanctionLNGDetailComponent; + case 'AdministrativeSanctionNRCED': + return AdministrativeSanctionNRCEDDetailComponent; + case 'WarningLNG': + return WarningLNGDetailComponent; + case 'WarningNRCED': + return WarningNRCEDDetailComponent; + case 'ConstructionPlanLNG': + return ConstructionPlanLNGDetailComponent; + case 'ManagementPlanLNG': + return ManagementPlanLNGDetailComponent; + case 'CourtConvictionBCMI': + return CourtConvictionBCMIDetailComponent; + case 'CourtConvictionLNG': + return CourtConvictionLNGDetailComponent; + case 'CourtConvictionNRCED': + return CourtConvictionNRCEDDetailComponent; + case 'CertificateAmendmentLNG': + return CertificateAmendmentLNGDetailComponent; + case 'CertificateAmendmentBCMI': + return CertificateAmendmentBCMIDetailComponent; + case 'CorrespondenceNRCED': + return CorrespondenceNRCEDDetailComponent; + case 'CorrespondenceBCMI': + return CorrespondenceBCMIDetailComponent; + case 'ReportNRCED': + return ReportNRCEDDetailComponent; + case 'ReportBCMI': + return ReportBCMIDetailComponent; + case 'DamSafetyInspectionNRCED': + return DamSafetyInspectionNRCEDDetailComponent; + case 'DamSafetyInspectionBCMI': + return DamSafetyInspectionBCMIDetailComponent; + case 'AnnualReportBCMI': + return AnnualReportBCMIDetailComponent; + default: + return null; + } + } + + // links is an array of objects that contain documents without upfile and with url already populated. + // documents is an array of objects that contain documents with upfile and without url. + // documentsToDelete is an array of objectIds whicfh map to existing documents in the database. + async handleDocumentChanges(links = [], documents = [], documentsToDelete = [], recordId, factoryService) { + const promises = []; + + // Handle adding links + links.forEach(async link => { + const formData = new FormData(); + formData.append('fileName', link.fileName); + formData.append('url', link.url); + promises.push(factoryService.createDocument(formData, recordId)); + }); + + // Handle adding S3 Docs + documents.forEach(async doc => { + const formData = new FormData(); + formData.append('fileName', doc.fileName); + formData.append('upfile', doc.upfile); + promises.push(factoryService.createDocument(formData, recordId)); + }); + + // Handle deleting documents + documentsToDelete.forEach(async docId => { + promises.push(factoryService.deleteDocument(docId, recordId)); + }); + + // Execute + return Promise.all(promises).catch(e => { + alert('Server Error: ' + e.error); + }); + } + + parseResForErrors(res) { + if (!res || !res.length || !res[0] || !res[0].length || !res[0][0]) { + alert('Failed to save record.'); + } + + if (res[0][0].status === 'failure') { + alert('Failed to save master record.'); + } + + if (res[0][0].flavours) { + let flavourFailure = false; + res[0][0].flavours.forEach(flavour => { + if (flavour.status === 'failure') { + flavourFailure = true; + } + }); + if (flavourFailure) { + alert('Failed to save one or more flavour records'); + } + } + } + + static getReflectiveInstance(context: any, name: string, ...args: any[]) { + const instance = Object.create(context[name].prototype); + + if (args) { + instance.constructor.apply(instance, args); + } + + return instance; + } + + /** + * Replaces the 'act' value in the given record object with a corresponding act code. + * @param {Object} record - The object containing legislation information. + * @param {ServiceFactory} factoryService - The service factory used to create data service instances. + * @returns {void} Modifies the record object in place. + */ + replaceActTitleWithCode(record, factoryService) { + if (!record || !record.legislation || !record.legislation[0] || !record.legislation[0].act) { + return; + } + + const actTitle = record.legislation[0].act; + const dataservice = new ActDataServiceNRPTI(factoryService); + const actCode = dataservice.getCodeFromTitle(actTitle); + if (!actCode) { + return; + } + record.legislation[0].act = actCode; + } + + /** + * Replaces the 'act' value in the given record object with a corresponding act code. + * @param {string} actCode - an intermediate code mapped to a title + * @param {ServiceFactory} factoryService - The service factory used to create data service instances. + * @returns {string} The title associated with the act code + */ + replaceActCodeWithTitle(actCode, factoryService) { + if (!actCode) { + return actCode; + } + const dataservice = new ActDataServiceNRPTI(factoryService); + const actTitle = dataservice.displayActTitleFull(actCode); + return actTitle; + } + + public minePermitTypes = ['ALG', 'OGP', 'AMD']; + + minePermitName(typeCode: string): string { + switch (typeCode) { + case 'OGP': + return 'Permit'; + case 'ALG': + return 'Amalgamated Permit'; + default: + return 'Permit Amendment'; // AMD + } + } + } diff --git a/angular/projects/common/src/app/utils/record-constants.ts b/angular/projects/common/src/app/utils/record-constants.ts index e73df4438..d878fb484 100644 --- a/angular/projects/common/src/app/utils/record-constants.ts +++ b/angular/projects/common/src/app/utils/record-constants.ts @@ -274,6 +274,7 @@ export class Picklists { public static readonly collectionTypePicklist = [ 'Annual Report', + 'Amalgamated Permit', 'Certificate Amendment', 'Certificate', 'Compliance Self Report', diff --git a/api/migrations/20240619192233-updateBCMICollectionTypeField.js b/api/migrations/20240619192233-updateBCMICollectionTypeField.js new file mode 100644 index 000000000..710984b2e --- /dev/null +++ b/api/migrations/20240619192233-updateBCMICollectionTypeField.js @@ -0,0 +1,74 @@ +'use strict'; + +var dbm; +var type; +var seed; + +/** + * Update the 'type' of CollectionBCMI entries + * Checks each permit in the 'record' array for its typeCode + * Assigns the type to the Collection based on the permit's typeCode, with ALG having priority + */ +exports.setup = function(options, seedLink) { + dbm = options.dbmigrate; + type = dbm.dataType; + seed = seedLink; +}; + +exports.up = async function(db) { + console.log('**** Updating bcmi collections type ****'); + const mClient = await db.connection.connect(db.connectionString, { native_parser: true }); + + try { + const nrpti = await mClient.collection('nrpti'); + + const collections = await nrpti.find({ _schemaName: 'CollectionBCMI' }).toArray(); + const permits = await nrpti.find({ _schemaName: 'PermitBCMI' }).toArray(); + + let count = 0; + + collections.forEach(async collection => { + let typeCode = ''; + // Go through each record attached to each collection + // For each record, check the typeCode + const oldType = collection.type; + for (const recordId of collection.records) { + const record = permits.find(permit => permit._id.toString() == recordId.toString()); + // ALG takes precedence, so don't overwrite + if (record && record.typeCode && typeCode != record.typeCode && typeCode != 'ALG') { + typeCode = record.typeCode; + } + } + if (typeCode) { + switch (typeCode) { + case 'OGP': + collection.type = 'Permit'; + break; + case 'ALG': + collection.type = 'Amalgamated Permit'; + break; + default: + collection.type = 'Permit Amendment'; + } + if (oldType != collection.type) { + count += 1; + await nrpti.update({ _id: collection._id }, { $set: collection }); + } + } + }); + + console.log('**** Finished updating ' + count + ' bcmi collection types ****'); + } catch (error) { + console.error(`Migration did not complete. Error processing collections: ${error.message}`); + } + + mClient.close(); +}; + +exports.down = function(db) { + return null; +}; + +exports._meta = { + version: 1 +}; diff --git a/api/src/controllers/mine-controller.js b/api/src/controllers/mine-controller.js index 28b325dd8..b0d82fd41 100644 --- a/api/src/controllers/mine-controller.js +++ b/api/src/controllers/mine-controller.js @@ -72,7 +72,7 @@ exports.protectedPut = async function (args, res, next) { const sanitizedObj = PutUtils.validateObjectAgainstModel(MineBCMI, incomingObj); - if (!sanitizedObj || sanitizedObj === {}) { + if (!sanitizedObj || Object.keys(sanitizedObj).length === 0) { // skip, as there are no changes to master record return; } diff --git a/api/src/integrations/core/datasource.js b/api/src/integrations/core/datasource.js index 53ea4cf4d..38a8f1fd3 100644 --- a/api/src/integrations/core/datasource.js +++ b/api/src/integrations/core/datasource.js @@ -230,12 +230,12 @@ class CoreDataSource { // Create a new collections if possible. if (savedRecord._schemaName === 'MineBCMI') { - await this.createorUpdateCollections(collectionUtils, permitInfo.permit, savedRecord); + await this.createorUpdateCollections(collectionUtils, permitUtils, permitInfo.permit, savedRecord); } else { if (!savedRecord.length > 0 || savedRecord[0].status !== 'success') { throw Error('processRecord - savedRecord is null.'); } - await this.createorUpdateCollections(collectionUtils, permitInfo.permit, savedRecord[0].object[0]); + await this.createorUpdateCollections(collectionUtils, permitUtils, permitInfo.permit, savedRecord[0].object[0]); } this.status.itemsProcessed++; @@ -511,7 +511,7 @@ class CoreDataSource { return completeRecords; } - async createorUpdateCollections(collectionUtils, permit, mineRecord) { + async createorUpdateCollections(collectionUtils,permitUtils, permit, mineRecord) { if (!collectionUtils) { throw new Error('createorUpdateCollections - param collectionUtils is null.'); } @@ -540,7 +540,7 @@ class CoreDataSource { project: mineRecord._id, name: amendment.description !== null ? amendment.description : 'Permit Documents', date: amendment.issue_date ? new Date(amendment.issue_date) : null, - type: amendment.permit_amendment_type_code === 'OGP' ? 'Permit' : 'Permit Amendment', + type: permitUtils.getPermitType(amendment.permit_amendment_type_code), agency: 'AGENCY_EMLI', records: (existingPermits && existingPermits.map(permit => permit._id)) || [] }; @@ -556,12 +556,14 @@ class CoreDataSource { if ( existingCollection.name !== amendment.description || Date.parse(existingCollection.date) !== Date.parse(amendment.issue_date) || - existingCollection.records.length != existingPermits.length + existingCollection.records.length != existingPermits.length || + existingCollection.type != permitUtils.getPermitType(amendment.permit_amendment_type_code) ) { const updateCollection = { _sourceRefCoreCollectionId: amendment.permit_amendment_guid, name: amendment.description, date: amendment.issue_date ? new Date(amendment.issue_date) : null, + type: permitUtils.getPermitType(amendment.permit_amendment_type_code), records: (existingPermits && existingPermits.map(permit => permit._id)) || [] }; diff --git a/api/src/integrations/core/permit-utils.js b/api/src/integrations/core/permit-utils.js index 928d6ceb5..789ff4658 100644 --- a/api/src/integrations/core/permit-utils.js +++ b/api/src/integrations/core/permit-utils.js @@ -109,6 +109,17 @@ class Permits extends BaseRecordUtils { return permits; } + + getPermitType(typeCode) { + switch(typeCode){ + case 'OGP': + return 'Permit'; + case 'ALG': + return 'Amalgamated Permit'; + default: + return 'Permit Amendment' //AMD + } + } } module.exports = Permits; diff --git a/api/src/models/bcmi/permit-bcmi.js b/api/src/models/bcmi/permit-bcmi.js index fa94ce891..e11af0d75 100644 --- a/api/src/models/bcmi/permit-bcmi.js +++ b/api/src/models/bcmi/permit-bcmi.js @@ -24,7 +24,7 @@ module.exports = require('../../utils/model-schema-generator')( amendmentStatusCode: { type: String, default: '' }, // Amendment doc type code, either OGP (original permit) or AMD (Amendment) // If the Type is AMD, the OGP document ref will be applied to the originalPermit - typeCode: { type: String, default: 'OGP' }, // OGP or AMD + typeCode: { type: String, default: 'OGP' }, // OGP or AMD or ALG // Original Permit GUID, only populated for AMD types originalPermit: { type: 'ObjectId', default: null, index: true }, receivedDate: { type: Date, default: null }, diff --git a/api/src/models/master/permit.js b/api/src/models/master/permit.js index 6cb40520e..9b14bb910 100644 --- a/api/src/models/master/permit.js +++ b/api/src/models/master/permit.js @@ -54,7 +54,7 @@ module.exports = require('../../utils/model-schema-generator')( amendmentStatusCode: { type: String, default: '' }, // Amendment doc type code, either OGP (original permit) or AMD (Amendment) // If the Type is AMD, the OGP document ref will be applied to the originalPermit - typeCode: { type: String, default: 'OGP' }, // OGP or AMD + typeCode: { type: String, default: 'OGP' }, // OGP or AMD or ALG // Original Permit GUID, only populated for AMD types originalPermit: { type: 'ObjectId', default: null, index: true },