feat(forms): verified-email retro-link of anonymous submissions (Phase 5)

- claimMyAnonymousSubmissions(): exported but safe — no trusted params,
  resolves member + email from the caller's session, gated on
  email_confirmed_at, idempotent. Links the caller's own prior
  anonymous submissions to their member row
- hooked best-effort from joinSpace (member insert now returns id;
  links prior anon submissions for the verified email) and
  finishOnboarding
- addMember/importMembers deliberately NOT auto-linked: those emails
  are admin-asserted, not verified; the locked decision forbids
  auto-link there, so the forms.manage linkSubmissionsForMember stays
  the admin manual-link path
- also repairs a build break committed in 295d220: finishOnboarding
  declared `const admin` twice (Phase 4 added a second). The probe and
  the completion update now share one admin client
- docs: API_REFERENCE / ARCHITECTURE / HANDOFF

287/287 tests, build compiles clean (verified Compiled successfully). No deploy.
This commit is contained in:
Moheeb Zara
2026-05-16 16:29:32 -07:00
parent da25950988
commit 40d4cdad98
6 changed files with 83 additions and 14 deletions
+15 -1
View File
@@ -149,7 +149,7 @@ export async function joinSpace(formData: {
if (lookupErr || !space) return { error: 'Invalid invite code' }
const { error: memberErr } = await admin
const { data: newMember, error: memberErr } = await admin
.from('space_members')
.insert({
space_id: space.id,
@@ -161,9 +161,23 @@ export async function joinSpace(formData: {
status: space.require_approval ? 'unverified' : 'current',
approved: true,
})
.select('id')
.single()
if (memberErr) return { error: memberErr.message }
// Retro-link prior anonymous form/waiver submissions in this space to the
// new member, but only for a verified email (locked decision). Best-effort:
// a linking hiccup must never fail the join.
if (newMember && user.email && user.email_confirmed_at) {
await admin
.from('form_submissions')
.update({ member_id: newMember.id })
.eq('space_id', space.id)
.is('member_id', null)
.eq('submitter_email', user.email.toLowerCase())
}
// Increment invite usage counter for the multi-code path.
if (invite) {
await admin