feat(subscription): domain_model for subscription and invoice (#9640)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Jagan <jaganelavarasan@gmail.com>
This commit is contained in:
Ankit Kumar Gupta
2025-10-09 15:13:56 +05:30
committed by GitHub
parent a968844589
commit 17986c3b31
15 changed files with 1093 additions and 386 deletions

View File

@ -0,0 +1,200 @@
use common_utils::{errors::CustomResult, types::keymanager::KeyManagerState};
pub use diesel_models::invoice::Invoice;
use error_stack::{report, ResultExt};
pub use hyperswitch_domain_models::{
behaviour::Conversion,
invoice::{Invoice as DomainInvoice, InvoiceInterface, InvoiceUpdate as DomainInvoiceUpdate},
merchant_key_store::MerchantKeyStore,
};
use router_env::{instrument, tracing};
use crate::{
connection, errors::StorageError, kv_router_store::KVRouterStore, DatabaseStore, MockDb,
RouterStore,
};
#[async_trait::async_trait]
impl<T: DatabaseStore> InvoiceInterface for RouterStore<T> {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_invoice_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_new: DomainInvoice,
) -> CustomResult<DomainInvoice, StorageError> {
let inv_new = invoice_new
.construct_new()
.await
.change_context(StorageError::DecryptionError)?;
let conn = connection::pg_connection_write(self).await?;
self.call_database(state, key_store, inv_new.insert(&conn))
.await
}
#[instrument(skip_all)]
async fn find_invoice_by_invoice_id(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
let conn = connection::pg_connection_read(self).await?;
self.call_database(
state,
key_store,
Invoice::find_invoice_by_id_invoice_id(&conn, invoice_id),
)
.await
}
#[instrument(skip_all)]
async fn update_invoice_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_id: String,
data: DomainInvoiceUpdate,
) -> CustomResult<DomainInvoice, StorageError> {
let inv_new = data
.construct_new()
.await
.change_context(StorageError::DecryptionError)?;
let conn = connection::pg_connection_write(self).await?;
self.call_database(
state,
key_store,
Invoice::update_invoice_entry(&conn, invoice_id, inv_new),
)
.await
}
#[instrument(skip_all)]
async fn get_latest_invoice_for_subscription(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
subscription_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
let conn = connection::pg_connection_write(self).await?;
let invoices: Vec<DomainInvoice> = self
.find_resources(
state,
key_store,
Invoice::list_invoices_by_subscription_id(
&conn,
subscription_id.clone(),
Some(1),
None,
false,
),
)
.await?;
invoices
.last()
.cloned()
.ok_or(report!(StorageError::ValueNotFound(format!(
"Invoice not found for subscription_id: {}",
subscription_id
))))
}
}
#[async_trait::async_trait]
impl<T: DatabaseStore> InvoiceInterface for KVRouterStore<T> {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_invoice_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_new: DomainInvoice,
) -> CustomResult<DomainInvoice, StorageError> {
self.router_store
.insert_invoice_entry(state, key_store, invoice_new)
.await
}
#[instrument(skip_all)]
async fn find_invoice_by_invoice_id(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
self.router_store
.find_invoice_by_invoice_id(state, key_store, invoice_id)
.await
}
#[instrument(skip_all)]
async fn update_invoice_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
invoice_id: String,
data: DomainInvoiceUpdate,
) -> CustomResult<DomainInvoice, StorageError> {
self.router_store
.update_invoice_entry(state, key_store, invoice_id, data)
.await
}
#[instrument(skip_all)]
async fn get_latest_invoice_for_subscription(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
subscription_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
self.router_store
.get_latest_invoice_for_subscription(state, key_store, subscription_id)
.await
}
}
#[async_trait::async_trait]
impl InvoiceInterface for MockDb {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_invoice_entry(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_invoice_new: DomainInvoice,
) -> CustomResult<DomainInvoice, StorageError> {
Err(StorageError::MockDbError)?
}
async fn find_invoice_by_invoice_id(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_invoice_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
Err(StorageError::MockDbError)?
}
async fn update_invoice_entry(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_invoice_id: String,
_data: DomainInvoiceUpdate,
) -> CustomResult<DomainInvoice, StorageError> {
Err(StorageError::MockDbError)?
}
async fn get_latest_invoice_for_subscription(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_subscription_id: String,
) -> CustomResult<DomainInvoice, StorageError> {
Err(StorageError::MockDbError)?
}
}

View File

@ -16,6 +16,7 @@ pub mod connection;
pub mod customers;
pub mod database;
pub mod errors;
pub mod invoice;
pub mod kv_router_store;
pub mod lookup;
pub mod mandate;
@ -28,6 +29,7 @@ pub mod payouts;
pub mod redis;
pub mod refund;
mod reverse_lookup;
pub mod subscription;
pub mod utils;
use common_utils::{errors::CustomResult, types::keymanager::KeyManagerState};

View File

@ -0,0 +1,155 @@
use common_utils::{errors::CustomResult, types::keymanager::KeyManagerState};
pub use diesel_models::subscription::Subscription;
use error_stack::ResultExt;
pub use hyperswitch_domain_models::{
behaviour::Conversion,
merchant_key_store::MerchantKeyStore,
subscription::{
Subscription as DomainSubscription, SubscriptionInterface,
SubscriptionUpdate as DomainSubscriptionUpdate,
},
};
use router_env::{instrument, tracing};
use crate::{
connection, errors::StorageError, kv_router_store::KVRouterStore, DatabaseStore, MockDb,
RouterStore,
};
#[async_trait::async_trait]
impl<T: DatabaseStore> SubscriptionInterface for RouterStore<T> {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_subscription_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
subscription_new: DomainSubscription,
) -> CustomResult<DomainSubscription, StorageError> {
let sub_new = subscription_new
.construct_new()
.await
.change_context(StorageError::DecryptionError)?;
let conn = connection::pg_connection_write(self).await?;
self.call_database(state, key_store, sub_new.insert(&conn))
.await
}
#[instrument(skip_all)]
async fn find_by_merchant_id_subscription_id(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
merchant_id: &common_utils::id_type::MerchantId,
subscription_id: String,
) -> CustomResult<DomainSubscription, StorageError> {
let conn = connection::pg_connection_write(self).await?;
self.call_database(
state,
key_store,
Subscription::find_by_merchant_id_subscription_id(&conn, merchant_id, subscription_id),
)
.await
}
#[instrument(skip_all)]
async fn update_subscription_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
merchant_id: &common_utils::id_type::MerchantId,
subscription_id: String,
data: DomainSubscriptionUpdate,
) -> CustomResult<DomainSubscription, StorageError> {
let sub_new = data
.construct_new()
.await
.change_context(StorageError::DecryptionError)?;
let conn = connection::pg_connection_write(self).await?;
self.call_database(
state,
key_store,
Subscription::update_subscription_entry(&conn, merchant_id, subscription_id, sub_new),
)
.await
}
}
#[async_trait::async_trait]
impl<T: DatabaseStore> SubscriptionInterface for KVRouterStore<T> {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_subscription_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
subscription_new: DomainSubscription,
) -> CustomResult<DomainSubscription, StorageError> {
self.router_store
.insert_subscription_entry(state, key_store, subscription_new)
.await
}
#[instrument(skip_all)]
async fn find_by_merchant_id_subscription_id(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
merchant_id: &common_utils::id_type::MerchantId,
subscription_id: String,
) -> CustomResult<DomainSubscription, StorageError> {
self.router_store
.find_by_merchant_id_subscription_id(state, key_store, merchant_id, subscription_id)
.await
}
#[instrument(skip_all)]
async fn update_subscription_entry(
&self,
state: &KeyManagerState,
key_store: &MerchantKeyStore,
merchant_id: &common_utils::id_type::MerchantId,
subscription_id: String,
data: DomainSubscriptionUpdate,
) -> CustomResult<DomainSubscription, StorageError> {
self.router_store
.update_subscription_entry(state, key_store, merchant_id, subscription_id, data)
.await
}
}
#[async_trait::async_trait]
impl SubscriptionInterface for MockDb {
type Error = StorageError;
#[instrument(skip_all)]
async fn insert_subscription_entry(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_subscription_new: DomainSubscription,
) -> CustomResult<DomainSubscription, StorageError> {
Err(StorageError::MockDbError)?
}
async fn find_by_merchant_id_subscription_id(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_merchant_id: &common_utils::id_type::MerchantId,
_subscription_id: String,
) -> CustomResult<DomainSubscription, StorageError> {
Err(StorageError::MockDbError)?
}
async fn update_subscription_entry(
&self,
_state: &KeyManagerState,
_key_store: &MerchantKeyStore,
_merchant_id: &common_utils::id_type::MerchantId,
_subscription_id: String,
_data: DomainSubscriptionUpdate,
) -> CustomResult<DomainSubscription, StorageError> {
Err(StorageError::MockDbError)?
}
}