Skip to content

Commit

Permalink
Merge branch 'main' into renovate/major-testcontainers-dotnet-monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis authored Nov 4, 2024
2 parents 41eedb4 + ad9bb88 commit 5adfd57
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
using Backbone.Crypto.Implementations;
using Backbone.Modules.Devices.Application.Extensions;
using Backbone.Modules.Devices.Infrastructure.Persistence;
using Microsoft.Extensions.Options;

namespace Backbone.AdminApi.Extensions;

public static class DevicesServiceCollectionExtensions
{
public static IServiceCollection AddDevices(this IServiceCollection services,
DevicesConfiguration configuration)
public static IServiceCollection AddDevices(this IServiceCollection services, IConfiguration configuration)
{
services.AddApplication();
services.AddApplication(configuration.GetSection("Application"));

services.ConfigureAndValidate<DevicesConfiguration.InfrastructureConfiguration>(configuration.GetSection("Infrastructure").Bind);

var infrastructureConfiguration = services.BuildServiceProvider().GetRequiredService<IOptions<DevicesConfiguration.InfrastructureConfiguration>>().Value;

services.AddDatabase(options =>
{
options.Provider = configuration.Infrastructure.SqlDatabase.Provider;
options.ConnectionString = configuration.Infrastructure.SqlDatabase.ConnectionString;
options.Provider = infrastructureConfiguration.SqlDatabase.Provider;
options.ConnectionString = infrastructureConfiguration.SqlDatabase.ConnectionString;
});

services.AddSingleton<ISignatureHelper, SignatureHelper>(_ => SignatureHelper.CreateEd25519WithRawKeyFormat());
Expand Down
7 changes: 2 additions & 5 deletions Applications/AdminApi/src/AdminApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Backbone.BuildingBlocks.Application.QuotaCheck;
using Backbone.BuildingBlocks.Infrastructure.Persistence.Database;
using Backbone.Infrastructure.EventBus;
using Backbone.Modules.Devices.Application;
using Backbone.Modules.Devices.Infrastructure.OpenIddict;
using Backbone.Modules.Devices.Infrastructure.Persistence.Database;
using Backbone.Modules.Devices.Infrastructure.PushNotifications;
Expand Down Expand Up @@ -96,9 +95,7 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config

services.AddSingleton<ApiKeyValidator>();

services
.ConfigureAndValidate<AdminConfiguration>(configuration.Bind)
.ConfigureAndValidate<ApplicationOptions>(options => configuration.GetSection("Modules:Devices:Application").Bind(options));
services.ConfigureAndValidate<AdminConfiguration>(configuration.Bind);

#pragma warning disable ASP0000 // We retrieve the Configuration via IOptions here so that it is validated
var parsedConfiguration = services.BuildServiceProvider().GetRequiredService<IOptions<AdminConfiguration>>().Value;
Expand All @@ -110,7 +107,7 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
.AddCustomFluentValidation()
.AddCustomIdentity(environment)
.AddDatabase(parsedConfiguration.Infrastructure.SqlDatabase)
.AddDevices(parsedConfiguration.Modules.Devices)
.AddDevices(configuration.GetSection("Modules:Devices"))
.AddQuotas(parsedConfiguration.Modules.Quotas)
.AddChallenges(parsedConfiguration.Modules.Challenges)
.AddHealthChecks();
Expand Down
2 changes: 1 addition & 1 deletion Applications/AdminCli/src/AdminCli/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static IServiceCollection ConfigureServices(string dbProvider, string db
options.AddApplicationStore<CustomOpenIddictEntityFrameworkCoreApplicationStore>();
});

services.AddApplication();
services.AddApplicationWithoutIdentityDeletion();

services.AddSingleton<IQuotaChecker, AlwaysSuccessQuotaChecker>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="ReHackt.Extensions.Options.Validation" Version="8.0.3" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
using Backbone.BuildingBlocks.Application.MediatR;
using Backbone.Modules.Devices.Application.Devices.Commands.RegisterDevice;
using Backbone.Modules.Devices.Application.Devices.Queries.ListDevices;
using Backbone.Modules.Devices.Domain.Entities.Identities;
using FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Backbone.Modules.Devices.Application.Extensions;

public static class IServiceCollectionExtensions
{
public static void AddApplication(this IServiceCollection services)
public static void AddApplication(this IServiceCollection services, IConfiguration applicationConfiguration)
{
services.ConfigureAndValidate<ApplicationOptions>(applicationConfiguration.Bind);

applicationConfiguration.GetSection("IdentityDeletion").Bind(IdentityDeletionConfiguration.Instance);

services.AddApplicationWithoutIdentityDeletion();
}

public static void AddApplicationWithoutIdentityDeletion(this IServiceCollection services)
{
services.AddMediatR(c => c
.RegisterServicesFromAssemblyContaining<RegisterDeviceCommand>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public async Task<ApproveDeletionProcessResponse> Handle(ApproveDeletionProcessC

await _identitiesRepository.Update(identity, cancellationToken);

var daysUntilDeletion = deletionProcess.GracePeriodEndsAt?.DaysUntilDate() ??
throw new Exception($"Expected '{nameof(deletionProcess.GracePeriodEndsAt)}' to be set but found 'null' instead.");
if (deletionProcess.GracePeriodEndsAt == null)
throw new Exception($"Expected '{nameof(deletionProcess.GracePeriodEndsAt)}' to be set but found 'null' instead.");

var daysUntilDeletion = deletionProcess.GracePeriodEndsAt.Value.DaysUntilDate();

await _notificationSender.SendNotification(identity.Address, new DeletionProcessApprovedNotification(daysUntilDeletion), cancellationToken);

return new ApproveDeletionProcessResponse(deletionProcess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,56 @@ public async Task Handle(SendDeletionProcessApprovalRemindersCommand request, Ca
foreach (var identity in identities)
{
var deletionProcess = identity.GetDeletionProcessInStatus(DeletionProcessStatus.WaitingForApproval) ?? throw new NotFoundException(nameof(IdentityDeletionProcess));
var daysUntilApprovalPeriodEnds = (deletionProcess.ApprovalPeriodEndsAt - SystemTime.UtcNow).Days;
var daysUntilApprovalPeriodEnds = (deletionProcess.ApprovalPeriodEndsAt - SystemTime.UtcNow).TotalDays;

if (deletionProcess.ApprovalReminder3SentAt != null)
{
_logger.NoApprovalReminderSent();
continue;
}

if (daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.ApprovalReminder3.Time)
if (daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.Instance.ApprovalReminder3.DaysBeforeEndOfApprovalPeriod)
{
await SendReminder3(identity, daysUntilApprovalPeriodEnds, deletionProcess.Id, cancellationToken);
continue;
}

if (deletionProcess.ApprovalReminder2SentAt != null) continue;
if (daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.ApprovalReminder2.Time)
if (daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.Instance.ApprovalReminder2.DaysBeforeEndOfApprovalPeriod)
{
await SendReminder2(identity, daysUntilApprovalPeriodEnds, deletionProcess.Id, cancellationToken);
continue;
}

if (deletionProcess.ApprovalReminder1SentAt == null && daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.ApprovalReminder1.Time)
if (deletionProcess.ApprovalReminder1SentAt == null && daysUntilApprovalPeriodEnds <= IdentityDeletionConfiguration.Instance.ApprovalReminder1.DaysBeforeEndOfApprovalPeriod)
{
await SendReminder1(identity, daysUntilApprovalPeriodEnds, deletionProcess.Id, cancellationToken);
}
}
}

private async Task SendReminder3(Identity identity, int daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder3(Identity identity, double daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification(daysUntilApprovalPeriodEnds), cancellationToken);
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
cancellationToken);
identity.DeletionProcessApprovalReminder3Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.ApprovalReminder3Sent(deletionProcessId);
}

private async Task SendReminder2(Identity identity, int daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder2(Identity identity, double daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification(daysUntilApprovalPeriodEnds), cancellationToken);
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
cancellationToken);
identity.DeletionProcessApprovalReminder2Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.ApprovalReminder2Sent(deletionProcessId);
}

private async Task SendReminder1(Identity identity, int daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder1(Identity identity, double daysUntilApprovalPeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification(daysUntilApprovalPeriodEnds), cancellationToken);
await _pushNotificationSender.SendNotification(identity.Address, new DeletionProcessWaitingForApprovalReminderPushNotification((int)Math.Ceiling(daysUntilApprovalPeriodEnds)),
cancellationToken);
identity.DeletionProcessApprovalReminder1Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.ApprovalReminder1Sent(deletionProcessId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,58 @@ public async Task Handle(SendDeletionProcessGracePeriodRemindersCommand request,
foreach (var identity in identities)
{
var deletionProcess = identity.GetDeletionProcessInStatus(DeletionProcessStatus.Approved) ?? throw new NotFoundException(nameof(IdentityDeletionProcess));
var daysToDeletion = (deletionProcess.GracePeriodEndsAt!.Value - SystemTime.UtcNow).Days;
var daysUntilDeletion = (deletionProcess.GracePeriodEndsAt!.Value - SystemTime.UtcNow).TotalDays;

if (deletionProcess.GracePeriodReminder3SentAt != null)
{
_logger.NoReminderSent();
continue;
}

if (daysToDeletion <= IdentityDeletionConfiguration.GracePeriodNotification3.Time)
if (daysUntilDeletion <= IdentityDeletionConfiguration.Instance.GracePeriodNotification3.DaysBeforeEndOfGracePeriod)
{
await SendReminder3(identity, daysToDeletion, deletionProcess.Id, cancellationToken);
await SendReminder3(identity, daysUntilDeletion, deletionProcess.Id, cancellationToken);
continue;
}

if (deletionProcess.GracePeriodReminder2SentAt != null) continue;
if (daysToDeletion <= IdentityDeletionConfiguration.GracePeriodNotification2.Time)
if (deletionProcess.GracePeriodReminder2SentAt != null)
continue;

if (daysUntilDeletion <= IdentityDeletionConfiguration.Instance.GracePeriodNotification2.DaysBeforeEndOfGracePeriod)
{
await SendReminder2(identity, daysToDeletion, deletionProcess.Id, cancellationToken);
await SendReminder2(identity, daysUntilDeletion, deletionProcess.Id, cancellationToken);
continue;
}

if (deletionProcess.GracePeriodReminder1SentAt == null && daysToDeletion <= IdentityDeletionConfiguration.GracePeriodNotification1.Time)
if (deletionProcess.GracePeriodReminder1SentAt != null)
continue;

if (daysUntilDeletion <= IdentityDeletionConfiguration.Instance.GracePeriodNotification1.DaysBeforeEndOfGracePeriod)
{
await SendReminder1(identity, daysToDeletion, deletionProcess.Id, cancellationToken);
await SendReminder1(identity, daysUntilDeletion, deletionProcess.Id, cancellationToken);
}
}
}

private async Task SendReminder3(Identity identity, int daysToDeletion, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder3(Identity identity, double daysUntilGracePeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification(daysToDeletion), cancellationToken);
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)), cancellationToken);
identity.DeletionGracePeriodReminder3Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.Reminder3Sent(deletionProcessId);
}

private async Task SendReminder2(Identity identity, int daysToDeletion, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder2(Identity identity, double daysUntilGracePeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification(daysToDeletion), cancellationToken);
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)), cancellationToken);
identity.DeletionGracePeriodReminder2Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.Reminder2Sent(deletionProcessId);
}

private async Task SendReminder1(Identity identity, int daysToDeletion, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
private async Task SendReminder1(Identity identity, double daysUntilGracePeriodEnds, IdentityDeletionProcessId deletionProcessId, CancellationToken cancellationToken)
{
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification(daysToDeletion), cancellationToken);
await _pushSender.SendNotification(identity.Address, new DeletionProcessGracePeriodReminderPushNotification((int)Math.Ceiling(daysUntilGracePeriodEnds)), cancellationToken);
identity.DeletionGracePeriodReminder1Sent();
await _identitiesRepository.Update(identity, cancellationToken);
_logger.Reminder1Sent(deletionProcessId);
Expand Down
4 changes: 1 addition & 3 deletions Modules/Devices/src/Devices.ConsumerApi/DevicesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.Crypto.Abstractions;
using Backbone.Crypto.Implementations;
using Backbone.Modules.Devices.Application;
using Backbone.Modules.Devices.Application.Extensions;
using Backbone.Modules.Devices.Infrastructure.Persistence;
using Backbone.Modules.Devices.Infrastructure.Persistence.Database;
Expand All @@ -21,12 +20,11 @@ public class DevicesModule : AbstractModule

public override void ConfigureServices(IServiceCollection services, IConfigurationSection configuration)
{
services.ConfigureAndValidate<ApplicationOptions>(options => configuration.GetSection("Application").Bind(options));
services.ConfigureAndValidate<Configuration>(configuration.Bind);

var parsedConfiguration = services.BuildServiceProvider().GetRequiredService<IOptions<Configuration>>().Value;

services.AddApplication();
services.AddApplication(configuration.GetSection("Application"));

services.AddDatabase(options =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
namespace Backbone.Modules.Devices.Domain.Entities.Identities;

public class IdentityDeletionConfiguration
public record IdentityDeletionConfiguration
{
public static int LengthOfApprovalPeriod => 7;
public static int LengthOfGracePeriod => 14;
public static IdentityDeletionConfiguration Instance { get; private set; } = new();

public static GracePeriodNotificationConfiguration GracePeriodNotification1 { get; } = new()
{
Time = 12
};
public double LengthOfApprovalPeriodInDays { get; set; } = 7;
public double LengthOfGracePeriodInDays { get; set; } = 14;

public static GracePeriodNotificationConfiguration GracePeriodNotification2 { get; } = new()
{
Time = 10
};
public GracePeriodNotificationConfiguration GracePeriodNotification1 { get; set; } = new() { DaysBeforeEndOfGracePeriod = 12 };
public GracePeriodNotificationConfiguration GracePeriodNotification2 { get; set; } = new() { DaysBeforeEndOfGracePeriod = 10 };
public GracePeriodNotificationConfiguration GracePeriodNotification3 { get; set; } = new() { DaysBeforeEndOfGracePeriod = 5 };

public static GracePeriodNotificationConfiguration GracePeriodNotification3 { get; } = new()
{
Time = 5
};

public static ApprovalReminderNotificationConfiguration ApprovalReminder1 { get; } = new()
{
Time = 6
};

public static ApprovalReminderNotificationConfiguration ApprovalReminder2 { get; } = new()
{
Time = 4
};

public static ApprovalReminderNotificationConfiguration ApprovalReminder3 { get; } = new()
{
Time = 2
};
public ApprovalReminderNotificationConfiguration ApprovalReminder1 { get; set; } = new() { DaysBeforeEndOfApprovalPeriod = 6 };
public ApprovalReminderNotificationConfiguration ApprovalReminder2 { get; set; } = new() { DaysBeforeEndOfApprovalPeriod = 4 };
public ApprovalReminderNotificationConfiguration ApprovalReminder3 { get; set; } = new() { DaysBeforeEndOfApprovalPeriod = 2 };
}

public class GracePeriodNotificationConfiguration
public class ApprovalReminderNotificationConfiguration
{
public int Time { get; init; }
public double DaysBeforeEndOfApprovalPeriod { get; set; }
}

public class ApprovalReminderNotificationConfiguration
public class GracePeriodNotificationConfiguration
{
public int Time { get; init; }
public double DaysBeforeEndOfGracePeriod { get; set; }
}
Loading

0 comments on commit 5adfd57

Please sign in to comment.