set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.do_grp_add_admins()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
declare
  admins int[];
begin
  admins := ( select array_agg(usr.id)
              from public.grp_members_admins() as usr
            );

  perform public.grp_add_members( to_grp => NEW.id
                                , add_usrs => admins
                                );

  return null;
end;
$function$
;

CREATE OR REPLACE FUNCTION public.grp_members(of_grp integer)
 RETURNS SETOF usr
 LANGUAGE plpgsql
 STABLE
AS $function$
begin
  return query select u.*
               from public.grp_usr gu
               inner join public.usr u on gu.usr = u.id
               where gu.grp = of_grp;
end;
$function$
;
