A new member who joins via invite code now walks through an admin-defined onboarding sequence before reaching the app. Schema (migration 022, folded into schema.sql Section 14): - space_onboarding_steps: step_key, step_type (welcome|code_of_conduct|profile|payment|content), title, body, config jsonb, is_enabled, is_required, is_system, sort_order, unique per (space, step_key). Full RLS: members read, admin/board write, admin delete non-system. - space_members.onboarding_completed_at + onboarding_progress jsonb. - Trigger seeds four default steps per space (welcome, code of conduct [required], profile, dues). Backfill seeds existing spaces and marks all existing members complete so the new gate never traps them. Flow: - /onboarding (top-level route, outside the (app) group so no redirect loop). Progress bar, step counter, back/continue, per-type rendering. Profile step calls updateMyProfile; payment step is a non-blocking nudge with optional link and "Remind me later". - (app)/layout.tsx redirects to /onboarding while onboarding_completed_at is null. - createSpace marks the founder complete: founders configure the space, they do not run the member flow (matches admin-vs-end-user persona research). - finishOnboarding re-checks required steps server-side so a tampered client cannot skip a required code-of-conduct acknowledgement. skipOnboarding only works when nothing is required. Admin config: - Settings -> Onboarding tab: reorder, edit title/body/payment link, toggle enabled/required, add custom content steps, delete non-system steps. Safety: - components/safe-markdown.tsx renders admin-authored markdown and a sanitized subset of HTML (rehype-raw + rehype-sanitize). Added those two deps. Also: - bio added to updateMyProfileSchema (members can self-set it; not blocked by the migration-015 trigger). - docs/UX_AND_PERSONAS.md: 4 personas and prioritized UX audit findings. Onboarding was finding #1; 2-6 deferred and tracked. 266/266 unit tests pass. Production build clean, /onboarding in the route table. Migration 022 applies idempotently on the next deploy.
213 lines
7.0 KiB
TypeScript
213 lines
7.0 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { requireMember, requireMemberWithRole, parseInput } from '@/lib/auth-helpers'
|
|
import {
|
|
createOnboardingStepSchema,
|
|
updateOnboardingStepSchema,
|
|
uuidSchema,
|
|
} from '@/lib/validations'
|
|
|
|
const ONBOARDING_ADMIN_ROLES = ['admin', 'board'] as const
|
|
|
|
// --- Admin: manage steps -----------------------------------------------------
|
|
|
|
export async function createOnboardingStep(input: {
|
|
step_type: 'welcome' | 'code_of_conduct' | 'profile' | 'payment' | 'content'
|
|
title: string
|
|
body?: string | null
|
|
config?: Record<string, unknown>
|
|
is_enabled?: boolean
|
|
is_required?: boolean
|
|
sort_order?: number
|
|
}) {
|
|
const v = parseInput(createOnboardingStepSchema, input)
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ONBOARDING_ADMIN_ROLES, 'Admin or board access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
// Custom steps get a generated key; built-in keys are reserved for the
|
|
// seeded rows so they stay addressable.
|
|
const stepKey = `custom-${crypto.randomUUID().slice(0, 12)}`
|
|
|
|
const { data, error } = await supabase
|
|
.from('space_onboarding_steps')
|
|
.insert({
|
|
space_id: member.space_id,
|
|
step_key: stepKey,
|
|
step_type: v.data.step_type,
|
|
title: v.data.title,
|
|
body: v.data.body ?? null,
|
|
config: v.data.config ?? {},
|
|
is_enabled: v.data.is_enabled ?? true,
|
|
is_required: v.data.is_required ?? false,
|
|
is_system: false,
|
|
sort_order: v.data.sort_order ?? 100,
|
|
})
|
|
.select('id')
|
|
.single()
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/settings')
|
|
return { id: data.id }
|
|
}
|
|
|
|
export async function updateOnboardingStep(stepId: string, updates: {
|
|
title?: string
|
|
body?: string | null
|
|
config?: Record<string, unknown>
|
|
is_enabled?: boolean
|
|
is_required?: boolean
|
|
sort_order?: number
|
|
}) {
|
|
const idCheck = parseInput(uuidSchema, stepId)
|
|
if (!idCheck.ok) return { error: 'Invalid step ID' }
|
|
const v = parseInput(updateOnboardingStepSchema, updates)
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ONBOARDING_ADMIN_ROLES, 'Admin or board access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const patch: Record<string, unknown> = {}
|
|
if (v.data.title !== undefined) patch.title = v.data.title
|
|
if (v.data.body !== undefined) patch.body = v.data.body
|
|
if (v.data.config !== undefined) patch.config = v.data.config
|
|
if (v.data.is_enabled !== undefined) patch.is_enabled = v.data.is_enabled
|
|
if (v.data.is_required !== undefined) patch.is_required = v.data.is_required
|
|
if (v.data.sort_order !== undefined) patch.sort_order = v.data.sort_order
|
|
|
|
const { error } = await supabase
|
|
.from('space_onboarding_steps')
|
|
.update(patch)
|
|
.eq('id', idCheck.data)
|
|
.eq('space_id', member.space_id)
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/settings')
|
|
return { success: true as const }
|
|
}
|
|
|
|
export async function deleteOnboardingStep(stepId: string) {
|
|
const v = parseInput(uuidSchema, stepId)
|
|
if (!v.ok) return { error: 'Invalid step ID' }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ['admin'], 'Admin access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
// RLS also blocks deleting is_system rows; surface a clear message.
|
|
const { error, count } = await supabase
|
|
.from('space_onboarding_steps')
|
|
.delete({ count: 'exact' })
|
|
.eq('id', v.data)
|
|
.eq('space_id', member.space_id)
|
|
|
|
if (error) return { error: error.message }
|
|
if (count === 0) return { error: 'Built-in steps cannot be deleted. Disable it instead.' }
|
|
revalidatePath('/settings')
|
|
return { success: true as const }
|
|
}
|
|
|
|
// --- Member: progress through the flow ---------------------------------------
|
|
|
|
export async function markOnboardingStepDone(stepId: string) {
|
|
const v = parseInput(uuidSchema, stepId)
|
|
if (!v.ok) return { error: 'Invalid step ID' }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { data: row } = await supabase
|
|
.from('space_members')
|
|
.select('onboarding_progress')
|
|
.eq('id', member.id)
|
|
.single()
|
|
|
|
const progress = (row?.onboarding_progress ?? {}) as { completed_step_ids?: string[] }
|
|
const done = new Set(progress.completed_step_ids ?? [])
|
|
done.add(v.data)
|
|
|
|
const { error } = await supabase
|
|
.from('space_members')
|
|
.update({ onboarding_progress: { ...progress, completed_step_ids: Array.from(done) } })
|
|
.eq('id', member.id)
|
|
|
|
if (error) return { error: error.message }
|
|
return { success: true as const }
|
|
}
|
|
|
|
// Completes onboarding. Server-side double-check: every enabled+required step
|
|
// must be in completed_step_ids, so a tampered client cannot skip a required
|
|
// code-of-conduct acknowledgement.
|
|
export async function finishOnboarding() {
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { data: steps } = await supabase
|
|
.from('space_onboarding_steps')
|
|
.select('id, is_required, is_enabled')
|
|
.eq('space_id', member.space_id)
|
|
.eq('is_enabled', true)
|
|
|
|
const { data: row } = await supabase
|
|
.from('space_members')
|
|
.select('onboarding_progress')
|
|
.eq('id', member.id)
|
|
.single()
|
|
|
|
const progress = (row?.onboarding_progress ?? {}) as { completed_step_ids?: string[] }
|
|
const done = new Set(progress.completed_step_ids ?? [])
|
|
const requiredMissing = (steps ?? []).filter(s => s.is_required && !done.has(s.id))
|
|
if (requiredMissing.length > 0) {
|
|
return { error: 'Please complete the required steps before finishing.' }
|
|
}
|
|
|
|
const { error } = await supabase
|
|
.from('space_members')
|
|
.update({ onboarding_completed_at: new Date().toISOString() })
|
|
.eq('id', member.id)
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/dashboard')
|
|
return { success: true as const }
|
|
}
|
|
|
|
// Skip is only honored when no enabled step is required.
|
|
export async function skipOnboarding() {
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { count } = await supabase
|
|
.from('space_onboarding_steps')
|
|
.select('*', { count: 'exact', head: true })
|
|
.eq('space_id', member.space_id)
|
|
.eq('is_enabled', true)
|
|
.eq('is_required', true)
|
|
|
|
if ((count ?? 0) > 0) {
|
|
return { error: 'This space requires you to complete onboarding.' }
|
|
}
|
|
|
|
const { error } = await supabase
|
|
.from('space_members')
|
|
.update({ onboarding_completed_at: new Date().toISOString() })
|
|
.eq('id', member.id)
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/dashboard')
|
|
return { success: true as const }
|
|
}
|