Skip to content

Commit

Permalink
fix: currupted data can not be committed to stage
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 17, 2022
1 parent 53c1627 commit bf06ee7
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 112 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "npm run resetTestDb && npx cross-env NODE_ENV=test mocha tests/**/*.spec.js --reporter spec --exit --timeout 30000",
"release": "./node_modules/.bin/standard-version && git push --tags",
"postinstall": "npm run requirements-check",
"resetdb": "rm -f ./data.sqlite3 && npx sequelize-cli db:migrate --env local && npx sequelize-cli db:seed:all --debug --env local",
"resetdb": "sequelize db:migrate:undo:all --env local && npx sequelize-cli db:migrate --env local && npx sequelize-cli db:seed:all --debug --env local",
"cleandb": "rm -f ./data.sqlite3 && npx sequelize-cli db:migrate --env local",
"resetTestDb": "rm -f ./test.sqlite3 && npx sequelize-cli db:migrate --env test && npx sequelize-cli db:seed:all --debug --env test && rm -f ./testMirror.sqlite3 && npx sequelize-cli db:migrate --env mirrorTest",
"resetMirrorDb": "npx sequelize-cli db:drop --env mirror && npx sequelize-cli db:create --env mirror && npx sequelize-cli db:migrate --env mirror --debug",
Expand Down
216 changes: 121 additions & 95 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,142 @@ import _ from 'lodash';
import * as fullNode from '../fullnode';

import { Staging, Project, Unit } from '../models';
import { assertStagingRecordExists } from '../utils/data-assertions';
import {
assertStagingRecordExists,
assertUnitRecordExists,
assertProjectRecordExists,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
const stagingData = await Staging.findAll();

const response = await Promise.all(
stagingData.map(async (data) => {
const workingData = _.cloneDeep(data.dataValues);
workingData.diff = {};
if (workingData.action === 'INSERT') {
workingData.diff.original = {};
workingData.diff.change = JSON.parse(workingData.data);
}

if (workingData.action === 'UPDATE') {
let original;
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
});
try {
const stagingData = await Staging.findAll();

const response = await Promise.all(
stagingData.map(async (data) => {
const workingData = _.cloneDeep(data.dataValues);
workingData.diff = {};
if (workingData.action === 'INSERT') {
workingData.diff.original = {};
workingData.diff.change = JSON.parse(workingData.data);
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { warehouseUnitId: workingData.uuid },
});
if (workingData.action === 'UPDATE') {
let original;
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
});
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { warehouseUnitId: workingData.uuid },
});
}

workingData.diff.original = original;
workingData.diff.change = JSON.parse(workingData.data);
}

workingData.diff.original = original;
workingData.diff.change = JSON.parse(workingData.data);
}

if (workingData.action === 'DELETE') {
let original;
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
});
if (workingData.action === 'DELETE') {
let original;
if (workingData.table === 'Projects') {
original = await Project.findOne({
where: { warehouseProjectId: workingData.uuid },
});
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { warehouseUnitId: workingData.uuid },
});
}

workingData.diff.original = original;
workingData.diff.change = {};
}

if (workingData.table === 'Units') {
original = await Unit.findOne({
where: { warehouseUnitId: workingData.uuid },
});
}

workingData.diff.original = original;
workingData.diff.change = {};
}

delete workingData.data;
delete workingData.data;

return workingData;
}),
);
return workingData;
}),
);

res.json(response);
res.json(response);
} catch (error) {
res.status(400).json({
message: 'Error retreiving staging table',
error: error.message,
});
}
};

export const commit = async (req, res) => {
const queryResponses = await Staging.findAll();

queryResponses.map(async (queryResponse) => {
const stagingRecord = queryResponse.dataValues;

const {
id: stagingRecordId,
uuid,
table,
action,
commited,
data: rawData,
} = stagingRecord;
let data = JSON.parse(rawData);

// set the commited flag to true
await Staging.update(
{ commited: true },
{ where: { id: stagingRecordId } },
try {
const queryResponses = await Staging.findAll();

await Promise.all(
queryResponses.map(async (queryResponse) => {
const stagingRecord = queryResponse.dataValues;

const {
id: stagingRecordId,
uuid,
table,
action,
commited,
data: rawData,
} = stagingRecord;
let data = JSON.parse(rawData);

if (table === 'Projects' && !commited) {
const customAssertionMessage = `The project record for the warehouseProjectId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
switch (action) {
case 'INSERT':
data.warehouseUnitId = uuid;
fullNode.createProjectRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
await assertProjectRecordExists(uuid, customAssertionMessage);
fullNode.updateProjectRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
await assertProjectRecordExists(uuid, customAssertionMessage);
fullNode.deleteProjectRecord(uuid, stagingRecordId);
break;
}
} else if (table === 'Units' && !commited) {
const customAssertionMessage = `The unit record for the warehouseUnitId: ${uuid} does not exist. Please remove ${uuid} from the staging table and try to commit again.`;
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
await assertUnitRecordExists(uuid, customAssertionMessage);
fullNode.updateUnitRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
await assertUnitRecordExists(uuid, customAssertionMessage);
fullNode.deleteUnitRecord(uuid, stagingRecordId);
break;
}
}

// set the commited flag to true
await Staging.update(
{ commited: true },
{ where: { id: stagingRecordId } },
);
}),
);

if (table === 'Projects' && !commited) {
switch (action) {
case 'INSERT':
data.warehouseUnitId = uuid;
fullNode.createProjectRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
fullNode.updateProjectRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
fullNode.deleteProjectRecord(uuid, stagingRecordId);
break;
}
} else if (table === 'Units' && !commited) {
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data, stagingRecordId);
break;
case 'UPDATE':
fullNode.updateUnitRecord(uuid, data, stagingRecordId);
break;
case 'DELETE':
fullNode.deleteUnitRecord(uuid, stagingRecordId);
break;
}
}
});

res.json({ message: 'Staging Table committed to full node' });
res.json({ message: 'Staging Table committed to full node' });
} catch (error) {
res.status(400).json({
message: 'Error commiting staging table',
error: error.message,
});
}
};

export const destroy = async (req, res) => {
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export const create = async (req, res) => {
data: JSON.stringify([newRecord]),
};

console.log(stagedData);

await Staging.create(stagedData);

res.json({
Expand Down
2 changes: 1 addition & 1 deletion src/models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export const safeMirrorDbHandler = (callback) => {
callback();
})
.catch(() => {
console.log('Mirror DB not connected');
// console.log('Mirror DB not connected');
});
};
22 changes: 13 additions & 9 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ app.use('/v1', V1Router);

sequelize.authenticate().then(() => console.log('Connected to database'));

app.use((err, req, res) => {
if (_.get(err, 'error.details')) {
// format Joi validation errors
return res.status(400).json({
message: 'API Validation error',
errors: err.error.details.map((detail) => detail.message),
});
app.use((err, req, res, next) => {
if (err) {
if (_.get(err, 'error.details')) {
// format Joi validation errors
return res.status(400).json({
message: 'API Validation error',
errors: err.error.details.map((detail) => detail.message),
});
}

return res.status(err.status).json(err);
}
return res.status(400).json(err);
});

next();
});
export default app;
16 changes: 12 additions & 4 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ export const assertOrgIsHomeOrg = async (orgUid) => {
return orgUid;
};

export const assertUnitRecordExists = async (warehouseUnitId) => {
export const assertUnitRecordExists = async (
warehouseUnitId,
customMessage,
) => {
const record = await Unit.findByPk(warehouseUnitId);
if (!record) {
throw new Error(
`The unit record for the warehouseUnitId: ${warehouseUnitId} does not exist.`,
customMessage ||
`The unit record for the warehouseUnitId: ${warehouseUnitId} does not exist.`,
);
}

Expand All @@ -57,11 +61,15 @@ export const assertStagingRecordExists = async (stagingId) => {
return record.dataValues;
};

export const assertProjectRecordExists = async (warehouseProjectId) => {
export const assertProjectRecordExists = async (
warehouseProjectId,
customMessage,
) => {
const record = await Project.findByPk(warehouseProjectId);
if (!record) {
throw new Error(
`The project record for the warehouseProjectId: ${warehouseProjectId} does not exist.`,
customMessage ||
`The project record for the warehouseProjectId: ${warehouseProjectId} does not exist.`,
);
}

Expand Down

0 comments on commit bf06ee7

Please sign in to comment.