|
| 1 | +import type { User } from '@supabase/supabase-js' |
| 2 | +import supabase, { isSupabaseConfigured } from '@/lib/supabase' |
| 3 | + |
| 4 | +export type ProfileRecord = { |
| 5 | + id: string |
| 6 | + full_name?: string | null |
| 7 | + avatar_url?: string | null |
| 8 | + bio: string | null |
| 9 | + website: string | null |
| 10 | + github_url: string | null |
| 11 | + x_url: string | null |
| 12 | + updated_at?: string | null |
| 13 | +} |
| 14 | + |
| 15 | +function githubUrlFromMetadata(metadata: Record<string, unknown> | null): string | null { |
| 16 | + if (!metadata) return null |
| 17 | + const direct = metadata.html_url |
| 18 | + if (typeof direct === 'string' && direct.startsWith('http')) return direct |
| 19 | + const handleEntry = [metadata.user_name, metadata.preferred_username, metadata.nickname].find( |
| 20 | + (value) => typeof value === 'string' && value.length > 0, |
| 21 | + ) |
| 22 | + if (typeof handleEntry === 'string' && handleEntry.length > 0) { |
| 23 | + return `https://github.com/${handleEntry}` |
| 24 | + } |
| 25 | + return null |
| 26 | +} |
| 27 | + |
| 28 | +export function profileDefaultsFromMetadata(user: User) { |
| 29 | + const metadata = (user.user_metadata as Record<string, unknown> | null) ?? null |
| 30 | + const fullName = typeof metadata?.full_name === 'string' ? metadata.full_name : null |
| 31 | + const avatarUrl = typeof metadata?.avatar_url === 'string' ? metadata.avatar_url : null |
| 32 | + const githubUrl = githubUrlFromMetadata(metadata) |
| 33 | + return { full_name: fullName, avatar_url: avatarUrl, github_url: githubUrl } |
| 34 | +} |
| 35 | + |
| 36 | +export function mapProfileRow(row: unknown): ProfileRecord | null { |
| 37 | + if (!row || typeof row !== 'object') return null |
| 38 | + const record = row as Record<string, unknown> |
| 39 | + const idRaw = record.id |
| 40 | + if (typeof idRaw !== 'string') return null |
| 41 | + return { |
| 42 | + id: idRaw, |
| 43 | + full_name: typeof record.full_name === 'string' ? record.full_name : null, |
| 44 | + avatar_url: typeof record.avatar_url === 'string' ? record.avatar_url : null, |
| 45 | + bio: (record.bio ?? null) as string | null, |
| 46 | + website: (record.website ?? null) as string | null, |
| 47 | + github_url: (record.github_url ?? null) as string | null, |
| 48 | + x_url: (record.x_url ?? null) as string | null, |
| 49 | + updated_at: (record.updated_at ?? null) as string | null, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Ensure the current user has a profile row. Returns the created or existing profile. |
| 55 | + */ |
| 56 | +export async function ensureProfileRecord(user: User): Promise<ProfileRecord | null> { |
| 57 | + if (!user || !isSupabaseConfigured || !supabase) return null |
| 58 | + |
| 59 | + const defaults = profileDefaultsFromMetadata(user) |
| 60 | + |
| 61 | + const payload = { |
| 62 | + id: user.id, |
| 63 | + full_name: defaults.full_name, |
| 64 | + avatar_url: defaults.avatar_url, |
| 65 | + github_url: defaults.github_url, |
| 66 | + } |
| 67 | + |
| 68 | + const selectColumns = 'id, full_name, avatar_url, bio, website, github_url, x_url, updated_at' |
| 69 | + |
| 70 | + const insertResult = await supabase |
| 71 | + .from('profiles') |
| 72 | + .insert(payload) |
| 73 | + .select(selectColumns) |
| 74 | + .maybeSingle() |
| 75 | + |
| 76 | + if (insertResult.error) { |
| 77 | + if (insertResult.error.code === '23505') { |
| 78 | + const existing = await supabase |
| 79 | + .from('profiles') |
| 80 | + .select(selectColumns) |
| 81 | + .eq('id', user.id) |
| 82 | + .maybeSingle() |
| 83 | + if (existing.error) throw existing.error |
| 84 | + return mapProfileRow(existing.data) |
| 85 | + } |
| 86 | + throw insertResult.error |
| 87 | + } |
| 88 | + |
| 89 | + return mapProfileRow(insertResult.data) |
| 90 | +} |
0 commit comments