diff --git a/src/persistence/supabase/area-repo.ts b/src/persistence/supabase/area-repo.ts new file mode 100644 index 0000000..86910c2 --- /dev/null +++ b/src/persistence/supabase/area-repo.ts @@ -0,0 +1,59 @@ +import { supabase } from '@/lib/supabase' +import type { Area } from '@/domain/types' + +function fromRow(row: Record): Area { + return { + id: row.id as string, + orgId: row.org_id as string, + name: row.name as string, + description: (row.description as string | null) ?? null, + createdAt: new Date(row.created_at as string).getTime(), + } +} + +export const supabaseAreaRepo = { + async getByOrg(orgId: string): Promise { + const { data, error } = await supabase + .from('areas') + .select('*') + .eq('org_id', orgId) + .order('name', { ascending: true }) + if (error) throw error + return (data ?? []).map(fromRow) + }, + + async create(data: { orgId: string; name: string; description?: string }): Promise { + const { data: row, error } = await supabase + .from('areas') + .insert({ + org_id: data.orgId, + name: data.name, + description: data.description ?? null, + }) + .select('*') + .single() + if (error) throw error + return fromRow(row as Record) + }, + + async update(id: string, data: { name?: string; description?: string }): Promise { + const { data: row, error } = await supabase + .from('areas') + .update({ + ...(data.name !== undefined && { name: data.name }), + ...(data.description !== undefined && { description: data.description }), + updated_at: new Date().toISOString(), + }) + .eq('id', id) + .select('*') + .single() + if (error) throw error + return fromRow(row as Record) + }, + + // Los procesos con esta área pasan a area_id = null (ON DELETE SET NULL en migración 017) + async delete(id: string): Promise { + const { error } = await supabase.from('areas').delete().eq('id', id) + if (error) throw error + }, +}