Files
medreport_mrb2b/supabase/migrations/20250723114200_company_params.sql
Danel Kungla 86b86c6752 add health benefit form
fix super admin
2025-07-23 16:33:24 +03:00

68 lines
2.1 KiB
PL/PgSQL

create table "medreport"."company_params" (
"id" uuid not null default gen_random_uuid(),
"benefit_occurance" text,
"benefit_amount" numeric,
"account_id" uuid,
"created_at" timestamp with time zone default now(),
"updated_at" timestamp with time zone default now(),
"slug" text
);
alter table "medreport"."company_params" enable row level security;
CREATE UNIQUE INDEX company_params_pkey ON medreport.company_params USING btree (id);
alter table "medreport"."company_params" add constraint "company_params_pkey" PRIMARY KEY using index "company_params_pkey";
alter table "medreport"."company_params" add constraint "company_params_account_id_fkey" FOREIGN KEY (account_id) REFERENCES medreport.accounts(id) ON DELETE CASCADE not valid;
alter table "medreport"."company_params" validate constraint "company_params_account_id_fkey";
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION medreport.insert_company_params_on_new_company()
RETURNS trigger
LANGUAGE plpgsql
AS $function$begin
insert into medreport.company_params (
account_id,
slug,
benefit_occurance,
benefit_amount
) values (
new.id,
new.slug,
null, -- or a default value like 'monthly'
null -- or a default numeric like 0
);
return new;
end;$function$
;
grant execute on function medreport.insert_company_params_on_new_company() to authenticated,
service_role;
CREATE TRIGGER trigger_create_company_params AFTER INSERT ON medreport.accounts FOR EACH ROW EXECUTE FUNCTION medreport.insert_company_params_on_new_company();
create policy "Allow select and update if user is account's primary owner"
on medreport.company_params
for all
using (
exists (
select 1 from medreport.accounts
where
accounts.id = company_params.account_id
and accounts.primary_owner_user_id = auth.uid()
)
)
with check (
exists (
select 1 from medreport.accounts
where
accounts.id = company_params.account_id
and accounts.primary_owner_user_id = auth.uid()
)
);
grant select, update on medreport.company_params to authenticated;