Skip to content

Commit

Permalink
Fix archive context documents rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjameson committed Aug 22, 2024
1 parent bea4694 commit 917538d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
29 changes: 19 additions & 10 deletions SQL Scripts/functions/archive_context_documents_rpc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DECLARE
_layer_id uuid;
_document_id uuid;
_row RECORD;
_layer_doc_id uuid;
BEGIN
-- Find the project for this context
SELECT p.id INTO _project_id FROM public.projects p
Expand All @@ -25,22 +26,30 @@ BEGIN
FOREACH _document_id IN ARRAY _document_ids
LOOP
-- Archive the context_documents record
UPDATE public.context_document cd
UPDATE public.context_documents cd
SET is_archived = TRUE
WHERE cd.document_id = _document_id AND cd.context_id = _context_id;

-- Archive any related layers
FOR _row IN SELECT * FROM public.layers l
INNER JOIN public.layer_contexts lc ON lc.context_id = _context_id
WHERE l.document_id = _document_id
FOR _row IN SELECT * FROM public.layer_contexts lc
WHERE lc.context_id = _context_id
LOOP
UPDATE public.layers
SET is_archived = TRUE
WHERE id = _row.id;

SELECT l.document_id INTO _layer_doc_id FROM public.layers l WHERE l.id = _row.layer_id;

IF _layer_doc_id = _document_id
THEN
IF _row.is_active_layer IS TRUE
THEN
UPDATE public.layers l
SET is_archived = TRUE
WHERE l.id = _row.layer_id;
END IF;

UPDATE public.layer_contexts lc
SET is_archived = TRUE
WHERE lc.context_id = _context_id AND lc.layer_id = _row.id;
UPDATE public.layer_contexts lc
SET is_archived = TRUE
WHERE lc.id = _row.id;
END IF;
END LOOP;

END LOOP;
Expand Down
61 changes: 61 additions & 0 deletions supabase/migrations/20240822181056_fix_context_documents_edit.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
set check_function_bodies = off;

CREATE
OR REPLACE FUNCTION archive_context_documents_rpc (
_context_id uuid,
_document_ids uuid[]
) RETURNS BOOLEAN AS $body$
DECLARE
_project_id uuid;
_layer_id uuid;
_document_id uuid;
_row RECORD;
_layer_doc_id uuid;
BEGIN
-- Find the project for this context
SELECT p.id INTO _project_id FROM public.projects p
INNER JOIN public.contexts c ON c.id = _context_id
WHERE p.id = c.project_id;

-- Check project policy that context documents can be updated by this user
IF NOT (check_action_policy_organization(auth.uid(), 'context_documents', 'UPDATE')
OR check_action_policy_project(auth.uid(), 'context_documents', 'UPDATE', _project_id))
THEN
RETURN FALSE;
END IF;

-- Iterate through the document ids and archive them in project_documents and all context_documents
FOREACH _document_id IN ARRAY _document_ids
LOOP
-- Archive the context_documents record
UPDATE public.context_documents cd
SET is_archived = TRUE
WHERE cd.document_id = _document_id AND cd.context_id = _context_id;

-- Archive any related layers
FOR _row IN SELECT * FROM public.layer_contexts lc
WHERE lc.context_id = _context_id
LOOP

SELECT l.document_id INTO _layer_doc_id FROM public.layers l WHERE l.id = _row.layer_id;

IF _layer_doc_id = _document_id
THEN
IF _row.is_active_layer IS TRUE
THEN
UPDATE public.layers l
SET is_archived = TRUE
WHERE l.id = _row.layer_id;
END IF;

UPDATE public.layer_contexts lc
SET is_archived = TRUE
WHERE lc.id = _row.id;
END IF;
END LOOP;

END LOOP;

RETURN TRUE;
END
$body$ LANGUAGE plpgsql SECURITY DEFINER;

0 comments on commit 917538d

Please sign in to comment.