farewell: корзина, отметка отправки, быстрое редактирование, карточки списка

This commit is contained in:
dev
2026-11-12 13:44:29 +03:00
parent 0e0b3c7cfd
commit b527993716
8 changed files with 391 additions and 31 deletions
+69 -8
View File
@@ -38,6 +38,9 @@ async function ensureFarewellTable(pool) {
ai_model TEXT,
ai_duration_ms INTEGER,
author_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
sent_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ,
purge_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
)
@@ -54,6 +57,9 @@ async function ensureFarewellTable(pool) {
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_model TEXT`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_duration_ms INTEGER`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS author_id INTEGER`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS sent_at TIMESTAMPTZ`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS purge_at TIMESTAMPTZ`);
await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now()`);
await pool.query(`CREATE INDEX IF NOT EXISTS idx_farewell_resident ON farewell_messages (resident)`);
await pool.query(`CREATE INDEX IF NOT EXISTS idx_farewell_pending ON farewell_messages (id) WHERE ai_status = 'pending'`);
@@ -87,7 +93,7 @@ async function createFarewell(pool, data) {
let templateText = null;
if (templateId) {
const params = [templateId];
let sql = `SELECT text FROM farewell_messages WHERE id = $1 AND ai_status = 'done'`;
let sql = `SELECT text FROM farewell_messages WHERE id = $1 AND ai_status = 'done' AND deleted_at IS NULL`;
if (!data.isAdmin) { params.push(data.authorId); sql += ` AND author_id = $${params.length}`; }
const r = await pool.query(sql, params);
if (!r.rows.length) { templateId = null; templateText = null; }
@@ -102,19 +108,24 @@ async function createFarewell(pool, data) {
return ins.rows[0];
}
async function listFarewells(pool, { isAdmin, userId, limit, offset, resident }) {
async function listFarewells(pool, { isAdmin, userId, limit, offset, resident, deleted }) {
const lim = Math.min(Math.max(parseInt(limit, 10) || 20, 1), 100);
const off = Math.max(parseInt(offset, 10) || 0, 0);
const params = [];
const cond = [];
if (!isAdmin) { params.push(userId); cond.push(`author_id = $${params.length}`); }
if (!isAdmin) { params.push(userId); cond.push(`f.author_id = $${params.length}`); }
const res = String(resident || '').trim();
if (res) { params.push('%' + res + '%'); cond.push(`resident ILIKE $${params.length}`); }
const where = cond.length ? 'WHERE ' + cond.join(' AND ') : '';
const total = await pool.query(`SELECT count(*)::int AS n FROM farewell_messages ${where}`, params);
if (res) { params.push('%' + res + '%'); cond.push(`f.resident ILIKE $${params.length}`); }
cond.push(deleted ? 'f.deleted_at IS NOT NULL' : 'f.deleted_at IS NULL');
const where = 'WHERE ' + cond.join(' AND ');
const total = await pool.query(`SELECT count(*)::int AS n FROM farewell_messages f ${where}`, params);
params.push(lim, off);
const rows = await pool.query(
`SELECT * FROM farewell_messages ${where} ORDER BY id DESC LIMIT $${params.length - 1} OFFSET $${params.length}`,
`SELECT f.*, u.name AS author_name, u.username AS author_username, u.avatar AS author_avatar
FROM farewell_messages f
LEFT JOIN users u ON u.id = f.author_id
${where}
ORDER BY f.id DESC LIMIT $${params.length - 1} OFFSET $${params.length}`,
params
);
return { items: rows.rows, total: total.rows[0].n };
@@ -125,7 +136,7 @@ async function listFarewellTemplates(pool, { isAdmin, userId, resident, limit })
const res = String(resident || '').trim();
if (!res) return [];
const params = ['%' + res + '%'];
let sql = `SELECT id, resident, topic, created_at, text FROM farewell_messages WHERE resident ILIKE $1 AND ai_status = 'done' AND COALESCE(text, '') <> ''`;
let sql = `SELECT id, resident, topic, created_at, text FROM farewell_messages WHERE resident ILIKE $1 AND ai_status = 'done' AND COALESCE(text, '') <> '' AND deleted_at IS NULL`;
if (!isAdmin) { params.push(userId); sql += ` AND author_id = $${params.length}`; }
params.push(lim);
sql += ` ORDER BY id DESC LIMIT $${params.length}`;
@@ -146,6 +157,50 @@ async function setFarewellStatus(pool, id, status, extra) {
return r.rows[0] || null;
}
async function updateFarewellText(pool, id, text) {
const r = await pool.query(
`UPDATE farewell_messages SET text = $2, updated_at = now() WHERE id = $1 AND deleted_at IS NULL RETURNING *`,
[id, text]
);
return r.rows[0] || null;
}
async function setFarewellSent(pool, id, sent) {
const r = await pool.query(
`UPDATE farewell_messages SET sent_at = $2, updated_at = now() WHERE id = $1 AND deleted_at IS NULL RETURNING *`,
[id, sent ? new Date() : null]
);
return r.rows[0] || null;
}
async function farewellForUser(pool, id, user) {
const row = await findFarewell(pool, id);
if (!row) return { notFound: true };
if (user && user.role !== 'admin' && row.author_id !== user.id) return { forbidden: true };
return { row };
}
async function deleteFarewell(pool, id, days) {
const r = await pool.query(
`UPDATE farewell_messages SET deleted_at = now(), purge_at = now() + ($2 || ' days')::interval WHERE id = $1 AND deleted_at IS NULL`,
[id, String(days)]
);
return r.rowCount > 0;
}
async function restoreFarewell(pool, id) {
const r = await pool.query(
`UPDATE farewell_messages SET deleted_at = NULL, purge_at = NULL WHERE id = $1 AND deleted_at IS NOT NULL`,
[id]
);
return r.rowCount > 0;
}
async function deleteFarewellPermanent(pool, id) {
const r = await pool.query(`DELETE FROM farewell_messages WHERE id = $1 AND deleted_at IS NOT NULL`, [id]);
return r.rowCount > 0;
}
module.exports = {
FAREWELL_AI_DEFAULT_PROMPT,
FAREWELL_RESIDENT_MAX,
@@ -157,4 +212,10 @@ module.exports = {
listFarewellTemplates,
findFarewell,
setFarewellStatus,
updateFarewellText,
setFarewellSent,
farewellForUser,
deleteFarewell,
restoreFarewell,
deleteFarewellPermanent,
};