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
feat(blocklist): return total_count in list blocklist response (#11594)
Ayush Anandde31a341f465
diff --git a/crates/api_models/src/blocklist.rs b/crates/api_models/src/blocklist.rsindex 1f9900704a..04cab94106 100644--- a/crates/api_models/src/blocklist.rs+++ b/crates/api_models/src/blocklist.rs@@ -45,6 +45,16 @@ pub struct ToggleBlocklistResponse {pub type AddToBlocklistResponse = BlocklistResponse;pub type DeleteFromBlocklistResponse = BlocklistResponse;+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]+pub struct ListBlocklistResponse {+ /// The number of blocked entries in the current response+ pub count: usize,+ /// The total number of blocked entries for the given data_kind+ pub total_count: usize,+ /// The list of blocked payment method entries+ pub data: Vec<BlocklistResponse>,+}+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]pub struct ListBlocklistQuery {#[schema(value_type = BlocklistDataKind)]@@ -68,6 +78,7 @@ pub struct ToggleBlocklistQuery {impl ApiEventMetric for BlocklistRequest {}impl ApiEventMetric for BlocklistResponse {}+impl ApiEventMetric for ListBlocklistResponse {}impl ApiEventMetric for ToggleBlocklistResponse {}impl ApiEventMetric for ListBlocklistQuery {}impl ApiEventMetric for GenerateFingerprintRequest {}diff --git a/crates/diesel_models/src/query/blocklist.rs b/crates/diesel_models/src/query/blocklist.rsindex b295765981..051c86bc71 100644--- a/crates/diesel_models/src/query/blocklist.rs+++ b/crates/diesel_models/src/query/blocklist.rs@@ -47,6 +47,20 @@ impl Blocklist {.await}+ pub async fn get_count_by_merchant_id_data_kind(+ conn: &PgPooledConn,+ merchant_id: &common_utils::id_type::MerchantId,+ data_kind: common_enums::BlocklistDataKind,+ ) -> StorageResult<usize> {+ generics::generic_count::<<Self as HasTable>::Table, _>(+ conn,+ dsl::merchant_id+ .eq(merchant_id.to_owned())+ .and(dsl::data_kind.eq(data_kind.to_owned())),+ )+ .await+ }+pub async fn list_by_merchant_id(conn: &PgPooledConn,merchant_id: &common_utils::id_type::MerchantId,diff --git a/crates/router/src/core/blocklist.rs b/crates/router/src/core/blocklist.rsindex c0c7e76c11..8a5b2dc1f3 100644--- a/crates/router/src/core/blocklist.rs+++ b/crates/router/src/core/blocklist.rs@@ -42,7 +42,7 @@ pub async fn list_blocklist_entries(state: SessionState,platform: domain::Platform,query: api_blocklist::ListBlocklistQuery,-) -> RouterResponse<Vec<api_blocklist::BlocklistResponse>> {+) -> RouterResponse<api_blocklist::ListBlocklistResponse> {utils::list_blocklist_entries_for_merchant(&state,platform.get_processor().get_account().get_id(),diff --git a/crates/router/src/core/blocklist/utils.rs b/crates/router/src/core/blocklist/utils.rsindex 93c1b859de..34a0cd6610 100644--- a/crates/router/src/core/blocklist/utils.rs+++ b/crates/router/src/core/blocklist/utils.rs@@ -91,20 +91,36 @@ pub async fn list_blocklist_entries_for_merchant(state: &SessionState,merchant_id: &common_utils::id_type::MerchantId,query: api_blocklist::ListBlocklistQuery,-) -> RouterResult<Vec<api_blocklist::BlocklistResponse>> {- state+) -> RouterResult<api_blocklist::ListBlocklistResponse> {+ let entries = state.store.list_blocklist_entries_by_merchant_id_data_kind(merchant_id,- query.data_kind,+ query.data_kind.clone(),query.limit.into(),query.offset.into(),).await.to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {message: "no blocklist records found".to_string(),- })- .map(|v| v.into_iter().map(ForeignInto::foreign_into).collect())+ })?;++ let total_count = state+ .store+ .get_blocklist_entries_count_by_merchant_id_data_kind(merchant_id, query.data_kind)+ .await+ .to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {+ message: "no blocklist records found".to_string(),+ })?;++ let data: Vec<api_blocklist::BlocklistResponse> =+ entries.into_iter().map(ForeignInto::foreign_into).collect();++ Ok(api_blocklist::ListBlocklistResponse {+ count: data.len(),+ total_count,+ data,+ })}fn validate_card_bin(bin: &str) -> RouterResult<()> {diff --git a/crates/router/src/db/blocklist.rs b/crates/router/src/db/blocklist.rsindex 4740af9f81..bc7c405483 100644--- a/crates/router/src/db/blocklist.rs+++ b/crates/router/src/db/blocklist.rs@@ -1,4 +1,4 @@-use error_stack::report;+use error_stack::{report, ResultExt};use router_env::{instrument, tracing};use storage_impl::MockDb;@@ -41,6 +41,12 @@ pub trait BlocklistInterface {limit: i64,offset: i64,) -> CustomResult<Vec<storage::Blocklist>, errors::StorageError>;++ async fn get_blocklist_entries_count_by_merchant_id_data_kind(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ data_kind: common_enums::BlocklistDataKind,+ ) -> CustomResult<usize, errors::StorageError>;}#[async_trait::async_trait]@@ -88,7 +94,7 @@ impl BlocklistInterface for Store {limit: i64,offset: i64,) -> CustomResult<Vec<storage::Blocklist>, errors::StorageError> {- let conn = connection::pg_connection_write(self).await?;+ let conn = connection::pg_connection_read(self).await?;storage::Blocklist::list_by_merchant_id_data_kind(&conn,merchant_id,@@ -97,7 +103,23 @@ impl BlocklistInterface for Store {offset,).await- .map_err(|error| report!(errors::StorageError::from(error)))+ .change_context(errors::StorageError::DatabaseError(report!(+ diesel_models::errors::DatabaseError::Others+ )))+ }++ #[instrument(skip_all)]+ async fn get_blocklist_entries_count_by_merchant_id_data_kind(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ data_kind: common_enums::BlocklistDataKind,+ ) -> CustomResult<usize, errors::StorageError> {+ let conn = connection::pg_connection_read(self).await?;+ storage::Blocklist::get_count_by_merchant_id_data_kind(&conn, merchant_id, data_kind)+ .await+ .change_context(errors::StorageError::DatabaseError(report!(+ diesel_models::errors::DatabaseError::Others+ )))}#[instrument(skip_all)]@@ -148,6 +170,14 @@ impl BlocklistInterface for MockDb {Err(errors::StorageError::MockDbError)?}+ async fn get_blocklist_entries_count_by_merchant_id_data_kind(+ &self,+ _merchant_id: &common_utils::id_type::MerchantId,+ _data_kind: common_enums::BlocklistDataKind,+ ) -> CustomResult<usize, errors::StorageError> {+ Err(errors::StorageError::MockDbError)?+ }+async fn delete_blocklist_entry_by_merchant_id_fingerprint_id(&self,_merchant_id: &common_utils::id_type::MerchantId,@@ -202,6 +232,17 @@ impl BlocklistInterface for KafkaStore {.await}+ #[instrument(skip_all)]+ async fn get_blocklist_entries_count_by_merchant_id_data_kind(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ data_kind: common_enums::BlocklistDataKind,+ ) -> CustomResult<usize, errors::StorageError> {+ self.diesel_store+ .get_blocklist_entries_count_by_merchant_id_data_kind(merchant_id, data_kind)+ .await+ }+#[instrument(skip_all)]async fn list_blocklist_entries_by_merchant_id(&self,diff --git a/crates/router/src/routes/blocklist.rs b/crates/router/src/routes/blocklist.rsindex e50503daf0..ad2858885d 100644--- a/crates/router/src/routes/blocklist.rs+++ b/crates/router/src/routes/blocklist.rs@@ -102,7 +102,7 @@ pub async fn remove_entry_from_blocklist(("data_kind" = BlocklistDataKind, Query, description = "Kind of the fingerprint list requested"),),responses(- (status = 200, description = "Blocked Fingerprints", body = BlocklistResponse),+ (status = 200, description = "Blocked Fingerprints", body = ListBlocklistResponse),(status = 400, description = "Invalid Data")),tag = "Blocklist",