feat(MED-85): update dispatch order to medipost retry

This commit is contained in:
2025-08-27 08:04:37 +03:00
parent da8b5aa59f
commit 2b2a0b8bc4
7 changed files with 141 additions and 6 deletions

View File

@@ -5,8 +5,32 @@ CREATE TABLE "audit"."medipost_dispatch" (
"is_success" boolean not null,
"error_message" text,
"created_at" timestamp with time zone not null default now(),
"changed_by" uuid not null
"changed_by" uuid default auth.uid()
);
grant usage on schema audit to service_role;
grant usage on schema audit to authenticated;
grant select, insert, update, delete on table audit.medipost_dispatch to authenticated;
grant usage on schema medreport to service_role;
alter table "audit"."medipost_dispatch" enable row level security;
create policy "service_role_select" on "audit"."medipost_dispatch" for select to service_role using (true);
create policy "service_role_insert" on "audit"."medipost_dispatch" for insert to service_role with check (true);
create policy "service_role_update" on "audit"."medipost_dispatch" for update to service_role using (true);
create policy "service_role_delete" on "audit"."medipost_dispatch" for delete to service_role using (true);
CREATE OR REPLACE FUNCTION medreport.get_medipost_dispatch_tries(p_medusa_order_id text)
returns integer
language plpgsql
security definer
as $function$
declare
tries integer;
begin
select count(*) from audit.medipost_dispatch m where m.medusa_order_id = p_medusa_order_id and m.created_at > now() - interval '1 day' and m.is_success = false into tries;
return tries;
end;
$function$;
grant execute on function medreport.get_medipost_dispatch_tries(text) to service_role;
grant select, insert, update, delete on table audit.medipost_dispatch to service_role;

View File

@@ -0,0 +1,42 @@
create extension if not exists pg_net;
create or replace function medreport.medipost_retry_dispatch(
order_id text
)
returns jsonb
language plpgsql
as $function$
declare
response_result record;
begin
select into response_result
net.http_post(
url := 'https://test.medreport.ee/api/job/medipost-retry-dispatch',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'x-jobs-api-key', 'fd26ec26-70ed-11f0-9e95-431ac3b15a84'
),
body := jsonb_build_object(
'medusaOrderId', order_id
)::text
) as request_id;
return jsonb_build_object(
'success', true
);
exception
when others then
return jsonb_build_object(
'success', false
);
end;
$function$;
grant execute on function medreport.medipost_retry_dispatch(text) to service_role;
comment on function medreport.medipost_retry_dispatch(text) is
'Manually trigger a medipost retry dispatch for a specific order ID.
Parameters:
- order_id: The medusa order ID to retry dispatch for
Returns: JSONB with success status and request details';