-
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 #42 from recogito/feature-galleries
Feature galleries
- Loading branch information
Showing
16 changed files
with
593 additions
and
3 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,18 @@ | ||
CREATE | ||
OR REPLACE FUNCTION archive_tag_definition_rpc(_tag_definition_id uuid) | ||
RETURNS BOOLEAN AS $body$ | ||
BEGIN | ||
-- Check project policy that tag definition can be updated by this user | ||
IF NOT (check_action_policy_user_from_tag_definition(auth.uid(), _tag_definition_id)) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Archive the tag definition | ||
UPDATE public.tag_definitions td | ||
SET is_archived = TRUE | ||
WHERE td.id = _tag_definition_id; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
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,15 @@ | ||
CREATE OR REPLACE FUNCTION archive_tags_for_target(_target_type tag_target_types, _target_id uuid) | ||
RETURNS bool | ||
AS | ||
$body$ | ||
BEGIN | ||
UPDATE public.tags t | ||
SET is_archived = TRUE | ||
FROM public.tag_definitions td | ||
WHERE td.id = t.tag_definition_id | ||
AND td.target_type = _target_type | ||
AND t.target_id = _target_id; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
12 changes: 12 additions & 0 deletions
12
SQL Scripts/functions/check_action_policy_user_from_tag_definition.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,12 @@ | ||
CREATE OR REPLACE FUNCTION check_action_policy_user_from_tag_definition(user_id uuid, tag_definition_id uuid) | ||
RETURNS bool | ||
AS $body$ | ||
DECLARE | ||
_scope VARCHAR; | ||
_scope_id UUID; | ||
BEGIN | ||
SELECT scope, scope_id INTO _scope, _scope_id FROM public.tag_definitions WHERE id = tag_definition_id; | ||
|
||
RETURN _scope = 'user' AND _scope_id = user_id; | ||
END ; | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
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
53 changes: 53 additions & 0 deletions
53
SQL Scripts/functions/create_tags_for_tag_definitions_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,53 @@ | ||
CREATE | ||
OR REPLACE FUNCTION create_tags_for_tag_definitions_rpc( | ||
_tag_definition_ids uuid[], | ||
_scope tag_scope_types, | ||
_scope_id uuid, | ||
_target_type tag_target_types, | ||
_target_id uuid | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_new_tag_definition_ids uuid[]; | ||
_tag_definition_id uuid; | ||
BEGIN | ||
-- Check authorization | ||
IF NOT (_scope = 'user' AND _scope_id = auth.uid()) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Delete any tags that are no longer in the list of tag_definition_ids | ||
UPDATE public.tags t | ||
SET is_archived = TRUE | ||
FROM public.tag_definitions td | ||
WHERE td.id = t.tag_definition_id | ||
AND td.scope = _scope | ||
AND td.scope_id = _scope_id | ||
AND td.target_type = _target_type | ||
AND t.target_id = _target_id | ||
AND NOT ( t.tag_definition_id = ANY( _tag_definition_ids )); | ||
|
||
-- Create new tags | ||
_new_tag_definition_ids := ARRAY( | ||
SELECT id | ||
FROM public.tag_definitions td | ||
WHERE td.is_archived = FALSE | ||
AND td.scope = _scope | ||
AND td.scope_id = _scope_id | ||
AND td.target_type = _target_type | ||
AND td.id = ANY( _tag_definition_ids ) | ||
AND NOT EXISTS (SELECT 1 | ||
FROM public.tags t | ||
WHERE t.tag_definition_id = td.id | ||
AND t.target_id = _target_id | ||
AND t.is_archived = FALSE) | ||
); | ||
|
||
FOREACH _tag_definition_id IN ARRAY _new_tag_definition_ids | ||
LOOP | ||
INSERT INTO public.tags (tag_definition_id, target_id) VALUES (_tag_definition_id, _target_id); | ||
END LOOP; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
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 @@ | ||
CREATE | ||
OR REPLACE FUNCTION create_tags_for_targets_rpc( | ||
_tag_definition_id uuid, | ||
_target_ids uuid[] | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_scope tag_scope_types; | ||
_scope_id uuid; | ||
BEGIN | ||
SELECT td.scope, td.scope_id INTO _scope, _scope_id | ||
FROM public.tag_definitions td | ||
WHERE td.id = _tag_definition_id; | ||
|
||
-- Check authorization | ||
IF NOT (public.check_action_policy_user_from_tag_definition(auth.uid(), _tag_definition_id)) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Delete any tags that are no longer in the list of tag_definition_ids | ||
UPDATE public.tags t | ||
SET is_archived = TRUE | ||
WHERE t.tag_definition_id = _tag_definition_id | ||
AND NOT ( t.target_id = ANY( _target_ids )); | ||
|
||
-- Create new tags | ||
INSERT INTO public.tags (tag_definition_id, target_id) | ||
SELECT _tag_definition_id, id | ||
FROM UNNEST( _target_ids ) AS id | ||
WHERE NOT EXISTS ( SELECT 1 | ||
FROM public.tags t | ||
WHERE t.tag_definition_id = _tag_definition_id | ||
AND t.target_id = id | ||
AND t.is_archived = FALSE ); | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
31 changes: 31 additions & 0 deletions
31
SQL Scripts/functions/update_project_documents_sort_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,31 @@ | ||
CREATE | ||
OR REPLACE FUNCTION update_project_documents_sort_rpc ( | ||
_project_id uuid, | ||
_document_ids uuid[] | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
current_index INT = 0; | ||
_document_id uuid; | ||
BEGIN | ||
-- Check project policy that project documents can be updated by this user | ||
IF NOT (check_action_policy_organization(auth.uid(), 'project_documents', 'UPDATE') | ||
OR check_action_policy_project(auth.uid(), 'project_documents', 'UPDATE', _project_id)) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
FOREACH _document_id IN ARRAY _document_ids | ||
LOOP | ||
|
||
UPDATE public.project_documents pd | ||
SET sort = current_index | ||
WHERE pd.document_id = _document_id | ||
AND pd.project_id = _project_id; | ||
|
||
current_index := current_index + 1; | ||
|
||
END LOOP; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
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
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
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
33 changes: 33 additions & 0 deletions
33
supabase/migrations/20241024105310_add_sort_to_project_documents.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,33 @@ | ||
alter table "public"."project_documents" add column sort integer default 0; | ||
|
||
CREATE | ||
OR REPLACE FUNCTION update_project_documents_sort_rpc ( | ||
_project_id uuid, | ||
_document_ids uuid[] | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_current_index integer = 0; | ||
_document_id uuid; | ||
BEGIN | ||
-- Check project policy that project documents can be updated by this user | ||
IF NOT (check_action_policy_organization(auth.uid(), 'project_documents', 'UPDATE') | ||
OR check_action_policy_project(auth.uid(), 'project_documents', 'UPDATE', _project_id)) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
FOREACH _document_id IN ARRAY _document_ids | ||
LOOP | ||
|
||
UPDATE public.project_documents pd | ||
SET sort = _current_index | ||
WHERE pd.document_id = _document_id | ||
AND pd.project_id = _project_id; | ||
|
||
_current_index := _current_index + 1; | ||
|
||
END LOOP; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
Oops, something went wrong.