Delete Step

This commit is contained in:
Takeshi Kimata
2024-03-09 14:10:53 +09:00
parent 62df98db89
commit 0481e91277
5 changed files with 130 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
const express = require("express");
const router = express.Router();
const defineStep = require("../../models/steps");
const defineCaseStep = require("../../models/caseSteps");
const { DataTypes, Op } = require("sequelize");
module.exports = function (sequelize) {
const Step = defineStep(sequelize, DataTypes);
const CaseStep = defineCaseStep(sequelize, DataTypes);
router.delete("/:stepId", async (req, res) => {
const stepId = req.params.stepId;
const caseId = req.query.parentCaseId;
try {
const step = await Step.findByPk(stepId);
if (!step) {
return res.status(404).send("Step not found");
}
// Get caseStep to be deleted.
const deletingCaseStep = await CaseStep.findOne({
where: {
StepId: stepId,
},
});
// Decrease stepNo for all caseSteps with greater than the caseStep to be deleted.
await CaseStep.update(
{ stepNo: sequelize.literal("stepNo - 1") },
{
where: {
CaseId: caseId,
stepNo: {
[Op.gt]: deletingCaseStep.stepNo,
},
},
}
);
await step.destroy();
res.status(204).send();
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
return router;
};