Skip to content

Commit

Permalink
https://github.com/performant-software/vico/issues/246
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjameson committed May 16, 2024
1 parent 20b31a0 commit d075d0a
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions SQL Scripts/functions/add_documents_to_project_rpc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit d075d0a

Please sign in to comment.