From 5580589d06ae050294c28d5efae05f7c75cea2ab Mon Sep 17 00:00:00 2001 From: MinhhTien <92145479+MinhhTien@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:24:33 +0700 Subject: [PATCH] improvement: add field isDeliveryAssigned in order (#75) --- src/common/contracts/error.ts | 5 +++++ src/order/dto/order.dto.ts | 3 +++ src/order/schemas/order.schema.ts | 4 ++++ src/order/services/order.service.ts | 20 ++++++++++++++++++++ src/task/services/task.service.ts | 17 +++++++++++++++-- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/common/contracts/error.ts b/src/common/contracts/error.ts index 53dd845..a5bdd41 100644 --- a/src/common/contracts/error.ts +++ b/src/common/contracts/error.ts @@ -101,6 +101,11 @@ export const Errors: Record = { message: 'Không tìm thấy nhân viên giao hàng', httpStatus: HttpStatus.BAD_REQUEST }, + ORDER_HAS_ASSIGNED_DELIVERY: { + error: 'ORDER_HAS_ASSIGNED_DELIVERY', + message: 'Đơn hàng đã được giao cho người vận chuyển', + httpStatus: HttpStatus.BAD_REQUEST + }, SHIPPING_TASK_INVALID: { error: 'SHIPPING_TASK_INVALID', message: 'Công việc được chọn không hợp lệ', diff --git a/src/order/dto/order.dto.ts b/src/order/dto/order.dto.ts index 26d6d11..0807145 100644 --- a/src/order/dto/order.dto.ts +++ b/src/order/dto/order.dto.ts @@ -84,6 +84,9 @@ export class OrderDto { @ApiPropertyOptional() notes?: string + + @ApiPropertyOptional() + isDeliveryAssigned?: boolean } export class OrderPaginateResponseDto extends DataResponse( diff --git a/src/order/schemas/order.schema.ts b/src/order/schemas/order.schema.ts index edd0a8c..a51e09a 100644 --- a/src/order/schemas/order.schema.ts +++ b/src/order/schemas/order.schema.ts @@ -144,6 +144,10 @@ export class Order { @Prop({ type: String }) notes?: string + @ApiPropertyOptional() + @Prop({ type: Boolean }) + isDeliveryAssigned?: boolean + @Prop({ type: String }) reason?: string } diff --git a/src/order/services/order.service.ts b/src/order/services/order.service.ts index 1448571..49eb5da 100644 --- a/src/order/services/order.service.ts +++ b/src/order/services/order.service.ts @@ -215,6 +215,26 @@ export class OrderService { return new SuccessResponse(true) } + public async assignDeliveryToOrder(orderId: string, session?: ClientSession) { + // 1. Update isDeliveryAssigned + const order = await this.orderRepository.findOneAndUpdate( + { + _id: orderId, + orderStatus: OrderStatus.CONFIRMED, + transactionStatus: TransactionStatus.CAPTURED + }, + { + $set: { isDeliveryAssigned: true } + }, + { + session + } + ) + if (!order) throw new AppException(Errors.ORDER_STATUS_INVALID) + + return order + } + public async deliveryOrder(orderId: string, userId: string, role: UserRole, session?: ClientSession) { // 1. Update order status and order history const orderHistory = new OrderHistoryDto(OrderStatus.DELIVERING, TransactionStatus.CAPTURED, userId, role) diff --git a/src/task/services/task.service.ts b/src/task/services/task.service.ts index cdf1133..aeee4d5 100644 --- a/src/task/services/task.service.ts +++ b/src/task/services/task.service.ts @@ -49,7 +49,17 @@ export class TaskService { throw new AppException(Errors.DELIVERY_STAFF_NOT_FOUND) } - // 2. Create shipping task + // 2. Check shipping task is already created by orderId + const shippingTask = await this.taskRepository.findOne({ + conditions: { + orderId: createShippingTaskDto.orderId + } + }) + if (shippingTask) { + throw new AppException(Errors.ORDER_HAS_ASSIGNED_DELIVERY) + } + + // 3. Create shipping task const task = await this.taskRepository.create( { ...createShippingTaskDto, @@ -60,7 +70,10 @@ export class TaskService { { session } ) - // 3. Send notification to staff + // 4. Update isDeliveryAssigned to order + await this.orderService.assignDeliveryToOrder(createShippingTaskDto.orderId, session) + + // 5. Send notification to staff await session.commitTransaction() return new IDResponse(task._id)