From 39bfdd101108317001ea4a9031bf27c570c9022e Mon Sep 17 00:00:00 2001 From: William Wills Date: Fri, 2 Aug 2024 10:40:34 -0400 Subject: [PATCH 1/2] feat: verificationBody is now not required for issuances --- src/models/governance/governance.model.js | 2 +- src/models/staging/staging.model.js | 6 +++++ src/utils/helpers.js | 27 +++++++++++++++++++++++ src/validations/governance.validations.js | 2 +- src/validations/issuances.validation.js | 2 +- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/models/governance/governance.model.js b/src/models/governance/governance.model.js index e70cabd8..76dc9124 100644 --- a/src/models/governance/governance.model.js +++ b/src/models/governance/governance.model.js @@ -164,7 +164,7 @@ class Governance extends Model { if (!governanceBodyId) { throw new Error( - 'There is no Goverance Body that you own that can be edited', + 'There is no Governance Body that you own that can be edited', ); } diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index 5b0af3e4..bc02d041 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -22,6 +22,7 @@ import { createXlsFromSequelizeResults, transformFullXslsToChangeList, } from '../../utils/xls'; +import { updateNilVerificationBodyAsEmptyString } from '../../utils/helpers.js'; class Staging extends Model { static changes = new rxjs.Subject(); @@ -488,6 +489,11 @@ class Staging extends Model { throw new Error('No records to send to DataLayer'); } + // replace nil issuance validationBody values with empty strings + stagedRecords.forEach((record) => + updateNilVerificationBodyAsEmptyString(record), + ); + const [unitsChangeList, projectsChangeList] = await Promise.all([ Unit.generateChangeListFromStagedData(stagedRecords, comment, author), Project.generateChangeListFromStagedData(stagedRecords, comment, author), diff --git a/src/utils/helpers.js b/src/utils/helpers.js index 3d2cef5e..4334cf44 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -93,3 +93,30 @@ export const getDataModelVersion = () => { const majorVersion = version.split('.')[0]; return `v${majorVersion}`; }; + +/** + * the issuance table does not allow the verificationBody to be null. by requirement this field is nullable. + * this function defines null or undefined verificationBody for all issuances that exist in a staged record + * @param stagedRecord from the staging table + */ +export const updateNilVerificationBodyAsEmptyString = (stagedRecord) => { + try { + if (stagedRecord?.data && stagedRecord?.table === 'Projects') { + const data = JSON.parse(stagedRecord.data); + data?.issuances?.forEach((issuance) => { + if (!issuance?.validationBody) { + issuance.validationBody = ''; + } + }); + stagedRecord.data = JSON.stringify(data); + } else if (stagedRecord?.data && stagedRecord?.table === 'Units') { + const data = JSON.parse(stagedRecord.data); + if (data?.issuance && !data.issuance?.validationBody) { + data.issuance.validationBody = ''; + } + stagedRecord.data = JSON.stringify(data); + } + } catch { + return; + } +}; diff --git a/src/validations/governance.validations.js b/src/validations/governance.validations.js index 1dc7d285..9600f514 100644 --- a/src/validations/governance.validations.js +++ b/src/validations/governance.validations.js @@ -23,7 +23,7 @@ export const governancePickListSchema = Joi.object().keys({ ratingType: Joi.array().items(Joi.string()).min(1).required(), unitType: Joi.array().items(Joi.string()).min(1).required(), unitStatus: Joi.array().items(Joi.string()).min(1).required(), - verificationBody: Joi.array().items(Joi.string()).min(1).required(), + verificationBody: Joi.array().items(Joi.string()).min(1).optional(), projectTags: Joi.array().items(Joi.string()).min(1).required(), unitTags: Joi.array().items(Joi.string()).min(1).required(), coBenefits: Joi.array().items(Joi.string()).min(1).required(), diff --git a/src/validations/issuances.validation.js b/src/validations/issuances.validation.js index 1c28d4c5..8c5f2d27 100644 --- a/src/validations/issuances.validation.js +++ b/src/validations/issuances.validation.js @@ -7,7 +7,7 @@ export const issuanceSchema = Joi.object({ startDate: Joi.date().required(), endDate: Joi.date().min(Joi.ref('startDate')).required(), verificationApproach: Joi.string().required(), - verificationBody: Joi.string().required(), + verificationBody: Joi.string().allow('').optional(), verificationReportDate: Joi.date().required(), timeStaged: Joi.date().timestamp().allow(null).optional(), updatedAt: Joi.date().allow(null).optional(), From 5e614b08f797bd23f6932265e33a35382dd4833d Mon Sep 17 00:00:00 2001 From: William Wills Date: Fri, 2 Aug 2024 11:17:18 -0400 Subject: [PATCH 2/2] feat: corrected verificationBody empty string function --- src/utils/helpers.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/utils/helpers.js b/src/utils/helpers.js index 4334cf44..e5d2dd73 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -97,24 +97,26 @@ export const getDataModelVersion = () => { /** * the issuance table does not allow the verificationBody to be null. by requirement this field is nullable. * this function defines null or undefined verificationBody for all issuances that exist in a staged record - * @param stagedRecord from the staging table + * @param stagedItem from the staging table */ -export const updateNilVerificationBodyAsEmptyString = (stagedRecord) => { +export const updateNilVerificationBodyAsEmptyString = (stagedItem) => { try { - if (stagedRecord?.data && stagedRecord?.table === 'Projects') { - const data = JSON.parse(stagedRecord.data); - data?.issuances?.forEach((issuance) => { - if (!issuance?.validationBody) { - issuance.validationBody = ''; + if (stagedItem?.data) { + const data = JSON.parse(stagedItem.data); + data?.forEach((changeRecord) => { + if (stagedItem?.table === 'Projects') { + changeRecord?.issuances?.forEach((issuance) => { + if (!issuance?.verificationBody) { + issuance.verificationBody = ''; + } + }); + } else if (stagedItem?.table === 'Units') { + if (data?.issuance && !data.issuance?.verificationBody) { + data.issuance.verificationBody = ''; + } } }); - stagedRecord.data = JSON.stringify(data); - } else if (stagedRecord?.data && stagedRecord?.table === 'Units') { - const data = JSON.parse(stagedRecord.data); - if (data?.issuance && !data.issuance?.validationBody) { - data.issuance.validationBody = ''; - } - stagedRecord.data = JSON.stringify(data); + stagedItem.data = JSON.stringify(data); } } catch { return;