Skip to content

Commit

Permalink
Merge pull request #55 from Chia-Network/feature/simulation2
Browse files Browse the repository at this point in the history
feat: handle staging commit
  • Loading branch information
MichaelTaylor3D authored Dec 16, 2021
2 parents cef53d1 + b33ed49 commit 427f0cc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 27 deletions.
27 changes: 19 additions & 8 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 committed to full node' });
};

export const destroy = (req, res) => {
Expand Down
24 changes: 12 additions & 12 deletions src/fullnode/fullnode.js
Original file line number Diff line number Diff line change
@@ -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);
};
64 changes: 57 additions & 7 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,113 @@
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({
where: {
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({
where: {
uuid,
},
});

if (stagingRecordId) {
await Staging.destroy({
where: {
id: stagingRecordId,
},
});
}
resolve();
}, THIRTY_SEC);
});
Expand Down

0 comments on commit 427f0cc

Please sign in to comment.