mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 12:15:40 +08:00
feat(events): add extracted fields based on req/res types (#2795)
This commit is contained in:
91
crates/common_utils/src/events.rs
Normal file
91
crates/common_utils/src/events.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use common_enums::{PaymentMethod, PaymentMethodType};
|
||||
use serde::Serialize;
|
||||
|
||||
pub trait ApiEventMetric {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
|
||||
#[serde(tag = "flow_type")]
|
||||
pub enum ApiEventsType {
|
||||
Payout,
|
||||
Payment {
|
||||
payment_id: String,
|
||||
},
|
||||
Refund {
|
||||
payment_id: Option<String>,
|
||||
refund_id: String,
|
||||
},
|
||||
PaymentMethod {
|
||||
payment_method_id: String,
|
||||
payment_method: Option<PaymentMethod>,
|
||||
payment_method_type: Option<PaymentMethodType>,
|
||||
},
|
||||
Customer {
|
||||
customer_id: String,
|
||||
},
|
||||
User {
|
||||
//specified merchant_id will overridden on global defined
|
||||
merchant_id: String,
|
||||
user_id: String,
|
||||
},
|
||||
PaymentMethodList {
|
||||
payment_id: Option<String>,
|
||||
},
|
||||
Webhooks {
|
||||
connector: String,
|
||||
payment_id: Option<String>,
|
||||
},
|
||||
Routing,
|
||||
ResourceListAPI,
|
||||
PaymentRedirectionResponse,
|
||||
// TODO: This has to be removed once the corresponding apiEventTypes are created
|
||||
Miscellaneous,
|
||||
}
|
||||
|
||||
impl ApiEventMetric for serde_json::Value {}
|
||||
impl ApiEventMetric for () {}
|
||||
|
||||
impl<Q: ApiEventMetric, E> ApiEventMetric for Result<Q, E> {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
match self {
|
||||
Ok(q) => q.get_api_event_type(),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Ideally all these types should be replaced by newtype responses
|
||||
impl<T> ApiEventMetric for Vec<T> {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
Some(ApiEventsType::Miscellaneous)
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_misc_api_event_type {
|
||||
($($type:ty),+) => {
|
||||
$(
|
||||
impl ApiEventMetric for $type {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
Some(ApiEventsType::Miscellaneous)
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
impl_misc_api_event_type!(
|
||||
String,
|
||||
(&String, &String),
|
||||
(Option<i64>, Option<i64>, String),
|
||||
bool
|
||||
);
|
||||
|
||||
impl<T: ApiEventMetric> ApiEventMetric for &T {
|
||||
fn get_api_event_type(&self) -> Option<ApiEventsType> {
|
||||
T::get_api_event_type(self)
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,8 @@ pub mod consts;
|
||||
pub mod crypto;
|
||||
pub mod custom_serde;
|
||||
pub mod errors;
|
||||
#[allow(missing_docs)] // Todo: add docs
|
||||
pub mod events;
|
||||
pub mod ext_traits;
|
||||
pub mod fp_utils;
|
||||
pub mod pii;
|
||||
|
||||
Reference in New Issue
Block a user