|
| 1 | +/** |
| 2 | + * The projects a person purge takes with it — specs/behaviors/person-lifecycle.md |
| 3 | + * → Purge: projects the person created (`createdById`, or `maintainerId` when |
| 4 | + * a project has no `createdById`) that nobody else touched, deleted with |
| 5 | + * everything that hangs off them. |
| 6 | + */ |
| 7 | +import type { Person, Project } from '@cfp/shared/schemas'; |
| 8 | + |
| 9 | +import type { InMemoryState } from '../store/memory/state.js'; |
| 10 | +import type { StateApply } from '../store/state-apply.js'; |
| 11 | +import type { DualStoreTx } from '../store/store.js'; |
| 12 | + |
| 13 | +function idsIn(index: Map<string, Set<string>>, key: string): string[] { |
| 14 | + return [...(index.get(key) ?? [])]; |
| 15 | +} |
| 16 | + |
| 17 | +/** Whether `person` created `project` — the field, else the maintainer for older records. */ |
| 18 | +function createdBy(project: Project, person: Person): boolean { |
| 19 | + return project.createdById ? project.createdById === person.id : project.maintainerId === person.id; |
| 20 | +} |
| 21 | + |
| 22 | +/** Whether anyone but `person` has a record on `project`. */ |
| 23 | +function touchedByOthers(state: InMemoryState, project: Project, person: Person): boolean { |
| 24 | + for (const id of idsIn(state.membershipsByProject, project.slug)) { |
| 25 | + if (state.projectMemberships.get(id)?.personSlug !== person.slug) return true; |
| 26 | + } |
| 27 | + for (const id of idsIn(state.updatesByProject, project.slug)) { |
| 28 | + if (state.projectUpdates.get(id)?.authorId !== person.id) return true; |
| 29 | + } |
| 30 | + for (const id of idsIn(state.buzzByProject, project.slug)) { |
| 31 | + if (state.projectBuzz.get(id)?.postedById !== person.id) return true; |
| 32 | + } |
| 33 | + for (const roleId of idsIn(state.helpWantedByProject, project.slug)) { |
| 34 | + if (state.helpWantedRoles.get(roleId)?.postedById !== person.id) return true; |
| 35 | + for (const id of idsIn(state.interestByRole, roleId)) { |
| 36 | + if (state.helpWantedInterest.get(id)?.personSlug !== person.slug) return true; |
| 37 | + } |
| 38 | + } |
| 39 | + for (const rsvp of state.eventRsvps.values()) { |
| 40 | + if (rsvp.projectId === project.id && rsvp.personSlug !== person.slug) return true; |
| 41 | + } |
| 42 | + return false; |
| 43 | +} |
| 44 | + |
| 45 | +/** Live or soft-deleted projects a purge of `person` deletes, by title. */ |
| 46 | +export function purgeableProjects(state: InMemoryState, person: Person): Project[] { |
| 47 | + return [...state.projects.values()] |
| 48 | + .filter((p) => createdBy(p, person) && !touchedByOthers(state, p, person)) |
| 49 | + .sort((a, b) => a.title.localeCompare(b.title)); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Hard-delete `project` and every record on it inside `tx`. Records already |
| 54 | + * deleted earlier in the same transaction (ids in `deleted`) are skipped, and |
| 55 | + * every id this deletes is added, so the person cascade can skip them too. |
| 56 | + */ |
| 57 | +export async function purgeProject( |
| 58 | + tx: DualStoreTx, |
| 59 | + state: InMemoryState, |
| 60 | + stateApply: StateApply, |
| 61 | + project: Project, |
| 62 | + deleted: Set<string>, |
| 63 | +): Promise<void> { |
| 64 | + const once = async (id: string, del: () => Promise<void>) => { |
| 65 | + if (deleted.has(id)) return; |
| 66 | + deleted.add(id); |
| 67 | + await del(); |
| 68 | + }; |
| 69 | + |
| 70 | + for (const id of idsIn(state.membershipsByProject, project.slug)) { |
| 71 | + const m = state.projectMemberships.get(id); |
| 72 | + if (m) await once(m.id, async () => { await tx.public['project-memberships'].delete(m); stateApply.removeMembership(m); }); |
| 73 | + } |
| 74 | + for (const id of idsIn(state.updatesByProject, project.slug)) { |
| 75 | + const u = state.projectUpdates.get(id); |
| 76 | + if (u) await once(u.id, async () => { await tx.public['project-updates'].delete(u); stateApply.removeProjectUpdate(u); }); |
| 77 | + } |
| 78 | + for (const id of idsIn(state.buzzByProject, project.slug)) { |
| 79 | + const b = state.projectBuzz.get(id); |
| 80 | + if (b) await once(b.id, async () => { await tx.public['project-buzz'].delete(b); stateApply.removeProjectBuzz(b); }); |
| 81 | + } |
| 82 | + for (const roleId of idsIn(state.helpWantedByProject, project.slug)) { |
| 83 | + for (const id of idsIn(state.interestByRole, roleId)) { |
| 84 | + const e = state.helpWantedInterest.get(id); |
| 85 | + if (e) await once(e.id, async () => { await tx.public['help-wanted-interest'].delete(e); stateApply.removeInterest(e); }); |
| 86 | + } |
| 87 | + await deleteTagAssignments(tx, state, stateApply, roleId, once); |
| 88 | + const r = state.helpWantedRoles.get(roleId); |
| 89 | + if (r) await once(r.id, async () => { await tx.public['help-wanted-roles'].delete(r); stateApply.removeHelpWantedRole(r); }); |
| 90 | + } |
| 91 | + for (const rsvp of [...state.eventRsvps.values()]) { |
| 92 | + if (rsvp.projectId !== project.id) continue; |
| 93 | + await once(rsvp.id, async () => { await tx.public['event-rsvps'].delete(rsvp); stateApply.removeEventRsvp(rsvp); }); |
| 94 | + } |
| 95 | + await deleteTagAssignments(tx, state, stateApply, project.id, once); |
| 96 | + // Deleting the record removes its attachment folder with it. |
| 97 | + await once(project.id, async () => { |
| 98 | + await tx.public.projects.delete(project); |
| 99 | + stateApply.removeProject(project.id, project.slug); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +async function deleteTagAssignments( |
| 104 | + tx: DualStoreTx, |
| 105 | + state: InMemoryState, |
| 106 | + stateApply: StateApply, |
| 107 | + taggableId: string, |
| 108 | + once: (id: string, del: () => Promise<void>) => Promise<void>, |
| 109 | +): Promise<void> { |
| 110 | + for (const id of idsIn(state.tagAssignmentsByTaggable, taggableId)) { |
| 111 | + const ta = state.tagAssignments.get(id); |
| 112 | + if (ta) await once(ta.id, async () => { await tx.public['tag-assignments'].delete(ta); stateApply.removeTagAssignment(ta); }); |
| 113 | + } |
| 114 | +} |
0 commit comments