Skip to content

Commit

Permalink
Possibility for the user to override the length of the grace period w…
Browse files Browse the repository at this point in the history
…hen starting an identity deletion process (#961)
  • Loading branch information
tnotheis authored Dec 4, 2024
1 parent 947f15f commit 05ee700
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<StartDeletionProcessAsOwnerResponse> Handle(StartDeletionProce
{
var identity = await _identitiesRepository.FindByAddress(_userContext.GetAddress(), cancellationToken, true) ?? throw new NotFoundException(nameof(Identity));

var deletionProcess = identity.StartDeletionProcessAsOwner(_userContext.GetDeviceId());
var deletionProcess = identity.StartDeletionProcessAsOwner(_userContext.GetDeviceId(), request.LengthOfGracePeriodInDays);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

namespace Backbone.Modules.Devices.Application.Identities.Commands.StartDeletionProcessAsOwner;

public class StartDeletionProcessAsOwnerCommand : IRequest<StartDeletionProcessAsOwnerResponse>;
public class StartDeletionProcessAsOwnerCommand : IRequest<StartDeletionProcessAsOwnerResponse>
{
public double? LengthOfGracePeriodInDays { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public async Task<IActionResult> CreateIdentity(CreateIdentityRequest request, C
[HttpPost("Self/DeletionProcesses")]
[ProducesResponseType(typeof(HttpResponseEnvelopeResult<StartDeletionProcessAsOwnerResponse>), StatusCodes.Status201Created)]
[ProducesError(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> StartDeletionProcess(CancellationToken cancellationToken)
public async Task<IActionResult> StartDeletionProcess(StartDeletionProcessAsOwnerCommand? request, CancellationToken cancellationToken)
{
var response = await _mediator.Send(new StartDeletionProcessAsOwnerCommand(), cancellationToken);
request ??= new StartDeletionProcessAsOwnerCommand();
var response = await _mediator.Send(request, cancellationToken);
return Created("", response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ public IdentityDeletionProcess StartDeletionProcessAsSupport()
return deletionProcess;
}

public IdentityDeletionProcess StartDeletionProcessAsOwner(DeviceId asDevice)
public IdentityDeletionProcess StartDeletionProcessAsOwner(DeviceId asDevice, double? lengthOfGracePeriodInDays = null)
{
EnsureNoActiveProcessExists();
EnsureIdentityOwnsDevice(asDevice);

TierIdBeforeDeletion = TierId;

var deletionProcess = IdentityDeletionProcess.StartAsOwner(Address, asDevice);
var deletionProcess = IdentityDeletionProcess.StartAsOwner(Address, asDevice, lengthOfGracePeriodInDays);
_deletionProcesses.Add(deletionProcess);

DeletionGracePeriodEndsAt = deletionProcess.GracePeriodEndsAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ private IdentityDeletionProcess(IdentityAddress createdBy, DeletionProcessStatus
RaiseDomainEvent(new IdentityDeletionProcessStartedDomainEvent(createdBy, Id, null));
}

private IdentityDeletionProcess(IdentityAddress createdBy, DeviceId createdByDevice)
private IdentityDeletionProcess(IdentityAddress createdBy, DeviceId createdByDevice, double? lengthOfGracePeriodInDays)
{
Id = IdentityDeletionProcessId.Generate();
IdentityAddress = null!;
CreatedAt = SystemTime.UtcNow;

ApproveInternally(createdBy, createdByDevice);
ApproveInternally(createdBy, createdByDevice, lengthOfGracePeriodInDays);

_auditLog = [IdentityDeletionProcessAuditLogEntry.ProcessStartedByOwner(Id, createdBy, createdByDevice)];
}
Expand Down Expand Up @@ -81,9 +81,9 @@ public static IdentityDeletionProcess StartAsSupport(IdentityAddress createdBy)
return new IdentityDeletionProcess(createdBy, DeletionProcessStatus.WaitingForApproval);
}

public static IdentityDeletionProcess StartAsOwner(IdentityAddress createdBy, DeviceId createdByDeviceId)
public static IdentityDeletionProcess StartAsOwner(IdentityAddress createdBy, DeviceId createdByDeviceId, double? lengthOfGracePeriodInDays)
{
return new IdentityDeletionProcess(createdBy, createdByDeviceId);
return new IdentityDeletionProcess(createdBy, createdByDeviceId, lengthOfGracePeriodInDays);
}

public bool IsActive()
Expand Down Expand Up @@ -150,11 +150,12 @@ public void Approve(IdentityAddress address, DeviceId approvedByDevice)
_auditLog.Add(IdentityDeletionProcessAuditLogEntry.ProcessApproved(Id, address, approvedByDevice));
}

private void ApproveInternally(IdentityAddress address, DeviceId createdByDevice)
private void ApproveInternally(IdentityAddress address, DeviceId createdByDevice, double? lengthOfGracePeriodInDays = null)
{
lengthOfGracePeriodInDays ??= IdentityDeletionConfiguration.Instance.LengthOfGracePeriodInDays;
ApprovedAt = SystemTime.UtcNow;
ApprovedByDevice = createdByDevice;
GracePeriodEndsAt = SystemTime.UtcNow.AddDays(IdentityDeletionConfiguration.Instance.LengthOfGracePeriodInDays);
GracePeriodEndsAt = SystemTime.UtcNow.AddDays(lengthOfGracePeriodInDays.Value);
ChangeStatus(DeletionProcessStatus.Approved, address, address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ public void Raises_domain_events()
identityToBeDeletedDomainEvent.IdentityAddress.Should().Be(activeIdentity.Address);
}

[Fact]
public void Passing_a_lengthOfDeletionGracePeriod_overrides_the_configured_value()
{
//Arrange
var activeIdentity = TestDataGenerator.CreateIdentity();
var activeDevice = activeIdentity.Devices[0];
SystemTime.Set("2000-01-01");

//Act
activeIdentity.StartDeletionProcessAsOwner(activeDevice.Id, 1);

// Assert
activeIdentity.DeletionGracePeriodEndsAt.Should().Be(DateTime.Parse("2000-01-02"));
activeIdentity.DeletionProcesses.First().GracePeriodEndsAt.Should().Be(DateTime.Parse("2000-01-02"));
}

private static void AssertDeletionProcessWasStarted(Identity activeIdentity)
{
activeIdentity.DeletionProcesses.Should().HaveCount(1);
Expand Down

0 comments on commit 05ee700

Please sign in to comment.