Allow admins to soft-delete cases and teams without schema changes

This commit is contained in:
Vladimir Kutkovoy committed 2026-11-21 10:31:35 +00:00
1 parent 3252eee87b
commit b54574499e
6 files changed
+234 -4

No files matched your search

+28 -1
View File
@@ -9,7 +9,7 @@ import { validateCaseIcon } from '../utils/case-icon.js';
import { expertRole } from '../utils/expert-roles.js';
import { assignmentCases, assertTechnicalAvailable } from '../utils/case-assignments.js';
const { Case, Team, User, UserInCase, Mark } = models;
const { Case, Team, TeamInCase, User, UserInCase, Mark } = models;
const include = [
{ model: Team, as: 'teams', through: { attributes: ['technicalComment'] } },
{
@@ -89,6 +89,33 @@ export default {
await task.update(caseFields(body));
res.json({ case: await Case.findByPk(task.id, { include }) });
},
async deleteCase({ params }, res) {
await sequelize.transaction(async transaction => {
const task = await Case.findByPk(params.caseId, { transaction, lock: transaction.LOCK.UPDATE });
if (!task) throw new AppErrorNotExist('case');
// Existing deleted_at hides the case while preserving teams, assignments and marks.
await task.destroy({ transaction });
});
res.status(204).end();
},
async deleteTeam({ params }, res) {
await sequelize.transaction(async transaction => {
const task = await Case.findByPk(params.caseId, { transaction, lock: transaction.LOCK.UPDATE });
if (!task) throw new AppErrorNotExist('case');
const link = await TeamInCase.findOne({
where: { caseId: task.id, teamId: params.teamId },
transaction,
lock: transaction.LOCK.UPDATE,
});
if (!link) throw new AppErrorNotExist('team');
const team = await Team.findByPk(params.teamId, { transaction, lock: transaction.LOCK.UPDATE });
if (!team) throw new AppErrorNotExist('team');
await link.destroy({ transaction });
// Old databases may link the same team to several cases. Keep other cases intact.
if (!(await team.countCases({ transaction }))) await team.destroy({ transaction });
});
res.status(204).end();
},
async createTeam({ params, body }, res) {
const payload = fields(body);
const team = await sequelize.transaction(async transaction => {