feat(router): get filters for payments (#1600)

This commit is contained in:
Apoorv Dixit
2023-07-07 16:10:01 +05:30
committed by GitHub
parent f77fdb7a6e
commit d5891ecbd4
12 changed files with 341 additions and 15 deletions

View File

@ -344,6 +344,7 @@ pub enum EventType {
Debug,
Default,
Eq,
Hash,
PartialEq,
serde::Deserialize,
serde::Serialize,

View File

@ -2,7 +2,10 @@ use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
use crate::{enums as storage_enums, schema::payment_attempt};
use crate::{
enums::{self as storage_enums},
schema::payment_attempt,
};
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize)]
#[diesel(table_name = payment_attempt)]
@ -52,6 +55,14 @@ pub struct PaymentAttempt {
pub error_reason: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Queryable, Serialize, Deserialize)]
pub struct PaymentListFilters {
pub connector: Vec<String>,
pub currency: Vec<storage_enums::Currency>,
pub status: Vec<storage_enums::IntentStatus>,
pub payment_method: Vec<storage_enums::PaymentMethod>,
}
#[derive(
Clone, Debug, Default, Insertable, router_derive::DebugAsDisplay, Serialize, Deserialize,
)]

View File

@ -1,13 +1,19 @@
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods, Table};
use error_stack::IntoReport;
use std::collections::HashSet;
use async_bb8_diesel::AsyncRunQueryDsl;
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods, QueryDsl, Table};
use error_stack::{IntoReport, ResultExt};
use router_env::{instrument, tracing};
use super::generics;
use crate::{
enums, errors,
enums::{self, IntentStatus},
errors::{self, DatabaseError},
payment_attempt::{
PaymentAttempt, PaymentAttemptNew, PaymentAttemptUpdate, PaymentAttemptUpdateInternal,
PaymentListFilters,
},
payment_intent::PaymentIntent,
schema::payment_attempt::dsl,
PgPooledConn, StorageResult,
};
@ -41,7 +47,7 @@ impl PaymentAttempt {
.await
{
Err(error) => match error.current_context() {
errors::DatabaseError::NoFieldsToUpdate => Ok(self),
DatabaseError::NoFieldsToUpdate => Ok(self),
_ => Err(error),
},
result => result,
@ -104,7 +110,7 @@ impl PaymentAttempt {
.await?
.into_iter()
.fold(
Err(errors::DatabaseError::NotFound).into_report(),
Err(DatabaseError::NotFound).into_report(),
|acc, cur| match acc {
Ok(value) if value.modified_at > cur.modified_at => Ok(value),
_ => Ok(cur),
@ -174,4 +180,73 @@ impl PaymentAttempt {
)
.await
}
pub async fn get_filters_for_payments(
conn: &PgPooledConn,
pi: &[PaymentIntent],
merchant_id: &str,
) -> StorageResult<PaymentListFilters> {
let active_attempts: Vec<String> = pi
.iter()
.map(|payment_intent| payment_intent.clone().active_attempt_id)
.collect();
let filter = <Self as HasTable>::table()
.filter(dsl::merchant_id.eq(merchant_id.to_owned()))
.filter(dsl::attempt_id.eq_any(active_attempts));
let intent_status: Vec<IntentStatus> = pi
.iter()
.map(|payment_intent| payment_intent.status)
.collect::<HashSet<IntentStatus>>()
.into_iter()
.collect();
let filter_connector = filter
.clone()
.select(dsl::connector)
.distinct()
.get_results_async::<Option<String>>(conn)
.await
.into_report()
.change_context(errors::DatabaseError::Others)
.attach_printable("Error filtering records by connector")?
.into_iter()
.flatten()
.collect::<Vec<String>>();
let filter_currency = filter
.clone()
.select(dsl::currency)
.distinct()
.get_results_async::<Option<enums::Currency>>(conn)
.await
.into_report()
.change_context(DatabaseError::Others)
.attach_printable("Error filtering records by currency")?
.into_iter()
.flatten()
.collect::<Vec<enums::Currency>>();
let filter_payment_method = filter
.clone()
.select(dsl::payment_method)
.distinct()
.get_results_async::<Option<enums::PaymentMethod>>(conn)
.await
.into_report()
.change_context(DatabaseError::Others)
.attach_printable("Error filtering records by payment method")?
.into_iter()
.flatten()
.collect::<Vec<enums::PaymentMethod>>();
let filters = PaymentListFilters {
connector: filter_connector,
currency: filter_currency,
status: intent_status,
payment_method: filter_payment_method,
};
Ok(filters)
}
}