Skip to content

Commit

Permalink
Updated layer_context trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjameson committed May 23, 2024
1 parent ce4e1b6 commit 84e25bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BEGIN
FOR _record IN SELECT * FROM public.group_users WHERE group_type = 'project' AND type_id = _project_group_id
LOOP
INSERT INTO public.context_users (context_id, user_id, role_id)
VALUES (_context_id,_record.user_id, _role_id);
VALUES (NEW.context_id,_record.user_id, _role_id);
END LOOP;
END IF;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
DROP TRIGGER IF EXISTS on_layer_context_created_check_open_edit ON public.layer_contexts;

CREATE TRIGGER on_layer_context_created_check_open_edit
AFTER INSERT ON public.layer_contexts FOR EACH ROW
EXECUTE PROCEDURE check_layer_context_for_open_edit ();
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
drop trigger if exists "on_layer_context_created_check_open_edit" on "public"."layer_contexts";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.check_layer_context_for_open_edit()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
_project_id uuid;
_context_name VARCHAR;
_is_project_default BOOLEAN;
_is_open_edit BOOLEAN;
_record RECORD;
_project_group_id uuid;
_role_id uuid;
_id uuid;
BEGIN
-- See if the layer is in the default context on an open edit project
SELECT c.project_id, c.name, c.is_project_default INTO _project_id, _context_name, _is_project_default FROM public.contexts c WHERE c.id = NEW.context_id;
SELECT is_open_edit INTO _is_open_edit FROM public.projects p WHERE p.id = _project_id;

RAISE LOG 'check_layer_context_for_open_edit';

IF _is_open_edit AND _is_project_default IS TRUE THEN
-- Get the project group
SELECT (id) INTO _project_group_id FROM public.project_groups WHERE project_id = _project_id and is_default = TRUE;

-- Get the role_id
SELECT g.role_id INTO _role_id FROM public.default_groups g WHERE g.group_type = 'layer' AND g.is_default = TRUE;

-- Add all project members to default context
FOR _record IN SELECT * FROM public.group_users WHERE group_type = 'project' AND type_id = _project_group_id
LOOP
INSERT INTO public.context_users (context_id, user_id, role_id)
VALUES (NEW.context_id,_record.user_id, _role_id);
END LOOP;
END IF;

RETURN NEW;
END
$function$
;


0 comments on commit 84e25bb

Please sign in to comment.