public async Task Send(string recipient, string messageText, Booking booking) { var message = new SentSmsMessage() { Booking = booking, RecipientNumber = recipient, MessageText = messageText, SentDate = DateTime.UtcNow, SmsMessageParts = new List <SentSmsMessagePart>(), }; _context.Add(message); await _context.SaveChangesAsync(); //send message var result = await _nexmoService.SendSms("BHPGK", message.RecipientNumber, message.MessageText); //save status foreach (var nexmoPart in result.Messages) { message.SmsMessageParts.Add(new SentSmsMessagePart() { GatewayMessageId = nexmoPart.MessageId, StatusCode = nexmoPart.Status, StatusText = nexmoPart.ErrorText, }); } await _context.SaveChangesAsync(); }
public async Task <ActionResult> AssignPilot(Guid id, AssignPilotPostbackModel newPilot) { var booking = _context.Bookings .Include(b => b.AssignedPilot) .Include(b => b.BookingEvents) .Include(b => b.BookedPilots).ThenInclude(bp => bp.Pilot) .FirstOrDefault(b => b.Id == id); var userId = _userManager.GetUserId(User); if (!User.IsAdmin() && booking.AssignedPilot.Id != userId) { return(RedirectToAction("Details", new { id = id, errorMessage = "Only admin or currently assigned pilot can assign new pilot" })); } //get optional pilot id from input var newPilotId = newPilot.NewPilotUserId; var originalPilot = booking.AssignedPilot; string errorMessage = null; //unassign the previous pilot if (booking.AssignedPilot != null) { var removedMessage = $"Removed assigned pilot {booking.AssignedPilot} ({booking.AssignedPilot.PhoneNumber.AsPhoneNumber()})"; if (!string.IsNullOrEmpty(newPilot.NewPilotMessage)) { removedMessage = removedMessage + $" due to {newPilot.NewPilotMessage}"; } _bookingService.AddEvent(booking, User, removedMessage); } ApplicationUser assignedPilot; if (string.IsNullOrEmpty(newPilotId)) { //try to find a new pilot (if none are available, set no pilot assigned) assignedPilot = await _bookingService.AssignNewPilotAsync(booking); } else { assignedPilot = _context.Users.Single(u => u.Id == newPilotId); _bookingService.AssignNewPilot(booking, assignedPilot); } await _context.SaveChangesAsync(); //notify pilot and passenger of the newly assigned pilot var bookingDateString = booking.BookingDate.ToString("dd.MM.yyyy"); if (assignedPilot != null) { await _messageService.SendNewPilotMessage(bookingDateString, booking, originalPilot, newPilot.NewPilotNotifyPassenger); if (originalPilot != null) { await _messageService.SendPilotUnassignedMessage(booking, originalPilot); } if (string.IsNullOrEmpty(newPilotId)) { _bookingService.AddEvent(booking, User, $"Assigned booking to {assignedPilot.Name} ({assignedPilot.PhoneNumber.AsPhoneNumber()})"); } else { _bookingService.AddEvent(booking, User, $"Forced booking to {assignedPilot.Name} ({assignedPilot.PhoneNumber.AsPhoneNumber()})"); } if (newPilot.NewPilotNotifyPassenger) { _bookingService.AddEvent(booking, User, $"Sent status update to passenger, {booking.PassengerName} ({booking.PassengerPhone.AsPhoneNumber()})"); } } else if (originalPilot != null) { await _messageService.SendMissingPilotMessage(bookingDateString, booking); await _messageService.SendPilotUnassignedMessage(booking, originalPilot); _bookingService.AddEvent(booking, User, $"No pilots available, sent message to tandem coordinator {_bookingCoordinatorSettings.Name} ({_bookingCoordinatorSettings.PhoneNumber.AsPhoneNumber()})"); _bookingService.AddEvent(booking, User, $"Sent status update to passenger, {booking.PassengerName} ({booking.PassengerPhone.AsPhoneNumber()})"); } else { errorMessage = "No pilots available. Please select pilot manually"; return(RedirectToAction("AssignPilot", new { Id = booking.Id, errorMessage })); } await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { Id = booking.Id, errorMessage })); }