Skip to content

Commit c6051d3

Browse files
Merge pull request #31 from StuartFerguson/task/#24_rejectduplicatemessageid
Message Id validation added to prevent duplicates
2 parents bcd9028 + 71ce1c1 commit c6051d3

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

MessagingService.DataTransferObjects/SendEmailRequest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
public class SendEmailRequest
99
{
1010
#region Properties
11+
12+
public Guid? MessageId { get; set; }
1113

1214
/// <summary>
1315
/// Gets or sets the body.

MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,27 @@ public void EmailAggregate_SendRequestToProvider_RequestSent()
2828
emailAggregate.Subject.ShouldBe(TestData.Subject);
2929
emailAggregate.Body.ShouldBe(TestData.Body);
3030
emailAggregate.IsHtml.ShouldBe(TestData.IsHtmlTrue);
31+
emailAggregate.MessageStatus.ShouldBe(MessageStatus.InProgress);
3132
// TODO: Get Recipients
3233
}
3334

35+
[Fact]
36+
public void EmailAggregate_SendRequestToProvider_RequestAlreadySent_ErrorThrown()
37+
{
38+
EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId);
39+
40+
emailAggregate.SendRequestToProvider(TestData.FromAddress, TestData.ToAddresses, TestData.Subject, TestData.Body, TestData.IsHtmlTrue);
41+
42+
Should.Throw<InvalidOperationException>(() =>
43+
{
44+
emailAggregate.SendRequestToProvider(TestData.FromAddress,
45+
TestData.ToAddresses,
46+
TestData.Subject,
47+
TestData.Body,
48+
TestData.IsHtmlTrue);
49+
});
50+
}
51+
3452
[Fact]
3553
public void EmailAggregate_ReceiveResponseFromProvider_ResponseReceived()
3654
{
@@ -41,6 +59,7 @@ public void EmailAggregate_ReceiveResponseFromProvider_ResponseReceived()
4159

4260
emailAggregate.ProviderRequestReference.ShouldBe(TestData.ProviderRequestReference);
4361
emailAggregate.ProviderEmailReference.ShouldBe(TestData.ProviderEmailReference);
62+
emailAggregate.MessageStatus.ShouldBe(MessageStatus.Sent);
4463
}
4564

4665
[Fact]

MessagingService.EmailMessageAggregate/EmailAggregate.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ public void SendRequestToProvider(String fromAddress,
233233
String body,
234234
Boolean isHtml)
235235
{
236+
if (this.MessageStatus != MessageStatus.NotSet)
237+
{
238+
throw new InvalidOperationException("Cannot send a message to provider that has already been sent");
239+
}
240+
236241
RequestSentToProviderEvent requestSentToProviderEvent = RequestSentToProviderEvent.Create(this.AggregateId, fromAddress, toAddresses, subject, body, isHtml);
237242

238243
this.ApplyAndPend(requestSentToProviderEvent);
@@ -327,7 +332,7 @@ private void PlayEvent(RequestSentToProviderEvent domainEvent)
327332
this.Subject = domainEvent.Subject;
328333
this.IsHtml = domainEvent.IsHtml;
329334
this.FromAddress = domainEvent.FromAddress;
330-
this.MessageStatus = MessageStatus.NotSet;
335+
this.MessageStatus = MessageStatus.InProgress;
331336

332337
foreach (String domainEventToAddress in domainEvent.ToAddresses)
333338
{

MessagingService.EmailMessageAggregate/MessageStatus.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public enum MessageStatus
1010
/// </summary>
1111
NotSet = 0,
1212

13+
InProgress,
14+
1315
/// <summary>
1416
/// The sent
1517
/// </summary>

MessagingService/Controllers/EmailController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task<IActionResult> PostEmail([FromBody] SendEmailRequestDTO sendEm
6565
return this.Forbid();
6666
}
6767

68-
Guid messageId = Guid.NewGuid();
68+
Guid messageId = sendEmailRequest.MessageId.HasValue ? sendEmailRequest.MessageId.Value : Guid.NewGuid();
6969

7070
// Create the command
7171
SendEmailRequest request = SendEmailRequest.Create(sendEmailRequest.ConnectionIdentifier,

0 commit comments

Comments
 (0)