feat(router): add api_models and openapi changes for refunds create api v2 (#6385)

This commit is contained in:
Sai Harsha Vardhan
2024-10-23 16:53:46 +05:30
committed by GitHub
parent 4ef48c39b3
commit 5a10e5867a
15 changed files with 469 additions and 38 deletions

View File

@ -23,10 +23,16 @@ pub enum ApiEventsType {
Payment {
payment_id: id_type::GlobalPaymentId,
},
#[cfg(feature = "v1")]
Refund {
payment_id: Option<id_type::PaymentId>,
refund_id: String,
},
#[cfg(feature = "v2")]
Refund {
payment_id: id_type::GlobalPaymentId,
refund_id: id_type::GlobalRefundId,
},
PaymentMethod {
payment_method_id: String,
payment_method: Option<PaymentMethod>,

View File

@ -10,6 +10,7 @@ mod merchant_connector_account;
mod organization;
mod payment;
mod profile;
mod refunds;
mod routing;
#[cfg(feature = "v2")]
@ -25,12 +26,16 @@ use diesel::{
sql_types,
};
#[cfg(feature = "v2")]
pub use global_id::{payment::GlobalPaymentId, payment_methods::GlobalPaymentMethodId, CellId};
pub use global_id::{
payment::GlobalPaymentId, payment_methods::GlobalPaymentMethodId, refunds::GlobalRefundId,
CellId,
};
pub use merchant::MerchantId;
pub use merchant_connector_account::MerchantConnectorAccountId;
pub use organization::OrganizationId;
pub use payment::{PaymentId, PaymentReferenceId};
pub use profile::ProfileId;
pub use refunds::RefundReferenceId;
pub use routing::RoutingId;
use serde::{Deserialize, Serialize};
use thiserror::Error;

View File

@ -1,6 +1,7 @@
#![allow(unused)]
pub mod payment;
pub mod payment_methods;
pub mod refunds;
use diesel::{backend::Backend, deserialize::FromSql, serialize::ToSql, sql_types};
use error_stack::ResultExt;
@ -24,6 +25,7 @@ pub(crate) enum GlobalEntity {
Customer,
Payment,
PaymentMethod,
Refund,
}
impl GlobalEntity {
@ -32,6 +34,7 @@ impl GlobalEntity {
Self::Customer => "cus",
Self::Payment => "pay",
Self::PaymentMethod => "pm",
Self::Refund => "ref",
}
}
}

View File

@ -0,0 +1,71 @@
use error_stack::ResultExt;
use crate::{errors, generate_id_with_default_len, generate_time_ordered_id_without_prefix, types};
/// A global id that can be used to identify a refund
#[derive(
Debug,
Clone,
Hash,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
diesel::expression::AsExpression,
)]
#[diesel(sql_type = diesel::sql_types::Text)]
pub struct GlobalRefundId(super::GlobalId);
// Database related implementations so that this field can be used directly in the database tables
crate::impl_queryable_id_type!(GlobalRefundId);
impl GlobalRefundId {
/// Get string representation of the id
pub fn get_string_repr(&self) -> &str {
self.0.get_string_repr()
}
/// Generate a new GlobalRefundId from a cell id
pub fn generate(cell_id: crate::id_type::CellId) -> Self {
let global_id = super::GlobalId::generate(cell_id, super::GlobalEntity::Refund);
Self(global_id)
}
}
// TODO: refactor the macro to include this id use case as well
impl TryFrom<std::borrow::Cow<'static, str>> for GlobalRefundId {
type Error = error_stack::Report<errors::ValidationError>;
fn try_from(value: std::borrow::Cow<'static, str>) -> Result<Self, Self::Error> {
use error_stack::ResultExt;
let merchant_ref_id = super::GlobalId::from_string(value).change_context(
errors::ValidationError::IncorrectValueProvided {
field_name: "refund_id",
},
)?;
Ok(Self(merchant_ref_id))
}
}
// TODO: refactor the macro to include this id use case as well
impl<DB> diesel::serialize::ToSql<diesel::sql_types::Text, DB> for GlobalRefundId
where
DB: diesel::backend::Backend,
super::GlobalId: diesel::serialize::ToSql<diesel::sql_types::Text, DB>,
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, DB>,
) -> diesel::serialize::Result {
self.0.to_sql(out)
}
}
impl<DB> diesel::deserialize::FromSql<diesel::sql_types::Text, DB> for GlobalRefundId
where
DB: diesel::backend::Backend,
super::GlobalId: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,
{
fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
super::GlobalId::from_sql(value).map(Self)
}
}

View File

@ -0,0 +1,10 @@
crate::id_type!(RefundReferenceId, "A type for refund_reference_id");
crate::impl_id_type_methods!(RefundReferenceId, "refund_reference_id");
// This is to display the `RefundReferenceId` as RefundReferenceId(abcd)
crate::impl_debug_id_type!(RefundReferenceId);
crate::impl_try_from_cow_str_id_type!(RefundReferenceId, "refund_reference_id");
// Database related implementations so that this field can be used directly in the database tables
crate::impl_queryable_id_type!(RefundReferenceId);
crate::impl_to_sql_from_sql_id_type!(RefundReferenceId);