From 2ab93a20d6cc4cdba6cc8c1f09245ae7c68bde26 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 18:27:39 -0500 Subject: [PATCH 1/6] fix: dont overwrite registryId on update --- package.json | 2 +- src/models/organizations/organizations.model.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9f38ddcb..34ff2db9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "climate-warehouse", - "version": "0.0.18", + "version": "0.0.19", "private": true, "bin": "build/server.js", "type": "module", diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 3ef6b332..c1caca66 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -200,7 +200,9 @@ class Organization extends Model { allSubscribedOrganizations.map((organization) => { const onResult = (data) => { const updateData = data.reduce((update, current) => { - update[current.key] = current.value; + if (current.key !== 'registryId') { + update[current.key] = current.value; + } return update; }, {}); From c054b0ff1bbfacc62ba61d9a087945e7dd8459b5 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 18:57:58 -0500 Subject: [PATCH 2/6] fix: dont send createdAt, updatedAt to datalayer --- src/controllers/project.controller.js | 4 +- src/controllers/units.controller.js | 4 +- .../organizations/organizations.model.js | 1 + src/models/units/units.model.js | 43 +++++++++++++------ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 6633074b..0ee8d493 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -89,7 +89,7 @@ export const create = async (req, res) => { uuid, action: 'INSERT', table: Project.stagingTableName, - data: JSON.stringify([newRecord]), + data: JSON.stringify([_.omit(newRecord, 'createdAt', 'updatedAt')]), }); res.json({ message: 'Project staged successfully' }); @@ -310,7 +310,7 @@ export const update = async (req, res) => { uuid: req.body.warehouseProjectId, action: 'UPDATE', table: Project.stagingTableName, - data: JSON.stringify(stagedRecord), + data: JSON.stringify(_.omit(stagedRecord, 'createdAt', 'updatedAt')), }; await Staging.upsert(stagedData); diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index e3d056eb..9ab784a4 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -92,7 +92,7 @@ export const create = async (req, res) => { uuid, action: 'INSERT', table: Unit.stagingTableName, - data: JSON.stringify([newRecord]), + data: JSON.stringify([_.omit(newRecord, 'createdAt', 'updatedAt')]), }; await Staging.create(stagedData); @@ -321,7 +321,7 @@ export const update = async (req, res) => { uuid: req.body.warehouseUnitId, action: 'UPDATE', table: Unit.stagingTableName, - data: JSON.stringify(stagedRecord), + data: JSON.stringify(_.omit(stagedRecord, 'createdAt', 'updatedAt')), }; await Staging.upsert(stagedData); diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index c1caca66..f09f3b4d 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -200,6 +200,7 @@ class Organization extends Model { allSubscribedOrganizations.map((organization) => { const onResult = (data) => { const updateData = data.reduce((update, current) => { + // TODO: this needs to pull the v1 record if (current.key !== 'registryId') { update[current.key] = current.value; } diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index f46e44e7..1d762351 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -11,7 +11,7 @@ import { import { Label, Issuance, Staging } from '../../models'; import { UnitMirror } from './units.model.mirror'; import ModelTypes from './units.modeltypes.cjs'; - +import { transformSerialNumberBlock } from '../../utils/helpers'; import { createXlsFromSequelizeResults, transformFullXslsToChangeList, @@ -25,34 +25,51 @@ const virtualFields = { unitBlockStart: { type: Sequelize.VIRTUAL, get() { - const rawValue = this.getDataValue('serialNumberBlock'); - if (!rawValue) { + const serialNumberBlock = this.getDataValue('serialNumberBlock'); + if (!serialNumberBlock) { return undefined; } - return rawValue.split('-')[0]; + const serialNumberPattern = this.getDataValue('serialNumberPattern'); + const [unitBlockStart] = transformSerialNumberBlock( + serialNumberBlock, + serialNumberPattern, + ); + + return unitBlockStart; }, }, unitBlockEnd: { type: Sequelize.VIRTUAL, get() { - const rawValue = this.getDataValue('serialNumberBlock'); - if (!rawValue) { + const serialNumberBlock = this.getDataValue('serialNumberBlock'); + if (!serialNumberBlock) { return undefined; } - return rawValue.split('-')[1]; + + const serialNumberPattern = this.getDataValue('serialNumberPattern'); + const [, unitBlockEnd] = transformSerialNumberBlock( + serialNumberBlock, + serialNumberPattern, + ); + + return unitBlockEnd; }, }, unitCount: { type: Sequelize.VIRTUAL, get() { - const rawValue = this.getDataValue('serialNumberBlock'); - if (!rawValue) { + const serialNumberBlock = this.getDataValue('serialNumberBlock'); + if (!serialNumberBlock) { return undefined; } - const blocks = rawValue.split('-'); - const blockStart = Number(blocks[0].split(/(\d+)/)[1]); - const blockEnd = Number(blocks[1].split(/(\d+)/)[1]); - return blockEnd - blockStart; + + const serialNumberPattern = this.getDataValue('serialNumberPattern'); + const [unitBlockStart, unitBlockEnd] = transformSerialNumberBlock( + serialNumberBlock, + serialNumberPattern, + ); + + return Number(unitBlockEnd) - Number(unitBlockStart); }, }, }; From 4703b4d07dcabf325ff8a5b49b2d089b41fd1988 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 19:09:06 -0500 Subject: [PATCH 3/6] fix: staging table diff --- src/models/staging/staging.model.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index 5469b1f3..377676e1 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -86,7 +86,7 @@ class Staging extends Model { }); } - diff.original = original; + diff.original = _.omit(original, 'createdAt', 'updatedAt'); diff.change = JSON.parse(data); } @@ -115,7 +115,7 @@ class Staging extends Model { }); } - diff.original = original; + diff.original = _.omit(original, 'createdAt', 'updatedAt'); diff.change = {}; } From 0eb10834c7b984e41eacc113df9f15a5a059c2a3 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 19:43:19 -0500 Subject: [PATCH 4/6] refactor: revert omit fields --- src/controllers/project.controller.js | 4 ++-- src/controllers/units.controller.js | 4 ++-- src/models/staging/staging.model.js | 4 ++-- tests/integration/project.spec.js | 2 +- tests/test-fixtures/staging-fixtures.js | 1 + 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 0ee8d493..6633074b 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -89,7 +89,7 @@ export const create = async (req, res) => { uuid, action: 'INSERT', table: Project.stagingTableName, - data: JSON.stringify([_.omit(newRecord, 'createdAt', 'updatedAt')]), + data: JSON.stringify([newRecord]), }); res.json({ message: 'Project staged successfully' }); @@ -310,7 +310,7 @@ export const update = async (req, res) => { uuid: req.body.warehouseProjectId, action: 'UPDATE', table: Project.stagingTableName, - data: JSON.stringify(_.omit(stagedRecord, 'createdAt', 'updatedAt')), + data: JSON.stringify(stagedRecord), }; await Staging.upsert(stagedData); diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index 9ab784a4..e3d056eb 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -92,7 +92,7 @@ export const create = async (req, res) => { uuid, action: 'INSERT', table: Unit.stagingTableName, - data: JSON.stringify([_.omit(newRecord, 'createdAt', 'updatedAt')]), + data: JSON.stringify([newRecord]), }; await Staging.create(stagedData); @@ -321,7 +321,7 @@ export const update = async (req, res) => { uuid: req.body.warehouseUnitId, action: 'UPDATE', table: Unit.stagingTableName, - data: JSON.stringify(_.omit(stagedRecord, 'createdAt', 'updatedAt')), + data: JSON.stringify(stagedRecord), }; await Staging.upsert(stagedData); diff --git a/src/models/staging/staging.model.js b/src/models/staging/staging.model.js index 377676e1..5469b1f3 100644 --- a/src/models/staging/staging.model.js +++ b/src/models/staging/staging.model.js @@ -86,7 +86,7 @@ class Staging extends Model { }); } - diff.original = _.omit(original, 'createdAt', 'updatedAt'); + diff.original = original; diff.change = JSON.parse(data); } @@ -115,7 +115,7 @@ class Staging extends Model { }); } - diff.original = _.omit(original, 'createdAt', 'updatedAt'); + diff.original = original; diff.change = {}; } diff --git a/tests/integration/project.spec.js b/tests/integration/project.spec.js index 62d81e34..f740b8c1 100644 --- a/tests/integration/project.spec.js +++ b/tests/integration/project.spec.js @@ -23,7 +23,7 @@ describe('Project Resource Integration Tests', function () { homeOrgUid = await testFixtures.getHomeOrgId(); }); - it('deletes a project end-to-end (with simulator)', async function () { + it.only('deletes a project end-to-end (with simulator)', async function () { /* Basic Idea for this test is that we are going to create a project and verify that the new project propagates through the data layer and into our db. Then we are going diff --git a/tests/test-fixtures/staging-fixtures.js b/tests/test-fixtures/staging-fixtures.js index e115e7d1..8cadb15c 100644 --- a/tests/test-fixtures/staging-fixtures.js +++ b/tests/test-fixtures/staging-fixtures.js @@ -14,6 +14,7 @@ export const resetStagingTable = async () => { export const getLastCreatedStagingRecord = async () => { const result = await supertest(app).get('/v1/staging'); + console.log('!!!!!!', result.body); expect(result.body).to.be.an('array'); return _.last(result.body); }; From ff827b9e47c9a2248f6ff8359c143bb326331b72 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 19:58:57 -0500 Subject: [PATCH 5/6] refactor: remove only --- tests/integration/project.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/project.spec.js b/tests/integration/project.spec.js index f740b8c1..62d81e34 100644 --- a/tests/integration/project.spec.js +++ b/tests/integration/project.spec.js @@ -23,7 +23,7 @@ describe('Project Resource Integration Tests', function () { homeOrgUid = await testFixtures.getHomeOrgId(); }); - it.only('deletes a project end-to-end (with simulator)', async function () { + it('deletes a project end-to-end (with simulator)', async function () { /* Basic Idea for this test is that we are going to create a project and verify that the new project propagates through the data layer and into our db. Then we are going From 5e11312143857c366d689685c226bc87d2f2a569 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sat, 12 Mar 2022 20:26:53 -0500 Subject: [PATCH 6/6] refactor: remove console.log --- tests/test-fixtures/staging-fixtures.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test-fixtures/staging-fixtures.js b/tests/test-fixtures/staging-fixtures.js index 8cadb15c..e115e7d1 100644 --- a/tests/test-fixtures/staging-fixtures.js +++ b/tests/test-fixtures/staging-fixtures.js @@ -14,7 +14,6 @@ export const resetStagingTable = async () => { export const getLastCreatedStagingRecord = async () => { const result = await supertest(app).get('/v1/staging'); - console.log('!!!!!!', result.body); expect(result.body).to.be.an('array'); return _.last(result.body); };