Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/recogito/recogito-server
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
lwjameson committed Oct 18, 2024
2 parents 98f5782 + a070dc0 commit 4f594ba
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
12 changes: 7 additions & 5 deletions SQL Scripts/functions/create_default_layer_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ DECLARE
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
BEGIN
FOR _role_id, _name, _description, _is_admin, _is_default IN SELECT role_id, name, description, is_admin, is_default
FROM public.default_groups
WHERE group_type = 'layer'
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT role_id, name, description, is_admin, is_default, is_read_only
FROM public.default_groups
WHERE group_type = 'layer'
LOOP
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default)
VALUES (_layer_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default);
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);

IF _is_admin IS TRUE AND NEW.created_by IS NOT NULL THEN
INSERT INTO public.group_users (group_type, type_id, user_id)
Expand Down
31 changes: 31 additions & 0 deletions SQL Scripts/utility/add_read_only_groups.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DO $$
DECLARE
_layer_group_id uuid;
_role_id uuid;
_name varchar;
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
_layer_id uuid;
BEGIN
-- Get the read-only default group
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT dg.role_id, dg.name, dg.description, dg.is_admin, dg.is_default, dg.is_read_only
FROM public.default_groups dg
WHERE dg.group_type = 'layer' AND dg.is_read_only IS TRUE
LOOP
-- Loop through all layers
FOR _layer_id IN SELECT l.id FROM public.layers l
LOOP
IF NOT EXISTS(SELECT 1 FROM public.layer_groups lg WHERE lg.layer_id = _layer_id AND lg.is_read_only IS TRUE)
THEN
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, _layer_id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);
END IF;
END LOOP;
END LOOP;
END
$$
12 changes: 6 additions & 6 deletions create-default-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ const main = async (options) => {
name: group.name,
description: group.description,
role_id: group.role_id,
is_admin: group.is_admin,
is_default: group.is_default,
is_read_only: group.is_read_only
is_admin: !!group.is_admin,
is_default: !!group.is_default,
is_read_only: !!group.is_read_only
});
});
config.layer_groups.forEach((group) => {
Expand All @@ -208,9 +208,9 @@ const main = async (options) => {
name: group.name,
description: group.description,
role_id: group.role_id,
is_admin: group.is_admin,
is_default: group.is_default,
is_read_only: group.is_read_only
is_admin: !!group.is_admin,
is_default: !!group.is_default,
is_read_only: !!group.is_read_only
});
});

Expand Down
69 changes: 69 additions & 0 deletions supabase/migrations/20241018181235_read-only-migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.create_default_layer_groups()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
_layer_group_id uuid;
_role_id uuid;
_name varchar;
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
BEGIN
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT role_id, name, description, is_admin, is_default, is_read_only
FROM public.default_groups
WHERE group_type = 'layer'
LOOP
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);

IF _is_admin IS TRUE AND NEW.created_by IS NOT NULL THEN
INSERT INTO public.group_users (group_type, type_id, user_id)
VALUES ('layer', _layer_group_id, NEW.created_by);
END IF;
END LOOP;
RETURN NEW;
END
$function$
;

DO $$
DECLARE
_layer_group_id uuid;
_role_id uuid;
_name varchar;
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
_layer_id uuid;
BEGIN
-- Get the read-only default group
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT dg.role_id, dg.name, dg.description, dg.is_admin, dg.is_default, dg.is_read_only
FROM public.default_groups dg
WHERE dg.group_type = 'layer' AND dg.is_read_only IS TRUE
LOOP
-- Loop through all layers
FOR _layer_id IN SELECT l.id FROM public.layers l
LOOP
IF NOT EXISTS(SELECT 1 FROM public.layer_groups lg WHERE lg.layer_id = _layer_id AND lg.is_read_only IS TRUE)
THEN
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, _layer_id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);
END IF;
END LOOP;
END LOOP;
END
$$


0 comments on commit 4f594ba

Please sign in to comment.