-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from recogito/develop
Develop to main
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
CREATE | ||
OR REPLACE FUNCTION archive_document_rpc ( | ||
_document_id uuid | ||
) RETURNS BOOLEAN AS $body$ | ||
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.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 | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
55 changes: 55 additions & 0 deletions
55
SQL Scripts/functions/get_available_layers(alternative).sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
CREATE | ||
OR REPLACE FUNCTION get_availabale_layers_rpc ( | ||
_project_id uuid | ||
) RETURNS TABLE ( | ||
document_id uuid, | ||
layer_id uuid, | ||
context_id uuid, | ||
is_active BOOLEAN, | ||
context_name VARCHAR | ||
) | ||
AS $body$ | ||
DECLARE | ||
_context_name VARCHAR; | ||
_document_id uuid; | ||
_contexts_row public.contexts % rowtype; | ||
_layer_context_row public.layer_contexts % rowtype; | ||
_layer_row public.layers % rowtype; | ||
BEGIN | ||
|
||
-- Check project policy that contexts can be selected by this user | ||
IF NOT (check_action_policy_organization(auth.uid(), 'contexts', 'SELECT') | ||
OR check_action_policy_project(auth.uid(), 'contexts', 'SELECT', _project_id)) | ||
THEN | ||
RETURN NEXT; | ||
END IF; | ||
|
||
-- Find all documents in the current context | ||
FOR _contexts_row IN SELECT * FROM public.contexts c | ||
WHERE c.project_id = _project_id | ||
LOOP | ||
RAISE LOG 'Found Context %', _contexts_row.id; | ||
FOR _layer_context_row IN SELECT * FROM public.layer_contexts lcx | ||
WHERE lcx.context_id = _contexts_row.id AND lcx.is_archived IS NOT TRUE | ||
LOOP | ||
RAISE LOG 'Found Layer Context %, Layer ID % ', _layer_context_row.id, _layer_context_row.layer_id; | ||
FOR _document_id IN SELECT cd.document_id | ||
FROM public.context_documents cd WHERE cd.context_id = _layer_context_row.context_id AND cd.is_archived IS NOT TRUE | ||
LOOP | ||
RAISE LOG 'Found Document %', _document_id; | ||
FOR _layer_row IN SELECT * FROM public.layers l | ||
WHERE l.id = _layer_context_row.layer_id AND l.document_id = _document_id AND l.is_archived IS NOT TRUE | ||
LOOP | ||
document_id := _document_id; | ||
context_id := _contexts_row.id; | ||
is_active := _layer_context_row.is_active_layer; | ||
layer_id := _layer_row.id; | ||
context_name := _contexts_row.name; | ||
RAISE LOG 'Found Available Document %', _document_id; | ||
RETURN NEXT; | ||
END LOOP; | ||
END LOOP; | ||
END LOOP; | ||
END LOOP; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
38 changes: 38 additions & 0 deletions
38
supabase/migrations/20240816204533_archive_document_rpc.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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$ | ||
; | ||
|
||
|