feat(payments): add support for aggregates in payments (#5654)

This commit is contained in:
Apoorv Dixit
2024-08-21 18:06:54 +05:30
committed by GitHub
parent 1e64ed79bc
commit 9f3b2fba3e
12 changed files with 154 additions and 14 deletions

View File

@ -3161,6 +3161,31 @@ pub async fn get_payment_filters(
))
}
#[cfg(feature = "olap")]
pub async fn get_aggregates_for_payments(
state: SessionState,
merchant: domain::MerchantAccount,
time_range: api::TimeRange,
) -> RouterResponse<api::PaymentsAggregateResponse> {
let db = state.store.as_ref();
let intent_status_with_count = db
.get_intent_status_with_count(merchant.get_id(), &time_range)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
let mut status_map: HashMap<enums::IntentStatus, i64> =
intent_status_with_count.into_iter().collect();
for status in enums::IntentStatus::iter() {
status_map.entry(status).or_default();
}
Ok(services::ApplicationResponse::Json(
api::PaymentsAggregateResponse {
status_with_count: status_map,
},
))
}
pub async fn add_process_sync_task(
db: &dyn StorageInterface,
payment_attempt: &storage::PaymentAttempt,

View File

@ -1617,6 +1617,17 @@ impl PaymentIntentInterface for KafkaStore {
.await
}
#[cfg(feature = "olap")]
async fn get_intent_status_with_count(
&self,
merchant_id: &id_type::MerchantId,
time_range: &api_models::payments::TimeRange,
) -> error_stack::Result<Vec<(common_enums::IntentStatus, i64)>, errors::DataStorageError> {
self.diesel_store
.get_intent_status_with_count(merchant_id, time_range)
.await
}
#[cfg(feature = "olap")]
async fn get_filtered_payment_intents_attempt(
&self,

View File

@ -539,6 +539,7 @@ impl Payments {
)
.service(web::resource("/filter").route(web::post().to(get_filters_for_payments)))
.service(web::resource("/v2/filter").route(web::get().to(get_payment_filters)))
.service(web::resource("/aggregate").route(web::get().to(get_payments_aggregates)))
.service(
web::resource("/{payment_id}/manual-update")
.route(web::put().to(payments_manual_update)),

View File

@ -126,6 +126,7 @@ impl From<Flow> for ApiIdentifier {
| Flow::PaymentsStart
| Flow::PaymentsList
| Flow::PaymentsFilters
| Flow::PaymentsAggregate
| Flow::PaymentsRedirect
| Flow::PaymentsIncrementalAuthorization
| Flow::PaymentsExternalAuthentication

View File

@ -1075,6 +1075,29 @@ pub async fn get_payment_filters(
.await
}
#[instrument(skip_all, fields(flow = ?Flow::PaymentsAggregate))]
#[cfg(feature = "olap")]
pub async fn get_payments_aggregates(
state: web::Data<app::AppState>,
req: actix_web::HttpRequest,
payload: web::Query<payment_types::TimeRange>,
) -> impl Responder {
let flow = Flow::PaymentsAggregate;
let payload = payload.into_inner();
Box::pin(api::server_wrap(
flow,
state,
&req,
payload,
|state, auth: auth::AuthenticationData, req, _| {
payments::get_aggregates_for_payments(state, auth.merchant_account, req)
},
&auth::JWTAuth(Permission::PaymentRead),
api_locking::LockAction::NotApplicable,
))
.await
}
#[cfg(feature = "oltp")]
#[instrument(skip_all, fields(flow = ?Flow::PaymentsApprove, payment_id))]
// #[post("/{payment_id}/approve")]

View File

@ -5,14 +5,14 @@ pub use api_models::payments::{
OnlineMandate, OpenBankingSessionToken, PayLaterData, PaymentIdType, PaymentListConstraints,
PaymentListFilterConstraints, PaymentListFilters, PaymentListFiltersV2, PaymentListResponse,
PaymentListResponseV2, PaymentMethodData, PaymentMethodDataRequest, PaymentMethodDataResponse,
PaymentOp, PaymentRetrieveBody, PaymentRetrieveBodyWithCredentials, PaymentsApproveRequest,
PaymentsCancelRequest, PaymentsCaptureRequest, PaymentsCompleteAuthorizeRequest,
PaymentsExternalAuthenticationRequest, PaymentsIncrementalAuthorizationRequest,
PaymentsManualUpdateRequest, PaymentsRedirectRequest, PaymentsRedirectionResponse,
PaymentsRejectRequest, PaymentsRequest, PaymentsResponse, PaymentsResponseForm,
PaymentsRetrieveRequest, PaymentsSessionRequest, PaymentsSessionResponse, PaymentsStartRequest,
PgRedirectResponse, PhoneDetails, RedirectionResponse, SessionToken, TimeRange, UrlDetails,
VerifyRequest, VerifyResponse, WalletData,
PaymentOp, PaymentRetrieveBody, PaymentRetrieveBodyWithCredentials, PaymentsAggregateResponse,
PaymentsApproveRequest, PaymentsCancelRequest, PaymentsCaptureRequest,
PaymentsCompleteAuthorizeRequest, PaymentsExternalAuthenticationRequest,
PaymentsIncrementalAuthorizationRequest, PaymentsManualUpdateRequest, PaymentsRedirectRequest,
PaymentsRedirectionResponse, PaymentsRejectRequest, PaymentsRequest, PaymentsResponse,
PaymentsResponseForm, PaymentsRetrieveRequest, PaymentsSessionRequest, PaymentsSessionResponse,
PaymentsStartRequest, PgRedirectResponse, PhoneDetails, RedirectionResponse, SessionToken,
TimeRange, UrlDetails, VerifyRequest, VerifyResponse, WalletData,
};
use error_stack::ResultExt;
pub use hyperswitch_domain_models::router_flow_types::payments::{