From dc95dbffefddc570fbaf83a5e3773d9ebc47143e Mon Sep 17 00:00:00 2001 From: gujiangjiang Date: Thu, 9 Oct 2025 04:42:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=B8=A0=E9=81=93=E6=8F=90=E7=A4=BA=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E4=BD=86=E5=AE=9E=E9=99=85=E6=88=90=E5=8A=9F=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/repositories/notification.ts | 67 +++++++++++++----------- 1 file changed, 36 insertions(+), 31 deletions(-) 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; }; // 获取所有通知模板