public virtual async Task <bool> SendEmailAsync(ContactUsRequest sendEmailRequest, EmailTemplate template = null)
        {
            if (simulateEmailResponsesService.IsThisSimulationRequest(sendEmailRequest.Email))
            {
                return(simulateEmailResponsesService.SimulateEmailResponse(sendEmailRequest.Email));
            }

            if (template == null)
            {
                template = emailTemplateRepository.GetByTemplateName(sendEmailRequest.TemplateName);
            }

            if (template == null)
            {
                return(false);
            }

            var from             = new EmailAddress(sendEmailRequest.Email, $"{sendEmailRequest.FirstName} {sendEmailRequest.LastName}");
            var subject          = template.Subject;
            var to               = template.To?.Split(';').Select(toEmail => new EmailAddress(toEmail.Trim(), toEmail.Trim())).ToList();
            var plainTextContent = mergeEmailContentService.MergeTemplateBodyWithContent(sendEmailRequest, template.BodyNoHtml);
            var htmlContent      = mergeEmailContentService.MergeTemplateBodyWithContent(sendEmailRequest, template.Body);
            var msg              = MailHelper.CreateSingleEmailToMultipleRecipients(@from, to, subject, plainTextContent, htmlContent);
            var clientResponse   = await sendGridClient.SendEmailAsync(msg);

            var auditResponse = mapper.Map <SendEmailResponse>(clientResponse);
            var result        = clientResponse.StatusCode.Equals(HttpStatusCode.Accepted);

            auditRepository.CreateAudit(sendEmailRequest, template, auditResponse);
            return(result);
        }
示例#2
0
        public async Task SendEmailAsyncTest(ContactUsRequest contactUsRequest, bool isThisSimulation, bool expectation)
        {
            //Assign
            var sendEmailService = new SendGridEmailService(fakeEmailTemplateRepository, fakeMergeEmailContentService, fakeAuditRepository, fakeSimulateEmailResponsesService, fakeSendGridClient, fakeMapper);

            A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).Returns(string.IsNullOrEmpty(contactUsRequest.TemplateName) ? null : goodEmailTemplate);
            A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).Returns(nameof(IMergeEmailContent <ContactUsRequest> .MergeTemplateBodyWithContent));
            A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).Returns(new Response(HttpStatusCode.Accepted, null, null));
            A.CallTo(() => fakeConfiguration.GetConfig <string>(A <string> ._)).Returns(string.Empty);
            A.CallTo(() => fakeSimulateEmailResponsesService.IsThisSimulationRequest(A <string> ._)).Returns(isThisSimulation);
            A.CallTo(() => fakeSimulateEmailResponsesService.SimulateEmailResponse(A <string> ._)).Returns(expectation);
            A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).DoesNothing();
            A.CallTo(() => fakeMapper.Map <SendEmailResponse>(A <object> ._)).Returns(A.Dummy <SendEmailResponse>());

            //Act
            var result = await sendEmailService.SendEmailAsync(contactUsRequest);

            //Assert
            result.Should().Be(expectation);
            if (isThisSimulation)
            {
                A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustNotHaveHappened();
            }
            else
            {
                A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).MustHaveHappened();
                if (expectation)
                {
                    A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustHaveHappened(Repeated.Exactly.Twice);
                    A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustHaveHappened();
                    A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustHaveHappened();
                }
                else
                {
                    A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustNotHaveHappened();
                    A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustNotHaveHappened();
                    A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustNotHaveHappened();
                }
            }
        }