Files
Sampras Lopes 6a0d183e7b fix(router_env): fix opentelemetry traces for router_env (#201)
Co-authored-by: Nishant Joshi <nishant.joshi@juspay.in>
2023-01-07 17:43:19 +05:30

77 lines
2.3 KiB
Rust

use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
use router_env::{instrument, tracing};
use super::generics;
use crate::{
errors,
payment_intent::{
PaymentIntent, PaymentIntentNew, PaymentIntentUpdate, PaymentIntentUpdateInternal,
},
schema::payment_intent::dsl,
PgPooledConn, StorageResult,
};
impl PaymentIntentNew {
#[instrument(skip(conn))]
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<PaymentIntent> {
generics::generic_insert(conn, self).await
}
}
impl PaymentIntent {
#[instrument(skip(conn))]
pub async fn update(
self,
conn: &PgPooledConn,
payment_intent: PaymentIntentUpdate,
) -> StorageResult<Self> {
match generics::generic_update_with_results::<<Self as HasTable>::Table, _, _, _>(
conn,
dsl::payment_id
.eq(self.payment_id.to_owned())
.and(dsl::merchant_id.eq(self.merchant_id.to_owned())),
PaymentIntentUpdateInternal::from(payment_intent),
)
.await
{
Err(error) => match error.current_context() {
errors::DatabaseError::NoFieldsToUpdate => Ok(self),
_ => Err(error),
},
Ok(mut payment_intents) => payment_intents
.pop()
.ok_or(error_stack::report!(errors::DatabaseError::NotFound)),
}
}
#[instrument(skip(conn))]
pub async fn find_by_payment_id_merchant_id(
conn: &PgPooledConn,
payment_id: &str,
merchant_id: &str,
) -> StorageResult<Self> {
generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
conn,
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::payment_id.eq(payment_id.to_owned())),
)
.await
}
#[instrument(skip(conn))]
pub async fn find_optional_by_payment_id_merchant_id(
conn: &PgPooledConn,
payment_id: &str,
merchant_id: &str,
) -> StorageResult<Option<Self>> {
generics::generic_find_one_optional::<<Self as HasTable>::Table, _, _>(
conn,
dsl::merchant_id
.eq(merchant_id.to_owned())
.and(dsl::payment_id.eq(payment_id.to_owned())),
)
.await
}
}