feat(lesson-reports): allow changing group on report edit with clash check

This commit is contained in:
dev
2026-10-19 14:39:47 +03:00
parent 7478031b14
commit bd6523cecf
2 changed files with 35 additions and 6 deletions
+19
View File
@@ -279,6 +279,25 @@ async function main() {
token, method: 'PUT', body: { topic: 'я'.repeat(301) }, token, method: 'PUT', body: { topic: 'я'.repeat(301) },
}); });
ok('lesson-report: слишком длинная тема -> 400', tooLong.status === 400, { status: tooLong.status, error: tooLong.data && tooLong.data.error }); ok('lesson-report: слишком длинная тема -> 400', tooLong.status === 400, { status: tooLong.status, error: tooLong.data && tooLong.data.error });
const badGroup = await api(`/api/lesson-reports/${withTopic.data.id}`, {
token, method: 'PUT', body: { group_id: 99999999 },
});
ok('lesson-report: смена группы на несуществующую -> 404', badGroup.status === 404, { status: badGroup.status, error: badGroup.data && badGroup.data.error });
const gid2 = lrGroups.data[1] && lrGroups.data[1].id;
if (gid2) {
await api(`/api/lesson-reports?group_id=${gid2}&date_from=2019-05-18&date_to=2019-05-18`, { token })
.then(r => (r.data.items || []).forEach(i => api(`/api/lesson-reports/${i.id}`, { token, method: 'DELETE' })));
const moved = await api(`/api/lesson-reports/${withTopic.data.id}`, {
token, method: 'PUT', body: { group_id: gid2 },
});
ok('lesson-report: смена группы при редактировании сохраняется',
moved.status === 200 && moved.data.group_id === gid2,
{ status: moved.status, group_id: moved.data.group_id });
} else {
console.log('SKIP lesson-report: смена группы — нет второй группы');
}
await api(`/api/lesson-reports/${withTopic.data.id}`, { token, method: 'DELETE' }); await api(`/api/lesson-reports/${withTopic.data.id}`, { token, method: 'DELETE' });
const versions = await api(`/api/lesson-reports/${plain.data.id}/versions`, { token }); const versions = await api(`/api/lesson-reports/${plain.data.id}/versions`, { token });
+16 -6
View File
@@ -8021,7 +8021,15 @@ app.post('/api/lesson-reports', requireAuth, async (req, res) => {
app.put('/api/lesson-reports/:id', 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); const { report, error, code } = await lessonReportById(req.user, req.params.id);
if (!report) return res.status(code || 404).json({ error }); 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); const curDate = String(report.lesson_date).slice(0, 10);
let date = curDate; let date = curDate;
if (lesson_date !== undefined && lesson_date !== null && lesson_date !== '') { 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() : ''; topicText = typeof topic === 'string' ? topic.trim() : '';
if (topicText.length > LESSON_REPORT_TOPIC_MAX) return res.status(400).json({ error: `Тема занятия длиннее ${LESSON_REPORT_TOPIC_MAX} символов` }); 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( const { rows: clash } = await pool.query(
'SELECT id FROM lesson_reports WHERE group_id = $1 AND lesson_date = $2 AND id <> $3', '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 nextTime = time === undefined ? report.lesson_time : time;
const aiWanted = !!body && ai_check === true && (await getSetting('lesson_ai_enabled', 'true')) !== 'false'; const aiWanted = !!body && ai_check === true && (await getSetting('lesson_ai_enabled', 'true')) !== 'false';
const { rows } = await pool.query( const { rows } = await pool.query(
`UPDATE lesson_reports SET `UPDATE lesson_reports SET
group_id = $8,
branch_id = $9,
lesson_date = $1::date, lesson_date = $1::date,
lesson_time = $2, lesson_time = $2,
topic = CASE WHEN $6::boolean THEN $3::text ELSE topic END, 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, ai_error = CASE WHEN $5::boolean THEN NULL ELSE ai_error END,
updated_at = now() updated_at = now()
WHERE id = $7 RETURNING *`, 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]; const updated = rows[0];
if (body) await saveLessonReportVersion(report.id, body, 'manual', req.user.id); 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(); invalidateLessonReports();
if (aiWanted) wakeLessonAiWorker(); if (aiWanted) wakeLessonAiWorker();
res.json(updated); res.json(updated);