@@ -3,12 +3,13 @@ import { z } from "zod";
33import { Bindings } from "../models/db" ;
44import * as NotificationService from "../services/NotificationService" ;
55
6- const notifications = new Hono < { Bindings : Bindings } > ( ) ;
6+ const notifications = new Hono < { Bindings : Bindings ; Variables : { jwtPayload : any } } > ( ) ;
77
88// 获取通知配置
99notifications . get ( "/" , async ( c ) => {
1010 try {
11- const config = await NotificationService . getNotificationConfig ( ) ;
11+ const userId = c . get ( "jwtPayload" ) . id ;
12+ const config = await NotificationService . getNotificationConfig ( userId ) ;
1213
1314 return c . json ( {
1415 success : true ,
@@ -30,7 +31,8 @@ notifications.get("/", async (c) => {
3031// 获取通知渠道列表
3132notifications . get ( "/channels" , async ( c ) => {
3233 try {
33- const channels = await NotificationService . getNotificationChannels ( ) ;
34+ const userId = c . get ( "jwtPayload" ) . id ;
35+ const channels = await NotificationService . getNotificationChannels ( userId ) ;
3436
3537 return c . json ( {
3638 success : true ,
@@ -53,6 +55,7 @@ notifications.get("/channels", async (c) => {
5355notifications . get ( "/channels/:id" , async ( c ) => {
5456 try {
5557 const id = parseInt ( c . req . param ( "id" ) ) ;
58+ const userId = c . get ( "jwtPayload" ) . id ;
5659
5760 if ( isNaN ( id ) ) {
5861 return c . json (
@@ -64,7 +67,7 @@ notifications.get("/channels/:id", async (c) => {
6467 ) ;
6568 }
6669
67- const channel = await NotificationService . getNotificationChannelById ( id ) ;
70+ const channel = await NotificationService . getNotificationChannelById ( id , userId ) ;
6871
6972 if ( ! channel ) {
7073 return c . json (
@@ -96,15 +99,14 @@ notifications.get("/channels/:id", async (c) => {
9699// 创建通知渠道
97100notifications . post ( "/channels" , async ( c ) => {
98101 try {
99- const db = c . env . DB ;
100102 const userId = c . get ( "jwtPayload" ) . id ;
101103 const body = await c . req . json ( ) ;
102104
103105 // 验证请求数据
104106 const schema = z . object ( {
105107 name : z . string ( ) . min ( 1 , "名称不能为空" ) ,
106108 type : z . string ( ) . min ( 1 , "类型不能为空" ) ,
107- config : z . string ( ) . min ( 1 , "配置不能为空" ) ,
109+ config : z . any ( ) ,
108110 enabled : z . boolean ( ) . optional ( ) ,
109111 } ) ;
110112
@@ -114,7 +116,7 @@ notifications.post("/channels", async (c) => {
114116 const result = await NotificationService . createNotificationChannel ( {
115117 name : validatedData . name ,
116118 type : validatedData . type ,
117- config : validatedData . config ,
119+ config : JSON . stringify ( validatedData . config ) ,
118120 enabled :
119121 validatedData . enabled !== undefined ? validatedData . enabled : true ,
120122 created_by : userId ,
@@ -153,10 +155,12 @@ notifications.post("/channels", async (c) => {
153155 }
154156} ) ;
155157
158+
156159// 更新通知渠道
157160notifications . put ( "/channels/:id" , async ( c ) => {
158161 try {
159162 const id = parseInt ( c . req . param ( "id" ) ) ;
163+ const userId = c . get ( "jwtPayload" ) . id ;
160164
161165 if ( isNaN ( id ) ) {
162166 return c . json (
@@ -174,15 +178,19 @@ notifications.put("/channels/:id", async (c) => {
174178 const schema = z . object ( {
175179 name : z . string ( ) . min ( 1 , "名称不能为空" ) . optional ( ) ,
176180 type : z . string ( ) . min ( 1 , "类型不能为空" ) . optional ( ) ,
177- config : z . string ( ) . min ( 1 , "配置不能为空" ) . optional ( ) ,
181+ config : z . any ( ) . optional ( ) ,
178182 enabled : z . boolean ( ) . optional ( ) ,
179183 } ) ;
180184
181185 const validatedData = schema . parse ( body ) ;
186+ if ( validatedData . config ) {
187+ validatedData . config = JSON . stringify ( validatedData . config )
188+ }
182189
183190 // 更新渠道
184191 const result = await NotificationService . updateNotificationChannel (
185192 id ,
193+ userId ,
186194 validatedData
187195 ) ;
188196
@@ -217,6 +225,7 @@ notifications.put("/channels/:id", async (c) => {
217225notifications . delete ( "/channels/:id" , async ( c ) => {
218226 try {
219227 const id = parseInt ( c . req . param ( "id" ) ) ;
228+ const userId = c . get ( "jwtPayload" ) . id ;
220229
221230 if ( isNaN ( id ) ) {
222231 return c . json (
@@ -228,7 +237,7 @@ notifications.delete("/channels/:id", async (c) => {
228237 ) ;
229238 }
230239
231- const result = await NotificationService . deleteNotificationChannel ( id ) ;
240+ const result = await NotificationService . deleteNotificationChannel ( id , userId ) ;
232241
233242 if ( ! result . success ) {
234243 return c . json (
@@ -257,10 +266,12 @@ notifications.delete("/channels/:id", async (c) => {
257266 }
258267} ) ;
259268
269+
260270// 获取通知模板列表
261271notifications . get ( "/templates" , async ( c ) => {
262272 try {
263- const templates = await NotificationService . getNotificationTemplates ( ) ;
273+ const userId = c . get ( "jwtPayload" ) . id ;
274+ const templates = await NotificationService . getNotificationTemplates ( userId ) ;
264275
265276 return c . json ( {
266277 success : true ,
@@ -283,6 +294,7 @@ notifications.get("/templates", async (c) => {
283294notifications . get ( "/templates/:id" , async ( c ) => {
284295 try {
285296 const id = parseInt ( c . req . param ( "id" ) ) ;
297+ const userId = c . get ( "jwtPayload" ) . id ; // 获取 userId
286298
287299 if ( isNaN ( id ) ) {
288300 return c . json (
@@ -294,7 +306,7 @@ notifications.get("/templates/:id", async (c) => {
294306 ) ;
295307 }
296308
297- const template = await NotificationService . getNotificationTemplateById ( id ) ;
309+ const template = await NotificationService . getNotificationTemplateById ( id , userId ) ; // 传入 userId
298310
299311 if ( ! template ) {
300312 return c . json (
@@ -390,6 +402,7 @@ notifications.post("/templates", async (c) => {
390402notifications . put ( "/templates/:id" , async ( c ) => {
391403 try {
392404 const id = parseInt ( c . req . param ( "id" ) ) ;
405+ const userId = c . get ( "jwtPayload" ) . id ; // 获取 userId
393406
394407 if ( isNaN ( id ) ) {
395408 return c . json (
@@ -417,6 +430,7 @@ notifications.put("/templates/:id", async (c) => {
417430 // 更新模板
418431 const result = await NotificationService . updateNotificationTemplate (
419432 id ,
433+ userId , // 传入 userId
420434 validatedData
421435 ) ;
422436
@@ -451,6 +465,7 @@ notifications.put("/templates/:id", async (c) => {
451465notifications . delete ( "/templates/:id" , async ( c ) => {
452466 try {
453467 const id = parseInt ( c . req . param ( "id" ) ) ;
468+ const userId = c . get ( "jwtPayload" ) . id ; // 获取 userId
454469
455470 if ( isNaN ( id ) ) {
456471 return c . json (
@@ -462,7 +477,7 @@ notifications.delete("/templates/:id", async (c) => {
462477 ) ;
463478 }
464479
465- const result = await NotificationService . deleteNotificationTemplate ( id ) ;
480+ const result = await NotificationService . deleteNotificationTemplate ( id , userId ) ; // 传入 userId
466481
467482 if ( ! result . success ) {
468483 return c . json (
@@ -491,6 +506,7 @@ notifications.delete("/templates/:id", async (c) => {
491506 }
492507} ) ;
493508
509+
494510// 保存通知设置
495511notifications . post ( "/settings" , async ( c ) => {
496512 try {
0 commit comments