public async Task <ResponseModel> Handle(OrderCreateRequest request, CancellationToken cancellationToken) { var station = await this.db.Stations.FirstOrDefaultAsync(x => x.Id == request.StationId); if (station == null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.NotFound, Message = "Cửa này này không tìm thấy hoặc đã ngừng hoạt động" }); } if (!station.HasAmbulatory && request.UseAmbulatory) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "Xin lỗi, cửa hàng này không cung cấp dịch vụ cứu hộ" }); } var user = await this.userManager.Users.FirstOrDefaultAsync(x => x.PhoneNumber == request.PhoneNumber); var order = await this.db.Orders .FirstOrDefaultAsync(x => x.UserId == user.Id && (x.Status != CommonConstants.OrderStatus.DONE && x.Status != CommonConstants.OrderStatus.REJECTED && x.Status != CommonConstants.OrderStatus.CANCLED)); if (order != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "Bạn đang có cuốc xe chưa hoàn thành, vui lòng huỷ để đặt cuốc xe mới" }); } order = this.mapper.Map <Order>(request); order.User = user; order.Status = CommonConstants.OrderStatus.WAITING; this.db.Orders.Add(order); await this.db.SaveChangesAsync(cancellationToken); order = await this.db.Orders .Include(x => x.User) .Include(x => x.Station) .ThenInclude(x => x.User) .Include(x => x.OrderDetails) .ThenInclude(x => x.Service) .FirstOrDefaultAsync(x => x.Id == order.Id); await fcmService.SendNotifications(fcmService.GetNotificationsByOrder(order)); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new OrderDetailViewModel(order) }); }
public async Task <ResponseModel> Handle(OrderEditRequest request, CancellationToken cancellationToken) { var order = await this.db.Orders .Include(x => x.OrderDetails) .Include(x => x.Station) .ThenInclude(x => x.User) .Include(x => x.Station) .ThenInclude(x => x.Services) .FirstOrDefaultAsync(x => x.Id == request.Id); if (order == null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.NotFound, Message = "Không tìm thấy cuốc xe này" }); } var requestUser = await userManager.Users .Where(x => x.PhoneNumber == request.PhoneNumber) .FirstOrDefaultAsync(); var requestRoles = await userManager.GetRolesAsync(requestUser); if (!IsAcceptableStatus(order.Status, request.Status) || !IsRolePermitted(requestRoles.ToList(), request.Status)) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.Forbidden, Message = "Bạn không được phép thực hiện hành động này (Tình trạng cuốc xe không phù hợp)" }); } if (requestRoles.Contains(CommonConstants.Roles.STATION) && order.Station.User.PhoneNumber != requestUser.PhoneNumber) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.Forbidden, Message = "Bạn không thể cập nhật cuốc xe của cửa hàng khác" }); } var isStatusChange = false; if (order.Status != request.Status) { order.Status = request.Status; isStatusChange = true; } if (request.OrderDetails.Any()) { var unknownServices = request.OrderDetails.Where(x => !order.Station.Services.Any(y => y.Id == x.ServiceId)).ToList(); if (unknownServices.Any()) { var unknownServiceIds = unknownServices.Select(x => x.ServiceId.ToString()).ToList(); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "Không tìm thấy dịch vụ: " + String.Join(";", unknownServiceIds) }); } order.OrderDetails.Clear(); order.OrderDetails .AddRange( request.OrderDetails.Select(x => new OrderDetail() { ServiceId = x.ServiceId, OrderId = order.Id } ) ); } await this.db.SaveChangesAsync(cancellationToken); order = await this.db.Orders .Include(x => x.User) .Include(x => x.Station) .ThenInclude(x => x.User) .Include(x => x.OrderDetails) .ThenInclude(x => x.Service) .FirstOrDefaultAsync(x => x.Id == order.Id); if (isStatusChange) { await fcmService.SendNotifications(fcmService.GetNotificationsByOrder(order)); } return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new OrderDetailViewModel(order) }); }