diff --git a/SQL Scripts/functions/create_default_layer_groups.sql b/SQL Scripts/functions/create_default_layer_groups.sql index 055d6d3..468a9f7 100644 --- a/SQL Scripts/functions/create_default_layer_groups.sql +++ b/SQL Scripts/functions/create_default_layer_groups.sql @@ -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) diff --git a/SQL Scripts/utility/add_read_only_groups.sql b/SQL Scripts/utility/add_read_only_groups.sql new file mode 100644 index 0000000..527f389 --- /dev/null +++ b/SQL Scripts/utility/add_read_only_groups.sql @@ -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 +$$ \ No newline at end of file diff --git a/create-default-groups.js b/create-default-groups.js index 5d0db69..8b31193 100644 --- a/create-default-groups.js +++ b/create-default-groups.js @@ -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) => { @@ -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 }); }); diff --git a/supabase/migrations/20241018181235_read-only-migration.sql b/supabase/migrations/20241018181235_read-only-migration.sql new file mode 100644 index 0000000..cd04a41 --- /dev/null +++ b/supabase/migrations/20241018181235_read-only-migration.sql @@ -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 +$$ + +