Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
Refactor(Customer): Introduced a wrapper function for the limit value in the Customer page and removed the profile_id field (#9986)
Venu Madhav Bandarupallidb61eb51071f
diff --git a/crates/api_models/src/customers.rs b/crates/api_models/src/customers.rsindex 536d630a81..5bc9d1fd3d 100644--- a/crates/api_models/src/customers.rs+++ b/crates/api_models/src/customers.rs@@ -1,3 +1,4 @@+use common_types::primitive_wrappers::CustomerListLimit;use common_utils::{crypto, custom_serde, id_type, pii, types::Description};use masking::Secret;use serde::{Deserialize, Serialize};@@ -62,7 +63,7 @@ pub struct CustomerListRequestWithConstraints {pub offset: Option<u32>,/// Limit#[schema(example = 32)]- pub limit: Option<u16>,+ pub limit: Option<CustomerListLimit>,/// Unique identifier for a customerpub customer_id: Option<id_type::CustomerId>,/// Filter with created time rangediff --git a/crates/common_types/src/consts.rs b/crates/common_types/src/consts.rsindex 2e622307d7..e8f78c4dbe 100644--- a/crates/common_types/src/consts.rs+++ b/crates/common_types/src/consts.rs@@ -14,6 +14,15 @@ pub const MAX_DISPUTE_POLLING_INTERVAL_IN_HOURS: i32 = 24;///Default Dispute Polling Interval In Hourspub const DEFAULT_DISPUTE_POLLING_INTERVAL_IN_HOURS: i32 = 24;+/// Customer List Lower Limit+pub const CUSTOMER_LIST_LOWER_LIMIT: u16 = 1;++/// Customer List Upper Limit+pub const CUSTOMER_LIST_UPPER_LIMIT: u16 = 100;++/// Customer List Default Limit+pub const CUSTOMER_LIST_DEFAULT_LIMIT: u16 = 20;+/// Default payment intent statuses that trigger a webhookpub const DEFAULT_PAYMENT_WEBHOOK_TRIGGER_STATUSES: &[common_enums::IntentStatus] = &[common_enums::IntentStatus::Succeeded,diff --git a/crates/common_types/src/primitive_wrappers.rs b/crates/common_types/src/primitive_wrappers.rsindex ccb6a08bb0..e017e3f510 100644--- a/crates/common_types/src/primitive_wrappers.rs+++ b/crates/common_types/src/primitive_wrappers.rs@@ -1,5 +1,6 @@pub use bool_wrappers::*;pub use safe_string::*;+pub use u16_wrappers::*;pub use u32_wrappers::*;mod bool_wrappers {use std::ops::Deref;@@ -435,6 +436,64 @@ mod u32_wrappers {}}+mod u16_wrappers {+ use std::ops::Deref;++ use serde::{de::Error, Deserialize, Serialize};++ use crate::consts::{+ CUSTOMER_LIST_DEFAULT_LIMIT, CUSTOMER_LIST_LOWER_LIMIT, CUSTOMER_LIST_UPPER_LIMIT,+ };++ /// Customer list limit wrapper with automatic validation+ #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]+ pub struct CustomerListLimit(u16);++ impl Deref for CustomerListLimit {+ type Target = u16;++ fn deref(&self) -> &Self::Target {+ &self.0+ }+ }++ impl<'de> Deserialize<'de> for CustomerListLimit {+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ let val = u16::deserialize(deserializer)?;+ Self::new(val).map_err(D::Error::custom)+ }+ }++ impl Default for CustomerListLimit {+ /// Default for `CustomerListLimit` is `20`+ fn default() -> Self {+ Self(CUSTOMER_LIST_DEFAULT_LIMIT)+ }+ }++ impl CustomerListLimit {+ /// Creates a new CustomerListLimit with validation+ pub fn new(value: u16) -> Result<Self, String> {+ if value < CUSTOMER_LIST_LOWER_LIMIT {+ Err(format!(+ "CustomerListLimit cannot be less than {}",+ CUSTOMER_LIST_LOWER_LIMIT+ ))+ } else if value > CUSTOMER_LIST_UPPER_LIMIT {+ Err(format!(+ "CustomerListLimit exceeds the maximum allowed value of {}",+ CUSTOMER_LIST_UPPER_LIMIT+ ))+ } else {+ Ok(Self(value))+ }+ }+ }+}+/// Safe string wrapper that validates input against XSS attacksmod safe_string {use std::ops::Deref;diff --git a/crates/router/src/core/customers.rs b/crates/router/src/core/customers.rsindex 42deeddf51..162afd38e6 100644--- a/crates/router/src/core/customers.rs+++ b/crates/router/src/core/customers.rs@@ -1,3 +1,4 @@+use common_types::primitive_wrappers::CustomerListLimit;use common_utils::{crypto::Encryptable,errors::ReportSwitchExt,@@ -22,10 +23,6 @@ use crate::{core::{errors::{self, StorageErrorExt},payment_methods::{cards, network_tokenization},- utils::{- self,- customer_validation::{CUSTOMER_LIST_LOWER_LIMIT, CUSTOMER_LIST_UPPER_LIMIT},- },},db::StorageInterface,pii::PeekInterface,@@ -627,20 +624,15 @@ pub async fn list_customers(pub async fn list_customers_with_count(state: SessionState,merchant_id: id_type::MerchantId,- _profile_id_list: Option<Vec<id_type::ProfileId>>,key_store: domain::MerchantKeyStore,request: customers::CustomerListRequestWithConstraints,) -> errors::CustomerResponse<customers::CustomerListResponse> {let db = state.store.as_ref();- let limit = utils::customer_validation::validate_customer_list_limit(request.limit)- .change_context(errors::CustomersErrorResponse::InvalidRequestData {- message: format!(- "limit should be between {CUSTOMER_LIST_LOWER_LIMIT} and {CUSTOMER_LIST_UPPER_LIMIT}"- ),- })?;-let customer_list_constraints = crate::db::customers::CustomerListConstraints {- limit: request.limit.unwrap_or(limit),+ limit: request+ .limit+ .map(|l| *l)+ .unwrap_or_else(|| *CustomerListLimit::default()),offset: request.offset,customer_id: request.customer_id,time_range: request.time_range,diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex aaff70cb30..baa59ff90b 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -1,4 +1,3 @@-pub mod customer_validation;pub mod refunds_transformers;pub mod refunds_validator;diff --git a/crates/router/src/core/utils/customer_validation.rs b/crates/router/src/core/utils/customer_validation.rsdeleted file mode 100644index 14df0fb0e0..0000000000--- a/crates/router/src/core/utils/customer_validation.rs+++ /dev/null@@ -1,21 +0,0 @@-use crate::core::errors::{self, CustomResult};--pub const CUSTOMER_LIST_LOWER_LIMIT: u16 = 1;-pub const CUSTOMER_LIST_UPPER_LIMIT: u16 = 100;-pub const CUSTOMER_LIST_DEFAULT_LIMIT: u16 = 10;--pub fn validate_customer_list_limit(- limit: Option<u16>,-) -> CustomResult<u16, errors::ApiErrorResponse> {- match limit {- Some(l) if (CUSTOMER_LIST_LOWER_LIMIT..=CUSTOMER_LIST_UPPER_LIMIT).contains(&l) => Ok(l),- Some(_) => Err(errors::ApiErrorResponse::InvalidRequestData {- message: format!(- "limit should be between {} and {}",- CUSTOMER_LIST_LOWER_LIMIT, CUSTOMER_LIST_UPPER_LIMIT- ),- }- .into()),- None => Ok(CUSTOMER_LIST_DEFAULT_LIMIT),- }-}diff --git a/crates/router/src/routes/customers.rs b/crates/router/src/routes/customers.rsindex 0c37afd10b..8c528d3201 100644--- a/crates/router/src/routes/customers.rs+++ b/crates/router/src/routes/customers.rs@@ -258,7 +258,6 @@ pub async fn customers_list_with_count(list_customers_with_count(state,auth.merchant_account.get_id().to_owned(),- None,auth.key_store,request,)@@ -296,7 +295,6 @@ pub async fn customers_list_with_count(list_customers_with_count(state,auth.merchant_account.get_id().to_owned(),- None,auth.key_store,request,)