Skip to content

Commit

Permalink
feat: add simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Dec 14, 2021
1 parent d41dbb9 commit d87d7c7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import * as fullNode from '../fullnode;';
import { Staging, StagingMock, Project, Unit } from '../models';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -50,6 +51,42 @@ export const findAll = async (req, res) => {
res.json(response);
};

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 data = JSON.parse(rawData);

if (table === 'Projects') {
switch (action) {
case 'INSERT':
fullNode.createProjectRecord(uuid, data);
break;
case 'UPDATE':
fullNode.updateProjectRecord(uuid, data);
break;
case 'DELETE':
fullNode.deleteProjectRecord(uuid);
break;
}
} else if (table === 'Unit') {
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data);
break;
case 'UPDATE':
fullNode.updateUnitRecord(uuid, data);
break;
case 'DELETE':
fullNode.deleteUnitRecord(uuid);
break;
}
}
});
res.json({ message: 'Not implemented' });
};

export const destroy = (req, res) => {
Staging.destroy({
where: {
Expand Down
25 changes: 25 additions & 0 deletions src/fullnode/fullnode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as simulator from './simulator';

export const updateProjectRecord = async (uuid, record) => {
await simulator.updateProjectRecord(uuid, record);
};

export const createProjectRecord = async (uuid, record) => {
await simulator.createProjectRecord(uuid, record);
};

export const deleteProjectRecord = async (uuid) => {
await simulator.deleteProjectRecord(uuid);
};

export const updateUnitRecord = async (uuid, record) => {
await simulator.updateUnitRecord(uuid, record);
};

export const createUnitRecord = async (uuid, record) => {
await simulator.createUnitRecord(uuid, record);
};

export const deleteUnitRecord = async (uuid) => {
await simulator.deleteUnitRecord(uuid);
};
1 change: 1 addition & 0 deletions src/fullnode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fullnode';
64 changes: 64 additions & 0 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Project, Unit } from '../models';
const THIRTY_SEC = 30000;

// Simulate 30 seconds passing before commited to node

export const updateProjectRecord = async (uuid, record) => {
await deleteProjectRecord(uuid);
await createProjectRecord(uuid, record);
};

export const createProjectRecord = (uuid, record) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Project.create({
...record,
warehouseProjectId: uuid,
});
resolve();
}, THIRTY_SEC);
});
};

export const deleteProjectRecord = (uuid) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Project.destroy({
where: {
warehouseProjectId: uuid,
},
});
resolve();
}, THIRTY_SEC);
});
};

export const updateUnitRecord = async (uuid, record) => {
await deleteUnitRecord(uuid);
await createUnitRecord(uuid, record);
};

export const createUnitRecord = (uuid, record) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Unit.create({
uuid,
...record,
});
resolve();
}, THIRTY_SEC);
});
};

export const deleteUnitRecord = (uuid) => {
return new Promise(async (resolve) => {
setTimeout(() => {
await Unit.destroy({
where: {
uuid,
},
});
resolve();
}, THIRTY_SEC);
});
};
2 changes: 2 additions & 0 deletions src/routes/v1/resources/stagings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ StagingRouter.delete(
StagingController.destroy,
);

StagingRouter.post('/commit', StagingController.commit);

export { StagingRouter };

0 comments on commit d87d7c7

Please sign in to comment.