-
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 #6 from leancodepl/feature/low-rate-email
Low rate email to admin
- Loading branch information
Showing
18 changed files
with
233 additions
and
25 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
4 changes: 1 addition & 3 deletions
4
backend/src/LeanCode.AppRating/CQRSServicesBuilderCQRSExtensions.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
10 changes: 10 additions & 0 deletions
10
backend/src/LeanCode.AppRating/Configuration/AppRatingReportsConfiguration.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,10 @@ | ||
namespace LeanCode.AppRating.Configuration; | ||
|
||
public sealed record class AppRatingReportsConfiguration( | ||
double LowRatingUpperBoundInclusive, | ||
string LowRatingEmailCulture, | ||
string LowRatingEmailSubjectKey, | ||
string FromEmail, | ||
string[] ToEmails, | ||
string[] BccEmails | ||
) { } |
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
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
8 changes: 8 additions & 0 deletions
8
backend/src/LeanCode.AppRating/EmailViewModels/LowRateSubmittedEmail.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,8 @@ | ||
namespace LeanCode.AppRating.EmailViewModels; | ||
|
||
public class LowRateSubmittedEmail | ||
{ | ||
public double Rating { get; set; } | ||
public string UserId { get; set; } = null!; | ||
public string? AdditionalComment { get; set; } | ||
} |
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
54 changes: 54 additions & 0 deletions
54
backend/src/LeanCode.AppRating/Handlers/SendEmailOnLowRateSubmittedEH.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,54 @@ | ||
using LeanCode.AppRating.Configuration; | ||
using LeanCode.AppRating.EmailViewModels; | ||
using LeanCode.SendGrid; | ||
using MassTransit; | ||
using SendGrid.Helpers.Mail; | ||
using Serilog; | ||
|
||
namespace LeanCode.AppRating.Handlers; | ||
|
||
public class SendEmailOnLowRateSubmittedEH<TUserId> : IConsumer<LowRateSubmitted<TUserId>> | ||
where TUserId : notnull, IEquatable<TUserId> | ||
{ | ||
private readonly ILogger logger = Log.ForContext<SendEmailOnLowRateSubmittedEH<TUserId>>(); | ||
private readonly SendGridRazorClient sendGridRazorClient; | ||
private readonly AppRatingReportsConfiguration appRatingReportsConfiguration; | ||
|
||
public SendEmailOnLowRateSubmittedEH( | ||
SendGridRazorClient sendGridRazorClient, | ||
AppRatingReportsConfiguration appRatingReportsConfiguration | ||
) | ||
{ | ||
this.sendGridRazorClient = sendGridRazorClient; | ||
this.appRatingReportsConfiguration = appRatingReportsConfiguration; | ||
} | ||
|
||
public async Task Consume(ConsumeContext<LowRateSubmitted<TUserId>> context) | ||
{ | ||
var vm = new LowRateSubmittedEmail | ||
{ | ||
UserId = context.Message.UserId.ToString()!, | ||
AdditionalComment = context.Message.AdditionalComment, | ||
Rating = context.Message.Rating, | ||
}; | ||
|
||
var message = new SendGridLocalizedRazorMessage(appRatingReportsConfiguration.LowRatingEmailCulture) | ||
.WithSubject(appRatingReportsConfiguration.LowRatingEmailSubjectKey) | ||
.WithSender(appRatingReportsConfiguration.FromEmail) | ||
.WithRecipients( | ||
appRatingReportsConfiguration.ToEmails.Select(e => new EmailAddress() { Email = e, }).ToList() | ||
) | ||
.WithBlindCarbonCopyRecipients( | ||
appRatingReportsConfiguration.BccEmails.Select(e => new EmailAddress() { Email = e, }).ToList() | ||
) | ||
.WithHtmlContent(vm) | ||
.WithPlainTextContent(vm) | ||
.WithNoTracking(); | ||
|
||
await sendGridRazorClient.SendEmailAsync(message, context.CancellationToken); | ||
logger.Information("Email about low rating from user {UserId} sent", context.Message.UserId); | ||
} | ||
} | ||
|
||
public sealed record class LowRateSubmitted<TUserId>(TUserId UserId, double Rating, string? AdditionalComment) | ||
where TUserId : notnull, IEquatable<TUserId>; |
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
31 changes: 31 additions & 0 deletions
31
backend/src/LeanCode.AppRating/MassTransitRegistrationConfigurationExtensions.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,31 @@ | ||
using LeanCode.AppRating.Handlers; | ||
using MassTransit; | ||
|
||
namespace LeanCode.AppRating; | ||
|
||
public static class MassTransitRegistrationConfigurationExtensions | ||
{ | ||
public static void AddAppRatingConsumers<TUserId>(this IRegistrationConfigurator configurator) | ||
where TUserId : notnull, IEquatable<TUserId> | ||
{ | ||
configurator.AddConsumer( | ||
typeof(SendEmailOnLowRateSubmittedEH<TUserId>), | ||
typeof(AppRatingConsumerDefinition<SendEmailOnLowRateSubmittedEH<TUserId>>) | ||
); | ||
} | ||
} | ||
|
||
public class AppRatingConsumerDefinition<TConsumer> : ConsumerDefinition<TConsumer> | ||
where TConsumer : class, IConsumer | ||
{ | ||
protected override void ConfigureConsumer( | ||
IReceiveEndpointConfigurator endpointConfigurator, | ||
IConsumerConfigurator<TConsumer> consumerConfigurator, | ||
IRegistrationContext context | ||
) | ||
{ | ||
endpointConfigurator.UseMessageRetry( | ||
r => r.Immediate(1).Incremental(3, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)) | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/tests/LeanCode.AppRating.IntegrationTests/App/SendGridRazorClientMock.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,21 @@ | ||
using LeanCode.SendGrid; | ||
using SendGrid.Helpers.Mail; | ||
|
||
namespace LeanCode.AppRating.IntegrationTests.App; | ||
|
||
public class SendGridRazorClientMock : SendGridRazorClient | ||
{ | ||
public int SentEmailsCount { get; private set; } | ||
|
||
public SendGridRazorClientMock() | ||
: base(default!, default!, default!) | ||
{ | ||
SentEmailsCount = 0; | ||
} | ||
|
||
public override Task SendEmailAsync(SendGridMessage msg, CancellationToken cancellationToken = default) | ||
{ | ||
SentEmailsCount++; | ||
return Task.CompletedTask; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
using System.Security.Claims; | ||
using LeanCode.AppRating.Configuration; | ||
using LeanCode.Components; | ||
using LeanCode.CQRS.AspNetCore; | ||
using LeanCode.CQRS.MassTransitRelay; | ||
using LeanCode.CQRS.Validation.Fluent; | ||
using LeanCode.IntegrationTestHelpers; | ||
using LeanCode.SendGrid; | ||
using LeanCode.Startup.MicrosoftDI; | ||
using MassTransit; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
|
@@ -46,7 +47,27 @@ public override void ConfigureServices(IServiceCollection services) | |
RegistrationContextExtensions.ConfigureEndpoints(cfg, ctx); | ||
} | ||
); | ||
|
||
busCfg.AddAppRatingConsumers<Guid>(); | ||
}); | ||
|
||
var sendGridRazorClientMock = new SendGridRazorClientMock(); | ||
|
||
services.AddSingleton<SendGridRazorClient>(sendGridRazorClientMock); | ||
services.AddSingleton(sendGridRazorClientMock); | ||
|
||
services.AddSingleton( | ||
new AppRatingReportsConfiguration( | ||
2.0, | ||
"en", | ||
"subject", | ||
"[email protected]", | ||
[ "[email protected]" ], | ||
[ "[email protected]" ] | ||
) | ||
); | ||
|
||
services.AddBusActivityMonitor(); | ||
} | ||
|
||
protected override void ConfigureApp(IApplicationBuilder app) | ||
|
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
Oops, something went wrong.