|
@@ -0,0 +1,129 @@
|
|
|
|
|
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
|
|
+import { z } from '@hono/zod-openapi';
|
|
|
|
|
+import { AppDataSource } from '@/server/data-source';
|
|
|
|
|
+import { CustomerService } from '@/server/modules/customers/customer.service';
|
|
|
|
|
+import { CustomerSchema } from '@/server/modules/customers/customer.entity';
|
|
|
|
|
+import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
|
|
+import { authMiddleware } from '@/server/middleware/auth';
|
|
|
|
|
+import { logger } from '@/server/utils/logger';
|
|
|
|
|
+import { AuthContext } from '@/server/types/context';
|
|
|
|
|
+
|
|
|
|
|
+// Initialize service
|
|
|
|
|
+const customerService = new CustomerService(AppDataSource);
|
|
|
|
|
+
|
|
|
|
|
+// Path parameters schema
|
|
|
|
|
+const ParamsSchema = z.object({
|
|
|
|
|
+ id: z.coerce.number().int().positive().openapi({
|
|
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
|
|
+ example: 1,
|
|
|
|
|
+ description: '客户ID'
|
|
|
|
|
+ })
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+// Update request schema (omit auto-generated fields)
|
|
|
|
|
+const UpdateCustomerSchema = CustomerSchema.omit({
|
|
|
|
|
+ id: true,
|
|
|
|
|
+ createdAt: true,
|
|
|
|
|
+ updatedAt: true,
|
|
|
|
|
+ isDeleted: true
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+// Success response schema
|
|
|
|
|
+const SuccessResponseSchema = z.object({
|
|
|
|
|
+ code: z.number().openapi({ example: 200 }),
|
|
|
|
|
+ message: z.string().openapi({ example: '客户更新成功' }),
|
|
|
|
|
+ data: CustomerSchema
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+// Route definition
|
|
|
|
|
+const routeDef = createRoute({
|
|
|
|
|
+ method: 'put',
|
|
|
|
|
+ path: '/{id}',
|
|
|
|
|
+ middleware: [authMiddleware],
|
|
|
|
|
+ request: {
|
|
|
|
|
+ params: ParamsSchema,
|
|
|
|
|
+ body: {
|
|
|
|
|
+ content: {
|
|
|
|
|
+ 'application/json': { schema: UpdateCustomerSchema }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ responses: {
|
|
|
|
|
+ 200: {
|
|
|
|
|
+ description: '客户更新成功',
|
|
|
|
|
+ content: {
|
|
|
|
|
+ 'application/json': { schema: SuccessResponseSchema }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ 400: {
|
|
|
|
|
+ description: '客户端错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 401: {
|
|
|
|
|
+ description: '未授权访问',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 404: {
|
|
|
|
|
+ description: '客户不存在',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ },
|
|
|
|
|
+ 500: {
|
|
|
|
|
+ description: '服务器错误',
|
|
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+// Create route instance
|
|
|
|
|
+const updateRoute = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
|
|
+ const customerData = c.req.valid('json');
|
|
|
|
|
+ const user = c.get('user');
|
|
|
|
|
+
|
|
|
|
|
+ if (!user) {
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 401,
|
|
|
|
|
+ message: '未授权访问'
|
|
|
|
|
+ }, 401);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logger.api(`用户 ${user.id} 更新客户: ${id}, 数据: %o`, customerData);
|
|
|
|
|
+
|
|
|
|
|
+ // Check if customer exists
|
|
|
|
|
+ const existingCustomer = await customerService.findOne(id);
|
|
|
|
|
+ if (!existingCustomer) {
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 404,
|
|
|
|
|
+ message: '客户不存在'
|
|
|
|
|
+ }, 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Update customer
|
|
|
|
|
+ const updatedCustomer = await customerService.update(id, customerData);
|
|
|
|
|
+
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 200,
|
|
|
|
|
+ message: '客户更新成功',
|
|
|
|
|
+ data: updatedCustomer
|
|
|
|
|
+ }, 200);
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ const error = err as Error;
|
|
|
|
|
+ logger.error('更新客户失败:', error);
|
|
|
|
|
+
|
|
|
|
|
+ // Handle validation errors
|
|
|
|
|
+ if (error instanceof z.ZodError) {
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 400,
|
|
|
|
|
+ message: '参数验证失败: ' + error.errors.map(e => e.message).join(', ')
|
|
|
|
|
+ }, 400);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return c.json({
|
|
|
|
|
+ code: 500,
|
|
|
|
|
+ message: error.message || '更新客户失败'
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+export default updateRoute;
|