public async Task SendRentalNotificationAsync(CancellationToken ct = default) { var uncompletedRents = await _unitOfWork.RentalRepository.GetUncompletedRentalsAsync(ct); foreach (var rent in uncompletedRents) { if (rent.EndDate.Date <= DateTime.Now.Date) { rent.Status = RentalStatus.Completed; _unitOfWork.RentalRepository.Update(rent); await _unitOfWork.CommitAsync(ct); EmailMessage message = new EmailMessage { SendToEmail = rent.User.Email, Subject = _configuration.AutomaticallyCloseRentMessageSubject, Message = string.Format( _configuration.AutomaticallyCloseRentMessageTemplate, $"{rent.Id} - {rent.Robot.Model.Company.Name} {rent.Robot.Model.Name}", rent.Id) }; await _emailSendService.SendMessageAsync(message, ct); message.SendToEmail = rent.Robot.User.Email; await _emailSendService.SendMessageAsync(message, ct); continue; } //Send notification to user until int hoursToComplete = (rent.EndDate - DateTime.Now).Hours; if (hoursToComplete <= 24) { EmailMessage message = new EmailMessage { SendToEmail = rent.User.Email, Subject = _configuration.ReturnRobotMessageSubject, Message = string.Format( _configuration.ReturnRobotMessageTemplate, $"{rent.Id} - {rent.Robot.Model.Company.Name} {rent.Robot.Model.Name}", rent.Id, hoursToComplete) }; await _emailSendService.SendMessageAsync(message, ct); } } }
public async Task SendNotificationByUserInterestsAsync(CancellationToken ct = default) { IEnumerable <UserInterestsSearch> userInterestsSearches = await _unitOfWork.UserInterestsSearchRepository.GetListAsync(ct); foreach (var item in userInterestsSearches) { Robot robot = await _unitOfWork.RobotRepository.GetRobotByUserInterestsAsync(item.UserId, item.Interests, ct); if (robot != null) { EmailMessage message = new EmailMessage { Subject = _configuration.NewRobotMessageSubject, Message = string.Format( _configuration.NewRobotMessageTemplate, $"{robot.Model.Company.Name} {robot.Model.Name}", robot.Id), SendToEmail = item.User.Email }; await _emailSendService.SendMessageAsync(message, ct); _unitOfWork.UserInterestsSearchRepository.Delete(item.Id); await _unitOfWork.CommitAsync(ct); } } }
public async Task SendEmailAsync(CreateRentalMessageDto item, CancellationToken ct = default) { var rental = await _unitOfWork.RentalRepository.GetAsync(item.RentalId, ct); string sendTo = string.Empty; string sendFrom = string.Empty; if (item.UserId == rental.UserId) { sendTo = rental.Robot.User.Email; sendFrom = rental.User.Email; } if (item.UserId == rental.Robot.UserId) { sendFrom = rental.Robot.User.Email; sendTo = rental.User.Email; } string messageToSend = string.Format( _emailSenderConfiguration.NewMessageNotificationTemplate, sendFrom, $"{rental.Id} - {rental.Robot.Model.Company.Name} {rental.Robot.Model.Name}", rental.Id, item.Message); EmailMessage message = new EmailMessage { Message = messageToSend, Subject = _emailSenderConfiguration.NewMessageNotificationSubject, SendToEmail = sendTo }; await _emailSendService.SendMessageAsync(message, ct); }