From 98f5782f26660299722459003c66b86699b960c9 Mon Sep 17 00:00:00 2001 From: lorinjameson Date: Fri, 18 Oct 2024 16:17:53 -0400 Subject: [PATCH] Fix for archiving active document --- .../functions/archive_document_rpc.sql | 2 +- ...0241018201234_fix_archive_document_spc.sql | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 supabase/migrations/20241018201234_fix_archive_document_spc.sql diff --git a/SQL Scripts/functions/archive_document_rpc.sql b/SQL Scripts/functions/archive_document_rpc.sql index 2f67274..8693847 100644 --- a/SQL Scripts/functions/archive_document_rpc.sql +++ b/SQL Scripts/functions/archive_document_rpc.sql @@ -17,7 +17,7 @@ BEGIN -- If the user is the creator or an Org Admin, archive the document IF _row.created_by = auth.uid() OR is_admin_organization(auth.uid()) THEN - IF NOT EXISTS(SELECT 1 FROM public.project_documents pd WHERE pd.id = _document_id AND pd.is_archived IS FALSE ) + IF NOT EXISTS(SELECT 1 FROM public.project_documents pd WHERE pd.document_id = _document_id AND pd.is_archived IS FALSE ) THEN UPDATE public.documents d SET is_archived = TRUE diff --git a/supabase/migrations/20241018201234_fix_archive_document_spc.sql b/supabase/migrations/20241018201234_fix_archive_document_spc.sql new file mode 100644 index 0000000..6974c64 --- /dev/null +++ b/supabase/migrations/20241018201234_fix_archive_document_spc.sql @@ -0,0 +1,38 @@ +set check_function_bodies = off; + +CREATE OR REPLACE FUNCTION public.archive_document_rpc(_document_id uuid) + RETURNS boolean + LANGUAGE plpgsql + SECURITY DEFINER +AS $function$ +DECLARE + _row public.documents % rowtype; +BEGIN + -- Check project policy that project documents can be updated by this user + IF NOT (check_action_policy_organization(auth.uid(), 'documents', 'UPDATE')) + THEN + RETURN FALSE; + END IF; + + -- Get the document + SELECT * INTO _row FROM public.documents d WHERE d.id = _document_id; + + -- If the user is the creator or an Org Admin, archive the document + IF _row.created_by = auth.uid() OR is_admin_organization(auth.uid()) + THEN + IF NOT EXISTS(SELECT 1 FROM public.project_documents pd WHERE pd.document_id = _document_id AND pd.is_archived IS FALSE ) + THEN + UPDATE public.documents d + SET is_archived = TRUE + WHERE d.id = _document_id; + + RETURN TRUE; + END IF; + END IF; + + RETURN FALSE; +END +$function$ +; + +