CREATE OR REPLACE FUNCTION medreport.order_has_medipost_dispatch_error(medusa_order_id text)
RETURNS boolean AS $$
SELECT EXISTS (
    SELECT 1 FROM medreport.medipost_actions
    WHERE medusa_order_id = $1
    AND action = 'send_order_to_medipost'
    AND has_error = true
);
$$ LANGUAGE sql STABLE;

grant execute on function medreport.order_has_medipost_dispatch_error(text) to service_role;
