caterium-app/supabase/migrations/20260917221500_import_order_history.sql
2026-09-17 20:15:32 +03:00

47 lines
3.5 KiB
PL/PgSQL

-- Imported history must preserve unknown or partial payments.
create or replace function public.sun_process_due_orders_internal(p_workspace uuid,p_now timestamptz default now())
returns jsonb
language plpgsql
security definer
set search_path='public' as $$
declare v_tz text; v_changed integer:=0; v_ids text[]:=array[]::text[]; r record; v_total numeric; v_iso text; v_orders jsonb;
begin
select timezone into v_tz from public.sun_workspaces where id=p_workspace;
if v_tz is null then return jsonb_build_object('changed',0,'ids','[]'::jsonb); end if;
v_iso:=to_char(p_now at time zone 'UTC','YYYY-MM-DD"T"HH24:MI:SS.MS"Z"');
for r in
select order_id,data,version from public.sun_v17_orders
where workspace_id=p_workspace
and lower(coalesce(data->>'status','')) not in ('отменён','отменен','отдан заказчику','завершён','завершен')
and coalesce(data->>'sunAutoCompletedAt','')=''
and coalesce(data->>'autoCompletionDisabled','false') <> 'true'
and public.sun_order_due_at(data,v_tz) is not null
and public.sun_order_due_at(data,v_tz) <= p_now
for update
loop
v_total:=case when coalesce(r.data->>'total','') ~ '^-?\d+(\.\d+)?$' then greatest(0,(r.data->>'total')::numeric) else 0 end;
update public.sun_v17_orders
set data=r.data||jsonb_build_object('prepayment',v_total,'balance',0,'status','Отдан заказчику','paymentStatus','paid','completedAt',coalesce(nullif(r.data->>'completedAt',''),v_iso),'paymentCompletedAt',coalesce(nullif(r.data->>'paymentCompletedAt',''),v_iso),'sunAutoCompletedAt',v_iso,'sunAutoCompletedV1770',true,'sunAutoCompletedVersion','17.7.0'),version=version+1,updated_at=p_now,updated_by=null
where workspace_id=p_workspace and order_id=r.order_id;
insert into public.sun_v17_change_events(workspace_id,entity,entity_key,operation,version,client_id,created_by)
values(p_workspace,'order',r.order_id,'upsert',r.version+1,'server:auto-complete',null);
v_changed:=v_changed+1;v_ids:=array_append(v_ids,r.order_id);
end loop;
if v_changed>0 then
select coalesce(jsonb_agg(o.data order by case when o.order_id ~ '^\d+$' then o.order_id::bigint else null end,o.order_id),'[]'::jsonb) into v_orders from public.sun_v17_orders o where o.workspace_id=p_workspace;
update public.sun_app_state s set payload=jsonb_set(coalesce(s.payload,'{}'::jsonb),'{storage,sunOrders}',jsonb_build_object('t','j','v',v_orders),true),revision=s.revision+1,updated_at=p_now,client_id='server:auto-complete' where s.workspace_id=p_workspace;
if not found then insert into public.sun_app_state(workspace_id,payload,revision,updated_at,client_id) values(p_workspace,jsonb_build_object('format','sun-cloud-v2','version',2,'storage',jsonb_build_object('sunOrders',jsonb_build_object('t','j','v',v_orders))),1,p_now,'server:auto-complete'); end if;
end if;
return jsonb_build_object('changed',v_changed,'ids',to_jsonb(v_ids),'workspace',p_workspace,'timezone',v_tz,'processed_at',v_iso);
end;$$;
-- The archive contains both order and customer data.
do $migration$
declare definition text;
begin
definition:=pg_get_functiondef('public.sun_can_read_storage_key(uuid,text)'::regprocedure);
definition:=replace(definition, 'when p_key=''sunOrders'' then', 'when p_key=''sunTelegramImportArchiveV1'' then public.sun_has_permission(p_workspace,''orders.view'') and public.sun_has_permission(p_workspace,''clients.view'')
when p_key=''sunOrders'' then');
execute definition;
end $migration$;