feat: migrate backend storage to postgres
Some checks failed
Deploy to Production / Build and Test (push) Successful in 10m51s
Deploy to Production / Deploy to Server (push) Failing after 6m41s

This commit is contained in:
你的用户名
2025-11-06 22:01:50 +08:00
parent 3646405a47
commit b68511b2e2
28 changed files with 2641 additions and 1801 deletions

View File

@@ -1,4 +1,4 @@
import db from './sqlite';
import { query } from './db';
interface TelegramNotificationConfig {
id: number;
@@ -24,18 +24,21 @@ interface TransactionNotificationData {
/**
* 获取所有启用的Telegram通知配置
*/
export function getEnabledNotificationConfigs(
export async function getEnabledNotificationConfigs(
notificationType: string = 'transaction',
): TelegramNotificationConfig[] {
const rows = db
.prepare<{ id: number; name: string; bot_token: string; chat_id: string; notification_types: string; is_enabled: number }>(
`
SELECT id, name, bot_token, chat_id, notification_types, is_enabled
FROM telegram_notification_configs
WHERE is_enabled = 1
`,
)
.all();
): Promise<TelegramNotificationConfig[]> {
const { rows } = await query<{
bot_token: string;
chat_id: string;
id: number;
is_enabled: boolean;
name: string;
notification_types: string;
}>(
`SELECT id, name, bot_token, chat_id, notification_types, is_enabled
FROM telegram_notification_configs
WHERE is_enabled = TRUE`,
);
return rows
.map((row) => ({
@@ -44,7 +47,7 @@ export function getEnabledNotificationConfigs(
botToken: row.bot_token,
chatId: row.chat_id,
notificationTypes: JSON.parse(row.notification_types) as string[],
isEnabled: row.is_enabled === 1,
isEnabled: row.is_enabled,
}))
.filter((config) => config.notificationTypes.includes(notificationType));
}
@@ -175,10 +178,10 @@ export async function notifyTransaction(
transaction: TransactionNotificationData,
action: string = 'created',
): Promise<void> {
const configs = getEnabledNotificationConfigs('transaction');
const configs = await getEnabledNotificationConfigs('transaction');
if (configs.length === 0) {
console.log('[telegram-bot] No enabled notification configs found');
console.warn('[telegram-bot] No enabled notification configs found');
return;
}
@@ -192,7 +195,7 @@ export async function notifyTransaction(
results.forEach((result, index) => {
if (result.status === 'fulfilled' && result.value) {
console.log(
console.warn(
`[telegram-bot] Sent notification via config: ${configs[index].name}`,
);
} else {
@@ -209,17 +212,18 @@ export async function notifyTransaction(
export async function testTelegramConfig(
botToken: string,
chatId: string,
): Promise<{ success: boolean; error?: string }> {
): Promise<{ error?: string; success: boolean }> {
try {
const testMessage = `🤖 KT财务系统\n\n✅ Telegram通知配置测试成功\n\n🕐 ${new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}`;
const success = await sendTelegramMessage(botToken, chatId, testMessage);
if (success) {
return { success: true };
} else {
return { success: false, error: '发送消息失败请检查Bot Token和Chat ID' };
}
return success
? { success: true }
: {
success: false,
error: '发送消息失败请检查Bot Token和Chat ID',
};
} catch (error: unknown) {
return {
success: false,