import { Op } from 'sequelize'; import { models } from '../models/index.js'; import { AppErrorInvalid, SystemError } from './errors.js'; export async function assignmentCases(caseIds, transaction) { const tasks = await models.Case.findAll({ where: { id: caseIds }, order: [['id', 'ASC']], lock: transaction.LOCK.UPDATE, transaction, }); if (tasks.length !== caseIds.length) throw new AppErrorInvalid('caseIds'); return tasks; } export async function assertTechnicalAvailable(user, tasks, transaction) { if (user.role !== 'technical') return; for (const task of tasks) { const assigned = await task.getUsers({ where: { role: 'technical', id: { [Op.ne]: user.id } }, lock: transaction.LOCK.UPDATE, transaction, }); if (assigned.length) throw new SystemError(409, `В кейсе «${task.name}» уже назначен технический эксперт.`); } }