public async Task NotifyAboutNewCommentAsync(ServiceRequestCreatedCommentDto createdComment) { var serviceRequest = await _serviceRequestDbSet.SingleAsync(s => s.Id == createdComment.ServiceRequestId); var organizationName = await GetOrganizationNameAsync(serviceRequest.OrganizationId); var serviceRequestNotificationRoleId = await GetUserNotificationRoleIdAsync(); var emails = await _usersDbSet .Where(x => x.Roles.Any(y => y.RoleId == serviceRequestNotificationRoleId) || x.Id == serviceRequest.EmployeeId) .Where(x => x.Id != createdComment.CommentedEmployeeId) .Select(x => x.Email) .ToListAsync(); var subject = Resources.Common.ServiceRequestAdminCommentedSubject; var userNotificationSettingsUrl = _appSettings.UserNotificationSettingsUrl(organizationName); var serviceRequestUrl = _appSettings.ServiceRequestUrl(organizationName, serviceRequest.Id); var emailTemplateViewModel = new ServiceRequestCommentEmailTemplateViewModel( userNotificationSettingsUrl, serviceRequest.Title, await GetUserFullNameAsync(createdComment.CommentedEmployeeId), createdComment.CommentContent, serviceRequestUrl); var body = _mailTemplate.Generate(emailTemplateViewModel, EmailPremiumTemplateCacheKeys.ServiceRequestComment); await _mailingService.SendEmailAsync(new EmailDto(emails, subject, body)); }
public async Task CreateCommentAsync(ServiceRequestCommentDto comment, UserAndOrganizationDto userAndOrganizationDto) { var serviceRequest = await _serviceRequestsDbSet .SingleOrDefaultAsync(x => x.Id == comment.ServiceRequestId && x.OrganizationId == userAndOrganizationDto.OrganizationId); if (serviceRequest == null) { throw new ValidationException(ErrorCodes.ContentDoesNotExist, "Service request does not exist"); } var timestamp = DateTime.UtcNow; var serviceRequestComment = new ServiceRequestComment { Content = comment.Content, EmployeeId = userAndOrganizationDto.UserId, OrganizationId = userAndOrganizationDto.OrganizationId, ServiceRequest = serviceRequest, CreatedBy = userAndOrganizationDto.UserId, ModifiedBy = userAndOrganizationDto.UserId, Modified = timestamp, Created = timestamp }; _serviceRequestCommentsDbSet.Add(serviceRequestComment); await _uow.SaveChangesAsync(false); var createdComment = new ServiceRequestCreatedCommentDto { ServiceRequestId = comment.ServiceRequestId, CommentedEmployeeId = serviceRequestComment.EmployeeId, CommentContent = serviceRequestComment.Content }; _asyncRunner.Run <IServiceRequestNotificationService>(async notifier => await notifier.NotifyAboutNewCommentAsync(createdComment), _uow.ConnectionName); }