-- Function "medreport.update_analysis_order_status"
-- Update an analysis order status
create
    or replace function medreport.update_analysis_order_status (
    order_id bigint,
    medusa_order_id_param text,
    status_param medreport.analysis_order_status
) returns medreport.analysis_orders
    set
        search_path = '' as $$
declare
    updated_order medreport.analysis_orders;
begin
    update medreport.analysis_orders
    set status = status_param
    where (id = order_id OR medusa_order_id = medusa_order_id_param)
    returning * into updated_order;

    return updated_order;

end;

$$ language plpgsql;

grant
    execute on function medreport.update_analysis_order_status (
    bigint,
    text,
    medreport.analysis_order_status
    ) to service_role;

-- example:
-- select medreport.update_analysis_order_status(-1, 'order_01K1TQQHZGPXKDHAH81TDSNGXR', 'CANCELLED')
