-- Generated by ops/demo/build-trial-extras.mjs. Fill empty trial categories only. begin; create or replace function public.caterium_trial_extras_seed_v1() returns jsonb language sql immutable security definer set search_path=public,pg_temp as $fn$ select $demo$__EXTRAS_JSON__$demo$::jsonb $fn$; revoke all on function public.caterium_trial_extras_seed_v1() from public,anon,authenticated; __STATUS_FUNCTION__ create or replace function public.caterium_install_trial_extras(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_catalog jsonb; v_stock jsonb; v_added jsonb; v_products 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; select * into v_state from public.sun_app_state where workspace_id=p_workspace for update; v_storage:=v_state.payload->'storage'; if coalesce(v_storage#>>'{sunTrialDemoV1,v,extrasVersion}','')='1' then return v_status; end if; v_catalog:=coalesce(v_storage#>'{sunBoxes,v}','[]'::jsonb);v_stock:=coalesce(v_storage#>'{sunStock,v}','[]'::jsonb); if jsonb_typeof(v_catalog)<>'array' or jsonb_typeof(v_stock)<>'array' then raise exception 'Неверный формат каталога или склада. Данные сохранены без изменений'; end if; v_seed:=public.caterium_trial_extras_seed_v1(); select coalesce(jsonb_agg(n order by ord),'[]'::jsonb) into v_added from jsonb_array_elements(v_seed->'items') with ordinality as x(n,ord) where not exists(select 1 from jsonb_array_elements(v_catalog) old where coalesce(old->>'category','0')=n->>'category' or old->>'id'=n->>'id'); select coalesce(jsonb_agg(n order by ord),'[]'::jsonb) into v_products from jsonb_array_elements(v_seed->'stock') with ordinality as x(n,ord) where not exists(select 1 from jsonb_array_elements(v_stock) old where old->>'id'=n->>'id' or (old->>'name'=n->>'name' and old->>'unit'=n->>'unit')) and exists(select 1 from jsonb_array_elements(v_added) i cross join lateral jsonb_array_elements(i->'ingredients') r where r->>0=n->>'name' and r->>2=n->>'unit'); v_marker:=(v_storage#>'{sunTrialDemoV1,v}')||jsonb_build_object('extrasVersion',1,'extrasInstalledAt',now()); v_storage:=v_storage||jsonb_build_object('sunBoxes',jsonb_build_object('t','j','v',v_catalog||v_added),'sunStock',jsonb_build_object('t','j','v',v_stock||v_products),'sunTrialDemoV1',jsonb_build_object('t','j','v',v_marker)); perform public.sun_save_app_state_v17(p_workspace,jsonb_set(v_state.payload,'{storage}',v_storage),'trial-extras-v1',v_state.revision); insert into public.sun_platform_audit_events(actor_user_id,action,target_workspace_id,details) values(auth.uid(),'trial_demo.extras_install',p_workspace,jsonb_build_object('version',1,'items',jsonb_array_length(v_added))); return public.caterium_trial_demo_status(p_workspace); end $fn$; revoke all on function public.caterium_install_trial_extras(uuid) from public,anon; grant execute on function public.caterium_install_trial_extras(uuid) to authenticated; __BANQUET_FUNCTION__ __INSTALL_FUNCTION__ notify pgrst,'reload schema'; commit;