caterium-app/ops/demo/trial-banquet.sql

48 lines
3.3 KiB
PL/PgSQL

-- Generated by ops/demo/build-trial-banquet.mjs. Additive demo upgrade only.
begin;
create or replace function public.caterium_trial_banquet_seed_v1()
returns jsonb language sql immutable security definer set search_path=public,pg_temp
as $fn$ select $demo$__BANQUET_JSON__$demo$::jsonb $fn$;
revoke all on function public.caterium_trial_banquet_seed_v1() from public,anon,authenticated;
__STATUS_FUNCTION__
create or replace function public.caterium_install_trial_banquet(p_workspace uuid)
returns jsonb language plpgsql security definer set search_path=public,pg_temp
as $fn$
declare
v_state public.sun_app_state%rowtype; v_status jsonb; v_storage jsonb;
v_seed jsonb; v_key text; v_items jsonb; v_added jsonb; v_marker jsonb;
begin
if auth.uid() is null or public.sun_member_role(p_workspace) is distinct from 'admin' then raise exception 'Administrator membership required'; end if;
perform 1 from public.sun_workspaces where id=p_workspace for update;
v_status:=public.caterium_trial_demo_status(p_workspace);
if not (v_status->>'eligible')::boolean then raise exception 'Демонстрационная база доступна в пробной или собственной тестовой компании разработчика'; end if;
if not (v_status->>'installed')::boolean then raise exception 'Сначала установите демонстрационную базу'; end if;
if not (v_status->>'canUpgrade')::boolean then return v_status; end if;
select * into v_state from public.sun_app_state where workspace_id=p_workspace for update;
v_storage:=v_state.payload->'storage'; v_seed:=public.caterium_trial_banquet_seed_v1();
foreach v_key in array array['sunBoxes','sunStock'] loop
v_items:=coalesce(v_storage#>array[v_key,'v'],'[]'::jsonb);
if jsonb_typeof(v_items)<>'array' then raise exception 'Неверный формат каталога или склада. Данные сохранены без изменений'; end if;
-- Keep every existing row byte-for-byte, including edited demo prices and balances.
select coalesce(jsonb_agg(n),'[]'::jsonb) into v_added
from jsonb_array_elements(v_seed->case when v_key='sunBoxes' then 'banquet' else 'stock' end) n
where not exists(select 1 from jsonb_array_elements(v_items) old where old->>'id'=n->>'id');
v_storage:=v_storage||jsonb_build_object(v_key,jsonb_build_object('t','j','v',v_items||v_added));
end loop;
v_marker:=(v_storage#>'{sunTrialDemoV1,v}')||jsonb_build_object('banquetVersion',1,'banquetInstalledAt',now());
v_storage:=jsonb_set(v_storage,'{sunTrialDemoV1,v}',v_marker);
perform public.sun_save_app_state_v17(p_workspace,jsonb_set(v_state.payload,'{storage}',v_storage),'trial-banquet-v1',v_state.revision);
insert into public.sun_platform_audit_events(actor_user_id,action,target_workspace_id,details)
values(auth.uid(),'trial_demo.banquet_install',p_workspace,jsonb_build_object('version',1,'dishes',18));
return public.caterium_trial_demo_status(p_workspace);
end $fn$;
revoke all on function public.caterium_install_trial_banquet(uuid) from public,anon;
grant execute on function public.caterium_install_trial_banquet(uuid) to authenticated;
-- Both onboarding's existing trigger and the install button use this entry point.
__INSTALL_FUNCTION__
notify pgrst,'reload schema';
commit;