From abe50858c5879404ae32a0c13f21d6c09c88512e Mon Sep 17 00:00:00 2001 From: "Michael.Taylor" Date: Wed, 15 Dec 2021 17:55:18 -0500 Subject: [PATCH] feat: add mandatory associations to models --- 0001-feat-handle-staging-commit.patch | 239 ++++++++++++++++++ src/models/co-benefits/co-benefits.model.js | 3 + src/models/locations/locations.model.js | 3 + src/models/ratings/ratings.model.js | 3 + .../related-projects.model.js | 3 + 5 files changed, 251 insertions(+) create mode 100644 0001-feat-handle-staging-commit.patch diff --git a/0001-feat-handle-staging-commit.patch b/0001-feat-handle-staging-commit.patch new file mode 100644 index 00000000..1f0e2416 --- /dev/null +++ b/0001-feat-handle-staging-commit.patch @@ -0,0 +1,239 @@ +From cb688a5c41d4c50d7b26f58130baab98d54feff3 Mon Sep 17 00:00:00 2001 +From: "Michael.Taylor" +Date: Wed, 15 Dec 2021 17:14:53 -0500 +Subject: [PATCH] feat: handle staging commit + +--- + src/controllers/staging.controller.js | 27 ++++++++---- + src/fullnode/fullnode.js | 24 +++++------ + src/fullnode/simulator.js | 62 ++++++++++++++++++++++++--- + 3 files changed, 86 insertions(+), 27 deletions(-) + +diff --git a/src/controllers/staging.controller.js b/src/controllers/staging.controller.js +index 1307d04..2cff3bb 100644 +--- a/src/controllers/staging.controller.js ++++ b/src/controllers/staging.controller.js +@@ -55,36 +55,47 @@ export const commit = async (req, res) => { + const queryResponse = await Staging.findAll(); + const stagingRecords = queryResponse.dataValues; + stagingRecords.forEach(async (stagingRecord) => { +- const { uuid, table, action, data: rawData } = stagingRecord; ++ const { ++ id: stagingRecordId, ++ uuid, ++ table, ++ action, ++ data: rawData, ++ } = stagingRecord; + const data = JSON.parse(rawData); + ++ await Staging.update( ++ { commited: true }, ++ { where: { id: stagingRecordId } }, ++ ); ++ + if (table === 'Projects') { + switch (action) { + case 'INSERT': +- fullNode.createProjectRecord(uuid, data); ++ fullNode.createProjectRecord(uuid, data, stagingRecordId); + break; + case 'UPDATE': +- fullNode.updateProjectRecord(uuid, data); ++ fullNode.updateProjectRecord(uuid, data, stagingRecordId); + break; + case 'DELETE': +- fullNode.deleteProjectRecord(uuid); ++ fullNode.deleteProjectRecord(uuid, stagingRecordId); + break; + } + } else if (table === 'Unit') { + switch (action) { + case 'INSERT': +- fullNode.createUnitRecord(uuid, data); ++ fullNode.createUnitRecord(uuid, data, stagingRecordId); + break; + case 'UPDATE': +- fullNode.updateUnitRecord(uuid, data); ++ fullNode.updateUnitRecord(uuid, data, stagingRecordId); + break; + case 'DELETE': +- fullNode.deleteUnitRecord(uuid); ++ fullNode.deleteUnitRecord(uuid, stagingRecordId); + break; + } + } + }); +- res.json({ message: 'Not implemented' }); ++ res.json({ message: 'Staging Table commited to fullnode' }); + }; + + export const destroy = (req, res) => { +diff --git a/src/fullnode/fullnode.js b/src/fullnode/fullnode.js +index 4ed8e3d..ce77bc5 100644 +--- a/src/fullnode/fullnode.js ++++ b/src/fullnode/fullnode.js +@@ -1,25 +1,25 @@ + import * as simulator from './simulator'; + +-export const updateProjectRecord = async (uuid, record) => { +- await simulator.updateProjectRecord(uuid, record); ++export const updateProjectRecord = async (uuid, record, stagingRecordId) => { ++ await simulator.updateProjectRecord(uuid, record, stagingRecordId); + }; + +-export const createProjectRecord = async (uuid, record) => { +- await simulator.createProjectRecord(uuid, record); ++export const createProjectRecord = async (uuid, record, stagingRecordId) => { ++ await simulator.createProjectRecord(uuid, record, stagingRecordId); + }; + +-export const deleteProjectRecord = async (uuid) => { +- await simulator.deleteProjectRecord(uuid); ++export const deleteProjectRecord = async (uuid, stagingRecordId) => { ++ await simulator.deleteProjectRecord(uuid, stagingRecordId); + }; + +-export const updateUnitRecord = async (uuid, record) => { +- await simulator.updateUnitRecord(uuid, record); ++export const updateUnitRecord = async (uuid, record, stagingRecordId) => { ++ await simulator.updateUnitRecord(uuid, record, stagingRecordId); + }; + +-export const createUnitRecord = async (uuid, record) => { +- await simulator.createUnitRecord(uuid, record); ++export const createUnitRecord = async (uuid, record, stagingRecordId) => { ++ await simulator.createUnitRecord(uuid, record, stagingRecordId); + }; + +-export const deleteUnitRecord = async (uuid) => { +- await simulator.deleteUnitRecord(uuid); ++export const deleteUnitRecord = async (uuid, stagingRecordId) => { ++ await simulator.deleteUnitRecord(uuid, stagingRecordId); + }; +diff --git a/src/fullnode/simulator.js b/src/fullnode/simulator.js +index 9d6e093..887733e 100644 +--- a/src/fullnode/simulator.js ++++ b/src/fullnode/simulator.js +@@ -1,26 +1,42 @@ +-import { Project, Unit } from '../models'; ++import { Project, Unit, Staging } from '../models'; + const THIRTY_SEC = 30000; + + // Simulate 30 seconds passing before commited to node + +-export const updateProjectRecord = async (uuid, record) => { ++export const updateProjectRecord = async (uuid, record, stagingRecordId) => { + await deleteProjectRecord(uuid); + await createProjectRecord(uuid, record); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + }; + +-export const createProjectRecord = (uuid, record) => { ++export const createProjectRecord = (uuid, record, stagingRecordId) => { + return new Promise((resolve) => { + setTimeout(async () => { + await Project.create({ + ...record, + warehouseProjectId: uuid, + }); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + resolve(); + }, THIRTY_SEC); + }); + }; + +-export const deleteProjectRecord = (uuid) => { ++export const deleteProjectRecord = (uuid, stagingRecordId) => { + return new Promise((resolve) => { + setTimeout(async () => { + await Project.destroy({ +@@ -28,29 +44,53 @@ export const deleteProjectRecord = (uuid) => { + warehouseProjectId: uuid, + }, + }); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + resolve(); + }, THIRTY_SEC); + }); + }; + +-export const updateUnitRecord = async (uuid, record) => { ++export const updateUnitRecord = async (uuid, record, stagingRecordId) => { + await deleteUnitRecord(uuid); + await createUnitRecord(uuid, record); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + }; + +-export const createUnitRecord = (uuid, record) => { ++export const createUnitRecord = (uuid, record, stagingRecordId) => { + return new Promise((resolve) => { + setTimeout(async () => { + await Unit.create({ + uuid, + ...record, + }); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + resolve(); + }, THIRTY_SEC); + }); + }; + +-export const deleteUnitRecord = (uuid) => { ++export const deleteUnitRecord = (uuid, stagingRecordId) => { + return new Promise((resolve) => { + setTimeout(async () => { + await Unit.destroy({ +@@ -58,6 +98,14 @@ export const deleteUnitRecord = (uuid) => { + uuid, + }, + }); ++ ++ if (stagingRecordId) { ++ await Staging.destroy({ ++ where: { ++ id: stagingRecordId, ++ }, ++ }); ++ } + resolve(); + }, THIRTY_SEC); + }); +-- +2.34.1.windows.1 + diff --git a/src/models/co-benefits/co-benefits.model.js b/src/models/co-benefits/co-benefits.model.js index 5137d409..b8d91ffb 100644 --- a/src/models/co-benefits/co-benefits.model.js +++ b/src/models/co-benefits/co-benefits.model.js @@ -10,6 +10,9 @@ class CoBenefit extends Model { static associate() { CoBenefit.belongsTo(Project, { onDelete: 'CASCADE', + foreignKey: { + allowNull: false, + }, }); } } diff --git a/src/models/locations/locations.model.js b/src/models/locations/locations.model.js index 4a5febea..10947deb 100644 --- a/src/models/locations/locations.model.js +++ b/src/models/locations/locations.model.js @@ -10,6 +10,9 @@ class ProjectLocation extends Model { static associate() { ProjectLocation.belongsTo(Project, { onDelete: 'CASCADE', + foreignKey: { + allowNull: false, + }, }); } } diff --git a/src/models/ratings/ratings.model.js b/src/models/ratings/ratings.model.js index c5af466d..2d0048b7 100644 --- a/src/models/ratings/ratings.model.js +++ b/src/models/ratings/ratings.model.js @@ -10,6 +10,9 @@ class Rating extends Model { static associate() { Rating.belongsTo(Project, { onDelete: 'CASCADE', + foreignKey: { + allowNull: false, + }, }); } } diff --git a/src/models/related-projects/related-projects.model.js b/src/models/related-projects/related-projects.model.js index 6f5465bb..d68a2ad7 100644 --- a/src/models/related-projects/related-projects.model.js +++ b/src/models/related-projects/related-projects.model.js @@ -11,6 +11,9 @@ class RelatedProject extends Model { static associate() { RelatedProject.belongsTo(Project, { onDelete: 'CASCADE', + foreignKey: { + allowNull: false, + }, }); } }