Files
kt-financial-system/apps/backend/api/telegram/notifications/[id].put.ts
你的用户名 b68511b2e2
Some checks failed
Deploy to Production / Build and Test (push) Successful in 10m51s
Deploy to Production / Deploy to Server (push) Failing after 6m41s
feat: migrate backend storage to postgres
2025-11-06 22:01:50 +08:00

117 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readBody } from 'h3';
import { query } from '~/utils/db';
import { useResponseError, useResponseSuccess } from '~/utils/response';
import { testTelegramConfig } from '~/utils/telegram-bot';
export default defineEventHandler(async (event) => {
const idParam = event.context.params?.id;
const id = Number(idParam);
if (!idParam || Number.isNaN(id)) {
return useResponseError('缺少ID参数', -1);
}
const body = await readBody(event);
// 如果更新了botToken或chatId需要测试配置
if (body.botToken !== undefined || body.chatId !== undefined) {
const { rows } = await query<{
bot_token: string;
chat_id: string;
}>(
'SELECT bot_token, chat_id FROM telegram_notification_configs WHERE id = $1',
[id],
);
const existing = rows[0];
if (!existing) {
return useResponseError('配置不存在', -1);
}
const tokenToTest = body.botToken ?? existing.bot_token;
const chatIdToTest = body.chatId ?? existing.chat_id;
const testResult = await testTelegramConfig(tokenToTest, chatIdToTest);
if (!testResult.success) {
return useResponseError(
`Telegram配置测试失败: ${testResult.error}`,
-1,
);
}
}
const updates: string[] = [];
const values: any[] = [];
if (body.name !== undefined) {
values.push(body.name);
updates.push(`name = $${values.length}`);
}
if (body.botToken !== undefined) {
values.push(body.botToken);
updates.push(`bot_token = $${values.length}`);
}
if (body.chatId !== undefined) {
values.push(body.chatId);
updates.push(`chat_id = $${values.length}`);
}
if (body.notificationTypes !== undefined) {
values.push(JSON.stringify(body.notificationTypes));
updates.push(`notification_types = $${values.length}`);
}
if (body.isEnabled !== undefined) {
values.push(body.isEnabled !== false);
updates.push(`is_enabled = $${values.length}`);
}
if (updates.length === 0) {
return useResponseError('没有可更新的字段', -1);
}
values.push(new Date().toISOString());
updates.push(`updated_at = $${values.length}`);
values.push(id);
const idPosition = values.length;
const updateResult = await query(
`UPDATE telegram_notification_configs
SET ${updates.join(', ')}
WHERE id = $${idPosition}`,
values,
);
if (updateResult.rowCount === 0) {
return useResponseError('配置不存在', -1);
}
const { rows: updatedRows } = await query<{
id: number;
name: string;
bot_token: string;
chat_id: string;
notification_types: string;
is_enabled: boolean;
created_at: string;
updated_at: string;
}>('SELECT * FROM telegram_notification_configs WHERE id = $1', [id]);
const updated = updatedRows[0];
if (!updated) {
return useResponseError('更新失败', -1);
}
return useResponseSuccess({
id: updated.id,
name: updated.name,
botToken: updated.bot_token,
chatId: updated.chat_id,
notificationTypes: JSON.parse(updated.notification_types) as string[],
isEnabled: updated.is_enabled,
createdAt: updated.created_at,
updatedAt: updated.updated_at,
});
});