| | | 1 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 2 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 3 | | using ArturRios.Heimdall.Command.Input; |
| | | 4 | | using ArturRios.Heimdall.Command.Output; |
| | | 5 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 7 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 8 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles <see cref="DeleteScopeCommand" /> (UC-04): locates the scope (AF-04a), then logically |
| | | 16 | | /// deletes it and cascades <c>IsDeleted = true</c> to its Users (via <c>SCOPE_USER</c>), Google |
| | | 17 | | /// Users, and applications. Owners (<c>SCOPE_OWNER</c>) are never modified. An already-deleted |
| | | 18 | | /// scope is an idempotent no-op (AF-04b). The response reports the totals of the scope's members, |
| | | 19 | | /// counted regardless of their individual deletion state. All failures are returned as errors on |
| | | 20 | | /// the <see cref="DataOutput{T}" /> rather than thrown. |
| | | 21 | | /// </summary> |
| | 9 | 22 | | public class DeleteScopeCommandHandler( |
| | 9 | 23 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 9 | 24 | | IAsyncRepository<Scope, long> scopeWriter, |
| | 9 | 25 | | IAsyncReadOnlyRepository<Person, long> personReader, |
| | 9 | 26 | | IAsyncRepository<Person, long> personWriter, |
| | 9 | 27 | | IAsyncReadOnlyRepository<GoogleUser, long> googleUserReader, |
| | 9 | 28 | | IAsyncRepository<GoogleUser, long> googleUserWriter, |
| | 9 | 29 | | IAsyncReadOnlyRepository<Application, long> applicationReader, |
| | 9 | 30 | | IAsyncRepository<Application, long> applicationWriter) |
| | | 31 | | : ICommandHandlerAsync<DeleteScopeCommand, DeleteScopeCommandOutput> |
| | | 32 | | { |
| | | 33 | | public async Task<DataOutput<DeleteScopeCommandOutput?>> HandleAsync(DeleteScopeCommand command, CancellationToken c |
| | | 34 | | { |
| | 9 | 35 | | var output = DataOutput<DeleteScopeCommandOutput?>.New; |
| | | 36 | | |
| | | 37 | | // Step 2 (AF-04a): locate the scope in ANY deletion state, so an already-deleted scope is |
| | | 38 | | // handled idempotently (AF-04b) rather than reported as not found. |
| | 9 | 39 | | var scope = await scopeReader.Query().FirstOrDefaultAsync(x => x.PublicId == command.Id); |
| | | 40 | | |
| | 9 | 41 | | if (scope is null) |
| | | 42 | | { |
| | 2 | 43 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Step 3: the scope's members, counted regardless of individual deletion state (both flows). |
| | 7 | 47 | | var users = await personReader.Query() |
| | 7 | 48 | | .Where(p => p.ScopeMembership != null && p.ScopeMembership.ScopeId == scope.Id) |
| | 7 | 49 | | .ToListAsync(); |
| | 7 | 50 | | var googleUsers = await googleUserReader.Query() |
| | 7 | 51 | | .Where(g => g.ScopeId == scope.Id) |
| | 7 | 52 | | .ToListAsync(); |
| | 7 | 53 | | var applications = await applicationReader.Query() |
| | 7 | 54 | | .Where(a => a.ScopeId == scope.Id) |
| | 7 | 55 | | .ToListAsync(); |
| | | 56 | | |
| | | 57 | | // AF-04b: an already-deleted scope is left untouched; the totals are still reported below. |
| | 7 | 58 | | if (!scope.IsDeleted) |
| | | 59 | | { |
| | 5 | 60 | | var now = DateTime.UtcNow; |
| | | 61 | | |
| | | 62 | | // Step 4: flip the scope itself. |
| | | 63 | | // The cascade is an administrative deletion like any other (NFR-20): nobody asked to be |
| | | 64 | | // erased, so the identities it sweeps up get the reversal window rather than the |
| | | 65 | | // statutory deadline. |
| | | 66 | | const int administrative = (int)DeletionKinds.Administrative; |
| | | 67 | | |
| | 5 | 68 | | scope.IsDeleted = true; |
| | 5 | 69 | | scope.UpdatedAt = now; |
| | | 70 | | |
| | 5 | 71 | | var scopeUpdate = await scopeWriter.UpdateAsync(scope); |
| | | 72 | | |
| | 5 | 73 | | if (!scopeUpdate.Success) |
| | | 74 | | { |
| | 0 | 75 | | return output.WithErrors(scopeUpdate.Errors); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Step 5: cascade to the members that are not already deleted. |
| | 11 | 79 | | var cascadeErrors = (await CascadeAsync(users, p => p.IsDeleted, |
| | 25 | 80 | | p => { p.IsDeleted = true; p.DeletedAt = now; p.DeletionKind = administrative; p.UpdatedAt = now; }, |
| | 5 | 81 | | personWriter)) |
| | 3 | 82 | | .Concat(await CascadeAsync(googleUsers, g => g.IsDeleted, |
| | 15 | 83 | | g => { g.IsDeleted = true; g.DeletedAt = now; g.DeletionKind = administrative; g.UpdatedAt = now; }, |
| | 5 | 84 | | googleUserWriter)) |
| | 3 | 85 | | .Concat(await CascadeAsync(applications, a => a.IsDeleted, |
| | 9 | 86 | | a => { a.IsDeleted = true; a.UpdatedAt = now; }, applicationWriter)) |
| | 5 | 87 | | .ToList(); |
| | | 88 | | |
| | 5 | 89 | | if (cascadeErrors.Count > 0) |
| | | 90 | | { |
| | 0 | 91 | | return output.WithErrors(cascadeErrors); |
| | | 92 | | } |
| | 5 | 93 | | } |
| | | 94 | | |
| | | 95 | | // Step 6: return the scope id and the member totals. |
| | 7 | 96 | | return output |
| | 7 | 97 | | .WithData(new DeleteScopeCommandOutput |
| | 7 | 98 | | { |
| | 7 | 99 | | Id = scope.PublicId, |
| | 7 | 100 | | UserCount = users.Count, |
| | 7 | 101 | | GoogleUserCount = googleUsers.Count, |
| | 7 | 102 | | ApplicationCount = applications.Count |
| | 7 | 103 | | }) |
| | 7 | 104 | | .WithMessage(ScopeMessages.ScopeDeletedSuccessfully); |
| | 9 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Flips <c>IsDeleted</c> (and <c>UpdatedAt</c>, via <paramref name="markDeleted" />) on the |
| | | 109 | | /// members that are not already deleted, then persists them. Returns any persistence errors, |
| | | 110 | | /// or an empty sequence when there is nothing to update. |
| | | 111 | | /// </summary> |
| | | 112 | | private static async Task<IEnumerable<string>> CascadeAsync<T>( |
| | | 113 | | IEnumerable<T> members, |
| | | 114 | | Func<T, bool> isDeleted, |
| | | 115 | | Action<T> markDeleted, |
| | | 116 | | IAsyncRepository<T, long> writer) where T : Entity<long> |
| | | 117 | | { |
| | 27 | 118 | | var pending = members.Where(member => !isDeleted(member)).ToList(); |
| | | 119 | | |
| | 15 | 120 | | if (pending.Count == 0) |
| | | 121 | | { |
| | 6 | 122 | | return []; |
| | | 123 | | } |
| | | 124 | | |
| | 40 | 125 | | foreach (var member in pending) |
| | | 126 | | { |
| | 11 | 127 | | markDeleted(member); |
| | | 128 | | } |
| | | 129 | | |
| | 9 | 130 | | var result = await writer.UpdateRangeAsync(pending); |
| | | 131 | | |
| | 9 | 132 | | return result.Success ? [] : result.Errors; |
| | 15 | 133 | | } |
| | | 134 | | } |