/// <summary> /// Отклонить входящий заказ. /// </summary> /// <param name="orderId"></param> /// <returns></returns> public async Task RejectAsync(int courierId, int orderId) { var courierShiftHistory = await _courierShiftHistoryService.FindCurrentOrNextAsync(courierId); if (courierShiftHistory is null) { throw new AppException(AppMessage.UndefinedCourierShiftHistory); } // var order = await _dbContext.Orders.FirstOrDefaultAsync(x => x.Id == orderId); if (order.OrderStatusCode.IsFinal) { throw new AppException(AppMessage.OrderStatusIsCompleted); } order.CourierShiftHistoryId = null; order.CourierId = null; order.OrderStatusCodeId = (int)OrderStatusCode.CourierSelected; var incomingOrder = new IncomingOrderHistory() { CourierShiftHistoryId = courierShiftHistory.Id, OrderId = orderId, StatusId = (int)IncomingOrderStatuses.Rejected, UpdatedAt = DateTime.Now, }; _dbContext.IncomingOrderHistories.Add(incomingOrder); await _dbContext.SaveChangesAsync(); await WebSocketService.ShiftChanged(courierId); }
/// <summary> /// Принять входящий заказ в работу. /// </summary> /// <param name="courierId"></param> /// <param name="orderId"></param> /// <returns></returns> public async Task <IncomingOrderDto> AcceptAsync(int courierId, int orderId) { var pendingOrder = await GetOrderIfPendingAsync(courierId, orderId); if (pendingOrder == null) { throw new AppException(new AppMessage(0, "Accept: Undefined error", HttpStatusCode.BadRequest)); } var courierShiftHistory = await _courierShiftHistoryService.FindCurrentOrNextAsync(courierId); if (courierShiftHistory is null) { throw new AppException(AppMessage.UndefinedCourierShiftHistory); } // var order = await _dbContext.Orders.FirstOrDefaultAsync(x => x.Id == orderId); if (order.OrderStatusCode.IsFinal) { throw new AppException(AppMessage.OrderStatusIsCompleted); } order.CourierShiftHistoryId = courierShiftHistory.Id; order.OrderStatusCodeId = (int)OrderStatusCode.CourierAccepted; var incomingOrder = new IncomingOrderHistory() { CourierShiftHistoryId = courierShiftHistory.Id, OrderId = orderId, StatusId = (int)IncomingOrderStatuses.Accepted, UpdatedAt = DateTime.Now, }; _dbContext.IncomingOrderHistories.Add(incomingOrder); await _dbContext.SaveChangesAsync(); await WebSocketService.ShiftChanged(courierId); // mocked return(new IncomingOrderDto() { Id = orderId, VendorLogo = pendingOrder.Vendor.Logo, ClientName = pendingOrder.Client.Name, Status = order.OrderStatusCode.Text, TotalCost = pendingOrder.TotalCost }); }
public async Task <NextStatus> UpdateStatus(int orderId, int statusId) { var order = await _dbContext.Orders .FirstOrDefaultAsync(x => x.Id == orderId); if (order is null) { throw new AppException(AppMessage.OrderNotExists); } int currentOrderStatusCodeId = order.OrderStatusCodeId; int nextOrderStatusCodeId = 0; if (order.OrderStatusCodeId == (int)OrderStatusCode.CourierAccepted) { currentOrderStatusCodeId = (int)OrderStatusCode.Prepared; nextOrderStatusCodeId = (int)OrderStatusCode.Delivered; } else if (order.OrderStatusCodeId == (int)OrderStatusCode.Prepared) { currentOrderStatusCodeId = (int)OrderStatusCode.Delivered; nextOrderStatusCodeId = -1; } order.OrderStatusCodeId = currentOrderStatusCodeId; await _dbContext.SaveChangesAsync(); var nextOrderStatusCode = await GetOrderStatusCodeAsync(nextOrderStatusCodeId); if (nextOrderStatusCode is null) { return(null); } var nextStatus = new NextStatus { Id = nextOrderStatusCode.Id, Name = nextOrderStatusCode.Text }; await WebSocketService.ShiftChanged((int)order.CourierId); return(nextStatus); }