fix(router): merchant account delete does not delete the merchant_key_store (#2367)

This commit is contained in:
Hudson C. Dalprá
2023-10-04 07:05:30 +13:00
committed by GitHub
parent 9a0e637b0f
commit 35f7ce0f4d
3 changed files with 76 additions and 2 deletions

View File

@ -27,4 +27,16 @@ impl MerchantKeyStore {
)
.await
}
#[instrument(skip(conn))]
pub async fn delete_by_merchant_id(
conn: &PgPooledConn,
merchant_id: &str,
) -> StorageResult<bool> {
generics::generic_delete::<<Self as HasTable>::Table, _>(
conn,
dsl::merchant_id.eq(merchant_id.to_owned()),
)
.await
}
}

View File

@ -465,11 +465,19 @@ pub async fn merchant_account_delete(
state: AppState,
merchant_id: String,
) -> RouterResponse<api::MerchantAccountDeleteResponse> {
let mut is_deleted = false;
let db = state.store.as_ref();
let is_deleted = db
let is_merchant_account_deleted = db
.delete_merchant_account_by_merchant_id(&merchant_id)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
if is_merchant_account_deleted {
let is_merchant_key_store_deleted = db
.delete_merchant_key_store_by_merchant_id(&merchant_id)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
is_deleted = is_merchant_account_deleted && is_merchant_key_store_deleted;
}
let response = api::MerchantAccountDeleteResponse {
merchant_id,
deleted: is_deleted,

View File

@ -1,7 +1,7 @@
use error_stack::{IntoReport, ResultExt};
use masking::Secret;
#[cfg(feature = "accounts_cache")]
use storage_impl::redis::cache::ACCOUNTS_CACHE;
use storage_impl::redis::cache::{CacheKind, ACCOUNTS_CACHE};
use crate::{
connection,
@ -27,6 +27,11 @@ pub trait MerchantKeyStoreInterface {
merchant_id: &str,
key: &Secret<Vec<u8>>,
) -> CustomResult<domain::MerchantKeyStore, errors::StorageError>;
async fn delete_merchant_key_store_by_merchant_id(
&self,
merchant_id: &str,
) -> CustomResult<bool, errors::StorageError>;
}
#[async_trait::async_trait]
@ -66,6 +71,7 @@ impl MerchantKeyStoreInterface for Store {
.map_err(Into::into)
.into_report()
};
#[cfg(not(feature = "accounts_cache"))]
{
fetch_func()
@ -90,6 +96,38 @@ impl MerchantKeyStoreInterface for Store {
.change_context(errors::StorageError::DecryptionError)
}
}
async fn delete_merchant_key_store_by_merchant_id(
&self,
merchant_id: &str,
) -> CustomResult<bool, errors::StorageError> {
let delete_func = || async {
let conn = connection::pg_connection_write(self).await?;
diesel_models::merchant_key_store::MerchantKeyStore::delete_by_merchant_id(
&conn,
merchant_id,
)
.await
.map_err(Into::into)
.into_report()
};
#[cfg(not(feature = "accounts_cache"))]
{
delete_func().await
}
#[cfg(feature = "accounts_cache")]
{
let key_store_cache_key = format!("merchant_key_store_{}", merchant_id);
super::cache::publish_and_redact(
self,
CacheKind::Accounts(key_store_cache_key.into()),
delete_func,
)
.await
}
}
}
#[async_trait::async_trait]
@ -140,6 +178,22 @@ impl MerchantKeyStoreInterface for MockDb {
.await
.change_context(errors::StorageError::DecryptionError)
}
async fn delete_merchant_key_store_by_merchant_id(
&self,
merchant_id: &str,
) -> CustomResult<bool, errors::StorageError> {
let mut merchant_key_stores = self.merchant_key_store.lock().await;
let index = merchant_key_stores
.iter()
.position(|mks| mks.merchant_id == merchant_id)
.ok_or(errors::StorageError::ValueNotFound(format!(
"No merchant key store found for merchant_id = {}",
merchant_id
)))?;
merchant_key_stores.remove(index);
Ok(true)
}
}
#[cfg(test)]