Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable payment and refund filter at DB query level #5839

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/diesel_models/src/query/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ impl PaymentAttempt {
payment_method: Option<Vec<enums::PaymentMethod>>,
payment_method_type: Option<Vec<enums::PaymentMethodType>>,
authentication_type: Option<Vec<enums::AuthenticationType>>,
profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,
merchant_connector_id: Option<Vec<common_utils::id_type::MerchantConnectorAccountId>>,
) -> StorageResult<i64> {
let mut filter = <Self as HasTable>::table()
Expand All @@ -346,6 +347,9 @@ impl PaymentAttempt {
if let Some(merchant_connector_id) = merchant_connector_id {
filter = filter.filter(dsl::merchant_connector_id.eq_any(merchant_connector_id))
}
if let Some(profile_id_list) = profile_id_list {
filter = filter.filter(dsl::profile_id.eq_any(profile_id_list))
}
router_env::logger::debug!(query = %debug_query::<Pg, _>(&filter).to_string());

db_metrics::track_database_call::<<Self as HasTable>::Table, _, _>(
Expand Down
1 change: 1 addition & 0 deletions crates/hyperswitch_domain_models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod payment_methods;
pub mod payments;
#[cfg(feature = "payouts")]
pub mod payouts;
pub mod refunds;
pub mod router_data;
pub mod router_data_v2;
pub mod router_flow_types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub trait PaymentAttemptInterface {
payment_method_type: Option<Vec<storage_enums::PaymentMethodType>>,
authentication_type: Option<Vec<storage_enums::AuthenticationType>>,
merchant_connector_id: Option<Vec<id_type::MerchantConnectorAccountId>>,
profile_id_list: Option<Vec<id_type::ProfileId>>,
storage_scheme: storage_enums::MerchantStorageScheme,
) -> error_stack::Result<i64, errors::StorageError>;
}
Expand Down
135 changes: 112 additions & 23 deletions crates/hyperswitch_domain_models/src/payments/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,16 @@ pub enum PaymentIntentFetchConstraints {
List(Box<PaymentIntentListParams>),
}

impl PaymentIntentFetchConstraints {
pub fn get_profile_id_list(&self) -> Option<Vec<id_type::ProfileId>> {
if let Self::List(pi_list_params) = self {
pi_list_params.profile_id.clone()
} else {
None
}
}
}

pub struct PaymentIntentListParams {
pub offset: u32,
pub starting_at: Option<PrimitiveDateTime>,
Expand All @@ -749,7 +759,7 @@ pub struct PaymentIntentListParams {
pub payment_method_type: Option<Vec<storage_enums::PaymentMethodType>>,
pub authentication_type: Option<Vec<storage_enums::AuthenticationType>>,
pub merchant_connector_id: Option<Vec<id_type::MerchantConnectorAccountId>>,
pub profile_id: Option<id_type::ProfileId>,
pub profile_id: Option<Vec<id_type::ProfileId>>,
pub customer_id: Option<id_type::CustomerId>,
pub starting_after_id: Option<id_type::PaymentId>,
pub ending_before_id: Option<id_type::PaymentId>,
Expand All @@ -759,10 +769,21 @@ pub struct PaymentIntentListParams {

impl From<api_models::payments::PaymentListConstraints> for PaymentIntentFetchConstraints {
fn from(value: api_models::payments::PaymentListConstraints) -> Self {
let api_models::payments::PaymentListConstraints {
customer_id,
starting_after,
ending_before,
limit,
created,
created_lt,
created_gt,
created_lte,
created_gte,
} = value;
Self::List(Box::new(PaymentIntentListParams {
offset: 0,
starting_at: value.created_gte.or(value.created_gt).or(value.created),
ending_at: value.created_lte.or(value.created_lt).or(value.created),
starting_at: created_gte.or(created_gt).or(created),
ending_at: created_lte.or(created_lt).or(created),
amount_filter: None,
connector: None,
currency: None,
Expand All @@ -772,10 +793,10 @@ impl From<api_models::payments::PaymentListConstraints> for PaymentIntentFetchCo
authentication_type: None,
merchant_connector_id: None,
profile_id: None,
customer_id: value.customer_id,
starting_after_id: value.starting_after,
ending_before_id: value.ending_before,
limit: Some(std::cmp::min(value.limit, PAYMENTS_LIST_MAX_LIMIT_V1)),
customer_id,
starting_after_id: starting_after,
ending_before_id: ending_before,
limit: Some(std::cmp::min(limit, PAYMENTS_LIST_MAX_LIMIT_V1)),
order: Default::default(),
}))
}
Expand Down Expand Up @@ -807,28 +828,96 @@ impl From<api_models::payments::TimeRange> for PaymentIntentFetchConstraints {

impl From<api_models::payments::PaymentListFilterConstraints> for PaymentIntentFetchConstraints {
fn from(value: api_models::payments::PaymentListFilterConstraints) -> Self {
if let Some(payment_intent_id) = value.payment_id {
let api_models::payments::PaymentListFilterConstraints {
payment_id,
profile_id,
customer_id,
limit,
offset,
amount_filter,
time_range,
connector,
currency,
status,
payment_method,
payment_method_type,
authentication_type,
merchant_connector_id,
order,
} = value;
if let Some(payment_intent_id) = payment_id {
Self::Single { payment_intent_id }
} else {
Self::List(Box::new(PaymentIntentListParams {
offset: value.offset.unwrap_or_default(),
starting_at: value.time_range.map(|t| t.start_time),
ending_at: value.time_range.and_then(|t| t.end_time),
amount_filter: value.amount_filter,
connector: value.connector,
currency: value.currency,
status: value.status,
payment_method: value.payment_method,
payment_method_type: value.payment_method_type,
authentication_type: value.authentication_type,
merchant_connector_id: value.merchant_connector_id,
profile_id: value.profile_id,
customer_id: value.customer_id,
offset: offset.unwrap_or_default(),
starting_at: time_range.map(|t| t.start_time),
ending_at: time_range.and_then(|t| t.end_time),
amount_filter,
connector,
currency,
status,
payment_method,
payment_method_type,
authentication_type,
merchant_connector_id,
profile_id: profile_id.map(|profile_id| vec![profile_id]),
customer_id,
starting_after_id: None,
ending_before_id: None,
limit: Some(std::cmp::min(value.limit, PAYMENTS_LIST_MAX_LIMIT_V2)),
order: value.order,
limit: Some(std::cmp::min(limit, PAYMENTS_LIST_MAX_LIMIT_V2)),
order,
}))
}
}
}

impl<T> TryFrom<(T, Option<Vec<id_type::ProfileId>>)> for PaymentIntentFetchConstraints
where
Self: From<T>,
{
type Error = error_stack::Report<errors::api_error_response::ApiErrorResponse>;
fn try_from(
(constraints, auth_profile_id_list): (T, Option<Vec<id_type::ProfileId>>),
) -> Result<Self, Self::Error> {
let payment_intent_constraints = Self::from(constraints);
if let Self::List(mut pi_list_params) = payment_intent_constraints {
let profile_id_from_request_body = pi_list_params.profile_id;
match (profile_id_from_request_body, auth_profile_id_list) {
(None, None) => pi_list_params.profile_id = None,
(None, Some(auth_profile_id_list)) => {
pi_list_params.profile_id = Some(auth_profile_id_list)
}
(Some(profile_id_from_request_body), None) => {
pi_list_params.profile_id = Some(profile_id_from_request_body)
}
(Some(profile_id_from_request_body), Some(auth_profile_id_list)) => {
let profile_id_from_request_body_is_available_in_auth_profile_id_list =
profile_id_from_request_body
.iter()
.all(|profile_id| auth_profile_id_list.contains(profile_id));

if profile_id_from_request_body_is_available_in_auth_profile_id_list {
pi_list_params.profile_id = Some(profile_id_from_request_body)
} else {
// This scenario is very unlikely to happen
let inaccessible_profile_ids: Vec<_> = profile_id_from_request_body
.iter()
.filter(|profile_id| !auth_profile_id_list.contains(profile_id))
.collect();
return Err(error_stack::Report::new(
errors::api_error_response::ApiErrorResponse::PreconditionFailed {
message: format!(
"Access not available for the given profile_id {:?}",
inaccessible_profile_ids
),
},
));
}
}
}
Ok(Self::List(pi_list_params))
} else {
Ok(payment_intent_constraints)
}
}
}
82 changes: 82 additions & 0 deletions crates/hyperswitch_domain_models/src/refunds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::errors;

pub struct RefundListConstraints {
pub payment_id: Option<common_utils::id_type::PaymentId>,
pub refund_id: Option<String>,
pub profile_id: Option<Vec<common_utils::id_type::ProfileId>>,
pub limit: Option<i64>,
pub offset: Option<i64>,
pub time_range: Option<api_models::payments::TimeRange>,
pub amount_filter: Option<api_models::payments::AmountFilter>,
pub connector: Option<Vec<String>>,
pub merchant_connector_id: Option<Vec<common_utils::id_type::MerchantConnectorAccountId>>,
pub currency: Option<Vec<common_enums::Currency>>,
pub refund_status: Option<Vec<common_enums::RefundStatus>>,
}

impl
TryFrom<(
api_models::refunds::RefundListRequest,
Option<Vec<common_utils::id_type::ProfileId>>,
)> for RefundListConstraints
{
type Error = error_stack::Report<errors::api_error_response::ApiErrorResponse>;

fn try_from(
(value, auth_profile_id_list): (
api_models::refunds::RefundListRequest,
Option<Vec<common_utils::id_type::ProfileId>>,
),
) -> Result<Self, Self::Error> {
let api_models::refunds::RefundListRequest {
connector,
currency,
refund_status,
payment_id,
refund_id,
profile_id,
limit,
offset,
time_range,
amount_filter,
merchant_connector_id,
} = value;
let profile_id_from_request_body = profile_id;
let profile_id_list = match (profile_id_from_request_body, auth_profile_id_list) {
(None, None) => None,
(None, Some(auth_profile_id_list)) => Some(auth_profile_id_list),
(Some(profile_id_from_request_body), None) => Some(vec![profile_id_from_request_body]),
(Some(profile_id_from_request_body), Some(auth_profile_id_list)) => {
let profile_id_from_request_body_is_available_in_auth_profile_id_list =
auth_profile_id_list.contains(&profile_id_from_request_body);

if profile_id_from_request_body_is_available_in_auth_profile_id_list {
Some(vec![profile_id_from_request_body])
} else {
// This scenario is very unlikely to happen
return Err(error_stack::Report::new(
errors::api_error_response::ApiErrorResponse::PreconditionFailed {
message: format!(
"Access not available for the given profile_id {:?}",
profile_id_from_request_body
),
},
));
}
}
};
Ok(Self {
payment_id,
refund_id,
profile_id: profile_id_list,
limit,
offset,
time_range,
amount_filter,
connector,
merchant_connector_id,
currency,
refund_status,
})
}
}
13 changes: 6 additions & 7 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2918,15 +2918,13 @@ pub async fn list_payments(
let db = state.store.as_ref();
let payment_intents = helpers::filter_by_constraints(
&state,
&constraints,
&(constraints, profile_id_list).try_into()?,
merchant_id,
&key_store,
merchant.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
let payment_intents =
utils::filter_objects_based_on_profile_id_list(profile_id_list, payment_intents);

let collected_futures = payment_intents.into_iter().map(|pi| {
async {
Expand Down Expand Up @@ -2989,25 +2987,25 @@ pub async fn apply_filters_on_payments(
) -> RouterResponse<api::PaymentListResponseV2> {
let limit = &constraints.limit;
helpers::validate_payment_list_request_for_joins(*limit)?;
let db = state.store.as_ref();
let db: &dyn StorageInterface = state.store.as_ref();
let pi_fetch_constraints = (constraints.clone(), profile_id_list.clone()).try_into()?;
let list: Vec<(storage::PaymentIntent, storage::PaymentAttempt)> = db
.get_filtered_payment_intents_attempt(
&(&state).into(),
merchant.get_id(),
&constraints.clone().into(),
&pi_fetch_constraints,
&merchant_key_store,
merchant.storage_scheme,
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
let list = utils::filter_objects_based_on_profile_id_list(profile_id_list, list);
let data: Vec<api::PaymentsResponse> =
list.into_iter().map(ForeignFrom::foreign_from).collect();

let active_attempt_ids = db
.get_filtered_active_attempt_ids_for_total_count(
merchant.get_id(),
&constraints.clone().into(),
&pi_fetch_constraints,
merchant.storage_scheme,
)
.await
Expand All @@ -3022,6 +3020,7 @@ pub async fn apply_filters_on_payments(
constraints.payment_method_type,
constraints.authentication_type,
constraints.merchant_connector_id,
pi_fetch_constraints.get_profile_id_list(),
merchant.storage_scheme,
)
.await
Expand Down
9 changes: 6 additions & 3 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use hyperswitch_domain_models::payments::payment_intent::CustomerData;
use hyperswitch_domain_models::{
mandates::MandateData,
payment_method_data::GetPaymentMethodType,
payments::{payment_attempt::PaymentAttempt, PaymentIntent},
payments::{
payment_attempt::PaymentAttempt, payment_intent::PaymentIntentFetchConstraints,
PaymentIntent,
},
router_data::KlarnaSdkResponse,
};
use hyperswitch_interfaces::integrity::{CheckIntegrity, FlowIntegrity, GetIntegrityObject};
Expand Down Expand Up @@ -2538,7 +2541,7 @@ where
#[cfg(feature = "olap")]
pub(super) async fn filter_by_constraints(
state: &SessionState,
constraints: &api::PaymentListConstraints,
constraints: &PaymentIntentFetchConstraints,
merchant_id: &id_type::MerchantId,
key_store: &domain::MerchantKeyStore,
storage_scheme: storage_enums::MerchantStorageScheme,
Expand All @@ -2548,7 +2551,7 @@ pub(super) async fn filter_by_constraints(
.filter_payment_intent_by_constraints(
&(state).into(),
merchant_id,
&constraints.clone().into(),
constraints,
key_store,
storage_scheme,
)
Expand Down
Loading
Loading