Commit ac772a4
authored
fix(organizations): route user-deletion org cascade through the removal seam and make org removal atomic (#3971)
* fix(users): route sole-owner org deletion through the organization removal seam
users.service.js#remove() called OrganizationsRepository.remove() directly
when deleting a sole-owned org, bypassing organizations.crud.service.js#remove()
and its runOrganizationRemovedHandlers call — the onOrganizationRemoved seam
modules/tasks/tasks.init.js registers for org-scoped task cleanup. Org-scoped
tasks (and any future handler) were silently orphaned.
The cascade now delegates the full org teardown to
OrganizationsCrudService.remove() (lazy-imported to avoid a static cycle with
users.service.js, matching billing.init.js's existing pattern). A handler
error propagates out of the crud service by design (aborts that org's own
removal) but is caught + logged at this call site so it never aborts the
surrounding user deletion.
Centralized the co-member currentOrganization reassignment's null-org guard
(#3709) into organizations.crud.service.js#remove() since it's now the sole
implementation — the crud service's own version lacked it, which would have
crashed on a dangling membership once other callers started relying on this
path too.
Also fixes a latent test-fragility bug in tasks.integration.tests.js: two
"not a user's task" tests referenced a task orphaned by a sibling test's user
deletion, which only stayed queryable because of the very bug fixed here;
replaced with a properly isolated foreign-user fixture.
Closes #3965
Claude-Session: https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup
* fix(organizations): make org removal atomic so user deletion cannot orphan an org
Phase-0 BLOCK finding on #3965: users.service.js#remove() wrapped the ENTIRE
OrganizationsCrudService.remove() call in try/catch, then unconditionally
deleted the user. Because the crud remove() wiped memberships and reassigned
co-members BEFORE running onOrganizationRemoved handlers and the final
OrganizationsRepository.remove(), a handler failure anywhere in that cascade
was swallowed while the user still got deleted — a half-removed org (zombie
doc, or membership rows dangling on a deleted userId).
Fix (Option A — atomic reorder, chosen over narrowing the seam's error
posture per-caller): organizations.crud.service.js#remove() now removes the
org repository doc BEFORE running onOrganizationRemoved handlers, so once
removal starts the org always ends fully removed regardless of handler
outcome. Handler failures are caught + logged there for reconciliation
(never re-thrown) — same posture for every caller, not special-cased for
the user-deletion path. A STRUCTURAL failure (membership wipe, reassignment,
or the org repository delete itself) is not caught and still propagates.
Verified only two callers of the crud remove()/removeById() exist:
organizations.controller.js's admin delete-org endpoint and
users.service.js's sole-owner cascade — both go through the same function,
so no per-caller option/param was needed; relaxing the handler-error
posture to best-effort is safe for both.
users.service.js#remove() drops its blanket try/catch around the seam call:
handler failures are already swallowed inside the seam, so anything that
still escapes is a structural failure, which must propagate and abort the
whole user deletion (pre-diff behavior) rather than delete the user on top
of a genuinely broken teardown.
Tests: organizations.crud.orgRemoval.unit.tests.js now asserts a handler
throw is logged but the org is still removed, plus a new structural-failure
case that still propagates. users.service.remove.orgRemovalSeam.unit.tests.js
adds org-doc-removed assertions on handler throw (no zombie) and two new
structural-failure cases (org repo delete / membership wipe throwing) that
assert the user is NOT removed.
Claude-Session: https://claude.ai/code/session_01WfNC8bt1TgL4AsiYgCEGup
* fix(organizations): isolate org-removal cleanup handlers so one failure never skips the rest
Per-handler try/catch in runOrganizationRemovedHandlers: every handler runs
even if an earlier one throws (a bug in one module's cleanup can no longer
orphan another module's org-scoped rows). Failures are collected and re-raised
as a single AggregateError; the crud service logs each one individually for
reconciliation. Best-effort semantics and the atomic-by-ordering removal are
unchanged.
Addresses CodeRabbit review on #3971.1 parent 9e6bc2d commit ac772a4
8 files changed
Lines changed: 424 additions & 45 deletions
File tree
- modules
- organizations
- lib
- services
- tests
- tasks/tests
- users
- services
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
8 | 16 | | |
9 | 17 | | |
10 | 18 | | |
| |||
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
27 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
28 | 38 | | |
29 | 39 | | |
| 40 | + | |
30 | 41 | | |
31 | 42 | | |
| 43 | + | |
32 | 44 | | |
33 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
34 | 53 | | |
35 | 54 | | |
36 | 55 | | |
| |||
Lines changed: 44 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
196 | 207 | | |
197 | 208 | | |
198 | 209 | | |
| |||
205 | 216 | | |
206 | 217 | | |
207 | 218 | | |
208 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
209 | 223 | | |
210 | 224 | | |
211 | | - | |
212 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
213 | 228 | | |
214 | 229 | | |
215 | 230 | | |
216 | 231 | | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
221 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
222 | 254 | | |
223 | 255 | | |
224 | 256 | | |
| |||
Lines changed: 94 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | | - | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
64 | | - | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
69 | | - | |
70 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
71 | 131 | | |
72 | 132 | | |
73 | 133 | | |
74 | 134 | | |
75 | 135 | | |
76 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
77 | 166 | | |
Lines changed: 22 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
48 | 67 | | |
49 | 68 | | |
50 | 69 | | |
| |||
0 commit comments