diff --git a/lib/core/src/persist/mod.rs b/lib/core/src/persist/mod.rs index 1dd3f04fc..868d14a20 100644 --- a/lib/core/src/persist/mod.rs +++ b/lib/core/src/persist/mod.rs @@ -845,15 +845,32 @@ fn filter_to_where_clause(req: &ListPaymentsRequest) -> (String, Vec = HashSet::from_iter(states.iter().map(|s| *s as i8)); - where_clause.push(format!( - "COALESCE(rs.state, ss.state, cs.state) in ({})", - states_hash - .iter() - .map(|t| format!("{}", t)) - .collect::>() - .join(", ") - )); + let deduped_states: Vec = states + .clone() + .into_iter() + .collect::>() + .into_iter() + .collect(); + let states_param = deduped_states + .iter() + .map(|t| (*t as i8).to_string()) + .collect::>() + .join(", "); + let tx_comfirmed_param = deduped_states + .iter() + .filter_map(|state| match state { + PaymentState::Pending | PaymentState::Complete => { + Some(((*state == PaymentState::Complete) as i8).to_string()) + } + _ => None, + }) + .collect::>() + .join(", "); + let states_query = match tx_comfirmed_param.is_empty() { + true => format!("COALESCE(rs.state, ss.state, cs.state) in ({states_param})"), + false => format!("(COALESCE(rs.id, ss.id, cs.id) IS NULL AND ptx.is_confirmed in ({tx_comfirmed_param}) OR COALESCE(rs.state, ss.state, cs.state) in ({states_param}))"), + }; + where_clause.push(states_query); } }