From d075d0ad2d8ace51ee1a2bf54af18dae700274de Mon Sep 17 00:00:00 2001 From: lorinjameson Date: Thu, 16 May 2024 11:46:21 -0400 Subject: [PATCH] https://github.com/performant-software/vico/issues/246 --- .../add_documents_to_project_rpc.sql | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/SQL Scripts/functions/add_documents_to_project_rpc.sql b/SQL Scripts/functions/add_documents_to_project_rpc.sql index 99ff556..2bc520a 100644 --- a/SQL Scripts/functions/add_documents_to_project_rpc.sql +++ b/SQL Scripts/functions/add_documents_to_project_rpc.sql @@ -7,6 +7,7 @@ DECLARE _context_id uuid; _layer_id uuid; _document_id uuid; + _row public.contexts % rowtype; BEGIN -- Check project policy that project documents can be updated by this user IF NOT check_action_policy_project(auth.uid(), 'project_documents', 'UPDATE', _project_id) THEN @@ -25,27 +26,46 @@ BEGIN -- Iterate through the document ids and add to project_documents and context_documents for the default context FOREACH _document_id IN ARRAY _document_ids LOOP - -- Add the document to project_documents - INSERT INTO public.project_documents - (created_by, created_at, project_id, document_id) - VALUES (auth.uid(), NOW(), _project_id, _document_id); - - -- Add a context_document record to the default context - INSERT INTO public.context_documents - (created_by, created_at, context_id, document_id) - VALUES (auth.uid(), NOW(), _context_id, _document_id); + IF EXISTS(SELECT * FROM public.project_documents pd WHERE pd.document_id = _document_id AND pd.project_id = _project_id AND pd.is_archived IS TRUE) + THEN + -- For now we will unarchive the project_document and the context_documents + -- associated with the document. This will restore and make visible any project annotations, etc + + -- Unarchive the project_documents record + UPDATE public.project_documents pd + SET is_archived = FALSE + WHERE pd.document_id = _document_id AND pd.project_id = _project_id; + + -- Unarchive the document in all contexts that contain it + FOR _row IN SELECT * FROM public.contexts c WHERE c.project_id = _project_id + LOOP + UPDATE public.context_documents + SET is_archived = FALSE + WHERE document_id = _document_id; + END LOOP; + ELSE + -- Add the document to project_documents + INSERT INTO public.project_documents + (created_by, created_at, project_id, document_id) + VALUES (auth.uid(), NOW(), _project_id, _document_id); + + -- Add a context_document record to the default context + INSERT INTO public.context_documents + (created_by, created_at, context_id, document_id) + VALUES (auth.uid(), NOW(), _context_id, _document_id); - -- Add the default layer - _layer_id = uuid_generate_v4(); + -- Add the default layer + _layer_id = uuid_generate_v4(); - INSERT INTO public.layers - (id, document_id, project_id) - VALUES (_layer_id, _document_id, _project_id); + INSERT INTO public.layers + (id, document_id, project_id) + VALUES (_layer_id, _document_id, _project_id); - -- Add the layer_context - INSERT INTO public.layer_contexts - (layer_id, context_id, is_active_layer) - VALUES (_layer_id, _context_id, TRUE); + -- Add the layer_context + INSERT INTO public.layer_contexts + (layer_id, context_id, is_active_layer) + VALUES (_layer_id, _context_id, TRUE); + END IF; END LOOP; RETURN TRUE;