public async Task <IHttpActionResult> Submit([FromBody] ApplicationSubmission submission) { var results = _applicationSubmissionValidator.Validate(submission); if (!results.IsValid) { BadRequest(results.Errors.ToString()); } var account = new AccountController(_employerService, _organizationService, _identityService); account.UserManager = UserManager; var userInfo = account.GetUserInfo(); _applicationService.ProcessModel(submission); // make sure user has permission to submit application var hasPermission = _identityService.HasSavePermission(userInfo, submission.Id); if (!hasPermission) { Unauthorized("Unauthorized"); } // Find all the attachments that are not in the file system ApplicationDocumentHelper applicationDocumentHelper = new ApplicationDocumentHelper(_applicationService, _attachmentService, _responseService); var getMissingAttachment = applicationDocumentHelper.FindAllApplicationAttachmentsNotExistInFileSystem(submission); if (getMissingAttachment != null && getMissingAttachment.Count > 0) { // Create file not found message // Alert user that one or more attachments are not exist in the file system var responseMessage = Request.CreateResponse(HttpStatusCode.NotFound); responseMessage.Content = new ObjectContent <List <VerifyAttachmentViewModel> >(getMissingAttachment, GlobalConfiguration.Configuration.Formatters.JsonFormatter); return(ResponseMessage(responseMessage)); } var user = UserManager.Users.SingleOrDefault(s => s.Id == userInfo.UserId); var org = user.Organizations.FirstOrDefault(x => x.ApplicationId == submission.Id); if (org.ApplicationStatusId == StatusIds.InProgress) { await _applicationService.SubmitApplicationAsync(submission); // Update Organization Status org.ApplicationStatusId = StatusIds.Submitted; user.Organizations.Select(x => x.Employer).ToList(); await UserManager.UpdateAsync(user); } // remove the associated application save _saveService.Remove(submission.Id); var response = await GetApplicationDocument(new Guid(submission.Id)); // Get return value from API call var contentResult = response as OkNegotiatedContentResult <byte[]>; var returnValue = contentResult.Content; if (returnValue == null) { InternalServerError("Get concatenate Pdf failed"); } // Calling Email Web API var baseUri = new Uri(AppSettings.Get <string>("EmailApiBaseUrl")); var httpClientConnectionLeaseTimeout = AppSettings.Get <int>("HttpClientConnectionLeaseTimeout"); // Get Http Client var httpClientInstance = MyHttpClient; httpClientInstance.DefaultRequestHeaders.Clear(); httpClientInstance.DefaultRequestHeaders.ConnectionClose = false; httpClientInstance.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (httpClientInstance.BaseAddress != baseUri) { httpClientInstance.BaseAddress = baseUri; } ServicePointManager.FindServicePoint(baseUri).ConnectionLeaseTimeout = httpClientConnectionLeaseTimeout; // Get Email Contents var certificationTeamEmailTemplatePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/CertificationTeamEmailTemplate.txt"); var certificationTeamEmailTemplateString = File.ReadAllText(certificationTeamEmailTemplatePath); var employerEmailTemplatePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/EmployerEmailTemplate.txt"); var employerEmailTemplateString = File.ReadAllText(employerEmailTemplatePath); var emailContents = _emailService.PrepareApplicationEmailContents(submission, certificationTeamEmailTemplateString, employerEmailTemplateString, EmailReceiver.Both); var pdfName = string.Format("14c_Application_{0}_{1}_{2}.pdf", submission.Employer.PhysicalAddress.State, DateTime.Now.ToString("yyyy-MM-dd"), Regex.Replace(submission.Employer.LegalName, @"\s+", "-")); // Call Document Management Web API foreach (var content in emailContents) { content.Value.Attachments = new Dictionary <string, byte[]>() { { pdfName, returnValue } }; await httpClientInstance.PostAsJsonAsync <EmailContent>("/api/email/sendemail", content.Value); } return(Ok()); }
public async Task <IHttpActionResult> Submit([FromBody] ApplicationSubmission submission) { var results = _applicationSubmissionValidator.Validate(submission); if (!results.IsValid) { BadRequest(results.Errors.ToString()); } _applicationService.ProcessModel(submission); // make sure user has rights to the EIN var hasEINClaim = _identityService.UserHasEINClaim(User, submission.EIN); if (!hasEINClaim) { Unauthorized("Unauthorized"); } await _applicationService.SubmitApplicationAsync(submission); // remove the associated application save _saveService.Remove(submission.EIN); var response = await GetApplicationDocument(new Guid(submission.Id)); // Get return value from API call var contentResult = response as OkNegotiatedContentResult <byte[]>; var returnValue = contentResult.Content; if (returnValue == null) { InternalServerError("Get concatenate Pdf failed"); } // Calling Email Web API var baseUri = new Uri(AppSettings.Get <string>("EmailApiBaseUrl")); var httpClientConnectionLeaseTimeout = AppSettings.Get <int>("HttpClientConnectionLeaseTimeout"); // Get Http Client var httpClientInstance = MyHttpClient; httpClientInstance.DefaultRequestHeaders.Clear(); httpClientInstance.DefaultRequestHeaders.ConnectionClose = false; httpClientInstance.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (httpClientInstance.BaseAddress != baseUri) { httpClientInstance.BaseAddress = baseUri; } ServicePointManager.FindServicePoint(baseUri).ConnectionLeaseTimeout = httpClientConnectionLeaseTimeout; // Get Email Contents var certificationTeamEmailTemplatePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/CertificationTeamEmailTemplate.txt"); var certificationTeamEmailTemplateString = File.ReadAllText(certificationTeamEmailTemplatePath); var employerEmailTemplatePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/EmployerEmailTemplate.txt"); var employerEmailTemplateString = File.ReadAllText(employerEmailTemplatePath); var emailContents = _emailService.PrepareApplicationEmailContents(submission, certificationTeamEmailTemplateString, employerEmailTemplateString, EmailReceiver.Both); // Call Document Management Web API foreach (var content in emailContents) { content.Value.Attachments = new Dictionary <string, byte[]>() { { "Concatenate.pdf", returnValue } }; await httpClientInstance.PostAsJsonAsync <EmailContent>("/api/email/sendemail", content.Value); } return(Ok()); }