public async Task <IActionResult> Insert([FromBody] WorkFlowDto workFlowDto) { var workFlow = _mapper.Map <workflow>(workFlowDto); workFlow.editor = GetSession().Username; _workFlowService.Validate(workFlow); var tasks = new List <Task>(); foreach (var reviewer in workFlow.reviewers) { tasks.Add(_userService.FindByUsernameAsync(reviewer.username)); } tasks.Add(_userService.FindByUsernameAsync(workFlow.editor)); await Task.WhenAll(tasks.ToArray()); var status = await _publicationService.GetStatusAsync(workFlow.publicationId); if (status != PublicationStatus.SUBMITED) { throw new ValidationException("Invalid status"); } HelperMethods.ThrowIfNotNull(await _workFlowService.FindByPublicationIdAsync(workFlow.publicationId)); await _publicationService.UpdateStatusAsync(workFlow.publicationId, PublicationStatus.ASSIGNED); await _workFlowService.InsertAsync(workFlow); await _workFlowService.SendEmailToReviewersAsync(workFlow); return(Ok()); }
private async Task SendStateChangedEmailAsync(string publicationId, string nextStatus, string userRole, string username) { var statusEnum = HelperMethods.StatusStringToEnum(nextStatus); var roleEnum = HelperMethods.RoleStringToEnum(userRole); var workflow = await _workFlowService.FindByPublicationIdAsync(publicationId); var publication = await _publicationDataAccess.FindByIdAsync(publicationId); var authors = new List <DataAccess.Model.User>(); foreach (var author in publication.authors) { authors.Add(await _userService.FindByUsernameAsync(author.username)); } var editor = await _userService.FindByUsernameAsync(workflow.editor); var reviewers = new List <DataAccess.Model.User>(); foreach (var reviewer in workflow.reviewers) { if (reviewer.status == ReviewerStatus.ACCEPTED.ToString().ToLower()) { reviewers.Add(await _userService.FindByUsernameAsync(reviewer.username)); } } string xmlFilePath = string.Empty; string[] to = null; string[] args = null; switch (roleEnum) { case Role.Editor: switch (statusEnum) { case PublicationStatus.ACCEPTED: xmlFilePath = AppSettings.Paths.EditorAcceptedMail; to = authors.Select(a => a.Email).ToArray(); args = new string[] { string.Join(",", authors.Select(a => a.Name).ToArray()), publication.title }; break; case PublicationStatus.DENIED: xmlFilePath = AppSettings.Paths.EditorDeniedMail; to = authors.Select(a => a.Email).ToArray(); args = new string[] { string.Join(",", authors.Select(a => a.Name).ToArray()), publication.title }; break; case PublicationStatus.SHOULD_REVISE: xmlFilePath = AppSettings.Paths.EditorShouldReviseMail; to = authors.Select(a => a.Email).ToArray(); args = new string[] { string.Join(",", authors.Select(a => a.Name).ToArray()), publication.title }; break; case PublicationStatus.IN_REVIEW: xmlFilePath = AppSettings.Paths.EditorInReviewMail; to = reviewers.Select(r => r.Email).ToArray(); args = new string[] { string.Join(",", reviewers.Select(r => r.Name).ToArray()), publication.title }; break; } break; case Role.Reviewer: switch (statusEnum) { case PublicationStatus.REVIEWED: xmlFilePath = AppSettings.Paths.ReviewerReviewed; to = new string[] { editor.Email }; var currentReviewer = reviewers.FirstOrDefault(r => r.Username == username); args = new string[] { editor.Name, publication.title, currentReviewer.Name }; break; } break; case Role.Author: switch (statusEnum) { case PublicationStatus.WITHDRAWN: xmlFilePath = AppSettings.Paths.AuthorWithdrawnMail; var toList = new List <string>(); toList.AddRange(reviewers.Select(r => r.Email).ToArray()); toList.Add(editor.Email); to = toList.ToArray(); args = new string[] { editor.Name, publication.title }; break; case PublicationStatus.REVISED: xmlFilePath = AppSettings.Paths.AuthorRevisedMail; var toList2 = new List <string>(); toList2.AddRange(reviewers.Select(r => r.Email).ToArray()); toList2.Add(editor.Email); to = toList2.ToArray(); args = new string[] { editor.Name, publication.title }; break; } break; } if (!string.IsNullOrEmpty(xmlFilePath)) { var path = Path.Combine(AppSettings.Paths.BasePath, xmlFilePath); var emailData = XmlUtility.DeserializeFromFile <EmailEntity>(path); var body = string.Format(emailData.Body, args); await _emailService.SendEmailAsync(emailData.Subject, body, to); } }