From ac38a21f9a9d9adabc5c3b8f828bd899b7c060fa Mon Sep 17 00:00:00 2001 From: Marc Rejohn Castillano Date: Mon, 9 Mar 2026 13:37:25 +0800 Subject: [PATCH] Fixed attendance checkout postgrest unable to choose the best function candidate --- ...90000_fix_attendance_checkout_overload.sql | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 supabase/migrations/20260309090000_fix_attendance_checkout_overload.sql diff --git a/supabase/migrations/20260309090000_fix_attendance_checkout_overload.sql b/supabase/migrations/20260309090000_fix_attendance_checkout_overload.sql new file mode 100644 index 00000000..6ce81500 --- /dev/null +++ b/supabase/migrations/20260309090000_fix_attendance_checkout_overload.sql @@ -0,0 +1,39 @@ +-- Fix: drop the old 3-parameter overload of attendance_check_out. +-- The migration 20260308160000 added a 4-parameter version (with p_justification DEFAULT NULL) +-- but CREATE OR REPLACE does not replace a function with a different signature — +-- it created a second overload instead. When called with 3 args both overloads match, +-- causing "Could not choose the best candidate function". + +DROP FUNCTION IF EXISTS public.attendance_check_out(uuid, double precision, double precision); + +-- Re-ensure the 4-parameter version exists (idempotent). +CREATE OR REPLACE FUNCTION public.attendance_check_out( + p_attendance_id uuid, + p_lat double precision, + p_lng double precision, + p_justification text DEFAULT NULL +) RETURNS void +LANGUAGE plpgsql SECURITY DEFINER +AS $$ +DECLARE + v_log attendance_logs%ROWTYPE; +BEGIN + SELECT * INTO v_log FROM attendance_logs WHERE id = p_attendance_id; + IF NOT FOUND THEN + RAISE EXCEPTION 'Attendance log not found'; + END IF; + IF v_log.user_id != auth.uid() THEN + RAISE EXCEPTION 'Not your attendance log'; + END IF; + IF v_log.check_out_at IS NOT NULL THEN + RAISE EXCEPTION 'Already checked out'; + END IF; + + UPDATE attendance_logs + SET check_out_at = now(), + check_out_lat = p_lat, + check_out_lng = p_lng, + check_out_justification = p_justification + WHERE id = p_attendance_id; +END; +$$;