Skip to content

Commit 21c9489

Browse files
authored
Add NotificationCreated analytics event (#43)
1 parent 1abf1e8 commit 21c9489

11 files changed

+68
-44
lines changed

src/Api/Endpoints/Notifications/CreateNotificationEndpoint.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
namespace Api.Endpoints.Notifications;
88

9-
internal sealed class CreateNotificationEndpoint :
9+
internal sealed class CreateNotificationEndpoint(INotificationsService notificationsService) :
1010
Endpoint<CreateNotificationRequest, ApiResponse<NotificationResponse>, CreateNotificationMapper>
1111
{
12-
private readonly INotificationsService _notificationsService;
13-
14-
public CreateNotificationEndpoint(INotificationsService notificationsService)
15-
{
16-
_notificationsService = notificationsService;
17-
}
18-
12+
private readonly INotificationsService _notificationsService = notificationsService;
13+
1914
public override void Configure()
2015
{
2116
Verbs(Http.POST);

src/Api/Endpoints/Templates/CreateTemplateEndpoint.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
namespace Api.Endpoints.Templates;
88

9-
internal sealed class CreateTemplateEndpoint :
9+
internal sealed class CreateTemplateEndpoint(ITemplatesService templatesService) :
1010
Endpoint<CreateTemplateRequest, ApiResponse<TemplateResponse>, CreateTemplatesMapper>
1111
{
12-
private readonly ITemplatesService _templatesService;
13-
14-
public CreateTemplateEndpoint(ITemplatesService templatesService)
15-
{
16-
_templatesService = templatesService;
17-
}
18-
12+
private readonly ITemplatesService _templatesService = templatesService;
13+
1914
public override void Configure()
2015
{
2116
Verbs(Http.POST);

src/Api/Endpoints/Templates/DeleteTemplatesEndpoint.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55

66
namespace Api.Endpoints.Templates;
77

8-
internal sealed class DeleteTemplatesEndpoint : Endpoint<DeleteTemplatesRequest>
8+
internal sealed class DeleteTemplatesEndpoint(ITemplatesService templatesService) : Endpoint<DeleteTemplatesRequest>
99
{
10-
private readonly ITemplatesService _templatesService;
11-
12-
public DeleteTemplatesEndpoint(ITemplatesService templatesService)
13-
{
14-
_templatesService = templatesService;
15-
}
16-
10+
private readonly ITemplatesService _templatesService = templatesService;
11+
1712
public override void Configure()
1813
{
1914
Verbs(Http.DELETE);

src/Api/Endpoints/Templates/GetTemplatesEndpoint.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
namespace Api.Endpoints.Templates;
88

9-
internal sealed class GetTemplatesEndpoint :
9+
internal sealed class GetTemplatesEndpoint(ITemplatesService templatesService) :
1010
Endpoint<GetTemplatesRequest, ApiResponse<TemplateResponse[]>, GetTemplateMapper>
1111
{
12-
private readonly ITemplatesService _templatesService;
13-
14-
public GetTemplatesEndpoint(ITemplatesService templatesService)
15-
{
16-
_templatesService = templatesService;
17-
}
18-
12+
private readonly ITemplatesService _templatesService = templatesService;
13+
1914
public override void Configure()
2015
{
2116
Verbs(Http.GET);

src/Api/Endpoints/Templates/UpdateTemplateEndpoint.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
namespace Api.Endpoints.Templates;
88

9-
internal sealed class UpdateTemplateEndpoint :
9+
internal sealed class UpdateTemplateEndpoint(ITemplatesService templatesService) :
1010
Endpoint<UpdateTemplateRequest, ApiResponse<TemplateResponse>, UpdateTemplateMapper>
1111
{
12-
private readonly ITemplatesService _templatesService;
13-
14-
public UpdateTemplateEndpoint(ITemplatesService templatesService)
15-
{
16-
_templatesService = templatesService;
17-
}
18-
12+
private readonly ITemplatesService _templatesService = templatesService;
13+
1914
public override void Configure()
2015
{
2116
Verbs(Http.PUT);

src/Core/Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="ErrorOr" Version="1.3.0" />
1111
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
12-
<PackageReference Include="trumpee.masstransit.messages" Version="1.0.0.16" />
12+
<PackageReference Include="trumpee.masstransit.messages" Version="1.0.0.17" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

src/Core/Services/NotificationService.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
using Core.Models.Notifications;
44
using ErrorOr;
55
using Infrastructure.Persistence.MassTransit;
6+
using Infrastructure.Persistence.MassTransit.Analytics;
67
using Infrastructure.Persistence.Mongo.Abstractions;
78
using Trumpee.MassTransit.Messages.Notifications;
89

910
namespace Core.Services;
1011

1112
internal class NotificationService(
1213
ITemplateFillerClient massTransitClient,
14+
INotificationsAnalyticsClient notificationsAnalyticsClient,
1315
INotificationsRepository notificationsRepository)
1416
: INotificationsService
1517
{
@@ -21,10 +23,15 @@ public async Task<ErrorOr<NotificationDto>> CreateNotification(
2123
ct.ThrowIfCancellationRequested();
2224
await notificationsRepository.InsertOne(notification);
2325

24-
var deliveryRequests = CreateDeliveryRequests(dto);
26+
var deliveryRequests = CreateDeliveryRequests(dto).ToList();
2527

2628
await massTransitClient.SendMessages(deliveryRequests, string.Empty);
27-
29+
foreach (var deliveryRequest in deliveryRequests)
30+
{
31+
await notificationsAnalyticsClient
32+
.SendNotificationCreated(deliveryRequest.NotificationId, ct);
33+
}
34+
2835
dto = dto with { Id = notification.Id.ToString() };
2936
return dto;
3037
}

src/Infrastructure/Infrastructure.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
1717
<PackageReference Include="MongoDB.Driver.Core" Version="2.22.0" />
1818
<PackageReference Include="Trumpee.MassTransit" Version="1.0.0.16" />
19-
<PackageReference Include="trumpee.masstransit.messages" Version="1.0.0.16" />
19+
<PackageReference Include="trumpee.masstransit.messages" Version="1.0.0.17" />
2020
</ItemGroup>
2121

2222
</Project>

src/Infrastructure/Persistence/DependencyInjection.cs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private static void AddMassTransit(this IServiceCollection services, IConfigurat
3636

3737
services.AddScoped<ITemplateFillerClient, TemplateFillerClient>();
3838
services.AddScoped<IUserAnalyticsClient, UserAnalyticsClient>();
39+
services.AddScoped<INotificationsAnalyticsClient, NotificationsAnalyticsClient>();
3940
}
4041

4142
private static void AddAuth0(this IServiceCollection services, IConfiguration config)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Infrastructure.Persistence.MassTransit.Analytics;
2+
3+
public interface INotificationsAnalyticsClient
4+
{
5+
Task SendNotificationCreated(string notificationId, CancellationToken ct);
6+
Task SendNotificationReceived(string notificationId, CancellationToken ct);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using MassTransit;
2+
using Trumpee.MassTransit;
3+
using Trumpee.MassTransit.Messages.Analytics.Notifications;
4+
using Trumpee.MassTransit.Messages.Analytics.Notifications.Payloads;
5+
6+
namespace Infrastructure.Persistence.MassTransit.Analytics;
7+
8+
public class NotificationsAnalyticsClient(
9+
ISendEndpointProvider endpointProvider) : INotificationsAnalyticsClient
10+
{
11+
private const string EventSource = "Trumpee Gateway";
12+
13+
private readonly ISendEndpointProvider _endpointProvider = endpointProvider;
14+
15+
private static readonly string NotificationCreated =
16+
QueueNames.Analytics.Notifications(typeof(NotificationCreatedPayload));
17+
18+
public Task SendNotificationCreated(string notificationId, CancellationToken ct)
19+
{
20+
var analyticsEvent = Notification.Created(EventSource, notificationId);
21+
return SendEvent(analyticsEvent, NotificationCreated, ct);
22+
}
23+
24+
public Task SendNotificationReceived(string notificationId, CancellationToken ct)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
private async Task SendEvent<T>(T analyticsEvent, string queueName, CancellationToken ct) where T : class
30+
{
31+
var endpoint = await _endpointProvider.GetSendEndpoint(new Uri(queueName));
32+
await endpoint.Send(analyticsEvent, ct);
33+
}
34+
}

0 commit comments

Comments
 (0)