feat(forms): link prior submissions on member add + email change
addMember and updateMember (when email set/changed) now call linkSubmissionsByEmail so an admin-established member email retroactively picks up earlier unlinked submissions. Service client (form_submissions has no client write policy), space-scoped.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { revalidatePath } from 'next/cache'
|
import { revalidatePath } from 'next/cache'
|
||||||
import { createClient } from '@/lib/supabase/server'
|
import { createClient } from '@/lib/supabase/server'
|
||||||
|
import { createAdminClient } from '@/lib/supabase/admin'
|
||||||
|
import { linkSubmissionsByEmail } from './forms'
|
||||||
import {
|
import {
|
||||||
requireMember,
|
requireMember,
|
||||||
requireMemberWithRole,
|
requireMemberWithRole,
|
||||||
@@ -54,6 +56,13 @@ export async function addMember(formData: {
|
|||||||
.single()
|
.single()
|
||||||
|
|
||||||
if (error) return { error: error.message }
|
if (error) return { error: error.message }
|
||||||
|
|
||||||
|
// Associate any prior unlinked form submissions for this email with the
|
||||||
|
// new member (admin-established email).
|
||||||
|
if (v.data.email) {
|
||||||
|
await linkSubmissionsByEmail(createAdminClient(), self.space_id, data.id as string, v.data.email)
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/members')
|
revalidatePath('/members')
|
||||||
return { data }
|
return { data }
|
||||||
}
|
}
|
||||||
@@ -89,6 +98,12 @@ export async function updateMember(
|
|||||||
.eq('space_id', self.space_id)
|
.eq('space_id', self.space_id)
|
||||||
|
|
||||||
if (error) return { error: error.message }
|
if (error) return { error: error.message }
|
||||||
|
|
||||||
|
// If the email was set/corrected, associate matching prior submissions.
|
||||||
|
if (patch.email) {
|
||||||
|
await linkSubmissionsByEmail(createAdminClient(), self.space_id, id, patch.email)
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/members')
|
revalidatePath('/members')
|
||||||
return { success: true as const }
|
return { success: true as const }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user