Sfoglia il codice sorgente

✨ feat(passengers): 增强乘客API错误处理和文档

- 为get、update、delete和setDefault接口添加403错误响应文档
- 为update和setDefault接口添加500错误处理逻辑
- 优化测试用例中无效乘客数据的测试方式

♻️ refactor(passengers): 清理未使用的导入

- 删除未使用的PassengerCreateSchema导入
yourname 3 mesi fa
parent
commit
1cb03c6f68

+ 22 - 1
src/server/api/passengers/index.ts

@@ -3,7 +3,6 @@ import { z } from '@hono/zod-openapi';
 import { authMiddleware } from '@/server/middleware/auth.middleware';
 import { PassengerService } from '@/server/modules/passengers/passenger.service';
 import {
-  PassengerCreateSchema,
   PassengerUserCreateSchema,
   PassengerUpdateSchema,
   PassengerResponseSchema,
@@ -111,6 +110,10 @@ const getRouteDef = createRoute({
       description: '未授权',
       content: { 'application/json': { schema: ErrorSchema } }
     },
+    403: {
+      description: '无权访问',
+      content: { 'application/json': { schema: ErrorSchema } }
+    },
     404: {
       description: '乘客不存在',
       content: { 'application/json': { schema: ErrorSchema } }
@@ -154,6 +157,10 @@ const updateRouteDef = createRoute({
       description: '未授权',
       content: { 'application/json': { schema: ErrorSchema } }
     },
+    403: {
+      description: '无权修改',
+      content: { 'application/json': { schema: ErrorSchema } }
+    },
     404: {
       description: '乘客不存在',
       content: { 'application/json': { schema: ErrorSchema } }
@@ -185,6 +192,10 @@ const deleteRouteDef = createRoute({
       description: '未授权',
       content: { 'application/json': { schema: ErrorSchema } }
     },
+    403: {
+      description: '无权删除',
+      content: { 'application/json': { schema: ErrorSchema } }
+    },
     404: {
       description: '乘客不存在',
       content: { 'application/json': { schema: ErrorSchema } }
@@ -219,6 +230,10 @@ const setDefaultRoute = createRoute({
       description: '未授权',
       content: { 'application/json': { schema: ErrorSchema } }
     },
+    403: {
+      description: '无权设置',
+      content: { 'application/json': { schema: ErrorSchema } }
+    },
     404: {
       description: '乘客不存在',
       content: { 'application/json': { schema: ErrorSchema } }
@@ -318,6 +333,9 @@ const app = new OpenAPIHono<AuthContext>()
       }
 
       const result = await passengerService.updatePassenger(id, data);
+      if (!result) {
+        return c.json({ code: 500, message: '更新乘客失败' }, 500);
+      }
       return c.json(result, 200);
     } catch (error) {
       console.error('更新乘客失败:', error);
@@ -368,6 +386,9 @@ const app = new OpenAPIHono<AuthContext>()
       }
 
       const result = await passengerService.setDefaultPassenger(user.id, id);
+      if (!result) {
+        return c.json({ code: 500, message: '设置默认乘客失败' }, 500);
+      }
       return c.json(result, 200);
     } catch (error) {
       console.error('设置默认乘客失败:', error);

+ 1 - 1
tests/integration/server/passengers.integration.test.ts

@@ -121,7 +121,7 @@ describe('用户端乘客API集成测试', () => {
 
     it('应该拒绝创建缺少必填字段的乘客', async () => {
       const passengerData = {
-        // 缺少name字段
+        name: '', // 空字符串作为无效值
         idType: IdType.ID_CARD,
         idNumber: '110101199001011234',
         phone: '13812345678',