-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Low rate email to admin #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
) { } |
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; } | ||
} |
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>; |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder, on one hand, we allow configuring too much (e.g. BCC, or default locale/culture), on the other, we set some things in stone and disallow any changes (consumer configuration). Where is the "ideal" line between ease of use and usability when it comes to configuring things? |
||
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)) | ||
); | ||
} | ||
} |
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; | ||
} | ||
} |
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this will ever be non-empty. :D