From bdbc9764963f82d461e914f2a2e4f2c78479da6a Mon Sep 17 00:00:00 2001 From: erik1110 Date: Tue, 6 Feb 2024 14:58:34 +0800 Subject: [PATCH] [fix] fix delete order --- src/features/order/order.service.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/features/order/order.service.ts b/src/features/order/order.service.ts index 38e2f86..4a2879e 100644 --- a/src/features/order/order.service.ts +++ b/src/features/order/order.service.ts @@ -97,6 +97,19 @@ export class OrderService { } async deleteMyOrder(id: string, req: Request) { + const existingOrder = await this.orderModel.findOne({ + _id: id, + orderUserId: req['user']._id, + }); + + if (!existingOrder) { + throw new AppError(HttpStatus.NOT_FOUND, 'UserError', '此訂單不存在'); + } + + if (existingOrder.status == -1) { + throw new AppError(HttpStatus.NOT_FOUND, 'UserError', '此訂單已被刪除'); + } + const result = await this.orderModel.findByIdAndUpdate( { _id: id, @@ -110,11 +123,7 @@ export class OrderService { runValidators: true, }, ); - if (!result) { - throw new AppError(HttpStatus.NOT_FOUND, 'UserError', '此訂單不存在'); - } else if (result.status == -1) { - throw new AppError(HttpStatus.NOT_FOUND, 'UserError', '此訂單已被刪除'); - } + return getHttpResponse.successResponse({ message: '刪除訂單', });