grant
    execute on function medreport.get_account_members (text) to authenticated,
    service_role;

create or replace function medreport.is_company_admin(account_slug text)
returns boolean
set search_path = ''
language plpgsql
as $$
declare
  is_owner boolean;
begin
  select exists (
    select 1
    from medreport.accounts_memberships am
    join medreport.accounts a on a.id = am.account_id
    where am.user_id = auth.uid()
      and am.account_role = 'owner'
      and a.slug = account_slug
  ) into is_owner;

  return is_owner;
end;
$$;

grant execute on function medreport.is_company_admin(text) to authenticated, service_role;

grant
execute on function medreport.team_account_workspace (text) to authenticated,
service_role;

grant execute on function medreport.get_account_invitations(text) to authenticated, service_role;

-- To create a new company user you need rows in Roles table
INSERT INTO medreport.roles (name, hierarchy_level)
VALUES 
  ('owner', 1),
  ('member', 2);

-- Add role permissions
insert into medreport.role_permissions (role, permission) values
('owner', 'roles.manage'),
('owner', 'billing.manage'),
('owner', 'settings.manage'),
('owner', 'members.manage'),
('owner', 'invites.manage');