diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx
index 9981f6d..3635ed9 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 77960d8..7a34c25 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 31fd0ba..59d6685 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 0000000..cc322e0
--- /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();