mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-04 14:07:18 +08:00 
			
		
		
		
	refactor(storage_models, errors): impl StorageErrorExt for error_stack::Result<T, errors::StorageError> (#886)
This commit is contained in:
		@ -156,9 +156,7 @@ pub async fn create_merchant_account(
 | 
			
		||||
    let merchant_account = db
 | 
			
		||||
        .insert_merchant(merchant_account)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_duplicate_response(errors::ApiErrorResponse::DuplicateMerchantAccount)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_duplicate_response(errors::ApiErrorResponse::DuplicateMerchantAccount)?;
 | 
			
		||||
 | 
			
		||||
    Ok(service_api::ApplicationResponse::Json(
 | 
			
		||||
        ForeignTryFrom::foreign_try_from(merchant_account).change_context(
 | 
			
		||||
@ -176,9 +174,7 @@ pub async fn get_merchant_account(
 | 
			
		||||
    let merchant_account = db
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&req.merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    Ok(service_api::ApplicationResponse::Json(
 | 
			
		||||
        ForeignTryFrom::foreign_try_from(merchant_account).change_context(
 | 
			
		||||
@ -267,9 +263,7 @@ pub async fn merchant_account_update(
 | 
			
		||||
    let response = db
 | 
			
		||||
        .update_specific_fields_in_merchant(merchant_id, updated_merchant_account)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    Ok(service_api::ApplicationResponse::Json(
 | 
			
		||||
        ForeignTryFrom::foreign_try_from(response).change_context(
 | 
			
		||||
@ -287,9 +281,7 @@ pub async fn merchant_account_delete(
 | 
			
		||||
    let is_deleted = db
 | 
			
		||||
        .delete_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
    let response = api::MerchantAccountDeleteResponse {
 | 
			
		||||
        merchant_id,
 | 
			
		||||
        deleted: is_deleted,
 | 
			
		||||
@ -330,9 +322,7 @@ async fn validate_merchant_id<S: Into<String>>(
 | 
			
		||||
) -> RouterResult<MerchantAccount> {
 | 
			
		||||
    db.find_merchant_account_by_merchant_id(&merchant_id.into())
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn get_business_details_wrapper(
 | 
			
		||||
@ -364,9 +354,7 @@ pub async fn create_payment_connector(
 | 
			
		||||
    let merchant_account = store
 | 
			
		||||
        .find_merchant_account_by_merchant_id(merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let (business_country, business_label) = get_business_details_wrapper(&req, &merchant_account)?;
 | 
			
		||||
 | 
			
		||||
@ -431,9 +419,7 @@ pub async fn create_payment_connector(
 | 
			
		||||
    let mca = store
 | 
			
		||||
        .insert_merchant_connector_account(merchant_connector_account)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_duplicate_response(errors::ApiErrorResponse::DuplicateMerchantConnectorAccount)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_duplicate_response(errors::ApiErrorResponse::DuplicateMerchantConnectorAccount)?;
 | 
			
		||||
 | 
			
		||||
    let mca_response = ForeignTryFrom::foreign_try_from(mca)?;
 | 
			
		||||
 | 
			
		||||
@ -448,9 +434,7 @@ pub async fn retrieve_payment_connector(
 | 
			
		||||
    let _merchant_account = store
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let mca = store
 | 
			
		||||
        .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
 | 
			
		||||
@ -458,9 +442,7 @@ pub async fn retrieve_payment_connector(
 | 
			
		||||
            &merchant_connector_id,
 | 
			
		||||
        )
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    Ok(service_api::ApplicationResponse::Json(
 | 
			
		||||
        ForeignTryFrom::foreign_try_from(mca)?,
 | 
			
		||||
@ -475,16 +457,12 @@ pub async fn list_payment_connectors(
 | 
			
		||||
    store
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|err| {
 | 
			
		||||
            err.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let merchant_connector_accounts = store
 | 
			
		||||
        .find_merchant_connector_account_by_merchant_id_and_disabled_list(&merchant_id, true)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)?;
 | 
			
		||||
    let mut response = vec![];
 | 
			
		||||
 | 
			
		||||
    // The can be eliminated once [#79711](https://github.com/rust-lang/rust/issues/79711) is stabilized
 | 
			
		||||
@ -504,9 +482,7 @@ pub async fn update_payment_connector(
 | 
			
		||||
    let _merchant_account = db
 | 
			
		||||
        .find_merchant_account_by_merchant_id(merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let mca = db
 | 
			
		||||
        .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
 | 
			
		||||
@ -514,9 +490,7 @@ pub async fn update_payment_connector(
 | 
			
		||||
            merchant_connector_id,
 | 
			
		||||
        )
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let payment_methods_enabled = req.payment_methods_enabled.map(|pm_enabled| {
 | 
			
		||||
        pm_enabled
 | 
			
		||||
@ -568,9 +542,7 @@ pub async fn delete_payment_connector(
 | 
			
		||||
    let _merchant_account = db
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let is_deleted = db
 | 
			
		||||
        .delete_merchant_connector_account_by_merchant_id_merchant_connector_id(
 | 
			
		||||
@ -578,9 +550,7 @@ pub async fn delete_payment_connector(
 | 
			
		||||
            &merchant_connector_id,
 | 
			
		||||
        )
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)?;
 | 
			
		||||
    let response = api::MerchantConnectorDeleteResponse {
 | 
			
		||||
        merchant_id,
 | 
			
		||||
        merchant_connector_id,
 | 
			
		||||
@ -598,9 +568,7 @@ pub async fn kv_for_merchant(
 | 
			
		||||
    let merchant_account = db
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let updated_merchant_account = match (enable, merchant_account.storage_scheme) {
 | 
			
		||||
        (true, enums::MerchantStorageScheme::RedisKv)
 | 
			
		||||
@ -650,9 +618,7 @@ pub async fn check_merchant_account_kv_status(
 | 
			
		||||
    let merchant_account = db
 | 
			
		||||
        .find_merchant_account_by_merchant_id(&merchant_id)
 | 
			
		||||
        .await
 | 
			
		||||
        .map_err(|error| {
 | 
			
		||||
            error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
 | 
			
		||||
        })?;
 | 
			
		||||
        .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
 | 
			
		||||
 | 
			
		||||
    let kv_status = matches!(
 | 
			
		||||
        merchant_account.storage_scheme,
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user