import { AppErrorInvalid, SystemError } from '../utils/errors.js'; import markTypes from '../config/markTypes.js'; import { sequelize, models } from '../models/index.js'; import { editableMarkTypes } from '../utils/expert-roles.js'; function validateExpertMark(role, markType, mark) { if (!Object.values(markTypes).includes(markType)) throw new AppErrorInvalid('markType'); if (!editableMarkTypes(role).includes(markType)) throw new SystemError(403, 'Этот критерий недоступен для вашей роли.'); if (!Number.isInteger(mark) || mark < 0 || mark > 5) throw new AppErrorInvalid('mark'); } function accessibleTeam(user, caseId, teamId) { const task = user.cases.find(c => c.id === caseId); if (!task) throw new SystemError(403, 'You do not have access to this case'); const team = task.teams.find(t => t.id === teamId); if (!team) throw new SystemError(403, 'You do not have access to this team'); return team; } async function saveMarks(user, caseId, teamId, entries, technicalComment) { accessibleTeam(user, caseId, teamId); if (technicalComment !== undefined) { if (user.role !== 'technical') throw new SystemError(403, 'Комментарий может редактировать только технический эксперт.'); if (typeof technicalComment !== 'string' || technicalComment.length > 5000) throw new AppErrorInvalid('technicalComment'); if (entries === undefined) entries = []; } if (!Array.isArray(entries)) throw new AppErrorInvalid('marks'); for (const entry of entries) { if (!entry || typeof entry !== 'object') throw new AppErrorInvalid('marks'); validateExpertMark(user.role, entry.markType, entry.mark); } if (new Set(entries.map(m => m.markType)).size !== entries.length) throw new AppErrorInvalid('markType'); await sequelize.transaction(async transaction => { const task = await models.Case.findByPk(caseId, { transaction, lock: transaction.LOCK.UPDATE }); if (!task) throw new SystemError(403, 'You do not have access to this case'); const users = await task.getUsers({ transaction }); if (!users.some(expert => expert.id === user.id)) throw new SystemError(403, 'You do not have access to this case'); const link = await models.TeamInCase.findOne({ where: { caseId, teamId }, transaction, lock: transaction.LOCK.UPDATE, }); if (!link) throw new SystemError(403, 'You do not have access to this team'); if (technicalComment !== undefined) await link.update({ technicalComment: technicalComment.trim() }, { transaction }); // Technical criteria are shared by all experts of the case, including its technical expert. const userIds = user.role === 'technical' ? users.map(expert => expert.id) : [user.id]; for (const { markType, mark } of entries) { await models.Mark.destroy({ where: { caseId, teamId, userId: userIds, markType }, force: true, transaction }); await models.Mark.bulkCreate( userIds.map(userId => ({ caseId, teamId, userId, markType, mark })), { transaction } ); } }); } export default { async get({ params: { caseId, teamId }, user }, res) { const team = accessibleTeam(user, caseId, teamId); res.json({ marks: team.marks, technicalComment: team.TeamInCase?.technicalComment || '' }); }, async add({ params: { caseId, teamId }, body: { markType, mark, technicalComment }, user }, res) { await saveMarks(user, caseId, teamId, [{ markType, mark }], technicalComment); res.end(); }, async addBatch({ params: { caseId, teamId }, body: { marks, technicalComment }, user }, res) { await saveMarks(user, caseId, teamId, marks, technicalComment); res.end(); }, };