From f9e886e12d22f970df408fdf3bb2b18af31bbcdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Ben=C3=ADtez?= Date: Tue, 7 Jul 2026 18:18:07 -0300 Subject: [PATCH] =?UTF-8?q?feat(area-repo):=20repositorio=20CRUD=20de=20?= =?UTF-8?q?=C3=A1reas=20por=20organizaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- src/persistence/supabase/area-repo.ts | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/persistence/supabase/area-repo.ts 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 + }, +}