Skip to content

Commit

Permalink
feat: add uuid validation to update and delete controller
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Jan 15, 2022
1 parent 567f87c commit 3a2b071
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
102 changes: 67 additions & 35 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import _ from 'lodash';

import csv from 'csvtojson';
import { Readable } from 'stream';
import { uuid as uuidv4 } from 'uuidv4';
import { sequelize } from '../models/database';

import {
Staging,
ProjectMock,
Project,
ProjectLocation,
Qualification,
Expand Down Expand Up @@ -126,6 +126,17 @@ export const findOne = async (req, res) => {

export const update = async (req, res) => {
try {
const originalRecordResult = await Project.findByPk(
req.body.warehouseProjectId,
);

if (!originalRecordResult) {
res.status(404).json({
message: `The Project record for the warehouseUnitId: ${req.body.warehouseProjectId} does not exist and can not be updated`,
});
return;
}

const stagedData = {
uuid: req.body.warehouseProjectId,
action: 'UPDATE',
Expand All @@ -148,6 +159,17 @@ export const update = async (req, res) => {

export const destroy = async (req, res) => {
try {
const originalRecordResult = await Project.findByPk(
req.body.warehouseProjectId,
);

if (!originalRecordResult) {
res.status(404).json({
message: `The Project record for the warehouseUnitId: ${req.body.warehouseProjectId} does not exist and can not be updated`,
});
return;
}

const stagedData = {
uuid: req.body.warehouseProjectId,
action: 'DELETE',
Expand Down Expand Up @@ -180,43 +202,53 @@ export const batchUpload = async (req, res) => {

csv()
.fromStream(stream)
.subscribe(
async (newRecord) => {
// When creating new projects assign a uuid to is so
.subscribe(async (newRecord) => {
let action = 'UPDATE';

if (newRecord.warehouseProjectId) {
// Fail if they supplied their own warehouseUnitId and it doesnt exist
const possibleExistingRecord = await Project.findByPk(
newRecord.warehouseProjectId,
);

if (!possibleExistingRecord) {
throw new Error(
`Trying to update a record that doesnt exists, ${newRecord.warehouseProjectId}. To fix, remove the warehouseProjectId from this record so it will be treated as a new record`,
);
}
} else {
// When creating new unitd assign a uuid to is so
// multiple organizations will always have unique ids
const uuid = uuidv4();

newRecord.warehouseProjectId = uuid;

// All new projects are assigned to the home orgUid
const orgUid = _.head(Object.keys(await Organization.getHomeOrg()));
newRecord.orgUid = orgUid;

// The new project is getting created in this registry
newRecord.currentRegistry = orgUid;
action = 'INSERT';
}

// Unless we are overriding, a new project originates in this org
if (!newRecord.registryOfOrigin) {
newRecord.registryOfOrigin = orgUid;
}
const orgUid = _.head(Object.keys(await Organization.getHomeOrg()));

try {
await Staging.create({
uuid,
action: 'INSERT',
table: 'Projects',
data: JSON.stringify([newRecord]),
});
res.json('Added project to stage');
} catch (err) {
res.json(err);
}
},
(err) => {
req
.status(400)
.json({ message: 'There was an error processing the csv file' });
},
() => res.json({ message: 'CSV processing complete' }),
);
// All new records are registered within this org, but give them a chance to override this
if (!newRecord.orgUid) {
newRecord.orgUid = orgUid;
}

const stagedData = {
uuid: newRecord.warehouseProjectId,
action: action,
table: 'Projects',
data: JSON.stringify([newRecord]),
};

await Staging.upsert(stagedData);
})
.on('error', (error) => {
if (!res.headersSent) {
res.status(400).json({ message: error.message });
}
})
.on('done', () => {
if (!res.headersSent) {
res.json({ message: 'CSV processing complete' });
}
});
};
18 changes: 18 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ export const findOne = async (req, res) => {

export const update = async (req, res) => {
try {
const originalRecordResult = await Unit.findByPk(req.body.warehouseUnitId);

if (!originalRecordResult) {
res.status(404).json({
message: `The unit record for the warehouseUnitId: ${req.body.warehouseUnitId} does not exist and can not be updated`,
});
return;
}

const stagedData = {
uuid: req.body.warehouseUnitId,
action: 'UPDATE',
Expand All @@ -125,12 +134,21 @@ export const update = async (req, res) => {
} catch (err) {
res.json({
message: 'Error updating new unit',
error: err.message,
});
}
};

export const destroy = async (req, res) => {
try {
const originalRecordResult = await Unit.findByPk(req.body.warehouseUnitId);

if (!originalRecordResult) {
res.status(404).json({
message: `The unit record for the warehouseUnitId: ${req.body.warehouseUnitId} does not exist and can not be deleted`,
});
return;
}
const stagedData = {
uuid: req.body.warehouseUnitId,
action: 'DELETE',
Expand Down

0 comments on commit 3a2b071

Please sign in to comment.