feat(payment_methods): added kv support for payment_methods table (#4311)

Co-authored-by: Akshay S <akshay.s@Akshay-Subramanian-D66TQ6D97K.local>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
akshay-97
2024-04-10 19:29:50 +05:30
committed by GitHub
parent 9448673c1c
commit eb3cecdd74
35 changed files with 1017 additions and 315 deletions

View File

@ -29,7 +29,7 @@ use crate::{
diesel_error_to_data_error,
errors::RedisErrorExt,
lookup::ReverseLookupInterface,
redis::kv_store::{kv_wrapper, KvOperation},
redis::kv_store::{kv_wrapper, KvOperation, PartitionKey},
utils::{self, pg_connection_read, pg_connection_write},
DataModelExt, DatabaseStore, KVRouterStore,
};
@ -50,10 +50,13 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
.await
}
MerchantStorageScheme::RedisKv => {
let key = format!(
"mid_{}_poa_{}",
new_payout_attempt.merchant_id, new_payout_attempt.payout_id
);
let merchant_id = new_payout_attempt.merchant_id.clone();
let payout_attempt_id = new_payout_attempt.payout_id.clone();
let key = PartitionKey::MerchantIdPayoutAttemptId {
merchant_id: &merchant_id,
payout_attempt_id: &payout_attempt_id,
};
let key_str = key.to_string();
let now = common_utils::date_time::now();
let created_attempt = PayoutAttempt {
payout_attempt_id: new_payout_attempt.payout_attempt_id.clone(),
@ -92,7 +95,7 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
"poa_{}_{}",
&created_attempt.merchant_id, &created_attempt.payout_attempt_id,
),
pk_id: key.clone(),
pk_id: key_str.clone(),
sk_id: field.clone(),
source: "payout_attempt".to_string(),
updated_by: storage_scheme.to_string(),
@ -107,15 +110,15 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
&created_attempt.clone().to_storage_model(),
redis_entry,
),
&key,
key,
)
.await
.map_err(|err| err.to_redis_failed_response(&key))?
.map_err(|err| err.to_redis_failed_response(&key_str))?
.try_into_hsetnx()
{
Ok(HsetnxReply::KeyNotSet) => Err(errors::StorageError::DuplicateValue {
entity: "payout attempt",
key: Some(key),
key: Some(key_str),
}
.into()),
Ok(HsetnxReply::KeySet) => Ok(created_attempt),
@ -140,7 +143,11 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
.await
}
MerchantStorageScheme::RedisKv => {
let key = format!("mid_{}_poa_{}", this.merchant_id, this.payout_id);
let key = PartitionKey::MerchantIdPayoutAttemptId {
merchant_id: &this.merchant_id,
payout_attempt_id: &this.payout_id,
};
let key_str = key.to_string();
let field = format!("poa_{}", this.payout_attempt_id);
let diesel_payout_update = payout_update.to_storage_model();
@ -169,10 +176,10 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
kv_wrapper::<(), _, _>(
self,
KvOperation::<DieselPayoutAttempt>::Hset((&field, redis_value), redis_entry),
&key,
key,
)
.await
.map_err(|err| err.to_redis_failed_response(&key))?
.map_err(|err| err.to_redis_failed_response(&key_str))?
.try_into_hset()
.change_context(errors::StorageError::KVError)?;
@ -211,7 +218,9 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
)
.await
);
let key = &lookup.pk_id;
let key = PartitionKey::CombinationKey {
combination: &lookup.pk_id,
};
Box::pin(utils::try_redis_get_else_try_database_get(
async {
kv_wrapper(

View File

@ -38,7 +38,7 @@ use crate::connection;
use crate::{
diesel_error_to_data_error,
errors::RedisErrorExt,
redis::kv_store::{kv_wrapper, KvOperation},
redis::kv_store::{kv_wrapper, KvOperation, PartitionKey},
utils::{self, pg_connection_read, pg_connection_write},
DataModelExt, DatabaseStore, KVRouterStore,
};
@ -56,7 +56,13 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
self.router_store.insert_payout(new, storage_scheme).await
}
MerchantStorageScheme::RedisKv => {
let key = format!("mid_{}_po_{}", new.merchant_id, new.payout_id);
let merchant_id = new.merchant_id.clone();
let payout_id = new.payout_id.clone();
let key = PartitionKey::MerchantIdPayoutId {
merchant_id: &merchant_id,
payout_id: &payout_id,
};
let key_str = key.to_string();
let field = format!("po_{}", new.payout_id);
let now = common_utils::date_time::now();
let created_payout = Payouts {
@ -95,15 +101,15 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
&created_payout.clone().to_storage_model(),
redis_entry,
),
&key,
key,
)
.await
.map_err(|err| err.to_redis_failed_response(&key))?
.map_err(|err| err.to_redis_failed_response(&key_str))?
.try_into_hsetnx()
{
Ok(HsetnxReply::KeyNotSet) => Err(StorageError::DuplicateValue {
entity: "payouts",
key: Some(key),
key: Some(key_str),
}
.into()),
Ok(HsetnxReply::KeySet) => Ok(created_payout),
@ -128,7 +134,11 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
.await
}
MerchantStorageScheme::RedisKv => {
let key = format!("mid_{}_po_{}", this.merchant_id, this.payout_id);
let key = PartitionKey::MerchantIdPayoutId {
merchant_id: &this.merchant_id,
payout_id: &this.payout_id,
};
let key_str = key.to_string();
let field = format!("po_{}", this.payout_id);
let diesel_payout_update = payout_update.to_storage_model();
@ -155,10 +165,10 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
kv_wrapper::<(), _, _>(
self,
KvOperation::<DieselPayouts>::Hset((&field, redis_value), redis_entry),
&key,
key,
)
.await
.map_err(|err| err.to_redis_failed_response(&key))?
.map_err(|err| err.to_redis_failed_response(&key_str))?
.try_into_hset()
.change_context(StorageError::KVError)?;
@ -186,14 +196,17 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
match storage_scheme {
MerchantStorageScheme::PostgresOnly => database_call().await,
MerchantStorageScheme::RedisKv => {
let key = format!("mid_{merchant_id}_po_{payout_id}");
let key = PartitionKey::MerchantIdPayoutId {
merchant_id,
payout_id,
};
let field = format!("po_{payout_id}");
Box::pin(utils::try_redis_get_else_try_database_get(
async {
kv_wrapper::<DieselPayouts, _, _>(
self,
KvOperation::<DieselPayouts>::HGet(&field),
&key,
key,
)
.await?
.try_into_hget()
@ -234,14 +247,17 @@ impl<T: DatabaseStore> PayoutsInterface for KVRouterStore<T> {
}))
}
MerchantStorageScheme::RedisKv => {
let key = format!("mid_{merchant_id}_po_{payout_id}");
let key = PartitionKey::MerchantIdPayoutId {
merchant_id,
payout_id,
};
let field = format!("po_{payout_id}");
Box::pin(utils::try_redis_get_else_try_database_get(
async {
kv_wrapper::<DieselPayouts, _, _>(
self,
KvOperation::<DieselPayouts>::HGet(&field),
&key,
key,
)
.await?
.try_into_hget()