Skip to content

Commit 1c62802

Browse files
committed
refactor(notifications): reuse jwt payload variable for user id
1 parent 0cc1267 commit 1c62802

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

backend/src/api/notifications.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const notifications = new Hono<{ Bindings: Bindings; Variables: { jwtPayload: Jw
99
// 获取通知配置
1010
notifications.get("/", async (c) => {
1111
try {
12-
const userId = (c.get("jwtPayload") as JwtPayload).id;
12+
const payload = c.get("jwtPayload") as JwtPayload;
13+
const userId = payload.id;
1314
const config = await NotificationService.getNotificationConfig(userId);
1415

1516
return c.json({
@@ -32,7 +33,8 @@ notifications.get("/", async (c) => {
3233
// 获取通知渠道列表
3334
notifications.get("/channels", async (c) => {
3435
try {
35-
const userId = (c.get("jwtPayload") as JwtPayload).id;
36+
const payload = c.get("jwtPayload") as JwtPayload;
37+
const userId = payload.id;
3638
const channels = await NotificationService.getNotificationChannels(userId);
3739

3840
return c.json({
@@ -56,7 +58,8 @@ notifications.get("/channels", async (c) => {
5658
notifications.get("/channels/:id", async (c) => {
5759
try {
5860
const id = parseInt(c.req.param("id"));
59-
const userId = (c.get("jwtPayload") as JwtPayload).id;
61+
const payload = c.get("jwtPayload") as JwtPayload;
62+
const userId = payload.id;
6063

6164
if (isNaN(id)) {
6265
return c.json(
@@ -100,7 +103,8 @@ notifications.get("/channels/:id", async (c) => {
100103
// 创建通知渠道
101104
notifications.post("/channels", async (c) => {
102105
try {
103-
const userId = (c.get("jwtPayload") as JwtPayload).id;
106+
const payload = c.get("jwtPayload") as JwtPayload;
107+
const userId = payload.id;
104108
const body = await c.req.json();
105109

106110
// 验证请求数据
@@ -161,7 +165,8 @@ notifications.post("/channels", async (c) => {
161165
notifications.put("/channels/:id", async (c) => {
162166
try {
163167
const id = parseInt(c.req.param("id"));
164-
const userId = (c.get("jwtPayload") as JwtPayload).id;
168+
const payload = c.get("jwtPayload") as JwtPayload;
169+
const userId = payload.id;
165170

166171
if (isNaN(id)) {
167172
return c.json(
@@ -226,7 +231,8 @@ notifications.put("/channels/:id", async (c) => {
226231
notifications.delete("/channels/:id", async (c) => {
227232
try {
228233
const id = parseInt(c.req.param("id"));
229-
const userId = (c.get("jwtPayload") as JwtPayload).id;
234+
const payload = c.get("jwtPayload") as JwtPayload;
235+
const userId = payload.id;
230236

231237
if (isNaN(id)) {
232238
return c.json(
@@ -271,7 +277,8 @@ notifications.delete("/channels/:id", async (c) => {
271277
// 获取通知模板列表
272278
notifications.get("/templates", async (c) => {
273279
try {
274-
const userId = (c.get("jwtPayload") as JwtPayload).id;
280+
const payload = c.get("jwtPayload") as JwtPayload;
281+
const userId = payload.id;
275282
const templates = await NotificationService.getNotificationTemplates(userId);
276283

277284
return c.json({
@@ -295,7 +302,8 @@ notifications.get("/templates", async (c) => {
295302
notifications.get("/templates/:id", async (c) => {
296303
try {
297304
const id = parseInt(c.req.param("id"));
298-
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
305+
const payload = c.get("jwtPayload") as JwtPayload;
306+
const userId = payload.id; // 获取 userId
299307

300308
if (isNaN(id)) {
301309
return c.json(
@@ -339,7 +347,8 @@ notifications.get("/templates/:id", async (c) => {
339347
// 创建通知模板
340348
notifications.post("/templates", async (c) => {
341349
try {
342-
const userId = (c.get("jwtPayload") as JwtPayload).id;
350+
const payload = c.get("jwtPayload") as JwtPayload;
351+
const userId = payload.id;
343352
const body = await c.req.json();
344353

345354
// 验证请求数据
@@ -403,7 +412,8 @@ notifications.post("/templates", async (c) => {
403412
notifications.put("/templates/:id", async (c) => {
404413
try {
405414
const id = parseInt(c.req.param("id"));
406-
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
415+
const payload = c.get("jwtPayload") as JwtPayload;
416+
const userId = payload.id; // 获取 userId
407417

408418
if (isNaN(id)) {
409419
return c.json(
@@ -466,7 +476,8 @@ notifications.put("/templates/:id", async (c) => {
466476
notifications.delete("/templates/:id", async (c) => {
467477
try {
468478
const id = parseInt(c.req.param("id"));
469-
const userId = (c.get("jwtPayload") as JwtPayload).id; // 获取 userId
479+
const payload = c.get("jwtPayload") as JwtPayload;
480+
const userId = payload.id; // 获取 userId
470481

471482
if (isNaN(id)) {
472483
return c.json(
@@ -511,7 +522,8 @@ notifications.delete("/templates/:id", async (c) => {
511522
// 保存通知设置
512523
notifications.post("/settings", async (c) => {
513524
try {
514-
const userId = (c.get("jwtPayload") as JwtPayload).id;
525+
const payload = c.get("jwtPayload") as JwtPayload;
526+
const userId = payload.id;
515527
const body = await c.req.json();
516528

517529
const schema = z.object({

0 commit comments

Comments
 (0)