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

@ -25,11 +25,23 @@ pub enum PartitionKey<'a> {
merchant_id: &'a str,
payment_id: &'a str,
},
MerchantIdPaymentIdCombination {
CombinationKey {
combination: &'a str,
},
MerchantIdCustomerId {
merchant_id: &'a str,
customer_id: &'a str,
},
MerchantIdPayoutId {
merchant_id: &'a str,
payout_id: &'a str,
},
MerchantIdPayoutAttemptId {
merchant_id: &'a str,
payout_attempt_id: &'a str,
},
}
// PartitionKey::MerchantIdPaymentId {merchant_id, payment_id}
impl<'a> std::fmt::Display for PartitionKey<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
@ -37,9 +49,19 @@ impl<'a> std::fmt::Display for PartitionKey<'a> {
merchant_id,
payment_id,
} => f.write_str(&format!("mid_{merchant_id}_pid_{payment_id}")),
PartitionKey::MerchantIdPaymentIdCombination { combination } => {
f.write_str(combination)
}
PartitionKey::CombinationKey { combination } => f.write_str(combination),
PartitionKey::MerchantIdCustomerId {
merchant_id,
customer_id,
} => f.write_str(&format!("mid_{merchant_id}_cust_{customer_id}")),
PartitionKey::MerchantIdPayoutId {
merchant_id,
payout_id,
} => f.write_str(&format!("mid_{merchant_id}_po_{payout_id}")),
PartitionKey::MerchantIdPayoutAttemptId {
merchant_id,
payout_attempt_id,
} => f.write_str(&format!("mid_{merchant_id}_poa_{payout_attempt_id}")),
}
}
}
@ -90,7 +112,7 @@ where
pub async fn kv_wrapper<'a, T, D, S>(
store: &KVRouterStore<D>,
op: KvOperation<'a, S>,
key: impl AsRef<str>,
partition_key: PartitionKey<'a>,
) -> CustomResult<KvResult<T>, RedisError>
where
T: de::DeserializeOwned,
@ -99,21 +121,20 @@ where
{
let redis_conn = store.get_redis_conn()?;
let key = key.as_ref();
let key = format!("{}", partition_key);
let type_name = std::any::type_name::<T>();
let operation = op.to_string();
let ttl = store.ttl_for_kv;
let partition_key = PartitionKey::MerchantIdPaymentIdCombination { combination: key };
let result = async {
match op {
KvOperation::Hset(value, sql) => {
logger::debug!(kv_operation= %operation, value = ?value);
redis_conn
.set_hash_fields(key, value, Some(ttl.into()))
.set_hash_fields(&key, value, Some(ttl.into()))
.await?;
store
@ -125,14 +146,14 @@ where
KvOperation::HGet(field) => {
let result = redis_conn
.get_hash_field_and_deserialize(key, field, type_name)
.get_hash_field_and_deserialize(&key, field, type_name)
.await?;
Ok(KvResult::HGet(result))
}
KvOperation::Scan(pattern) => {
let result: Vec<T> = redis_conn
.hscan_and_deserialize(key, pattern, None)
.hscan_and_deserialize(&key, pattern, None)
.await
.and_then(|result| {
if result.is_empty() {
@ -150,7 +171,7 @@ where
value.check_for_constraints(&redis_conn).await?;
let result = redis_conn
.serialize_and_set_hash_field_if_not_exist(key, field, value, Some(ttl))
.serialize_and_set_hash_field_if_not_exist(&key, field, value, Some(ttl))
.await?;
if matches!(result, redis_interface::HsetnxReply::KeySet) {
@ -167,7 +188,7 @@ where
logger::debug!(kv_operation= %operation, value = ?value);
let result = redis_conn
.serialize_and_set_key_if_not_exist(key, value, Some(ttl.into()))
.serialize_and_set_key_if_not_exist(&key, value, Some(ttl.into()))
.await?;
value.check_for_constraints(&redis_conn).await?;
@ -183,7 +204,7 @@ where
}
KvOperation::Get => {
let result = redis_conn.get_and_deserialize_key(key, type_name).await?;
let result = redis_conn.get_and_deserialize_key(&key, type_name).await?;
Ok(KvResult::Get(result))
}
}