Skip to content

Commit

Permalink
feat: add mandatory associations to models
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Dec 15, 2021
1 parent c0fdacd commit abe5085
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 0 deletions.
239 changes: 239 additions & 0 deletions 0001-feat-handle-staging-commit.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
From cb688a5c41d4c50d7b26f58130baab98d54feff3 Mon Sep 17 00:00:00 2001
From: "Michael.Taylor" <[email protected]>
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

3 changes: 3 additions & 0 deletions src/models/co-benefits/co-benefits.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class CoBenefit extends Model {
static associate() {
CoBenefit.belongsTo(Project, {
onDelete: 'CASCADE',
foreignKey: {
allowNull: false,
},
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/locations/locations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class ProjectLocation extends Model {
static associate() {
ProjectLocation.belongsTo(Project, {
onDelete: 'CASCADE',
foreignKey: {
allowNull: false,
},
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/ratings/ratings.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Rating extends Model {
static associate() {
Rating.belongsTo(Project, {
onDelete: 'CASCADE',
foreignKey: {
allowNull: false,
},
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/related-projects/related-projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class RelatedProject extends Model {
static associate() {
RelatedProject.belongsTo(Project, {
onDelete: 'CASCADE',
foreignKey: {
allowNull: false,
},
});
}
}
Expand Down

0 comments on commit abe5085

Please sign in to comment.