Skip to content

Commit

Permalink
Delete unonboarded devices from database instead of soft deleting them (
Browse files Browse the repository at this point in the history
#971)

* feat: actually delete the device instead of soft deleting it

* chore: remove unused methods

* feat: add migrations

* chore: remove redundant spaces
  • Loading branch information
tnotheis authored Dec 11, 2024
1 parent dfec9ae commit e937878
Show file tree
Hide file tree
Showing 12 changed files with 1,976 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ public async Task Handle(DeleteDeviceCommand request, CancellationToken cancella
var deviceId = DeviceId.Parse(request.DeviceId);
var deviceThatIsBeingDeleted = await _identitiesRepository.GetDeviceById(deviceId, cancellationToken, track: true) ?? throw new NotFoundException(nameof(Device));

deviceThatIsBeingDeleted.MarkAsDeleted(_userContext.GetDeviceId(), _userContext.GetAddress());
await _identitiesRepository.Update(deviceThatIsBeingDeleted, cancellationToken);
deviceThatIsBeingDeleted.EnsureCanBeDeleted(_userContext.GetAddress());

_logger.MarkedDeviceAsDeleted();
await _identitiesRepository.DeleteDevice(deviceThatIsBeingDeleted, cancellationToken);

_logger.DeviceDeleted();
}
}

internal static partial class DeleteDeviceLogs
{
[LoggerMessage(
EventId = 776010,
EventName = "Devices.MarkDeviceAsDeleted.MarkedDeviceAsDeleted",
EventName = "Devices.DeleteDevice.DeviceDeleted",
Level = LogLevel.Information,
Message = "Successfully marked the device as deleted.")]
public static partial void MarkedDeviceAsDeleted(this ILogger logger);
Message = "The device was deleted.")]
public static partial void DeviceDeleted(this ILogger logger);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface IIdentitiesRepository
Task Update(Device device, CancellationToken cancellationToken);
Task<T[]> FindDevices<T>(Expression<Func<Device, bool>> filter, Expression<Func<Device, T>> selector, CancellationToken cancellationToken, bool track = false);
Task<bool> HasBackupDevice(IdentityAddress identity, CancellationToken cancellationToken);
Task DeleteDevice(Device device, CancellationToken cancellationToken);

#endregion

Expand Down
25 changes: 8 additions & 17 deletions Modules/Devices/src/Devices.Domain/Entities/Identities/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ public Device(Identity identity, CommunicationLanguage communicationLanguage, De

public bool IsBackupDevice { get; private set; }

public DateTime? DeletedAt { get; set; }
public DeviceId? DeletedByDevice { get; set; }

public bool IsOnboarded => User.HasLoggedIn;

public void EnsureCanBeDeleted(IdentityAddress addressOfActiveIdentity)
{
var error = CanBeDeletedBy(addressOfActiveIdentity);

if (error != null)
throw new DomainException(error);
}

private DomainError? CanBeDeletedBy(IdentityAddress addressOfActiveIdentity)
{
if (IsOnboarded)
Expand All @@ -105,17 +110,6 @@ public bool Update(CommunicationLanguage communicationLanguage)
return hasChanges;
}

public void MarkAsDeleted(DeviceId deletedByDevice, IdentityAddress addressOfActiveIdentity)
{
var error = CanBeDeletedBy(addressOfActiveIdentity);

if (error != null)
throw new DomainException(error);

DeletedAt = SystemTime.UtcNow;
DeletedByDevice = deletedByDevice;
}

public void LoginOccurred()
{
if (IsBackupDevice)
Expand All @@ -134,9 +128,6 @@ public static Device CreateTestDevice(Identity identity, CommunicationLanguage c

#region Expressions

public static Expression<Func<Device, bool>> IsNotDeleted =>
device => device.DeletedAt == null && device.DeletedByDevice == null;

public static Expression<Func<Device, bool>> IsBackup =>
device => device.IsBackupDevice;

Expand Down
Loading

0 comments on commit e937878

Please sign in to comment.