Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public void MessagingRequestHandler_SendEmailRequest_IsHandled()

}

[Fact]
public void MessagingRequestHandler_ResendEmailRequest_IsHandled()
{
Mock<IMessagingDomainService> messagingDomainService = new Mock<IMessagingDomainService>();
MessagingRequestHandler handler = new MessagingRequestHandler(messagingDomainService.Object);

ResendEmailRequest command = TestData.ResendEmailRequest;

Should.NotThrow(async () => {
await handler.Handle(command, CancellationToken.None);
});
}

[Fact]
public void MessagingRequestHandler_SendSMSRequest_IsHandled()
{
Expand Down
8 changes: 8 additions & 0 deletions MessagingService.BusinessLogic.Tests/Requests/RequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@ public void SendSMSRequest_CanBeCreated_IsCreated()
request.Destination.ShouldBe(TestData.Destination);
request.Message.ShouldBe(TestData.Message);
}

[Fact]
public void ResendEmailRequest_CanBeCreated_IsCreated() {
ResendEmailRequest request = ResendEmailRequest.Create(TestData.ConnectionIdentifier, TestData.MessageId);
request.ShouldNotBeNull();
request.ConnectionIdentifier.ShouldBe(TestData.ConnectionIdentifier);
request.MessageId.ShouldBe(TestData.MessageId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ await messagingDomainService.SendEmailMessage(TestData.ConnectionIdentifier,
CancellationToken.None);
}

[Fact]
public async Task MessagingDomainService_ResendEmailMessage_MessageSent()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IAggregateRepository<SMSAggregate, DomainEvent>> smsAggregateRepository = new Mock<IAggregateRepository<SMSAggregate, DomainEvent>>();
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy
.Setup(e => e.SendEmail(It.IsAny<Guid>(),
It.IsAny<String>(),
It.IsAny<List<String>>(),
It.IsAny<String>(),
It.IsAny<String>(),
It.IsAny<Boolean>(),
It.IsAny<List<EmailAttachment>>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TestData.SuccessfulEmailServiceProxyResponse);
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();

MessagingDomainService messagingDomainService =
new MessagingDomainService(emailAggregateRepository.Object, smsAggregateRepository.Object, emailServiceProxy.Object, smsServiceProxy.Object);

await messagingDomainService.ResendEmailMessage(TestData.ConnectionIdentifier,
TestData.MessageId,
CancellationToken.None);
}

[Fact]
public async Task MessagingDomainService_SendSMSMessage_MessageSent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
///
/// </summary>
/// <seealso cref="MediatR.IRequestHandler{MessagingService.BusinessLogic.Requests.SendEmailRequest, System.String}" />
public class MessagingRequestHandler : IRequestHandler<SendEmailRequest, String>, IRequestHandler<SendSMSRequest, String>
public class MessagingRequestHandler : IRequestHandler<SendEmailRequest, String>,
IRequestHandler<SendSMSRequest, String>,
IRequestHandler<ResendEmailRequest,String>
{
#region Fields

Expand Down Expand Up @@ -74,5 +76,12 @@ await this.MessagingDomainService.SendSMSMessage(request.ConnectionIdentifier,
}

#endregion

public async Task<String> Handle(ResendEmailRequest request,
CancellationToken cancellationToken) {
await this.MessagingDomainService.ResendEmailMessage(request.ConnectionIdentifier, request.MessageId, cancellationToken);

return String.Empty;
}
}
}
12 changes: 12 additions & 0 deletions MessagingService.BusinessLogic/Requests/EmailAttachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace MessagingService.BusinessLogic.Requests;

using System;

public class EmailAttachment
{
public String Filename { get; set; }

public String FileData { get; set; }

public FileType FileType { get; set; }
}
7 changes: 7 additions & 0 deletions MessagingService.BusinessLogic/Requests/FileType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MessagingService.BusinessLogic.Requests;

public enum FileType
{
None = 0,
PDF
}
48 changes: 48 additions & 0 deletions MessagingService.BusinessLogic/Requests/ResendEmailRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace MessagingService.BusinessLogic.Requests;

using System;
using MediatR;

public class ResendEmailRequest : IRequest<String>
{
#region Constructors

private ResendEmailRequest(Guid connectionIdentifier,
Guid messageId)
{
this.ConnectionIdentifier = connectionIdentifier;
this.MessageId = messageId;
}

#endregion

#region Properties

/// <summary>
/// Gets the connection identifier.
/// </summary>
/// <value>
/// The connection identifier.
/// </value>
public Guid ConnectionIdentifier { get; }

/// <summary>
/// Gets the message identifier.
/// </summary>
/// <value>
/// The message identifier.
/// </value>
public Guid MessageId { get; }

#endregion

#region Methods

public static ResendEmailRequest Create(Guid connectionIdentifier,
Guid messageId)
{
return new ResendEmailRequest(connectionIdentifier, messageId);
}

#endregion
}
15 changes: 0 additions & 15 deletions MessagingService.BusinessLogic/Requests/SendEmailRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,4 @@ public static SendEmailRequest Create(Guid connectionIdentifier,

#endregion
}

public class EmailAttachment
{
public String Filename { get; set; }

public String FileData { get; set; }

public FileType FileType { get; set; }
}

public enum FileType
{
None = 0,
PDF
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Task SendSMSMessage(Guid connectionIdentifier,
String message,
CancellationToken cancellationToken);

Task ResendEmailMessage(Guid connectionIdentifier,
Guid messageId,
CancellationToken cancellationToken);

#endregion
}
}
26 changes: 26 additions & 0 deletions MessagingService.BusinessLogic/Services/MessagingDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using EmailMessage.DomainEvents;
Expand Down Expand Up @@ -139,6 +140,31 @@ public async Task SendSMSMessage(Guid connectionIdentifier,
// Save Changes to persistance
await this.SmsAggregateRepository.SaveChanges(smsAggregate, cancellationToken);
}

public async Task ResendEmailMessage(Guid connectionIdentifier,
Guid messageId,
CancellationToken cancellationToken) {
// Rehydrate Email Message aggregate
EmailAggregate emailAggregate = await this.EmailAggregateRepository.GetLatestVersion(messageId, cancellationToken);

// re-send message to provider (record event)
emailAggregate.ResendRequestToProvider();

// Make call to Email provider here
EmailServiceProxyResponse emailResponse =
await this.EmailServiceProxy.SendEmail(messageId, emailAggregate.FromAddress,
emailAggregate.GetToAddresses(),
emailAggregate.Subject,
emailAggregate.Body,
emailAggregate.IsHtml, null,
cancellationToken);

// response message from provider (record event)
emailAggregate.ReceiveResponseFromProvider(emailResponse.RequestIdentifier, emailResponse.EmailIdentifier);

// Save Changes to persistance
await this.EmailAggregateRepository.SaveChanges(emailAggregate, cancellationToken);
}
}

#endregion
Expand Down
4 changes: 4 additions & 0 deletions MessagingService.Client/IMessagingServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Task<SendEmailResponse> SendEmail(String accessToken,
SendEmailRequest request,
CancellationToken cancellationToken);

Task ResendEmail(String accessToken,
ResendEmailRequest request,
CancellationToken cancellationToken);

/// <summary>
/// Sends the SMS.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions MessagingService.Client/MessagingServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ public async Task<SendEmailResponse> SendEmail(String accessToken,
return response;
}

public async Task ResendEmail(String accessToken,
ResendEmailRequest resendEmailRequest,
CancellationToken cancellationToken) {
String requestUri = this.BuildRequestUrl("/api/email/resend");

try {
String requestSerialised = JsonConvert.SerializeObject(resendEmailRequest);

StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json");

// Add the access token to the client headers
this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

// Make the Http Call here
HttpResponseMessage httpResponse = await this.HttpClient.PostAsync(requestUri, httpContent, cancellationToken);

httpResponse.EnsureSuccessStatusCode();
}
catch(Exception ex) {
// An exception has occurred, add some additional information to the message
Exception exception = new Exception("Error re-sending email message.", ex);

throw exception;
}
}

/// <summary>
/// Sends the SMS.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions MessagingService.DataTransferObjects/ResendEmailRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace MessagingService.DataTransferObjects
{
using System;
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;

[ExcludeFromCodeCoverage]
public class ResendEmailRequest
{
[JsonProperty("message_id")]
public Guid MessageId { get; set; }

[JsonProperty("connection_identifier")]
public Guid ConnectionIdentifier { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,15 @@ public void EmailMessageMarkedAsSpamEvent_CanBeCreated_IsCreated()
messageMarkedAsSpamEvent.ProviderStatus.ShouldBe(TestData.ProviderStatusDescription);
messageMarkedAsSpamEvent.SpamDateTime.ShouldBe(TestData.SpamDateTime);
}

[Fact]
public void RequestResentToEmailProviderEvent_CanBeCreated_IsCreated() {
RequestResentToEmailProviderEvent requestResentToEmailProviderEvent = new RequestResentToEmailProviderEvent(TestData.MessageId);

requestResentToEmailProviderEvent.ShouldNotBeNull();
requestResentToEmailProviderEvent.AggregateId.ShouldBe(TestData.MessageId);
requestResentToEmailProviderEvent.EventId.ShouldNotBe(Guid.Empty);
requestResentToEmailProviderEvent.MessageId.ShouldBe(TestData.MessageId);
}
}
}
Loading