Per-file type-only fixes surfaced after the Supabase client was correctly typed. All fixes preserve runtime behavior; no any/ts-ignore/ts-expect-error. - lib/supabase/client.ts: annotate the browser client as SupabaseClient<Database> (same ssr/js drift as server.ts; fixes it centrally for all callers). - Supabase embedded-select joins whose FK metadata is missing from the generated types are bridged with `as unknown as <target>` + a TODO to remove after regenerating types/database.ts. - setState optimistic-update variance and raw <select> enum values cast to the precise element/union type; .insert payload fields (Json, enums) given the precise column type via satisfies/field casts. - Added narrowing guards where a value was legitimately nullable (comms channel, public form page) without changing the intended path. The 14 remaining errors are confined to the four files fixed in Phase B (real bugs + the tautological actions.test rewrite). 639 tests still green. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { requireMemberWithRole } from '@/lib/auth-helpers'
|
|
import { ADMIN_ROLES } from '@/lib/permissions'
|
|
import { importMembersSchema } from '@/lib/validations'
|
|
import type { TablesInsert } from '@/types/database'
|
|
|
|
export async function importMembers(rows: unknown) {
|
|
if (!Array.isArray(rows) || rows.length === 0) {
|
|
return { error: 'No rows to import' }
|
|
}
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ADMIN_ROLES, 'Admin access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
// Validate per row so one bad row does not reject the whole file; the
|
|
// schema lowercases email and normalizes dates via flexibleDateTime.
|
|
const rowSchema = importMembersSchema.element
|
|
const valid: Array<TablesInsert<'space_members'>> = []
|
|
let skipped = 0
|
|
for (const raw of rows) {
|
|
const r = rowSchema.safeParse(raw)
|
|
if (!r.success) { skipped++; continue }
|
|
valid.push({
|
|
space_id: member.space_id,
|
|
display_name: r.data.display_name,
|
|
email: r.data.email,
|
|
phone: r.data.phone ?? null,
|
|
tier: r.data.tier ?? 'basic',
|
|
role: 'member',
|
|
status: 'current',
|
|
approved: true,
|
|
joined_at: r.data.joined_at ?? new Date().toISOString(),
|
|
last_paid_at: r.data.last_paid_at ?? null,
|
|
has_card_access: r.data.has_card_access ?? false,
|
|
})
|
|
}
|
|
|
|
if (valid.length === 0) {
|
|
return { error: `No valid rows to import (${skipped} skipped: bad email, name, tier, or date).` }
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('space_members')
|
|
.upsert(valid, { onConflict: 'space_id,email', ignoreDuplicates: false })
|
|
.select()
|
|
|
|
if (error) return { error: error.message }
|
|
revalidatePath('/members')
|
|
return { data, count: data.length, skipped }
|
|
}
|