Bläddra i källkod

✨ feat(system-config): 完善系统配置API错误处理和响应格式

- 为创建、删除、更新接口添加ErrorSchema错误响应格式定义
- 统一错误响应格式为{code, message}结构
- 删除接口响应码从200改为204 No Content
- 删除接口成功时返回空响应体而非{success: true}

♻️ refactor(system-config): 优化系统配置服务缓存处理

- 重写delete方法,删除配置时自动清除相关缓存
- 移除update方法中未使用的user变量

✅ test(system-config): 更新集成测试以匹配新的API响应格式

- 更新删除接口测试,验证204响应状态码而非200
yourname 1 månad sedan
förälder
incheckning
7bb00af756

+ 6 - 4
packages/core-module-mt/system-config-module-mt/src/routes/custom/create-system-config.mt.ts

@@ -1,6 +1,6 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { authMiddleware } from '../../../../auth-module-mt/src/middleware/index.mt';
-import { AppDataSource, parseWithAwait } from '@d8d/shared-utils';
+import { AppDataSource, parseWithAwait, ErrorSchema } from '@d8d/shared-utils';
 import { AuthContext } from '@d8d/shared-types';
 import { SystemConfigServiceMt } from '../../services/system-config.service.mt';
 import { CreateSystemConfigSchema, SystemConfigSchema } from '../../schemas/system-config.schema.mt';
@@ -28,10 +28,12 @@ const createSystemConfigRoute = createRoute({
       }
     },
     400: {
-      description: '请求参数错误'
+      description: '请求参数错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     500: {
-      description: '服务器内部错误'
+      description: '服务器内部错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     }
   }
 });
@@ -60,7 +62,7 @@ const createSystemConfigRoutes = new OpenAPIHono<AuthContext>()
     } catch (error) {
       console.error('创建系统配置失败:', error);
       return c.json(
-        { error: error instanceof Error ? error.message : '创建系统配置失败' },
+        { code: 500, message: error instanceof Error ? error.message : '创建系统配置失败' },
         500
       );
     }

+ 14 - 18
packages/core-module-mt/system-config-module-mt/src/routes/custom/delete-system-config.mt.ts

@@ -1,7 +1,7 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { z } from '@hono/zod-openapi';
 import { authMiddleware } from '../../../../auth-module-mt/src/middleware/index.mt';
-import { AppDataSource } from '@d8d/shared-utils';
+import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
 import { AuthContext } from '@d8d/shared-types';
 import { SystemConfigServiceMt } from '../../services/system-config.service.mt';
 
@@ -18,24 +18,20 @@ const deleteSystemConfigRoute = createRoute({
     })
   },
   responses: {
-    200: {
-      description: '系统配置删除成功',
-      content: {
-        'application/json': {
-          schema: z.object({
-            success: z.boolean().openapi({
-              description: '删除操作是否成功',
-              example: true
-            })
-          })
-        }
-      }
+    204: {
+      description: '系统配置删除成功'
+    },
+    400: {
+      description: '请求参数错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     404: {
-      description: '系统配置不存在'
+      description: '系统配置不存在',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     500: {
-      description: '服务器内部错误'
+      description: '服务器内部错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     }
   }
 });
@@ -52,14 +48,14 @@ const deleteSystemConfigRoutes = new OpenAPIHono<AuthContext>()
       const result = await systemConfigService.delete(id);
 
       if (!result) {
-        return c.json({ error: '系统配置不存在' }, 404);
+        return c.json({ code: 404, message: '系统配置不存在' }, 404);
       }
 
-      return c.json({ success: true }, 200);
+      return c.body(null, 204);
     } catch (error) {
       console.error('删除系统配置失败:', error);
       return c.json(
-        { error: error instanceof Error ? error.message : '删除系统配置失败' },
+        { code: 500, message: error instanceof Error ? error.message : '删除系统配置失败' },
         500
       );
     }

+ 9 - 7
packages/core-module-mt/system-config-module-mt/src/routes/custom/update-system-config.mt.ts

@@ -1,7 +1,7 @@
 import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
 import { z } from '@hono/zod-openapi';
 import { authMiddleware } from '../../../../auth-module-mt/src/middleware/index.mt';
-import { AppDataSource, parseWithAwait } from '@d8d/shared-utils';
+import { AppDataSource, parseWithAwait, ErrorSchema } from '@d8d/shared-utils';
 import { AuthContext } from '@d8d/shared-types';
 import { SystemConfigServiceMt } from '../../services/system-config.service.mt';
 import { UpdateSystemConfigSchema, SystemConfigSchema } from '../../schemas/system-config.schema.mt';
@@ -35,13 +35,16 @@ const updateSystemConfigRoute = createRoute({
       }
     },
     400: {
-      description: '请求参数错误'
+      description: '请求参数错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     404: {
-      description: '系统配置不存在'
+      description: '系统配置不存在',
+      content: { 'application/json': { schema: ErrorSchema } }
     },
     500: {
-      description: '服务器内部错误'
+      description: '服务器内部错误',
+      content: { 'application/json': { schema: ErrorSchema } }
     }
   }
 });
@@ -51,7 +54,6 @@ const updateSystemConfigRoutes = new OpenAPIHono<AuthContext>()
   .openapi(updateSystemConfigRoute, async (c) => {
     const { id } = c.req.valid('param');
     const data = c.req.valid('json');
-    const user = c.get('user');
 
     try {
       const systemConfigService = new SystemConfigServiceMt(AppDataSource);
@@ -60,7 +62,7 @@ const updateSystemConfigRoutes = new OpenAPIHono<AuthContext>()
       const result = await systemConfigService.update(id, data);
 
       if (!result) {
-        return c.json({ error: '系统配置不存在' }, 404);
+        return c.json({ code: 404, message: '系统配置不存在' }, 404);
       }
 
       // 验证响应格式
@@ -70,7 +72,7 @@ const updateSystemConfigRoutes = new OpenAPIHono<AuthContext>()
     } catch (error) {
       console.error('更新系统配置失败:', error);
       return c.json(
-        { error: error instanceof Error ? error.message : '更新系统配置失败' },
+        { code: 500, message: error instanceof Error ? error.message : '更新系统配置失败' },
         500
       );
     }

+ 18 - 0
packages/core-module-mt/system-config-module-mt/src/services/system-config.service.mt.ts

@@ -198,6 +198,24 @@ export class SystemConfigServiceMt extends GenericCrudService<SystemConfigMt> {
     return updatedConfig;
   }
 
+  /**
+   * 重写delete方法,在删除时清除缓存
+   */
+  async delete(id: number): Promise<boolean> {
+    // 先获取配置信息,以便后续清除缓存
+    const config = await this.getById(id);
+
+    const result = await super.delete(id);
+
+    if (result && config) {
+      // 清除相关缓存
+      const effectiveTenantId = config.tenantId ?? 0;
+      await redisUtil.deleteSystemConfig(effectiveTenantId, config.configKey);
+    }
+
+    return result;
+  }
+
   /**
    * 缓存预热 - 预加载常用配置到缓存
    */

+ 1 - 1
packages/core-module-mt/system-config-module-mt/tests/integration/system-config.routes.integration.test.ts

@@ -424,7 +424,7 @@ describe('系统配置路由API集成测试 (使用hono/testing)', () => {
         }
       });
 
-      expect(response.status).toBe(200);
+      expect(response.status).toBe(204);
 
       // 验证缓存已被刷新
       const afterDeleteCache = await redisUtil.getSystemConfig(testUser.tenantId, config.configKey);