refactor: Pass country and currency as json format in MCA (#523)

This commit is contained in:
Manoj Ghorela
2023-02-24 19:39:03 +05:30
committed by GitHub
parent 32de632ded
commit d27e6be599
5 changed files with 212 additions and 37 deletions

View File

@ -1,4 +1,4 @@
use common_utils::ext_traits::ValueExt;
use common_utils::{ext_traits::ValueExt, fp_utils::when};
use error_stack::{report, FutureExt, ResultExt};
use storage_models::{enums, merchant_account};
use uuid::Uuid;
@ -239,6 +239,38 @@ async fn validate_merchant_id<S: Into<String>>(
error.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
})
}
fn validate_pm_enabled(pm: &api::PaymentMethods) -> RouterResult<()> {
if let Some(ac) = pm.accepted_countries.to_owned() {
when(ac.enable_all && ac.enable_only.is_some(), || {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "In case all countries are enabled,provide the disable_only country"
.to_string(),
})
})?;
when(!ac.enable_all && ac.disable_only.is_some(), || {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "In case enable_all is false, provide the enable_only country".to_string(),
})
})?;
};
if let Some(ac) = pm.accepted_currencies.to_owned() {
when(ac.enable_all && ac.enable_only.is_some(), || {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "In case all currencies are enabled, provide the disable_only currency"
.to_string(),
})
})?;
when(!ac.enable_all && ac.disable_only.is_some(), || {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "In case enable_all is false, provide the enable_only currency"
.to_string(),
})
})?;
};
Ok(())
}
// Payment Connector API - Every merchant and connector can have an instance of (merchant <> connector)
// with unique merchant_connector_id for Create Operation
@ -259,6 +291,7 @@ pub async fn create_payment_connector(
let payment_methods_enabled = match req.payment_methods_enabled {
Some(val) => {
for pm in val.into_iter() {
validate_pm_enabled(&pm)?;
let pm_value = utils::Encode::<api::PaymentMethods>::encode_to_value(&pm)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable(
@ -386,6 +419,9 @@ pub async fn update_payment_connector(
pm_enabled
.iter()
.flat_map(|payment_method| {
validate_pm_enabled(payment_method)
.change_context(errors::ParsingError)
.attach_printable("Validation for accepted country and currency failed")?;
utils::Encode::<api::PaymentMethods>::encode_to_value(payment_method)
})
.collect::<Vec<serde_json::Value>>()