forked from kgrzybek/modular-monolith-with-ddd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kgrzybek#85 from PiotrWachulec/feature/PaymentsEma…
…ilNotification Added sending confirmation email after subscription creation or renewal
- Loading branch information
Showing
15 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/Modules/Payments/Application/Payers/GetPayer/GetPayerQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Modules/Payments/Application/Payers/GetPayerEmail/PayerEmailProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Data; | ||
using Dapper; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Payers.GetPayerEmail | ||
{ | ||
public static class PayerEmailProvider | ||
{ | ||
public static async Task<string> GetPayerEmail(Guid payerId, ISqlConnectionFactory sqlConnectionFactory) | ||
{ | ||
var connection = sqlConnectionFactory.GetOpenConnection(); | ||
|
||
const string sql = "SELECT " + | ||
"[Payer].[Email] " + | ||
"FROM [payments].[Payers] AS [Payer] " + | ||
"WHERE [Payer].[Id] = @PayerId"; | ||
|
||
return await connection.QuerySingleAsync<string>(sql, new | ||
{ | ||
payerId | ||
}); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...on/Subscriptions/CreateSubscription/SubscriptionCreatedEnqueueEmailConfirmationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Data; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Payers.GetPayer; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Payers.GetPayerEmail; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionCreationConfirmationEmail; | ||
using CompanyName.MyMeetings.Modules.Payments.Domain.Subscriptions; | ||
using Dapper; | ||
using MediatR; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.CreateSubscription | ||
{ | ||
public class SubscriptionCreatedEnqueueEmailConfirmationHandler : INotificationHandler<SubscriptionCreatedNotification> | ||
{ | ||
private readonly ICommandsScheduler _commandsScheduler; | ||
private readonly ISqlConnectionFactory _sqlConnectionFactory; | ||
|
||
public SubscriptionCreatedEnqueueEmailConfirmationHandler( | ||
ICommandsScheduler commandsScheduler, | ||
ISqlConnectionFactory sqlConnectionFactory) | ||
{ | ||
_commandsScheduler = commandsScheduler; | ||
_sqlConnectionFactory = sqlConnectionFactory; | ||
} | ||
|
||
public async Task Handle(SubscriptionCreatedNotification notification, CancellationToken cancellationToken) | ||
{ | ||
var payerEmail = await PayerEmailProvider.GetPayerEmail( | ||
notification.DomainEvent.PayerId, | ||
_sqlConnectionFactory); | ||
|
||
await _commandsScheduler.EnqueueAsync(new SendSubscriptionCreationConfirmationEmailCommand( | ||
Guid.NewGuid(), | ||
new SubscriptionId(notification.DomainEvent.SubscriptionId), | ||
payerEmail)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ion/Subscriptions/RenewSubscription/SubscriptionRenewedEnqueueEmailConfirmationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Data; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Payers.GetPayer; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Payers.GetPayerEmail; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionRenewalConfirmationEmail; | ||
using CompanyName.MyMeetings.Modules.Payments.Domain.Subscriptions; | ||
using Dapper; | ||
using MediatR; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.RenewSubscription | ||
{ | ||
public class SubscriptionRenewedEnqueueEmailConfirmationHandler : INotificationHandler<SubscriptionRenewedNotification> | ||
{ | ||
private readonly ICommandsScheduler _commandsScheduler; | ||
private readonly ISqlConnectionFactory _sqlConnectionFactory; | ||
|
||
public SubscriptionRenewedEnqueueEmailConfirmationHandler( | ||
ICommandsScheduler commandsScheduler, | ||
ISqlConnectionFactory sqlConnectionFactory) | ||
{ | ||
_commandsScheduler = commandsScheduler; | ||
_sqlConnectionFactory = sqlConnectionFactory; | ||
} | ||
|
||
public async Task Handle(SubscriptionRenewedNotification notification, CancellationToken cancellationToken) | ||
{ | ||
var payerEmail = await PayerEmailProvider.GetPayerEmail( | ||
notification.DomainEvent.PayerId, | ||
_sqlConnectionFactory); | ||
|
||
await _commandsScheduler.EnqueueAsync(new SendSubscriptionRenewalConfirmationEmailCommand( | ||
Guid.NewGuid(), | ||
new SubscriptionId(notification.DomainEvent.SubscriptionId), | ||
payerEmail)); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...SubscriptionCreationConfirmationEmail/SendSubscriptionCreationConfirmationEmailCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using CompanyName.MyMeetings.Modules.Payments.Domain.Subscriptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionCreationConfirmationEmail | ||
{ | ||
public class SendSubscriptionCreationConfirmationEmailCommand : InternalCommandBase | ||
{ | ||
internal SubscriptionId SubscriptionId { get; } | ||
|
||
internal string Email { get; } | ||
|
||
[JsonConstructor] | ||
public SendSubscriptionCreationConfirmationEmailCommand( | ||
Guid id, | ||
SubscriptionId subscriptionId, | ||
string email) | ||
: base(id) | ||
{ | ||
SubscriptionId = subscriptionId; | ||
Email = email; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ptionCreationConfirmationEmail/SendSubscriptionCreationConfirmationEmailCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Emails; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using MediatR; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionCreationConfirmationEmail | ||
{ | ||
public class SendSubscriptionCreationConfirmationEmailCommandHandler : ICommandHandler<SendSubscriptionCreationConfirmationEmailCommand> | ||
{ | ||
private readonly IEmailSender _emailSender; | ||
|
||
public SendSubscriptionCreationConfirmationEmailCommandHandler(IEmailSender emailSender) | ||
{ | ||
_emailSender = emailSender; | ||
} | ||
|
||
public Task<Unit> Handle(SendSubscriptionCreationConfirmationEmailCommand request, CancellationToken cancellationToken) | ||
{ | ||
var emailMessage = new EmailMessage(request.Email, "MyMeetings - Subscription purchased", | ||
$"Subscription {request.SubscriptionId.Value} was successfully paid and created with ❤ for you!"); | ||
|
||
_emailSender.SendEmail(emailMessage); | ||
return Unit.Task; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ndSubscriptionRenewalConfirmationEmail/SendSubscriptionRenewalConfirmationEmailCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using CompanyName.MyMeetings.Modules.Payments.Domain.Subscriptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionRenewalConfirmationEmail | ||
{ | ||
public class SendSubscriptionRenewalConfirmationEmailCommand : InternalCommandBase | ||
{ | ||
internal SubscriptionId SubscriptionId { get; } | ||
|
||
internal string Email { get; } | ||
|
||
[JsonConstructor] | ||
public SendSubscriptionRenewalConfirmationEmailCommand( | ||
Guid id, | ||
SubscriptionId subscriptionId, | ||
string email) | ||
: base(id) | ||
{ | ||
SubscriptionId = subscriptionId; | ||
Email = email; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...riptionRenewalConfirmationEmail/SendSubscriptionRenewalConfirmationEmailCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Emails; | ||
using CompanyName.MyMeetings.Modules.Payments.Application.Configuration.Commands; | ||
using MediatR; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Application.Subscriptions.SendSubscriptionRenewalConfirmationEmail | ||
{ | ||
public class SendSubscriptionRenewalConfirmationEmailCommandHandler : ICommandHandler<SendSubscriptionRenewalConfirmationEmailCommand> | ||
{ | ||
private readonly IEmailSender _emailSender; | ||
|
||
public SendSubscriptionRenewalConfirmationEmailCommandHandler(IEmailSender emailSender) | ||
{ | ||
_emailSender = emailSender; | ||
} | ||
|
||
public Task<Unit> Handle(SendSubscriptionRenewalConfirmationEmailCommand request, CancellationToken cancellationToken) | ||
{ | ||
var emailMessage = new EmailMessage(request.Email, "MyMeetings - Subscription renewed", | ||
$"Subscription {request.SubscriptionId.Value} was successfully paid and renewed with ❤ for you!"); | ||
|
||
_emailSender.SendEmail(emailMessage); | ||
return Unit.Task; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Modules/Payments/Infrastructure/Configuration/Email/EmailModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Autofac; | ||
using CompanyName.MyMeetings.BuildingBlocks.Application.Emails; | ||
using CompanyName.MyMeetings.BuildingBlocks.Infrastructure.Emails; | ||
|
||
namespace CompanyName.MyMeetings.Modules.Payments.Infrastructure.Configuration.Email | ||
{ | ||
internal class EmailModule : Module | ||
{ | ||
private readonly EmailsConfiguration _configuration; | ||
|
||
public EmailModule(EmailsConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
protected override void Load(ContainerBuilder builder) | ||
{ | ||
builder.RegisterType<EmailSender>() | ||
.As<IEmailSender>() | ||
.WithParameter("configuration", _configuration) | ||
.InstancePerLifetimeScope(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ public class TestBase | |
|
||
protected IEmailSender EmailSender; | ||
|
||
protected EmailsConfiguration EmailsConfiguration; | ||
|
||
protected EventsBusMock EventsBus; | ||
|
||
protected ExecutionContextMock ExecutionContext; | ||
|
@@ -53,13 +55,15 @@ public async Task BeforeEachTest() | |
|
||
Logger = Substitute.For<ILogger>(); | ||
EmailSender = Substitute.For<IEmailSender>(); | ||
EmailsConfiguration = new EmailsConfiguration("[email protected]"); | ||
EventsBus = new EventsBusMock(); | ||
ExecutionContext = new ExecutionContextMock(Guid.NewGuid()); | ||
|
||
PaymentsStartup.Initialize( | ||
ConnectionString, | ||
ExecutionContext, | ||
Logger, | ||
EmailsConfiguration, | ||
EventsBus, | ||
true); | ||
|
||
|
3 changes: 1 addition & 2 deletions
3
...dUserRegistrationConfirmationEmail/SendUserRegistrationConfirmationEmailCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters