|
|
@@ -1,7 +1,7 @@
|
|
|
import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
import { z } from '@hono/zod-openapi';
|
|
|
import { authMiddleware } from '@d8d/auth-module';
|
|
|
-import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
|
|
|
+import { AppDataSource, ErrorSchema, parseWithAwait } from '@d8d/shared-utils';
|
|
|
import { AuthContext } from '@d8d/shared-types';
|
|
|
import { OrderGoodsSchema, CreateOrderGoodsDto, UpdateOrderGoodsDto } from '../../schemas/order-goods.schema';
|
|
|
import { UserOrderGoodsService } from '../../services/user-order-goods.service';
|
|
|
@@ -75,8 +75,12 @@ const getUserOrderItemRoute = createRoute({
|
|
|
description: '认证失败',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
},
|
|
|
+ 403: {
|
|
|
+ description: '无权访问该订单商品',
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
+ },
|
|
|
404: {
|
|
|
- description: '订单商品不存在或无权访问',
|
|
|
+ description: '订单商品不存在',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
}
|
|
|
}
|
|
|
@@ -146,8 +150,12 @@ const updateUserOrderItemRoute = createRoute({
|
|
|
description: '认证失败',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
},
|
|
|
+ 403: {
|
|
|
+ description: '无权更新该订单商品',
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
+ },
|
|
|
404: {
|
|
|
- description: '订单商品不存在或无权访问',
|
|
|
+ description: '订单商品不存在',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
}
|
|
|
}
|
|
|
@@ -173,8 +181,12 @@ const deleteUserOrderItemRoute = createRoute({
|
|
|
description: '认证失败',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
},
|
|
|
+ 403: {
|
|
|
+ description: '无权删除该订单商品',
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
+ },
|
|
|
404: {
|
|
|
- description: '订单商品不存在或无权访问',
|
|
|
+ description: '订单商品不存在',
|
|
|
content: { 'application/json': { schema: ErrorSchema } }
|
|
|
}
|
|
|
}
|
|
|
@@ -187,15 +199,18 @@ const app = new OpenAPIHono<AuthContext>()
|
|
|
|
|
|
const orderGoodsService = new UserOrderGoodsService(AppDataSource);
|
|
|
const [data, total] = await orderGoodsService.getUserOrderGoodsList(
|
|
|
- parseInt(page),
|
|
|
- parseInt(pageSize),
|
|
|
+ page,
|
|
|
+ pageSize,
|
|
|
keyword,
|
|
|
user?.id
|
|
|
);
|
|
|
|
|
|
+ // 使用 parseWithAwait 确保数据格式正确
|
|
|
+ const validatedData = await parseWithAwait(z.array(OrderGoodsSchema), data);
|
|
|
+
|
|
|
return c.json({
|
|
|
- data,
|
|
|
- pagination: { total, current: parseInt(page), pageSize: parseInt(pageSize) }
|
|
|
+ data: validatedData,
|
|
|
+ pagination: { total, current: page, pageSize: pageSize }
|
|
|
}, 200);
|
|
|
})
|
|
|
.openapi(getUserOrderItemRoute, async (c) => {
|
|
|
@@ -209,7 +224,9 @@ const app = new OpenAPIHono<AuthContext>()
|
|
|
return c.json({ code: 404, message: '订单商品不存在' }, 404);
|
|
|
}
|
|
|
|
|
|
- return c.json(orderGoods, 200);
|
|
|
+ // 使用 parseWithAwait 确保数据格式正确
|
|
|
+ const validatedData = await parseWithAwait(OrderGoodsSchema, orderGoods);
|
|
|
+ return c.json(validatedData, 200);
|
|
|
} catch (error) {
|
|
|
if (error instanceof Error && error.message.includes('无权')) {
|
|
|
return c.json({ code: 403, message: error.message }, 403);
|
|
|
@@ -224,7 +241,9 @@ const app = new OpenAPIHono<AuthContext>()
|
|
|
try {
|
|
|
const orderGoodsService = new UserOrderGoodsService(AppDataSource);
|
|
|
const orderGoods = await orderGoodsService.createUserOrderGoods(data, user?.id);
|
|
|
- return c.json(orderGoods, 201);
|
|
|
+ // 使用 parseWithAwait 确保数据格式正确
|
|
|
+ const validatedData = await parseWithAwait(OrderGoodsSchema, orderGoods);
|
|
|
+ return c.json(validatedData, 201);
|
|
|
} catch (error) {
|
|
|
if (error instanceof Error && error.message.includes('无权')) {
|
|
|
return c.json({ code: 403, message: error.message }, 403);
|
|
|
@@ -244,7 +263,9 @@ const app = new OpenAPIHono<AuthContext>()
|
|
|
return c.json({ code: 404, message: '订单商品不存在' }, 404);
|
|
|
}
|
|
|
|
|
|
- return c.json(orderGoods, 200);
|
|
|
+ // 使用 parseWithAwait 确保数据格式正确
|
|
|
+ const validatedData = await parseWithAwait(OrderGoodsSchema, orderGoods);
|
|
|
+ return c.json(validatedData, 200);
|
|
|
} catch (error) {
|
|
|
if (error instanceof Error && error.message.includes('无权')) {
|
|
|
return c.json({ code: 403, message: error.message }, 403);
|