fix(router): correct limit for payments list by filters (#2060)

This commit is contained in:
Apoorv Dixit
2023-09-01 15:04:18 +05:30
committed by GitHub
parent 8b22f38dd6
commit b7d6d31504
5 changed files with 36 additions and 18 deletions

View File

@ -1991,8 +1991,9 @@ pub struct PaymentListResponseV2 {
pub struct PaymentListFilterConstraints { pub struct PaymentListFilterConstraints {
/// The identifier for payment /// The identifier for payment
pub payment_id: Option<String>, pub payment_id: Option<String>,
/// The limit on the number of objects. The max limit is 20 /// The limit on the number of objects. The default limit is 10 and max limit is 20
pub limit: Option<u32>, #[serde(default = "default_limit")]
pub limit: u32,
/// The starting point within a list of objects /// The starting point within a list of objects
pub offset: Option<u32>, pub offset: Option<u32>,
/// The time range for which objects are needed. TimeRange has two fields start_time and end_time from which objects can be filtered as per required scenarios (created_at, time less than, greater than etc). /// The time range for which objects are needed. TimeRange has two fields start_time and end_time from which objects can be filtered as per required scenarios (created_at, time less than, greater than etc).

View File

@ -18,3 +18,8 @@ pub const TOKEN_TTL: i64 = 900;
pub static FRM_CONFIGS_EG: &str = r#" pub static FRM_CONFIGS_EG: &str = r#"
[{"gateway":"stripe","payment_methods":[{"payment_method":"card","payment_method_types":[{"payment_method_type":"credit","card_networks":["Visa"],"flow":"pre","action":"cancel_txn"},{"payment_method_type":"debit","card_networks":["Visa"],"flow":"pre"}]}]}] [{"gateway":"stripe","payment_methods":[{"payment_method":"card","payment_method_types":[{"payment_method_type":"credit","card_networks":["Visa"],"flow":"pre","action":"cancel_txn"},{"payment_method_type":"debit","card_networks":["Visa"],"flow":"pre"}]}]}]
"#; "#;
/// Maximum limit for payments list get api
pub const PAYMENTS_LIST_MAX_LIMIT_V1: u32 = 100;
/// Maximum limit for payments list post api with filters
pub const PAYMENTS_LIST_MAX_LIMIT_V2: u32 = 20;

View File

@ -1,10 +1,12 @@
use common_enums as storage_enums; use common_enums as storage_enums;
use common_utils::pii; use common_utils::{
consts::{PAYMENTS_LIST_MAX_LIMIT_V1, PAYMENTS_LIST_MAX_LIMIT_V2},
pii,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime; use time::PrimitiveDateTime;
use crate::{errors, MerchantStorageScheme}; use crate::{errors, MerchantStorageScheme};
const MAX_LIMIT: u32 = 100;
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait PaymentIntentInterface { pub trait PaymentIntentInterface {
async fn update_payment_intent( async fn update_payment_intent(
@ -388,7 +390,7 @@ impl From<api_models::payments::PaymentListConstraints> for PaymentIntentFetchCo
customer_id: value.customer_id, customer_id: value.customer_id,
starting_after_id: value.starting_after, starting_after_id: value.starting_after,
ending_before_id: value.ending_before, ending_before_id: value.ending_before,
limit: Some(std::cmp::min(value.limit, MAX_LIMIT)), limit: Some(std::cmp::min(value.limit, PAYMENTS_LIST_MAX_LIMIT_V1)),
} }
} }
} }
@ -427,7 +429,7 @@ impl From<api_models::payments::PaymentListFilterConstraints> for PaymentIntentF
customer_id: None, customer_id: None,
starting_after_id: None, starting_after_id: None,
ending_before_id: None, ending_before_id: None,
limit: value.limit, limit: Some(std::cmp::min(value.limit, PAYMENTS_LIST_MAX_LIMIT_V2)),
} }
} }
} }

View File

@ -42,8 +42,6 @@ use crate::{
utils::{add_connector_http_status_code_metrics, Encode, OptionExt, ValueExt}, utils::{add_connector_http_status_code_metrics, Encode, OptionExt, ValueExt},
}; };
#[cfg(feature = "olap")]
const PAYMENTS_LIST_MAX_LIMIT: u32 = 20;
#[instrument(skip_all, fields(payment_id, merchant_id))] #[instrument(skip_all, fields(payment_id, merchant_id))]
pub async fn payments_operation_core<F, Req, Op, FData>( pub async fn payments_operation_core<F, Req, Op, FData>(
state: &AppState, state: &AppState,
@ -1326,9 +1324,9 @@ pub async fn apply_filters_on_payments(
use crate::types::transformers::ForeignFrom; use crate::types::transformers::ForeignFrom;
let limit = &constraints.limit.unwrap_or(PAYMENTS_LIST_MAX_LIMIT); let limit = &constraints.limit;
helpers::validate_payment_list_request_for_joins(*limit, PAYMENTS_LIST_MAX_LIMIT)?; helpers::validate_payment_list_request_for_joins(*limit)?;
let list: Vec<(storage::PaymentIntent, storage::PaymentAttempt)> = db let list: Vec<(storage::PaymentIntent, storage::PaymentAttempt)> = db
.get_filtered_payment_intents_attempt( .get_filtered_payment_intents_attempt(
&merchant.merchant_id, &merchant.merchant_id,

View File

@ -1683,21 +1683,33 @@ pub(super) async fn filter_by_constraints(
pub(super) fn validate_payment_list_request( pub(super) fn validate_payment_list_request(
req: &api::PaymentListConstraints, req: &api::PaymentListConstraints,
) -> CustomResult<(), errors::ApiErrorResponse> { ) -> CustomResult<(), errors::ApiErrorResponse> {
utils::when(req.limit > 100 || req.limit < 1, || { use common_utils::consts::PAYMENTS_LIST_MAX_LIMIT_V1;
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "limit should be in between 1 and 100".to_string(), utils::when(
}) req.limit > PAYMENTS_LIST_MAX_LIMIT_V1 || req.limit < 1,
})?; || {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: format!(
"limit should be in between 1 and {}",
PAYMENTS_LIST_MAX_LIMIT_V1
),
})
},
)?;
Ok(()) Ok(())
} }
#[cfg(feature = "olap")] #[cfg(feature = "olap")]
pub(super) fn validate_payment_list_request_for_joins( pub(super) fn validate_payment_list_request_for_joins(
limit: u32, limit: u32,
max_limit: u32,
) -> CustomResult<(), errors::ApiErrorResponse> { ) -> CustomResult<(), errors::ApiErrorResponse> {
utils::when(limit > max_limit || limit < 1, || { use common_utils::consts::PAYMENTS_LIST_MAX_LIMIT_V2;
utils::when(!(1..=PAYMENTS_LIST_MAX_LIMIT_V2).contains(&limit), || {
Err(errors::ApiErrorResponse::InvalidRequestData { Err(errors::ApiErrorResponse::InvalidRequestData {
message: format!("limit should be in between 1 and {}", max_limit), message: format!(
"limit should be in between 1 and {}",
PAYMENTS_LIST_MAX_LIMIT_V2
),
}) })
})?; })?;
Ok(()) Ok(())