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
38 changes: 22 additions & 16 deletions MessagingService.BusinessLogic/Services/MessagingDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using EmailMessage.DomainEvents;
using EmailMessageAggregate;
using EmailServices;
using Microsoft.Extensions.Logging;
using Models;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.Aggregate;
Expand Down Expand Up @@ -158,20 +154,23 @@
{
Result result = await ApplySMSUpdates(async (SMSAggregate smsAggregate) => {
// Check if this message has been sent before
if (smsAggregate.GetMessageStatus() != SMSMessageAggregate.MessageStatus.NotSet)
{
if (smsAggregate.GetMessageStatus() != SMSMessageAggregate.MessageStatus.NotSet) {
return Result.Success();
}

// send message to provider (record event)
smsAggregate.SendRequestToProvider(sender, destination, message);
Result stateResult = smsAggregate.SendRequestToProvider(sender, destination, message);
if (stateResult.IsFailed)
return stateResult;

// Make call to SMS provider here
SMSServiceProxyResponse smsResponse =
await this.SmsServiceProxy.SendSMS(messageId, sender, destination, message, cancellationToken);

// response message from provider (record event)
smsAggregate.ReceiveResponseFromProvider(smsResponse.SMSIdentifier);
stateResult= smsAggregate.ReceiveResponseFromProvider(smsResponse.SMSIdentifier);
if (stateResult.IsFailed)
return stateResult;

return Result.Success();
}, messageId, cancellationToken, false);
Expand Down Expand Up @@ -219,14 +218,18 @@
public async Task<Result> ResendSMSMessage(Guid connectionIdentifier, Guid messageId, CancellationToken cancellationToken){
Result result = await ApplySMSUpdates(async (SMSAggregate smsAggregate) => {
// re-send message to provider (record event)
smsAggregate.ResendRequestToProvider();
Result stateResult = smsAggregate.ResendRequestToProvider();
if (stateResult.IsFailed)
return stateResult;

// Make call to SMS provider here
SMSServiceProxyResponse smsResponse =
await this.SmsServiceProxy.SendSMS(messageId, smsAggregate.Sender, smsAggregate.Destination, smsAggregate.Message, cancellationToken);

// response message from provider (record event)
smsAggregate.ReceiveResponseFromProvider(smsResponse.SMSIdentifier);
stateResult = smsAggregate.ReceiveResponseFromProvider(smsResponse.SMSIdentifier);
if (stateResult.IsFailed)
return stateResult;

return Result.Success();
}, messageId, cancellationToken);
Expand All @@ -235,7 +238,7 @@

public async Task<Result> UpdateMessageStatus(EmailCommands.UpdateMessageStatusCommand command,
CancellationToken cancellationToken) {
Result result = await ApplyEmailUpdates(async (EmailAggregate emailAggregate) => {

Check warning on line 241 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 241 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Result stateResult;

Expand Down Expand Up @@ -272,28 +275,31 @@

public async Task<Result> UpdateMessageStatus(SMSCommands.UpdateMessageStatusCommand command,
CancellationToken cancellationToken) {
Result result = await ApplySMSUpdates(async (SMSAggregate smsAggregate) => {

Check warning on line 278 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 278 in MessagingService.BusinessLogic/Services/MessagingDomainService.cs

View workflow job for this annotation

GitHub Actions / Build and Test Pull Requests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
Result stateResult;

switch (command.Status) {
case SMSServices.MessageStatus.Delivered:
case SMSServices.MessageStatus.Sent:
case SMSServices.MessageStatus.InProgress:
smsAggregate.MarkMessageAsDelivered(command.Description, command.Timestamp);
stateResult = smsAggregate.MarkMessageAsDelivered(command.Description, command.Timestamp);
break;
case SMSServices.MessageStatus.Expired:
smsAggregate.MarkMessageAsExpired(command.Description, command.Timestamp);
stateResult = smsAggregate.MarkMessageAsExpired(command.Description, command.Timestamp);
break;
case SMSServices.MessageStatus.Rejected:
smsAggregate.MarkMessageAsRejected(command.Description, command.Timestamp);
stateResult = smsAggregate.MarkMessageAsRejected(command.Description, command.Timestamp);
break;
case SMSServices.MessageStatus.Undeliverable:
smsAggregate.MarkMessageAsUndeliverable(command.Description, command.Timestamp);
stateResult = smsAggregate.MarkMessageAsUndeliverable(command.Description, command.Timestamp);
break;
default:
smsAggregate.MarkMessageAsRejected(command.Description, command.Timestamp);
stateResult = smsAggregate.MarkMessageAsRejected(command.Description, command.Timestamp);
break;
}

if (stateResult.IsFailed)
return stateResult;

return Result.Success();
}, command.MessageId, cancellationToken);
return result;
Expand Down
37 changes: 28 additions & 9 deletions MessagingService.SMSAggregate.Tests/SMSAggregateTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using SimpleResults;

namespace MessagingService.SMSAggregate.Tests
{
Expand Down Expand Up @@ -95,7 +96,9 @@ public void SMSAggregate_MarkMessageAsDelivered_IncorrectState_ErrorThrown(Messa
break;
}

Should.Throw<InvalidOperationException>(() => smsAggregate.MarkMessageAsDelivered(TestData.ProviderStatusDescription, TestData.DeliveredDateTime));
var result = smsAggregate.MarkMessageAsDelivered(TestData.ProviderStatusDescription, TestData.DeliveredDateTime);
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand Down Expand Up @@ -147,7 +150,9 @@ public void SMSAggregate_MarkMessageAsExpired_IncorrectState_ErrorThrown(Message
break;
}

Should.Throw<InvalidOperationException>(() => smsAggregate.MarkMessageAsExpired(TestData.ProviderStatusDescription, TestData.DeliveredDateTime));
var result = smsAggregate.MarkMessageAsExpired(TestData.ProviderStatusDescription, TestData.DeliveredDateTime);
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand Down Expand Up @@ -199,7 +204,9 @@ public void SMSAggregate_MarkMessageAsUndeliverable_IncorrectState_ErrorThrown(M
break;
}

Should.Throw<InvalidOperationException>(() => smsAggregate.MarkMessageAsUndeliverable(TestData.ProviderStatusDescription, TestData.DeliveredDateTime));
var result = smsAggregate.MarkMessageAsUndeliverable(TestData.ProviderStatusDescription, TestData.DeliveredDateTime);
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand Down Expand Up @@ -251,7 +258,9 @@ public void SMSAggregate_MarkMessageAsRejected_IncorrectState_ErrorThrown(Messag
break;
}

Should.Throw<InvalidOperationException>(() => smsAggregate.MarkMessageAsRejected(TestData.ProviderStatusDescription, TestData.DeliveredDateTime));
var result = smsAggregate.MarkMessageAsRejected(TestData.ProviderStatusDescription, TestData.DeliveredDateTime);
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand Down Expand Up @@ -288,7 +297,9 @@ public void SMSAggregate_ResendRequestToProvider_NotSet_ErrorThrown()
{
SMSAggregate smsAggregate = SMSAggregate.Create(TestData.MessageId);

Should.Throw<InvalidOperationException>(() => smsAggregate.ResendRequestToProvider());
var result = smsAggregate.ResendRequestToProvider();
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand All @@ -297,7 +308,9 @@ public void SMSAggregate_ResendRequestToProvider_InProgress_ErrorThrown()
SMSAggregate smsAggregate = SMSAggregate.Create(TestData.MessageId);

smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);
Should.Throw<InvalidOperationException>(() => smsAggregate.ResendRequestToProvider());
var result = smsAggregate.ResendRequestToProvider();
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand All @@ -308,7 +321,9 @@ public void SMSAggregate_ResendRequestToProvider_Rejected_ErrorThrown()
smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);
smsAggregate.ReceiveResponseFromProvider(TestData.ProviderSMSReference);
smsAggregate.MarkMessageAsRejected(TestData.ProviderStatusDescription, TestData.RejectedDateTime);
Should.Throw<InvalidOperationException>(() => smsAggregate.ResendRequestToProvider());
var result = smsAggregate.ResendRequestToProvider();
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand All @@ -319,7 +334,9 @@ public void SMSAggregate_ResendRequestToProvider_Expired_ErrorThrown()
smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);
smsAggregate.ReceiveResponseFromProvider(TestData.ProviderSMSReference);
smsAggregate.MarkMessageAsExpired(TestData.ProviderStatusDescription, TestData.ExpiredDateTime);
Should.Throw<InvalidOperationException>(() => smsAggregate.ResendRequestToProvider());
var result = smsAggregate.ResendRequestToProvider();
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand All @@ -330,7 +347,9 @@ public void SMSAggregate_ResendRequestToProvider_Undelivered_ErrorThrown()
smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);
smsAggregate.ReceiveResponseFromProvider(TestData.ProviderSMSReference);
smsAggregate.MarkMessageAsUndeliverable(TestData.ProviderStatusDescription, TestData.UndeliveredDateTime);
Should.Throw<InvalidOperationException>(() => smsAggregate.ResendRequestToProvider());
var result = smsAggregate.ResendRequestToProvider();
result.IsFailed.ShouldBeTrue();
result.Status.ShouldBe(ResultStatus.Invalid);
}

[Fact]
Expand Down
Loading
Loading