feat(lesson-reports): allow changing group on report edit with clash check
This commit is contained in:
@@ -8021,7 +8021,15 @@ app.post('/api/lesson-reports', requireAuth, async (req, res) => {
|
||||
app.put('/api/lesson-reports/:id', requireAuth, async (req, res) => {
|
||||
const { report, error, code } = await lessonReportById(req.user, req.params.id);
|
||||
if (!report) return res.status(code || 404).json({ error });
|
||||
const { lesson_date, lesson_time, topic, text, ai_check } = req.body || {};
|
||||
const { group_id, lesson_date, lesson_time, topic, text, ai_check } = req.body || {};
|
||||
let nextGroup = report.group_id;
|
||||
let nextBranch = report.branch_id;
|
||||
if (group_id !== undefined && group_id !== null && Number(group_id) !== report.group_id) {
|
||||
const grp = await lessonReportGroup(req.user, group_id);
|
||||
if (grp.error) return res.status(grp.code || 400).json({ error: grp.error });
|
||||
nextGroup = grp.group.id;
|
||||
nextBranch = grp.group.branch_id || null;
|
||||
}
|
||||
const curDate = String(report.lesson_date).slice(0, 10);
|
||||
let date = curDate;
|
||||
if (lesson_date !== undefined && lesson_date !== null && lesson_date !== '') {
|
||||
@@ -8041,17 +8049,19 @@ app.put('/api/lesson-reports/:id', requireAuth, async (req, res) => {
|
||||
topicText = typeof topic === 'string' ? topic.trim() : '';
|
||||
if (topicText.length > LESSON_REPORT_TOPIC_MAX) return res.status(400).json({ error: `Тема занятия длиннее ${LESSON_REPORT_TOPIC_MAX} символов` });
|
||||
}
|
||||
if (date !== curDate) {
|
||||
if (date !== curDate || nextGroup !== report.group_id) {
|
||||
const { rows: clash } = await pool.query(
|
||||
'SELECT id FROM lesson_reports WHERE group_id = $1 AND lesson_date = $2 AND id <> $3',
|
||||
[report.group_id, date, report.id]
|
||||
[nextGroup, date, report.id]
|
||||
);
|
||||
if (clash.length) return res.status(409).json({ error: 'За эту группу и дату уже есть другой отчёт' });
|
||||
if (clash.length) return res.status(409).json({ error: 'За эту группу и дату уже есть другой отчёт', id: clash[0].id });
|
||||
}
|
||||
const nextTime = time === undefined ? report.lesson_time : time;
|
||||
const aiWanted = !!body && ai_check === true && (await getSetting('lesson_ai_enabled', 'true')) !== 'false';
|
||||
const { rows } = await pool.query(
|
||||
`UPDATE lesson_reports SET
|
||||
group_id = $8,
|
||||
branch_id = $9,
|
||||
lesson_date = $1::date,
|
||||
lesson_time = $2,
|
||||
topic = CASE WHEN $6::boolean THEN $3::text ELSE topic END,
|
||||
@@ -8063,11 +8073,11 @@ app.put('/api/lesson-reports/:id', requireAuth, async (req, res) => {
|
||||
ai_error = CASE WHEN $5::boolean THEN NULL ELSE ai_error END,
|
||||
updated_at = now()
|
||||
WHERE id = $7 RETURNING *`,
|
||||
[date, nextTime, topicText === undefined ? null : (topicText || null), body, aiWanted, topic !== undefined, report.id]
|
||||
[date, nextTime, topicText === undefined ? null : (topicText || null), body, aiWanted, topic !== undefined, report.id, nextGroup, nextBranch]
|
||||
);
|
||||
const updated = rows[0];
|
||||
if (body) await saveLessonReportVersion(report.id, body, 'manual', req.user.id);
|
||||
await logAudit(req, 'lesson_report.update', { id: report.id, lesson_date: date, ai_status: updated.ai_status });
|
||||
await logAudit(req, 'lesson_report.update', { id: report.id, group_id: nextGroup, lesson_date: date, ai_status: updated.ai_status });
|
||||
invalidateLessonReports();
|
||||
if (aiWanted) wakeLessonAiWorker();
|
||||
res.json(updated);
|
||||
|
||||
Reference in New Issue
Block a user