|
| 1 | +using Taskdeck.Application.DTOs; |
| 2 | +using Taskdeck.Application.Interfaces; |
| 3 | +using Taskdeck.Domain.Common; |
| 4 | +using Taskdeck.Domain.Entities; |
| 5 | +using Taskdeck.Domain.Exceptions; |
| 6 | + |
| 7 | +namespace Taskdeck.Application.Services; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Analyzes a proposal's operations to produce a 7-category side-effect breakdown |
| 11 | +/// (Cards, Subtasks, Comments, Activity log, Notifications, Webhooks, Calendar) |
| 12 | +/// and a reversibility posture. |
| 13 | +/// </summary> |
| 14 | +public sealed class SideEffectAnalyzer : ISideEffectAnalyzer |
| 15 | +{ |
| 16 | + // Action types that actively mutate cards |
| 17 | + private static readonly HashSet<string> CardMutatingActions = new(StringComparer.OrdinalIgnoreCase) |
| 18 | + { |
| 19 | + "create", "move", "archive", "update", "delete", "bulk_move" |
| 20 | + }; |
| 21 | + |
| 22 | + private readonly IUnitOfWork _unitOfWork; |
| 23 | + |
| 24 | + public SideEffectAnalyzer(IUnitOfWork unitOfWork) |
| 25 | + { |
| 26 | + _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); |
| 27 | + } |
| 28 | + |
| 29 | + public async Task<Result<ProposalSideEffectsDto>> AnalyzeAsync( |
| 30 | + Guid proposalId, |
| 31 | + CancellationToken cancellationToken = default) |
| 32 | + { |
| 33 | + var proposal = await _unitOfWork.AutomationProposals.GetByIdAsync(proposalId, cancellationToken); |
| 34 | + if (proposal is null) |
| 35 | + return Result.Failure<ProposalSideEffectsDto>(ErrorCodes.NotFound, "Proposal not found."); |
| 36 | + |
| 37 | + var operations = proposal.Operations; |
| 38 | + |
| 39 | + // Determine webhook status for the board |
| 40 | + bool hasActiveWebhooks = false; |
| 41 | + if (proposal.BoardId.HasValue) |
| 42 | + { |
| 43 | + var webhookSubs = await _unitOfWork.OutboundWebhookSubscriptions |
| 44 | + .GetActiveByBoardAsync(proposal.BoardId.Value, cancellationToken); |
| 45 | + hasActiveWebhooks = webhookSubs.Count > 0; |
| 46 | + } |
| 47 | + |
| 48 | + var rows = BuildSideEffectRows(operations, hasActiveWebhooks); |
| 49 | + var reversibility = ComputeReversibility(operations, proposal.RiskLevel); |
| 50 | + |
| 51 | + var dto = new ProposalSideEffectsDto( |
| 52 | + Rows: rows.Select(r => new SideEffectRowDto(r.Key, r.Value, r.Tone.ToString().ToLowerInvariant())).ToList(), |
| 53 | + Reversibility: new ReversibilityDto(reversibility.Summary, reversibility.Description, reversibility.WindowMs)); |
| 54 | + |
| 55 | + return Result.Success(dto); |
| 56 | + } |
| 57 | + |
| 58 | + internal static IReadOnlyList<SideEffectRow> BuildSideEffectRows( |
| 59 | + IReadOnlyList<AutomationProposalOperation> operations, |
| 60 | + bool hasActiveWebhooks) |
| 61 | + { |
| 62 | + bool hasCardMutation = operations.Any(op => |
| 63 | + CardMutatingActions.Contains(op.ActionType) && |
| 64 | + string.Equals(op.TargetType, "card", StringComparison.OrdinalIgnoreCase)); |
| 65 | + bool hasColumnMutation = operations.Any(op => |
| 66 | + string.Equals(op.TargetType, "column", StringComparison.OrdinalIgnoreCase)); |
| 67 | + bool hasBoardMutation = hasCardMutation || hasColumnMutation; |
| 68 | + bool hasAnyOperation = operations.Count > 0; |
| 69 | + |
| 70 | + return new List<SideEffectRow> |
| 71 | + { |
| 72 | + new( |
| 73 | + "Cards", |
| 74 | + hasBoardMutation |
| 75 | + ? hasCardMutation && hasColumnMutation |
| 76 | + ? "Creates, moves, or archives cards and adds columns on the board" |
| 77 | + : hasCardMutation |
| 78 | + ? "Creates, moves, or archives cards on the board" |
| 79 | + : "Adds columns to the board (no direct card mutations)" |
| 80 | + : "No board mutations", |
| 81 | + hasBoardMutation ? SideEffectTone.Active : SideEffectTone.Passive), |
| 82 | + |
| 83 | + new( |
| 84 | + "Subtasks", |
| 85 | + "Subtask management not yet supported", |
| 86 | + SideEffectTone.Passive), |
| 87 | + |
| 88 | + new( |
| 89 | + "Comments", |
| 90 | + "Proposals do not create comments", |
| 91 | + SideEffectTone.Passive), |
| 92 | + |
| 93 | + new( |
| 94 | + "Activity log", |
| 95 | + hasAnyOperation |
| 96 | + ? "Audit entries will be recorded for all applied operations" |
| 97 | + : "No operations to log", |
| 98 | + hasAnyOperation ? SideEffectTone.Active : SideEffectTone.Passive), |
| 99 | + |
| 100 | + new( |
| 101 | + "Notifications", |
| 102 | + hasAnyOperation |
| 103 | + ? "Approval or rejection generates notifications" |
| 104 | + : "No notifications generated", |
| 105 | + hasAnyOperation ? SideEffectTone.Active : SideEffectTone.Passive), |
| 106 | + |
| 107 | + new( |
| 108 | + "Webhooks", |
| 109 | + hasActiveWebhooks && hasAnyOperation |
| 110 | + ? "Outbound webhooks configured for this board will fire" |
| 111 | + : hasActiveWebhooks |
| 112 | + ? "Outbound webhooks configured but no operations to trigger them" |
| 113 | + : "No outbound webhooks configured", |
| 114 | + hasActiveWebhooks && hasAnyOperation ? SideEffectTone.Active : SideEffectTone.Passive), |
| 115 | + |
| 116 | + new( |
| 117 | + "Calendar", |
| 118 | + "Calendar integration not yet available", |
| 119 | + SideEffectTone.Passive) |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + internal static Reversibility ComputeReversibility( |
| 124 | + IReadOnlyList<AutomationProposalOperation> operations, |
| 125 | + RiskLevel riskLevel) |
| 126 | + { |
| 127 | + // Base window is 6 hours |
| 128 | + long windowMs = Reversibility.DefaultWindowMs; |
| 129 | + |
| 130 | + // Adjust window based on risk level |
| 131 | + string summary; |
| 132 | + string description; |
| 133 | + |
| 134 | + switch (riskLevel) |
| 135 | + { |
| 136 | + case RiskLevel.Critical: |
| 137 | + windowMs = Reversibility.DefaultWindowMs / 2; // 3 hours -- tighter for critical |
| 138 | + summary = "3 hours · manual intervention required"; |
| 139 | + description = "Critical-risk operations may require manual intervention to reverse. " + |
| 140 | + "Archive and delete operations can be recovered within the window, " + |
| 141 | + "but downstream effects (webhooks, notifications) cannot be recalled."; |
| 142 | + break; |
| 143 | + |
| 144 | + case RiskLevel.High: |
| 145 | + windowMs = Reversibility.DefaultWindowMs; // 6 hours |
| 146 | + summary = "6 hours · single keystroke"; |
| 147 | + description = "High-risk operations can be reversed within the window. " + |
| 148 | + "Card moves and updates are fully reversible; " + |
| 149 | + "archived cards can be restored from the archive."; |
| 150 | + break; |
| 151 | + |
| 152 | + case RiskLevel.Medium: |
| 153 | + windowMs = Reversibility.DefaultWindowMs; // 6 hours |
| 154 | + summary = "6 hours · single keystroke"; |
| 155 | + description = "Medium-risk operations are fully reversible within the window. " + |
| 156 | + "All board mutations can be undone from the activity log."; |
| 157 | + break; |
| 158 | + |
| 159 | + case RiskLevel.Low: |
| 160 | + default: |
| 161 | + windowMs = Reversibility.DefaultWindowMs; // 6 hours |
| 162 | + summary = "6 hours · single keystroke"; |
| 163 | + description = "Low-risk operations are fully reversible within the window. " + |
| 164 | + "All board mutations can be undone from the activity log."; |
| 165 | + break; |
| 166 | + } |
| 167 | + |
| 168 | + // If there are no operations, the proposal is a no-op |
| 169 | + if (operations.Count == 0) |
| 170 | + { |
| 171 | + summary = "6 hours · no operations"; |
| 172 | + description = "This proposal contains no operations and will have no effect."; |
| 173 | + windowMs = Reversibility.DefaultWindowMs; |
| 174 | + } |
| 175 | + |
| 176 | + return new Reversibility(summary, description, windowMs); |
| 177 | + } |
| 178 | +} |
0 commit comments