farewell: корзина, отметка отправки, быстрое редактирование, карточки списка
This commit is contained in:
@@ -67,7 +67,7 @@ const { lessonReportWriteBlocked } = require('./lib/lesson-reports');
|
||||
const { groupOwnedBy } = require('./lib/groups');
|
||||
const { ensureProjectFileSizeColumn, backfillProjectFileSizes } = require('./lib/file-sizes');
|
||||
const { resolveImgbbEndpoint, friendlyApiError } = require('./lib/feedback-imgbb');
|
||||
const { ensureFarewellTable, createFarewell, listFarewells, listFarewellTemplates, findFarewell, setFarewellStatus, FAREWELL_AI_DEFAULT_PROMPT } = require('./lib/farewell');
|
||||
const { ensureFarewellTable, createFarewell, listFarewells, listFarewellTemplates, findFarewell, setFarewellStatus, updateFarewellText, setFarewellSent, farewellForUser, deleteFarewell, restoreFarewell, deleteFarewellPermanent, FAREWELL_AI_DEFAULT_PROMPT } = require('./lib/farewell');
|
||||
const { aiHttpErrorFromResponse, aiHttpErrorText, aiHttpErrorDetail } = require('./lib/ai-errors');
|
||||
const { findBrokenFaceRefs, scanAllBrokenFaceRefs, cleanupAllBrokenFaceRefs, deleteFaceRef } = require('./lib/photo-cleanup');
|
||||
const {
|
||||
@@ -1738,6 +1738,8 @@ async function purgeScheduledDeletions() {
|
||||
await pool.query('DELETE FROM user_notes WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const fbres = await pool.query('DELETE FROM feedbacks WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const fbcount = fbres.rowCount;
|
||||
const fwres = await pool.query('DELETE FROM farewell_messages WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const fwcount = fwres.rowCount;
|
||||
const bires = await pool.query('DELETE FROM broken_images WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
const bicount = bires.rowCount;
|
||||
const strefs = await pool.query('SELECT id FROM students WHERE purge_at IS NOT NULL AND purge_at <= now()');
|
||||
@@ -1760,7 +1762,7 @@ async function purgeScheduledDeletions() {
|
||||
if (fbcount) invalidateFeedbacks();
|
||||
if (stcount) invalidateStudents();
|
||||
if (bicount) invalidateAll();
|
||||
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, students: stcount, broken_images: bicount };
|
||||
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, farewell_messages: fwcount, students: stcount, broken_images: bicount };
|
||||
}
|
||||
|
||||
function safeUnlink(relPath) {
|
||||
@@ -6209,6 +6211,7 @@ app.get('/api/farewells', requireAuth, async (req, res) => {
|
||||
limit: req.query.limit,
|
||||
offset: req.query.offset,
|
||||
resident: req.query.resident,
|
||||
deleted: req.query.deleted === '1' || req.query.deleted === 'true',
|
||||
});
|
||||
res.json({ items, total });
|
||||
} catch (err) {
|
||||
@@ -6247,12 +6250,106 @@ app.post('/api/farewells/:id/ai/retry', requireAuth, async (req, res) => {
|
||||
const row = await findFarewell(pool, id);
|
||||
if (!row) return res.status(404).json({ error: 'Not found' });
|
||||
if (req.user.role !== 'admin' && row.author_id !== req.user.id) return res.status(403).json({ error: 'Нет доступа' });
|
||||
if (row.deleted_at) return res.status(400).json({ error: 'Прощальное сообщение удалено' });
|
||||
await setFarewellStatus(pool, id, 'pending', { aiError: null });
|
||||
logAudit(req, 'farewell.ai.retry', { farewell_id: id });
|
||||
wakeFarewellWorker();
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.put('/api/farewells/:id', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const { row, notFound, forbidden } = await farewellForUser(pool, id, req.user);
|
||||
if (notFound) return res.status(404).json({ error: 'Not found' });
|
||||
if (forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||
if (row.deleted_at) return res.status(400).json({ error: 'Прощальное сообщение удалено' });
|
||||
const text = typeof req.body?.text === 'string' ? req.body.text.slice(0, 100000) : null;
|
||||
if (text == null) return res.status(400).json({ error: 'Некорректный текст' });
|
||||
if (!text.trim()) return res.status(400).json({ error: 'Введите текст сообщения' });
|
||||
const upd = await updateFarewellText(pool, id, text.trim());
|
||||
if (!upd) return res.status(400).json({ error: 'Прощальное сообщение удалено' });
|
||||
logAudit(req, 'farewell.update', { farewell_id: id });
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/api/farewells/:id/sent', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const { row, notFound, forbidden } = await farewellForUser(pool, id, req.user);
|
||||
if (notFound) return res.status(404).json({ error: 'Not found' });
|
||||
if (forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||
if (row.deleted_at) return res.status(400).json({ error: 'Прощальное сообщение удалено' });
|
||||
const sent = typeof req.body?.sent === 'boolean' ? req.body.sent : null;
|
||||
if (sent === null) return res.status(400).json({ error: 'Некорректный статус' });
|
||||
const upd = await setFarewellSent(pool, id, sent);
|
||||
if (!upd) return res.status(400).json({ error: 'Прощальное сообщение удалено' });
|
||||
logAudit(req, 'farewell.sent', { farewell_id: id, sent });
|
||||
res.json({ ok: true, sent });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/farewells/:id', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const { notFound, forbidden } = await farewellForUser(pool, id, req.user);
|
||||
if (notFound) return res.status(404).json({ error: 'Not found' });
|
||||
if (forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||
const days = await trashPurgeDays();
|
||||
const ok = await deleteFarewell(pool, id, days);
|
||||
if (!ok) return res.status(404).json({ error: 'Уже удалено' });
|
||||
logAudit(req, 'farewell.delete', { farewell_id: id });
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/api/farewells/:id/restore', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const { notFound, forbidden } = await farewellForUser(pool, id, req.user);
|
||||
if (notFound) return res.status(404).json({ error: 'Not found' });
|
||||
if (forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||
const ok = await restoreFarewell(pool, id);
|
||||
if (!ok) return res.status(404).json({ error: 'Не удалено' });
|
||||
logAudit(req, 'farewell.restore', { farewell_id: id });
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/farewells/:id/permanent', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
if (!Number.isInteger(id) || id < 1) return res.status(400).json({ error: 'Invalid id' });
|
||||
const { notFound, forbidden } = await farewellForUser(pool, id, req.user);
|
||||
if (notFound) return res.status(404).json({ error: 'Not found' });
|
||||
if (forbidden) return res.status(403).json({ error: 'Нет доступа' });
|
||||
const ok = await deleteFarewellPermanent(pool, id);
|
||||
if (!ok) return res.status(404).json({ error: 'Не удалено' });
|
||||
logAudit(req, 'farewell.permanent-delete', { farewell_id: id });
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Internal error' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.delete('/api/users/:id', requireAuth, requireAdmin, async (req, res) => {
|
||||
if (req.params.id === String(req.user.id)) {
|
||||
@@ -7931,8 +8028,8 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
const okUser = x.author_id == null || (await client.query('SELECT 1 FROM users WHERE id = $1', [x.author_id])).rowCount;
|
||||
const okTpl = x.template_id == null || x.template_id === x.id || (await client.query('SELECT 1 FROM farewell_messages WHERE id = $1', [x.template_id])).rowCount;
|
||||
await client.query(
|
||||
'INSERT INTO farewell_messages (id, resident, topic, source_feedback_ids, template_id, template_text, prompt, ai_profile, ai_status, text, ai_error, ai_prompt, ai_model, ai_duration_ms, author_id, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,COALESCE($16, now()),COALESCE($17, now()))',
|
||||
[x.id, x.resident, x.topic, Array.isArray(x.source_feedback_ids) ? x.source_feedback_ids : [], okTpl ? x.template_id : null, x.template_text, x.prompt, x.ai_profile, x.ai_status || 'pending', x.text, x.ai_error, x.ai_prompt, x.ai_model, x.ai_duration_ms, okUser ? x.author_id : null, x.created_at, x.updated_at]
|
||||
'INSERT INTO farewell_messages (id, resident, topic, source_feedback_ids, template_id, template_text, prompt, ai_profile, ai_status, text, ai_error, ai_prompt, ai_model, ai_duration_ms, author_id, sent_at, deleted_at, purge_at, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,COALESCE($19, now()),COALESCE($20, now()))',
|
||||
[x.id, x.resident, x.topic, Array.isArray(x.source_feedback_ids) ? x.source_feedback_ids : [], okTpl ? x.template_id : null, x.template_text, x.prompt, x.ai_profile, x.ai_status || 'pending', x.text, x.ai_error, x.ai_prompt, x.ai_model, x.ai_duration_ms, okUser ? x.author_id : null, x.sent_at, x.deleted_at, x.purge_at, x.created_at, x.updated_at]
|
||||
);
|
||||
}
|
||||
for (const x of ndata.audit_log) {
|
||||
|
||||
Reference in New Issue
Block a user