Skip to content

Commit

Permalink
fix: split unit validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Jan 6, 2022
1 parent 74b3e99 commit 985fa97
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ export const split = async (req, res) => {
where: { uuid: req.body.unitUid },
});

if (!originalRecord) {
res.status(404).json({
message: `The unit record for the uuid: ${req.body.unitUid} does not exist`,
});
return;
}

const sumOfSplitUnits = req.body.records.reduce(
(previousValue, currentValue) =>
previousValue.unitCount + currentValue.unitCount,
);

if (sumOfSplitUnits !== originalRecord.unitCount) {
res.status(404).json({
message: `The sum of the split units is ${sumOfSplitUnits} and the original record is ${originalRecord.unitCount}. These should be the same.`,
});
return;
}

const splitRecords = req.body.records.map((record) => {
const newRecord = _.cloneDeep(originalRecord);
newRecord.uuid = uuidv4();
Expand All @@ -172,12 +191,11 @@ export const split = async (req, res) => {
await Staging.upsert(stagedData);

res.json({
message: 'Unit split successfully',
message: 'Unit split successful',
});
} catch (err) {
res.json({
message: 'Error splitting unit',
err,
});
}
};

0 comments on commit 985fa97

Please sign in to comment.