update runCase at once

This commit is contained in:
Takeshi Kimata
2024-07-07 22:45:40 +09:00
parent 908400eb86
commit 6f562f3532
12 changed files with 143 additions and 364 deletions

View File

@@ -8,31 +8,68 @@ module.exports = function (sequelize) {
const { verifyProjectReporterFromRunId } = require('../../middleware/verifyEditable')(sequelize);
const RunCase = defineRunCase(sequelize, DataTypes);
router.put('/', verifySignedIn, verifyProjectReporterFromRunId, async (req, res) => {
router.post('/update', verifySignedIn, verifyProjectReporterFromRunId, async (req, res) => {
const runId = req.query.runId;
const caseId = req.query.caseId;
const status = req.query.status;
const runCases = req.body;
const t = await sequelize.transaction();
console.log('############## start edit');
console.log(runCases);
console.log('############## end edit');
const createRunCase = async (runCase) => {
const newRunCase = await RunCase.create(
{
runId: runId,
caseId: runCase.caseId,
status: 0,
},
{ transaction: t }
);
return newRunCase;
};
const deleteRunCase = async (runCase) => {
await RunCase.destroy({
where: { runId: runId, caseId: runCase.caseId },
transaction: t,
});
return null;
};
const updateRunCase = async (runCase) => {
await RunCase.update(
{
status: runCase.status,
},
{
where: { id: runCase.id },
transaction: t,
}
);
return step;
};
try {
const runCase = await RunCase.findOne({
where: {
runId: runId,
caseId: caseId,
},
});
const results = await Promise.all(
runCases.map(async (step) => {
if (step.editState === 'new') {
return createRunCase(step);
} else if (step.editState === 'deleted') {
return deleteRunCase(step);
} else if (step.editState === 'changed') {
return updateRunCase(step);
} else if (step.editState === 'notChanged') {
return step;
}
})
);
if (!runCase) {
return res.status(404).send('Runcase not found');
}
await runCase.update({
runId,
caseId,
status,
});
res.json(runCase);
await t.commit();
res.json(results.filter((result) => result !== null));
} catch (error) {
console.error(error);
await t.rollback();
res.status(500).send('Internal Server Error');
}
});