ваше сообщение коммита
This commit is contained in:
48
backend/services/aiAssistantSettingsService.js
Normal file
48
backend/services/aiAssistantSettingsService.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const db = require('../db');
|
||||
const TABLE = 'ai_assistant_settings';
|
||||
|
||||
async function getSettings() {
|
||||
const { rows } = await db.getQuery()(`SELECT * FROM ${TABLE} ORDER BY id LIMIT 1`);
|
||||
const settings = rows[0] || null;
|
||||
if (!settings) return null;
|
||||
|
||||
// Получаем связанные данные из telegram_settings и email_settings
|
||||
let telegramBot = null;
|
||||
let supportEmail = null;
|
||||
if (settings.telegram_settings_id) {
|
||||
const tg = await db.getQuery()('SELECT * FROM telegram_settings WHERE id = $1', [settings.telegram_settings_id]);
|
||||
telegramBot = tg.rows[0] || null;
|
||||
}
|
||||
if (settings.email_settings_id) {
|
||||
const em = await db.getQuery()('SELECT * FROM email_settings WHERE id = $1', [settings.email_settings_id]);
|
||||
supportEmail = em.rows[0] || null;
|
||||
}
|
||||
return {
|
||||
...settings,
|
||||
telegramBot,
|
||||
supportEmail
|
||||
};
|
||||
}
|
||||
|
||||
async function upsertSettings({ system_prompt, selected_rag_tables, languages, model, rules, updated_by, telegram_settings_id, email_settings_id, system_message }) {
|
||||
const { rows } = await db.getQuery()(
|
||||
`INSERT INTO ${TABLE} (id, system_prompt, selected_rag_tables, languages, model, rules, updated_at, updated_by, telegram_settings_id, email_settings_id, system_message)
|
||||
VALUES (1, $1, $2, $3, $4, $5, NOW(), $6, $7, $8, $9)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
system_prompt = EXCLUDED.system_prompt,
|
||||
selected_rag_tables = EXCLUDED.selected_rag_tables,
|
||||
languages = EXCLUDED.languages,
|
||||
model = EXCLUDED.model,
|
||||
rules = EXCLUDED.rules,
|
||||
updated_at = NOW(),
|
||||
updated_by = EXCLUDED.updated_by,
|
||||
telegram_settings_id = EXCLUDED.telegram_settings_id,
|
||||
email_settings_id = EXCLUDED.email_settings_id,
|
||||
system_message = EXCLUDED.system_message
|
||||
RETURNING *`,
|
||||
[system_prompt, selected_rag_tables, languages, model, rules, updated_by, telegram_settings_id, email_settings_id, system_message]
|
||||
);
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
module.exports = { getSettings, upsertSettings };
|
||||
Reference in New Issue
Block a user