diff --git a/backend/src/repositories/notification.ts b/backend/src/repositories/notification.ts index 31f9326..9441770 100644 --- a/backend/src/repositories/notification.ts +++ b/backend/src/repositories/notification.ts @@ -79,44 +79,49 @@ export const updateNotificationChannel = async ( export const deleteNotificationChannel = async ( id: number ): Promise => { - // 先删除通知历史记录表中的关联记录 - await db - .delete(notificationHistory) - .where(eq(notificationHistory.channel_id, id)); + try { + // 先删除通知历史记录表中的关联记录 + await db + .delete(notificationHistory) + .where(eq(notificationHistory.channel_id, id)); - // 再检查并更新通知设置中的channels列表 - const allSettings = await db.select().from(notificationSettings); + // 再检查并更新通知设置中的channels列表 + const allSettings = await db.select().from(notificationSettings); - // 遍历所有设置,从channels列表中移除要删除的渠道ID - if (allSettings && allSettings.length > 0) { - for (const setting of allSettings) { - try { - const channelsList = JSON.parse(setting.channels || "[]"); - const newChannelsList = channelsList.filter( - (channelId: number) => channelId !== id - ); + // 遍历所有设置,从channels列表中移除要删除的渠道ID + if (allSettings && allSettings.length > 0) { + for (const setting of allSettings) { + try { + const channelsList = JSON.parse(setting.channels || "[]"); + const newChannelsList = channelsList.filter( + (channelId: number) => channelId !== id + ); - // 如果列表变化了,更新数据库 - if (JSON.stringify(channelsList) !== JSON.stringify(newChannelsList)) { - await db - .update(notificationSettings) - .set({ - channels: JSON.stringify(newChannelsList), - }) - .where(eq(notificationSettings.id, setting.id)); + // 如果列表变化了,更新数据库 + if (JSON.stringify(channelsList) !== JSON.stringify(newChannelsList)) { + await db + .update(notificationSettings) + .set({ + channels: JSON.stringify(newChannelsList), + }) + .where(eq(notificationSettings.id, setting.id)); + } + } catch (error) { + console.error("解析通知设置渠道列表出错:", error); } - } catch (error) { - console.error("解析通知设置渠道列表出错:", error); } } + + // 最后删除通知渠道本身 + await db + .delete(notificationChannels) + .where(eq(notificationChannels.id, id)); + + return true; // 假设没有错误就是成功 + } catch (error) { + console.error("删除通知渠道失败:", error); + return false; } - - // 最后删除通知渠道本身 - const result = await db - .delete(notificationChannels) - .where(eq(notificationChannels.id, id)); - - return result.length > 0; }; // 获取所有通知模板