farewell: дата, тумблер ИИ и готовый текст в модалке, прошлый отзыв тумблером
This commit is contained in:
+23
-5
@@ -26,6 +26,7 @@ async function ensureFarewellTable(pool) {
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
resident TEXT NOT NULL,
|
||||
topic TEXT,
|
||||
message_date DATE,
|
||||
source_feedback_ids INT[] NOT NULL DEFAULT '{}'::int[],
|
||||
template_id BIGINT REFERENCES farewell_messages(id) ON DELETE SET NULL,
|
||||
template_text TEXT,
|
||||
@@ -57,6 +58,7 @@ 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 message_date DATE`);
|
||||
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`);
|
||||
@@ -70,11 +72,26 @@ async function createFarewell(pool, data) {
|
||||
if (!resident) { const e = new Error('Укажите фамилию и имя ученика'); e.status = 400; throw e; }
|
||||
if (resident.length > FAREWELL_RESIDENT_MAX) { const e = new Error('Слишком длинное имя ученика'); e.status = 400; throw e; }
|
||||
const topic = String(data.topic || '').trim().slice(0, FAREWELL_TOPIC_MAX) || null;
|
||||
const messageDate = /^\d{4}-\d{2}-\d{2}$/.test(String(data.messageDate || '')) ? String(data.messageDate) : null;
|
||||
const text = String(data.text || '').trim().slice(0, 100000) || null;
|
||||
const prompt = String(data.prompt || '').trim().slice(0, FAREWELL_PROMPT_MAX) || null;
|
||||
const aiProfile = String(data.aiProfile || '').trim().slice(0, 100) || null;
|
||||
const ids = cleanInts(data.sourceFeedbackIds);
|
||||
const aiCheck = data.aiCheck !== false;
|
||||
let ids = cleanInts(data.sourceFeedbackIds);
|
||||
|
||||
if (aiCheck && data.pastReview && !ids.length) {
|
||||
const params = [resident];
|
||||
let sql = `SELECT id FROM feedbacks WHERE resident = $1 AND sent_at IS NOT NULL AND deleted_at IS NULL`;
|
||||
if (Array.isArray(data.branchIds)) {
|
||||
if (!data.branchIds.length) { const e = new Error('Нет доступных филиалов'); e.status = 403; throw e; }
|
||||
params.push(data.branchIds);
|
||||
sql += ` AND branch_id = ANY($${params.length}::int[])`;
|
||||
}
|
||||
sql += ` ORDER BY feedback_date DESC, id DESC LIMIT 10`;
|
||||
const r = await pool.query(sql, params);
|
||||
ids = r.rows.map(x => x.id).reverse();
|
||||
}
|
||||
|
||||
let accessible = ids;
|
||||
if (ids.length) {
|
||||
const params = [ids];
|
||||
let sql = `SELECT id FROM feedbacks WHERE id = ANY($1::int[]) AND deleted_at IS NULL`;
|
||||
@@ -101,9 +118,9 @@ async function createFarewell(pool, data) {
|
||||
}
|
||||
|
||||
const ins = await pool.query(
|
||||
`INSERT INTO farewell_messages (resident, topic, source_feedback_ids, template_id, template_text, prompt, ai_profile, ai_status, author_id)
|
||||
VALUES ($1, $2, $3::int[], $4, $5, $6, $7, 'pending', $8) RETURNING *`,
|
||||
[resident, topic, ids, templateId, templateText, prompt, aiProfile, data.authorId || null]
|
||||
`INSERT INTO farewell_messages (resident, topic, message_date, source_feedback_ids, template_id, template_text, prompt, ai_profile, ai_status, text, author_id)
|
||||
VALUES ($1, $2, $3, $4::int[], $5, $6, $7, $8, $9, $10, $11) RETURNING *`,
|
||||
[resident, topic, messageDate, ids, templateId, templateText, prompt, aiProfile, aiCheck ? 'pending' : 'none', text, data.authorId || null]
|
||||
);
|
||||
return ins.rows[0];
|
||||
}
|
||||
@@ -153,6 +170,7 @@ async function setFarewellStatus(pool, id, status, extra) {
|
||||
const sets = ['ai_status = $2', 'updated_at = now()'];
|
||||
const params = [id, status];
|
||||
if (extra && extra.aiError !== undefined) { params.push(extra.aiError || null); sets.push(`ai_error = $${params.length}`); }
|
||||
if (extra && extra.clearPrompt) sets.push('ai_prompt = NULL');
|
||||
const r = await pool.query(`UPDATE farewell_messages SET ${sets.join(', ')} WHERE id = $1 RETURNING *`, params);
|
||||
return r.rows[0] || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user