Skip to content

Commit

Permalink
Logs (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubzehner authored Dec 14, 2023
1 parent 14726a1 commit 2dd167e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Microsoft.Extensions.Logging;
using SensoBackend.Application.Abstractions;
using SensoBackend.Application.Modules.Medications;
using SensoBackend.Domain.Exceptions;
Expand All @@ -12,10 +13,13 @@ public sealed class CreateReminderTests : IDisposable
private readonly AppDbContext _context = Database.CreateFixture();
private readonly IMediator _mediator = Substitute.For<IMediator>();
private readonly IHangfireWrapper _hangfireWrapper = Substitute.For<IHangfireWrapper>();
private readonly ILogger<CreateReminderHandler> _logger = Substitute.For<
ILogger<CreateReminderHandler>
>();
private readonly CreateReminderHandler _sut;

public CreateReminderTests() =>
_sut = new CreateReminderHandler(_context, _mediator, _hangfireWrapper);
_sut = new CreateReminderHandler(_context, _mediator, _hangfireWrapper, _logger);

public void Dispose() => _context.Dispose();

Expand Down
22 changes: 20 additions & 2 deletions SensoBackend/Application/Modules/Medications/CreateReminder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentValidation;
using Cronos;
using FluentValidation;
using JetBrains.Annotations;
using Mapster;
using MediatR;
Expand Down Expand Up @@ -50,7 +51,8 @@ public CreateReminderValidator()
public sealed class CreateReminderHandler(
AppDbContext context,
IMediator mediator,
IHangfireWrapper hangfireWrapper
IHangfireWrapper hangfireWrapper,
ILogger<CreateReminderHandler> logger
) : IRequestHandler<CreateReminderRequest, ReminderDto>
{
public async Task<ReminderDto> Handle(CreateReminderRequest request, CancellationToken ct)
Expand Down Expand Up @@ -104,12 +106,28 @@ public async Task<ReminderDto> Handle(CreateReminderRequest request, Cancellatio

if (reminder.Cron is not null)
{
var cronExpression = CronExpression.Parse(reminder.Cron);
var nextOccurrence = cronExpression.GetNextOccurrence(DateTime.UtcNow);
logger.LogInformation(
"Adding reminder {ReminderId} to hangfire with cron {Cron} that will fire at {NextOccurrence}",
reminder.Id,
reminder.Cron,
nextOccurrence
);

hangfireWrapper.AddOrUpdate(
reminder.Id.ToString(),
() => CreateReminderAlert(reminder.Id, reminder.SeniorId),
reminder.Cron
);
}
else
{
logger.LogInformation(
"Reminder {ReminderId} has no cron expression - not adding to hangfire",
reminder.Id
);
}

return ReminderUtils.AdaptToDto(context, reminder).Result;
}
Expand Down
1 change: 1 addition & 0 deletions SensoBackend/SensoBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Asp.Versioning.Mvc" Version="7.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.1.0" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Expo.Server.SDK" Version="1.0.2" />
<PackageReference Include="FluentValidation" Version="11.8.1" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.8.1" />
Expand Down

0 comments on commit 2dd167e

Please sign in to comment.