Example #1
0
        public async Task <bool> SendStatusEmailAsync(decimal submissionObjectId, FeedbackMessageUpdateDto feedbackMessage = null)
        {
            var submissionInfo = await _submissionRepo.GetSubmissionInfoForEmailAsync(submissionObjectId);

            submissionInfo.SubmissionDate = DateUtils.ConvertUtcToPacificTime(submissionInfo.SubmissionDate);

            var resultUrl = string.Format(_config.GetValue <string>("Smtp:SubmissionResult"), submissionInfo.ServiceAreaNumber, submissionObjectId);

            var env         = _config.GetEnvironment();
            var environment = env == HmcrEnvironments.Prod ? " " : $" [{env}] ";
            var result      = submissionInfo.Success ? "SUCCESS" : "ERROR";
            var subject     = $"HMCR{environment}report submission({submissionObjectId}) result - {result}";

            var htmlBodyTemplate = submissionInfo.Success ? _emailBody.SuccessHtmlBody() : _emailBody.ErrorHtmlBody(submissionInfo);
            var htmlBody         = string.Format(htmlBodyTemplate,
                                                 submissionInfo.FileName, submissionInfo.FileType, submissionInfo.ServiceAreaNumber, submissionInfo.SubmissionDate.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 submissionObjectId, submissionInfo.NumOfRecords, submissionInfo.NumOfDuplicateRecords, submissionInfo.NumOfReplacedRecords,
                                                 submissionInfo.NumOfErrorRecords, submissionInfo.NumOfWarningRecords, resultUrl);

            var textBody = htmlBody.HtmlToPlainText();

            var isSent    = true;
            var isError   = !submissionInfo.Success;
            var errorText = "";

            try
            {
                SendEmailToUsersInServiceArea(submissionInfo.ServiceAreaNumber, subject, htmlBody, textBody);
            }
            catch (Exception ex)
            {
                isSent    = false;
                errorText = ex.Message;

                _logger.LogError($"Email for the submission {submissionObjectId} failed.");
                _logger.LogError(ex.ToString());
            }

            if (feedbackMessage == null)
            {
                var feedback = new FeedbackMessageDto
                {
                    SubmissionObjectId   = submissionObjectId,
                    CommunicationSubject = subject,
                    CommunicationText    = htmlBody,
                    CommunicationDate    = DateTime.UtcNow,
                    IsSent        = isSent,
                    IsError       = isError,
                    SendErrorText = errorText
                };

                await _feedbackRepo.CreateFeedbackMessageAsync(feedback);
            }
            else
            {
                feedbackMessage.SubmissionObjectId   = submissionObjectId;
                feedbackMessage.CommunicationSubject = subject;
                feedbackMessage.CommunicationText    = htmlBody;
                feedbackMessage.CommunicationDate    = DateTime.UtcNow;
                feedbackMessage.IsSent        = isSent;
                feedbackMessage.IsError       = isError;
                feedbackMessage.SendErrorText = errorText;

                await _feedbackRepo.UpdateFeedbackMessageAsync(feedbackMessage);
            }

            _unitOfWork.Commit();

            var finished = isSent ? "Finished" : "Failed";
            var sending  = feedbackMessage == null ? "sending" : "resending";

            _logger.LogInformation($"[Hangfire] {finished} {sending} email for submission {submissionObjectId}", submissionObjectId);

            return(isSent);
        }
 public async Task <HmrFeedbackMessage> CreateFeedbackMessageAsync(FeedbackMessageDto feedback)
 {
     return(await AddAsync(feedback));
 }