Skip to content

Commit

Permalink
improvement: add field isDeliveryAssigned in order (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhhTien authored Mar 11, 2024
1 parent fd10bba commit 5580589
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/contracts/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export const Errors: Record<string, ErrorResponse> = {
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ệ',
Expand Down
3 changes: 3 additions & 0 deletions src/order/dto/order.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class OrderDto {

@ApiPropertyOptional()
notes?: string

@ApiPropertyOptional()
isDeliveryAssigned?: boolean
}

export class OrderPaginateResponseDto extends DataResponse(
Expand Down
4 changes: 4 additions & 0 deletions src/order/schemas/order.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export class Order {
@Prop({ type: String })
notes?: string

@ApiPropertyOptional()
@Prop({ type: Boolean })
isDeliveryAssigned?: boolean

@Prop({ type: String })
reason?: string
}
Expand Down
20 changes: 20 additions & 0 deletions src/order/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions src/task/services/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 5580589

Please sign in to comment.