From fd41e9efcffd1bd2f9e94703f10a9674bfbe1563 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Fri, 6 May 2022 13:56:51 -0400 Subject: [PATCH] feat: add unitCount fields to unit and split forms --- src/controllers/units.controller.js | 33 ++++++-------- ...20220504180739-add-serial-number-fields.js | 2 +- src/models/projects/projects.model.js | 2 - src/utils/data-assertions.js | 45 +++++++------------ src/validations/units.validations.js | 4 +- tests/integration/unit.spec.js | 17 ++++--- tests/test-data/new-unit.json | 5 ++- tests/test-data/update-unit.json | 5 ++- 8 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index f30adbd5..5a3064ec 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -10,13 +10,11 @@ import { columnsToInclude, optionallyPaginatedResponse, paginationParams, - createSerialNumberStr, } from '../utils/helpers'; import { assertOrgIsHomeOrg, assertUnitRecordExists, - assertSumOfSplitUnitsIsValid, assertCsvFileInRequest, assertHomeOrgExists, assertNoPendingCommits, @@ -51,6 +49,7 @@ export const create = async (req, res) => { newRecord.warehouseUnitId = uuid; newRecord.timeStaged = Math.floor(Date.now() / 1000); + newRecord.serialNumberBlock = `${newRecord.unitBlockStart}-${newRecord.unitBlockEnd}`; // All new units are assigned to the home orgUid const { orgUid } = await Organization.getHomeOrg(); @@ -270,6 +269,7 @@ export const update = async (req, res) => { // All new units are assigned to the home orgUid const { orgUid } = await Organization.getHomeOrg(); updatedRecord.orgUid = orgUid; + updatedRecord.serialNumberBlock = `${updatedRecord.unitBlockStart}-${updatedRecord.unitBlockEnd}`; if (updatedRecord.labels) { const promises = updatedRecord.labels.map(async (childRecord) => { @@ -382,13 +382,7 @@ export const split = async (req, res) => { await assertOrgIsHomeOrg(originalRecord.orgUid); - const { unitBlockStart } = assertSumOfSplitUnitsIsValid( - originalRecord.serialNumberBlock, - new RegExp(originalRecord.serialNumberPattern), - req.body.records, - ); - - let lastAvailableUnitBlock = unitBlockStart; + let totalSplitCount = 0; const splitRecords = await Promise.all( req.body.records.map(async (record, index) => { @@ -399,18 +393,11 @@ export const split = async (req, res) => { } newRecord.unitCount = record.unitCount; + totalSplitCount += record.unitCount; - const newUnitBlockStart = lastAvailableUnitBlock; - lastAvailableUnitBlock += Number(record.unitCount); - const newUnitBlockEnd = lastAvailableUnitBlock; - // move to the next available block - lastAvailableUnitBlock += 1; - - newRecord.serialNumberBlock = createSerialNumberStr( - originalRecord.serialNumberBlock, - newUnitBlockStart, - newUnitBlockEnd, - ); + newRecord.serialNumberBlock = `${record.unitBlockStart}-${record.unitBlockEnd}`; + newRecord.unitBlockStart = record.unitBlockStart; + newRecord.unitBlockEnd = record.unitBlockEnd; if (record.unitOwner) { newRecord.unitOwner = record.unitOwner; @@ -430,6 +417,12 @@ export const split = async (req, res) => { }), ); + if (totalSplitCount !== originalRecord.unitCount) { + throw new Error( + `Your total split coount is ${totalSplitCount} units and the original record is ${originalRecord.unitCount} units`, + ); + } + const stagedData = { uuid: req.body.warehouseUnitId, action: 'UPDATE', diff --git a/src/database/migrations/20220504180739-add-serial-number-fields.js b/src/database/migrations/20220504180739-add-serial-number-fields.js index c2baa968..3b31bbec 100644 --- a/src/database/migrations/20220504180739-add-serial-number-fields.js +++ b/src/database/migrations/20220504180739-add-serial-number-fields.js @@ -12,7 +12,7 @@ export default { allowNull: true, }), queryInterface.addColumn('units', 'unitCount', { - type: Sequelize.STRING, + type: Sequelize.INTEGER, allowNull: true, }), ]); diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 0e9ae63f..257f6f4e 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -363,8 +363,6 @@ class Project extends Model { isUpdateComment, ); - console.log('#############', commentChangeList); - return { projects: [ ..._.get(insertChangeList, 'project', []), diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index 07c9f624..f629899a 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -3,12 +3,25 @@ import _ from 'lodash'; import { Organization, Unit, Project, Staging, Meta } from '../models'; -import { transformSerialNumberBlock } from '../utils/helpers'; import datalayer from '../datalayer'; import { formatModelAssociationName } from './model-utils.js'; import { getConfig } from '../utils/config-loader'; -const { IS_GOVERNANCE_BODY, READ_ONLY, USE_SIMULATOR } = getConfig().APP; +const { IS_GOVERNANCE_BODY, READ_ONLY, USE_SIMULATOR, CHIA_NETWORK } = + getConfig().APP; + +export const assertChiaNetworkMatchInConfiguration = async () => { + if (!USE_SIMULATOR) { + const networkInfo = await datalayer.getActiveNetwork(); + const network = _.get(networkInfo, 'network_name', ''); + + if (!network.includes(CHIA_NETWORK)) { + throw new Error( + `Your node is on ${network} but your climate warehouse is set to ${CHIA_NETWORK}, please change your config so they match`, + ); + } + } +}; export const assertCanBeGovernanceBody = async () => { if (IS_GOVERNANCE_BODY !== 'true') { @@ -210,31 +223,3 @@ export const assertProjectRecordExists = async ( return record.dataValues; }; - -export const assertSumOfSplitUnitsIsValid = ( - serialNumberBlock, - serialNumberPattern, - splitRecords, -) => { - const sumOfSplitUnits = splitRecords.reduce( - (previousValue, currentValue) => - previousValue.unitCount + currentValue.unitCount, - ); - - const [unitBlockStart, unitBlockEnd, unitCount] = transformSerialNumberBlock( - serialNumberBlock, - serialNumberPattern, - ); - - if (sumOfSplitUnits !== unitCount) { - throw new Error( - `The sum of the split units is ${sumOfSplitUnits} and the original record is ${unitCount}. These should be the same.`, - ); - } - - return { - unitBlockStart, - unitBlockEnd, - unitCount, - }; -}; diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index 66954429..c88739f9 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -15,8 +15,6 @@ const unitsBaseSchema = { .custom(pickListValidation('countries', 'countryJurisdictionOfOwner')) .required(), inCountryJurisdictionOfOwner: Joi.string().optional(), - // must be in the form ABC123-XYZ456 - serialNumberBlock: Joi.string().required(), unitBlockStart: Joi.string().required(), unitBlockEnd: Joi.string().required(), unitCount: Joi.number().integer().required(), @@ -77,6 +75,8 @@ export const unitsSplitSchema = Joi.object({ .items( Joi.object().keys({ unitCount: Joi.number().required(), + unitBlockStart: Joi.string().required(), + unitBlockEnd: Joi.string().required(), unitOwner: Joi.string().optional(), countryJurisdictionOfOwner: Joi.string() .custom(pickListValidation('countries', 'countryJurisdictionOfOwner')) diff --git a/tests/integration/unit.spec.js b/tests/integration/unit.spec.js index 225b69b6..f96c6ada 100644 --- a/tests/integration/unit.spec.js +++ b/tests/integration/unit.spec.js @@ -113,8 +113,9 @@ describe('Unit Resource Integration Tests', function () { it('splits an existing unit end-to-end (with simulator)', async function () { // create and commit the unit to be deleted const createdUnitResult = await supertest(app).post('/v1/units').send({ - serialNumberBlock: 'AXJJFSLGHSHEJ9000-AXJJFSLGHSHEJ9010', - serialNumberPattern: '[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$', + unitBlockStart: 'AXJJFSLGHSHEJ9000', + unitBlockEnd: 'AXJJFSLGHSHEJ9010', + unitCount: 10, countryJurisdictionOfOwner: 'Austria', unitOwner: 'TEST_OWNER', unitType: 'Reduction - nature', @@ -171,10 +172,14 @@ describe('Unit Resource Integration Tests', function () { records: [ { unitCount: unitRecord.unitCount - 1, + unitBlockStart: 'AXJJFSLGHSHEJ9000', + unitBlockEnd: 'AXJJFSLGHSHEJ9009', unitOwner: newUnitOwner, }, { unitCount: 1, + unitBlockStart: 'AXJJFSLGHSHEJ9009', + unitBlockEnd: 'AXJJFSLGHSHEJ9010', }, ], }; @@ -209,7 +214,7 @@ describe('Unit Resource Integration Tests', function () { expect(splitRecord1.unitOwner).to.equal(newUnitOwner); expect(splitRecord2.unitOwner).to.equal(unitRecord.unitOwner); - expect(splitRecord1.unitCount).to.equal(10); + expect(splitRecord1.unitCount).to.equal(9); expect(splitRecord2.unitCount).to.equal(1); // Expect the split unitscounts to add up to the original unit count @@ -290,9 +295,6 @@ describe('Unit Resource Integration Tests', function () { 'labels', // mapped associated field 'issuance', // mapped associated field 'issuanceId', - 'unitBlockStart', // virtual field - 'unitBlockEnd', // virtual field - 'unitCount', // virtual field 'createdAt', // meta field 'updatedAt', // meta field ]), @@ -334,9 +336,6 @@ describe('Unit Resource Integration Tests', function () { 'labels', // mapped associated field 'issuance', // mapped associated field 'issuanceId', - 'unitBlockStart', // virtual field - 'unitBlockEnd', // virtual field - 'unitCount', // virtual field 'createdAt', // meta field 'updatedAt', // meta field ]), diff --git a/tests/test-data/new-unit.json b/tests/test-data/new-unit.json index 0c26c3ff..be0a8cc2 100644 --- a/tests/test-data/new-unit.json +++ b/tests/test-data/new-unit.json @@ -3,8 +3,9 @@ "countryJurisdictionOfOwner": "Netherlands", "projectLocationId": "lkjw2er1-nj23-1212-532-dsjdx5-k131", "inCountryJurisdictionOfOwner": "California", - "serialNumberBlock": "AXJJFSLGHSHEJ1000-AXJJFSLGHSHEJ1010", - "serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$", + "unitBlockStart": "AXJJFSLGHSHEJ1000", + "unitBlockEnd": "AXJJFSLGHSHEJ1010", + "unitCount": 10, "vintageYear": 2016, "unitType": "Removal - nature", "marketplace": "California Carbon", diff --git a/tests/test-data/update-unit.json b/tests/test-data/update-unit.json index fa0d71fb..cf9a8dc4 100644 --- a/tests/test-data/update-unit.json +++ b/tests/test-data/update-unit.json @@ -3,8 +3,9 @@ "countryJurisdictionOfOwner": "Australia", "projectLocationId": "UPDATED", "inCountryJurisdictionOfOwner": "UPDATED", - "serialNumberBlock": "AXJJFSLGHSHEJ2000-AXJJFSLGHSHEJ2010", - "serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$", + "unitBlockStart": "AXJJFSLGHSHEJ2000", + "unitBlockEnd": "AXJJFSLGHSHEJ2010", + "unitCount": 10, "vintageYear": 1970, "unitType": "Reduction - technical", "marketplace": "UPDATED",