From fe2220f485e286f72d4af513819155df29177fb1 Mon Sep 17 00:00:00 2001 From: Pinx0 Date: Sun, 10 Nov 2024 12:04:39 +0100 Subject: [PATCH] feat: move count increase to a trigger --- src/app/casos-activos/solicitudes/page.tsx | 14 +++---- src/components/Toggle.tsx | 31 +++++++-------- src/lib/service.ts | 38 ------------------- ...update help request totals via trigger.sql | 23 +++++++++++ 4 files changed, 46 insertions(+), 60 deletions(-) create mode 100644 supabase/migrations/20241110101532_update help request totals via trigger.sql diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx index 9981f6d6..3635ed99 100644 --- a/src/app/casos-activos/solicitudes/page.tsx +++ b/src/app/casos-activos/solicitudes/page.tsx @@ -190,13 +190,13 @@ function Solicitudes() { ))} - {/*
- -
*/} +
+ +
diff --git a/src/components/Toggle.tsx b/src/components/Toggle.tsx index 77960d80..7a34c25b 100644 --- a/src/components/Toggle.tsx +++ b/src/components/Toggle.tsx @@ -8,26 +8,27 @@ export const Toggle = ({ checked, handleChange, label }: ToggleProps) => { -
- -
+
); }; diff --git a/src/lib/service.ts b/src/lib/service.ts index 31fd0bad..59d66856 100644 --- a/src/lib/service.ts +++ b/src/lib/service.ts @@ -70,49 +70,11 @@ export const helpRequestService = { async assign(requestData: HelpRequestAssignmentInsert) { const { data, error } = await supabase.from('help_request_assignments').insert([requestData]).select(); if (error) throw error; - - const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase - .from('help_requests') - .select('*') - .eq('id', requestData.help_request_id); - if (errorGettingLinkedData) throw errorGettingLinkedData; - if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea'); - - const { error: errorUpdatingAssigneesCount } = await supabase - .from('help_requests') - .update({ asignees_count: linkedRequestData[0].asignees_count + 1 }) - .eq('id', requestData.help_request_id); - if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount; - return data[0]; }, async unassign(id: number) { - const { data, error: errorFindingRow } = await supabase.from('help_request_assignments').select('*').eq('id', id); - if (errorFindingRow || !data) { - throw new Error('No se puede encontrar la tarea'); - } - - const requestId = data[0].help_request_id; - const { error: errorDeletingAssignment } = await supabase.from('help_request_assignments').delete().eq('id', id); if (errorDeletingAssignment) throw errorDeletingAssignment; - - const { data: linkedRequestData, error: errorGettingLinkedData } = await supabase - .from('help_requests') - .select('*') - .eq('id', requestId); - - if (errorGettingLinkedData) throw errorGettingLinkedData; - if (!linkedRequestData) throw new Error('No se puede encontrar esta tarea'); - - const { asignees_count } = linkedRequestData[0]; - const newNumberAssignees = asignees_count <= 0 ? 0 : asignees_count - 1; - - const { error: errorUpdatingAssigneesCount } = await supabase - .from('help_requests') - .update({ asignees_count: newNumberAssignees }) - .eq('id', requestId); - if (errorUpdatingAssigneesCount) throw errorUpdatingAssigneesCount; }, async getByType(type: any) { diff --git a/supabase/migrations/20241110101532_update help request totals via trigger.sql b/supabase/migrations/20241110101532_update help request totals via trigger.sql new file mode 100644 index 00000000..cc322e0d --- /dev/null +++ b/supabase/migrations/20241110101532_update help request totals via trigger.sql @@ -0,0 +1,23 @@ +CREATE OR REPLACE FUNCTION update_asignees_count() +RETURNS TRIGGER AS $$ +BEGIN + RAISE NOTICE 'Trigger executed on operation: %, help_request_id: %', TG_OP, COALESCE(NEW.help_request_id, OLD.help_request_id); + + IF TG_OP = 'INSERT' THEN + -- Increment assignments_count on assignment add + UPDATE public.help_requests + SET asignees_count = asignees_count + 1 + WHERE id = NEW.help_request_id; + ELSIF TG_OP = 'DELETE' THEN + -- Decrement assignments_count on assignment removal + UPDATE public.help_requests + SET asignees_count = asignees_count - 1 + WHERE id = OLD.help_request_id; + END IF; + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER trigger_update_assignments_count +AFTER INSERT OR DELETE ON public.help_request_assignments +FOR EACH ROW EXECUTE FUNCTION update_asignees_count();