fix(handover): keep a claimed handover when its Guardian deletes their account

created_by_id cascaded, so a Guardian who deleted their own account took
every handover they had issued with them, including a claimed one whose
decision the new owner had not made yet. The claim had already applied
the proposal, possibly MANAGE for a co-Guardian, and the owner lost the
one place that asks them to confirm it.

The column is nullable now and cleared on delete (migration 0390). An
unclaimed link without a creator is refused like any link whose creator
lost access. A decision that names a Guardian whose account is gone is
refused with 422 instead of failing on a grant for a missing account.
This commit is contained in:
Marc-André Bombeck committed 2026-10-08 22:37:56 +02:00
1 parent c7cee9c8a7
commit 98183700ce
4 files changed
+94 -6

No files matched your search

+31 -2
View File
@@ -199,9 +199,12 @@ function proposalFor(state: HandoverState, grantId: string): HandoverAccess {
async function isActiveGuardian(
db: Pick<Transaction, "accountGrant">,
profileId: string,
guardianId: string,
guardianId: string | null,
now: Date,
): Promise<boolean> {
// A link whose creator's account is gone (`created_by_id` cleared) has no
// Guardian behind it.
if (guardianId === null) return false;
const grant = await db.accountGrant.findFirst({
where: {
grantorId: profileId,
@@ -793,7 +796,23 @@ export async function decideHandover(input: {
throw new HandoverError("no_pending");
}
const claim = state.claim;
const known = new Set(claim.guardians.map((e) => e.grantId));
// A former Guardian whose account is gone has nothing left to decide:
// the pending read leaves them out, and a decision that names them
// anyway is refused like one about a stranger, rather than failing on
// a grant written for an account that no longer exists.
const present = new Set(
(
await tx.user.findMany({
where: { id: { in: claim.guardians.map((e) => e.guardianId) } },
select: { id: true },
})
).map((u) => u.id),
);
const known = new Set(
claim.guardians
.filter((e) => present.has(e.guardianId))
.map((e) => e.grantId),
);
if (input.decisions.some((d) => !known.has(d.grantId))) {
throw new HandoverError("unknown_guardian");
}
@@ -807,6 +826,16 @@ export async function decideHandover(input: {
}[] = [];
const guardians: StoredClaimEntry[] = [];
for (const entry of claim.guardians) {
if (!present.has(entry.guardianId)) {
guardians.push(entry);
outcome.push({
grantId: entry.grantId,
access: entry.applied,
changed: false,
skipped: true,
});
continue;
}
const target =
input.decisions.find((d) => d.grantId === entry.grantId)?.access ??
entry.applied;