import type { FaunosteoClient } from './client' import type { Tables, TablesUpdate } from './database.types' export type Profile = Tables<'profiles'> /** Columns the signed-in user may change (column grant in the profiles migrations). */ export type OwnProfilePatch = Pick< TablesUpdate<'profiles'>, | 'display_name' | 'first_name' | 'last_name' | 'phone' | 'locale' | 'timezone' | 'practice_name' | 'professional_title' | 'siret' > export class ProfileError extends Error { readonly code: string | null constructor(message: string, code: string | null, cause?: unknown) { super(message, { cause }) this.name = 'ProfileError' this.code = code } } /** The row RLS lets the user see: their own. Null when the trigger has not run yet. */ export async function fetchOwnProfile( client: FaunosteoClient, userId: string, ): Promise { const { data, error } = await client .from('profiles') .select('*') .eq('id', userId) .maybeSingle() if (error) throw new ProfileError(error.message, error.code, error) return data } export async function updateOwnProfile( client: FaunosteoClient, userId: string, patch: OwnProfilePatch, ): Promise { const { data, error } = await client .from('profiles') .update(patch) .eq('id', userId) .select('*') .single() if (error) throw new ProfileError(error.message, error.code, error) return data }