|
| 1 | +use api_models::analytics::{outgoing_webhook_event::OutgoingWebhookLogsRequest, Granularity}; |
| 2 | +use common_utils::errors::ReportSwitchExt; |
| 3 | +use error_stack::ResultExt; |
| 4 | +use time::PrimitiveDateTime; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + query::{Aggregate, GroupByClause, QueryBuilder, ToSql, Window}, |
| 8 | + types::{AnalyticsCollection, AnalyticsDataSource, FiltersError, FiltersResult, LoadRow}, |
| 9 | +}; |
| 10 | +pub trait OutgoingWebhookLogsFilterAnalytics: LoadRow<OutgoingWebhookLogsResult> {} |
| 11 | + |
| 12 | +pub async fn get_outgoing_webhook_event<T>( |
| 13 | + merchant_id: &String, |
| 14 | + query_param: OutgoingWebhookLogsRequest, |
| 15 | + pool: &T, |
| 16 | +) -> FiltersResult<Vec<OutgoingWebhookLogsResult>> |
| 17 | +where |
| 18 | + T: AnalyticsDataSource + OutgoingWebhookLogsFilterAnalytics, |
| 19 | + PrimitiveDateTime: ToSql<T>, |
| 20 | + AnalyticsCollection: ToSql<T>, |
| 21 | + Granularity: GroupByClause<T>, |
| 22 | + Aggregate<&'static str>: ToSql<T>, |
| 23 | + Window<&'static str>: ToSql<T>, |
| 24 | +{ |
| 25 | + let mut query_builder: QueryBuilder<T> = |
| 26 | + QueryBuilder::new(AnalyticsCollection::OutgoingWebhookEvent); |
| 27 | + query_builder.add_select_column("*").switch()?; |
| 28 | + |
| 29 | + query_builder |
| 30 | + .add_filter_clause("merchant_id", merchant_id) |
| 31 | + .switch()?; |
| 32 | + query_builder |
| 33 | + .add_filter_clause("payment_id", query_param.payment_id) |
| 34 | + .switch()?; |
| 35 | + |
| 36 | + if let Some(event_id) = query_param.event_id { |
| 37 | + query_builder |
| 38 | + .add_filter_clause("event_id", &event_id) |
| 39 | + .switch()?; |
| 40 | + } |
| 41 | + if let Some(refund_id) = query_param.refund_id { |
| 42 | + query_builder |
| 43 | + .add_filter_clause("refund_id", &refund_id) |
| 44 | + .switch()?; |
| 45 | + } |
| 46 | + if let Some(dispute_id) = query_param.dispute_id { |
| 47 | + query_builder |
| 48 | + .add_filter_clause("dispute_id", &dispute_id) |
| 49 | + .switch()?; |
| 50 | + } |
| 51 | + if let Some(mandate_id) = query_param.mandate_id { |
| 52 | + query_builder |
| 53 | + .add_filter_clause("mandate_id", &mandate_id) |
| 54 | + .switch()?; |
| 55 | + } |
| 56 | + if let Some(payment_method_id) = query_param.payment_method_id { |
| 57 | + query_builder |
| 58 | + .add_filter_clause("payment_method_id", &payment_method_id) |
| 59 | + .switch()?; |
| 60 | + } |
| 61 | + if let Some(attempt_id) = query_param.attempt_id { |
| 62 | + query_builder |
| 63 | + .add_filter_clause("attempt_id", &attempt_id) |
| 64 | + .switch()?; |
| 65 | + } |
| 66 | + //TODO!: update the execute_query function to return reports instead of plain errors... |
| 67 | + query_builder |
| 68 | + .execute_query::<OutgoingWebhookLogsResult, _>(pool) |
| 69 | + .await |
| 70 | + .change_context(FiltersError::QueryBuildingError)? |
| 71 | + .change_context(FiltersError::QueryExecutionFailure) |
| 72 | +} |
| 73 | +#[derive(Debug, serde::Serialize, serde::Deserialize)] |
| 74 | +pub struct OutgoingWebhookLogsResult { |
| 75 | + pub merchant_id: String, |
| 76 | + pub event_id: String, |
| 77 | + pub event_type: String, |
| 78 | + pub outgoing_webhook_event_type: String, |
| 79 | + pub payment_id: String, |
| 80 | + pub refund_id: Option<String>, |
| 81 | + pub attempt_id: Option<String>, |
| 82 | + pub dispute_id: Option<String>, |
| 83 | + pub payment_method_id: Option<String>, |
| 84 | + pub mandate_id: Option<String>, |
| 85 | + pub content: Option<String>, |
| 86 | + pub is_error: bool, |
| 87 | + pub error: Option<String>, |
| 88 | + #[serde(with = "common_utils::custom_serde::iso8601")] |
| 89 | + pub created_at: PrimitiveDateTime, |
| 90 | +} |
0 commit comments