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(refunds): Refunds aggregate api (#5795)
Gitanjli00386f32954a
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.jsonindex 2af9ccb87d..b80319add8 100644--- a/api-reference-v2/openapi_spec.json+++ b/api-reference-v2/openapi_spec.json@@ -16469,6 +16469,22 @@}}},+ "RefundAggregateResponse": {+ "type": "object",+ "required": [+ "status_with_count"+ ],+ "properties": {+ "status_with_count": {+ "type": "object",+ "description": "The list of refund status with their count",+ "additionalProperties": {+ "type": "integer",+ "format": "int64"+ }+ }+ }+ },"RefundListRequest": {"allOf": [{diff --git a/api-reference/openapi_spec.json b/api-reference/openapi_spec.jsonindex 8bc958c619..58d11fc49a 100644--- a/api-reference/openapi_spec.json+++ b/api-reference/openapi_spec.json@@ -20927,6 +20927,22 @@}}},+ "RefundAggregateResponse": {+ "type": "object",+ "required": [+ "status_with_count"+ ],+ "properties": {+ "status_with_count": {+ "type": "object",+ "description": "The list of refund status with their count",+ "additionalProperties": {+ "type": "integer",+ "format": "int64"+ }+ }+ }+ },"RefundListRequest": {"allOf": [{diff --git a/crates/api_models/src/events/refund.rs b/crates/api_models/src/events/refund.rsindex 0717b66c6b..d180753735 100644--- a/crates/api_models/src/events/refund.rs+++ b/crates/api_models/src/events/refund.rs@@ -1,9 +1,9 @@use common_utils::events::{ApiEventMetric, ApiEventsType};use crate::refunds::{- RefundListFilters, RefundListMetaData, RefundListRequest, RefundListResponse,- RefundManualUpdateRequest, RefundRequest, RefundResponse, RefundUpdateRequest,- RefundsRetrieveRequest,+ RefundAggregateResponse, RefundListFilters, RefundListMetaData, RefundListRequest,+ RefundListResponse, RefundManualUpdateRequest, RefundRequest, RefundResponse,+ RefundUpdateRequest, RefundsRetrieveRequest,};impl ApiEventMetric for RefundRequest {@@ -66,6 +66,12 @@ impl ApiEventMetric for RefundListResponse {}}+impl ApiEventMetric for RefundAggregateResponse {+ fn get_api_event_type(&self) -> Option<ApiEventsType> {+ Some(ApiEventsType::ResourceListAPI)+ }+}+impl ApiEventMetric for RefundListMetaData {fn get_api_event_type(&self) -> Option<ApiEventsType> {Some(ApiEventsType::ResourceListAPI)diff --git a/crates/api_models/src/refunds.rs b/crates/api_models/src/refunds.rsindex 649c0dbf5c..edc0e75bdf 100644--- a/crates/api_models/src/refunds.rs+++ b/crates/api_models/src/refunds.rs@@ -234,6 +234,12 @@ pub struct RefundListFilters {pub refund_status: Vec<enums::RefundStatus>,}+#[derive(Clone, Debug, serde::Serialize, ToSchema)]+pub struct RefundAggregateResponse {+ /// The list of refund status with their count+ pub status_with_count: HashMap<enums::RefundStatus, i64>,+}+/// The status for refunds#[derive(Debug,diff --git a/crates/openapi/src/openapi.rs b/crates/openapi/src/openapi.rsindex 4724f448e4..93975cc57c 100644--- a/crates/openapi/src/openapi.rs+++ b/crates/openapi/src/openapi.rs@@ -463,6 +463,7 @@ Never share your secret api keys. Keep them guarded and secure.api_models::payment_methods::PaymentMethodCollectLinkResponse,api_models::refunds::RefundListRequest,api_models::refunds::RefundListResponse,+ api_models::refunds::RefundAggregateResponse,api_models::payments::TimeRange,api_models::payments::AmountFilter,api_models::mandates::MandateRevokedResponse,diff --git a/crates/openapi/src/openapi_v2.rs b/crates/openapi/src/openapi_v2.rsindex 0debe22db0..dd25884331 100644--- a/crates/openapi/src/openapi_v2.rs+++ b/crates/openapi/src/openapi_v2.rs@@ -381,6 +381,7 @@ Never share your secret api keys. Keep them guarded and secure.api_models::payment_methods::PaymentMethodCollectLinkResponse,api_models::refunds::RefundListRequest,api_models::refunds::RefundListResponse,+ api_models::refunds::RefundAggregateResponse,api_models::payments::TimeRange,api_models::payments::AmountFilter,api_models::mandates::MandateRevokedResponse,diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rsindex a5a13c8304..c30ef75565 100644--- a/crates/router/src/core/refunds.rs+++ b/crates/router/src/core/refunds.rs@@ -1034,6 +1034,32 @@ pub async fn get_filters_for_refunds())}+#[instrument(skip_all)]+#[cfg(feature = "olap")]+pub async fn get_aggregates_for_refunds(+ state: SessionState,+ merchant: domain::MerchantAccount,+ time_range: api::TimeRange,+) -> RouterResponse<api_models::refunds::RefundAggregateResponse> {+ let db = state.store.as_ref();+ let refund_status_with_count = db+ .get_refund_status_with_count(merchant.get_id(), &time_range, merchant.storage_scheme)+ .await+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("Failed to find status count")?;+ let mut status_map: HashMap<enums::RefundStatus, i64> =+ refund_status_with_count.into_iter().collect();+ for status in enums::RefundStatus::iter() {+ status_map.entry(status).or_default();+ }++ Ok(services::ApplicationResponse::Json(+ api_models::refunds::RefundAggregateResponse {+ status_with_count: status_map,+ },+ ))+}+impl ForeignFrom<storage::Refund> for api::RefundResponse {fn foreign_from(refund: storage::Refund) -> Self {let refund = refund;diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rsindex 76111afc6b..641b4c8b0e 100644--- a/crates/router/src/db/kafka_store.rs+++ b/crates/router/src/db/kafka_store.rs@@ -2394,6 +2394,18 @@ impl RefundInterface for KafkaStore {.await}+ #[cfg(feature = "olap")]+ async fn get_refund_status_with_count(+ &self,+ merchant_id: &id_type::MerchantId,+ constraints: &api_models::payments::TimeRange,+ storage_scheme: MerchantStorageScheme,+ ) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {+ self.diesel_store+ .get_refund_status_with_count(merchant_id, constraints, storage_scheme)+ .await+ }+#[cfg(feature = "olap")]async fn get_total_count_of_refunds(&self,diff --git a/crates/router/src/db/refund.rs b/crates/router/src/db/refund.rsindex 6208a06d14..e8cebc43ae 100644--- a/crates/router/src/db/refund.rs+++ b/crates/router/src/db/refund.rs@@ -1,5 +1,5 @@#[cfg(feature = "olap")]-use std::collections::HashSet;+use std::collections::{HashMap, HashSet};#[cfg(feature = "olap")]use common_utils::types::MinorUnit;@@ -84,6 +84,14 @@ pub trait RefundInterface {storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError>;+ #[cfg(feature = "olap")]+ async fn get_refund_status_with_count(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ constraints: &api_models::payments::TimeRange,+ storage_scheme: enums::MerchantStorageScheme,+ ) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError>;+#[cfg(feature = "olap")]async fn get_total_count_of_refunds(&self,@@ -251,6 +259,21 @@ mod storage {.await.map_err(|error|report!(errors::StorageError::from(error)))}++ #[cfg(feature = "olap")]+ #[instrument(skip_all)]+ async fn get_refund_status_with_count(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ time_range: &api_models::payments::TimeRange,+ _storage_scheme: enums::MerchantStorageScheme,+ ) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {+ let conn = connection::pg_connection_read(self).await?;+ <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, merchant_id, time_range)+ .await+ .map_err(|error|report!(errors::StorageError::from(error)))+ }+#[cfg(feature = "olap")]#[instrument(skip_all)]async fn get_total_count_of_refunds(@@ -785,6 +808,20 @@ mod storage {.map_err(|error|report!(errors::StorageError::from(error)))}+ #[cfg(feature = "olap")]+ #[instrument(skip_all)]+ async fn get_refund_status_with_count(+ &self,+ merchant_id: &common_utils::id_type::MerchantId,+ constraints: &api_models::payments::TimeRange,+ _storage_scheme: enums::MerchantStorageScheme,+ ) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {+ let conn = connection::pg_connection_read(self).await?;+ <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, merchant_id, constraints)+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ }+#[cfg(feature = "olap")]#[instrument(skip_all)]async fn get_total_count_of_refunds(@@ -1123,6 +1160,41 @@ impl RefundInterface for MockDb {Ok(refund_meta_data)}+ #[cfg(feature = "olap")]+ async fn get_refund_status_with_count(+ &self,+ _merchant_id: &common_utils::id_type::MerchantId,+ time_range: &api_models::payments::TimeRange,+ _storage_scheme: enums::MerchantStorageScheme,+ ) -> CustomResult<Vec<(api_models::enums::RefundStatus, i64)>, errors::StorageError> {+ let refunds = self.refunds.lock().await;++ let start_time = time_range.start_time;+ let end_time = time_range+ .end_time+ .unwrap_or_else(common_utils::date_time::now);++ let filtered_refunds = refunds+ .iter()+ .filter(|refund| refund.created_at >= start_time && refund.created_at <= end_time)+ .cloned()+ .collect::<Vec<diesel_models::refund::Refund>>();++ let mut refund_status_counts: HashMap<api_models::enums::RefundStatus, i64> =+ HashMap::new();++ for refund in filtered_refunds {+ *refund_status_counts+ .entry(refund.refund_status)+ .or_insert(0) += 1;+ }++ let result: Vec<(api_models::enums::RefundStatus, i64)> =+ refund_status_counts.into_iter().collect();++ Ok(result)+ }+#[cfg(feature = "olap")]async fn get_total_count_of_refunds(&self,diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rsindex 210ded9c25..c36bf10b8c 100644--- a/crates/router/src/routes/app.rs+++ b/crates/router/src/routes/app.rs@@ -995,6 +995,7 @@ impl Refunds {.service(web::resource("/profile/list").route(web::post().to(refunds_list_profile))).service(web::resource("/filter").route(web::post().to(refunds_filter_list))).service(web::resource("/v2/filter").route(web::get().to(get_refunds_filters)))+ .service(web::resource("/aggregate").route(web::get().to(get_refunds_aggregates))).service(web::resource("/v2/profile/filter").route(web::get().to(get_refunds_filters_profile)),diff --git a/crates/router/src/routes/lock_utils.rs b/crates/router/src/routes/lock_utils.rsindex 13148801aa..0cdfd90e99 100644--- a/crates/router/src/routes/lock_utils.rs+++ b/crates/router/src/routes/lock_utils.rs@@ -153,6 +153,7 @@ impl From<Flow> for ApiIdentifier {| Flow::RefundsUpdate| Flow::RefundsList| Flow::RefundsFilters+ | Flow::RefundsAggregate| Flow::RefundsManualUpdate => Self::Refunds,Flow::FrmFulfillmentdiff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rsindex 4b964dba62..2f5cb33283 100644--- a/crates/router/src/routes/refunds.rs+++ b/crates/router/src/routes/refunds.rs@@ -421,6 +421,36 @@ pub async fn get_refunds_filters_profile(.await}+#[instrument(skip_all, fields(flow = ?Flow::RefundsAggregate))]+#[cfg(feature = "olap")]+pub async fn get_refunds_aggregates(+ state: web::Data<AppState>,+ req: HttpRequest,+ query_params: web::Query<api_models::payments::TimeRange>,+) -> HttpResponse {+ let flow = Flow::RefundsAggregate;+ let query_params = query_params.into_inner();+ Box::pin(api::server_wrap(+ flow,+ state,+ &req,+ query_params,+ |state, auth: auth::AuthenticationData, req, _| {+ get_aggregates_for_refunds(state, auth.merchant_account, req)+ },+ auth::auth_type(+ &auth::HeaderAuth(auth::ApiKeyAuth),+ &auth::JWTAuth {+ permission: Permission::RefundRead,+ minimum_entity_level: EntityType::Merchant,+ },+ req.headers(),+ ),+ api_locking::LockAction::NotApplicable,+ ))+ .await+}+#[instrument(skip_all, fields(flow = ?Flow::RefundsManualUpdate))]#[cfg(feature = "olap")]pub async fn refunds_manual_update(diff --git a/crates/router/src/types/storage/refund.rs b/crates/router/src/types/storage/refund.rsindex 257982a0fb..8e0040abfa 100644--- a/crates/router/src/types/storage/refund.rs+++ b/crates/router/src/types/storage/refund.rs@@ -37,6 +37,12 @@ pub trait RefundDbExt: Sized {merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &refunds::RefundListConstraints,) -> CustomResult<i64, errors::DatabaseError>;++ async fn get_refund_status_with_count(+ conn: &PgPooledConn,+ merchant_id: &common_utils::id_type::MerchantId,+ time_range: &api_models::payments::TimeRange,+ ) -> CustomResult<Vec<(RefundStatus, i64)>, errors::DatabaseError>;}#[async_trait::async_trait]@@ -289,4 +295,33 @@ impl RefundDbExt for Refund {.change_context(errors::DatabaseError::NotFound).attach_printable_lazy(|| "Error filtering count of refunds")}++ async fn get_refund_status_with_count(+ conn: &PgPooledConn,+ merchant_id: &common_utils::id_type::MerchantId,+ time_range: &api_models::payments::TimeRange,+ ) -> CustomResult<Vec<(RefundStatus, i64)>, errors::DatabaseError> {+ let mut query = <Self as HasTable>::table()+ .group_by(dsl::refund_status)+ .select((dsl::refund_status, diesel::dsl::count_star()))+ .filter(dsl::merchant_id.eq(merchant_id.to_owned()))+ .into_boxed();++ query = query.filter(dsl::created_at.ge(time_range.start_time));++ query = match time_range.end_time {+ Some(ending_at) => query.filter(dsl::created_at.le(ending_at)),+ None => query,+ };++ logger::debug!(filter = %diesel::debug_query::<diesel::pg::Pg,_>(&query).to_string());++ db_metrics::track_database_call::<<Self as HasTable>::Table, _, _>(+ query.get_results_async::<(RefundStatus, i64)>(conn),+ db_metrics::DatabaseOperation::Count,+ )+ .await+ .change_context(errors::DatabaseError::NotFound)+ .attach_printable_lazy(|| "Error filtering status count of refunds")+ }}diff --git a/crates/router_env/src/logger/types.rs b/crates/router_env/src/logger/types.rsindex 79ab704fbb..6126ca9afb 100644--- a/crates/router_env/src/logger/types.rs+++ b/crates/router_env/src/logger/types.rs@@ -212,6 +212,8 @@ pub enum Flow {RefundsList,/// Refunds filters flowRefundsFilters,+ /// Refunds aggregates flow+ RefundsAggregate,// Retrieve forex flow.RetrieveForexFlow,/// Toggles recon service for a merchant.