diff --git a/SQL Scripts/functions/check_layer_context_for_open_edit.sql b/SQL Scripts/functions/check_layer_context_for_open_edit.sql index fedc804..b0d2e51 100644 --- a/SQL Scripts/functions/check_layer_context_for_open_edit.sql +++ b/SQL Scripts/functions/check_layer_context_for_open_edit.sql @@ -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; diff --git a/SQL Scripts/triggers/layer_contexts/on_layer_context_created_check_open_edit.sql b/SQL Scripts/triggers/layer_contexts/on_layer_context_created_check_open_edit.sql index 63767a8..3e04e60 100644 --- a/SQL Scripts/triggers/layer_contexts/on_layer_context_created_check_open_edit.sql +++ b/SQL Scripts/triggers/layer_contexts/on_layer_context_created_check_open_edit.sql @@ -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 (); \ No newline at end of file diff --git a/supabase/migrations/20240523204158_remove_layer_context_trigger.sql b/supabase/migrations/20240523204158_remove_layer_context_trigger.sql new file mode 100644 index 0000000..263aba2 --- /dev/null +++ b/supabase/migrations/20240523204158_remove_layer_context_trigger.sql @@ -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$ +; + +