Repository: juspay/hyperswitch
File: crates/euclid_wasm/src/lib.rs (Rust)

Code before:
    Ok(serde_wasm_bindgen::to_value(&converted_amount)?)
}

/// This function can be used by the frontend to get all the two letter country codes
/// along with their country names.
#[wasm_bindgen(js_name=getTwoLetterCountryCode)]
pub fn get_two_letter_country_code() -> JsResult {
    let country_code_with_name = CountryAlpha2::iter()
        .map(|country_code| CountryCodeWithName {
            code: country_code,
            name: common_enums::Country::from_alpha2(country_code),
        })
        .collect::<Vec<_>>();

    Ok(serde_wasm_bindgen::to_value(&country_code_with_name)?)
}

/// This function can be used by the frontend to get all the merchant category codes
/// along with their names.
#[wasm_bindgen(js_name=getMerchantCategoryCodeWithName)]
pub fn get_merchant_category_code_with_name() -> JsResult {
    let merchant_category_codes_with_name = vec![5413, 7011, 763, 8111, 5021, 4816, 5661]
        .into_iter()
        .filter_map(|mcc_value| {
            let mcc = MerchantCategoryCode::new(mcc_value).ok()?;
            let mcc_name = mcc.get_category_name().ok()?;
            Some(MerchantCategoryCodeWithName {
                code: mcc.clone(),
                name: mcc_name.to_string(),
            })
        })
        .collect::<Vec<_>>();

    Ok(serde_wasm_bindgen::to_value(
        &merchant_category_codes_with_name,
    )?)
}

/// This function can be used by the frontend to provide the WASM with information about
/// all the merchant's connector accounts. The input argument is a vector of all the merchant's
/// connector accounts from the API.
#[cfg(feature = "v1")]
#[wasm_bindgen(js_name = seedKnowledgeGraph)]
pub fn seed_knowledge_graph(mcas: JsValue) -> JsResult {
    let mcas: Vec<api_models::admin::MerchantConnectorResponse> =
        serde_wasm_bindgen::from_value(mcas)?;
    let connectors: Vec<ast::ConnectorChoice> = mcas
        .iter()
        .map(|mca| {
            Ok::<_, strum::ParseError>(ast::ConnectorChoice {
                connector: RoutableConnectors::from_str(&mca.connector_name)?,

Review comment (bug):
Can we log the error in case of error ?

If we are not able to get the category name for an mcc it will be skipped and the remaining mcc will be displayed in the list. And it would be difficult to debug why an mcc is being skipped without any logs.

Code after:
        .get()
        .ok_or("Forex Data not seeded")
        .err_to_js()?;
    let from_currency: common_enums::Currency = serde_wasm_bindgen::from_value(from_currency)?;
    let to_currency: common_enums::Currency = serde_wasm_bindgen::from_value(to_currency)?;
    let converted_amount = convert_currency(forex_data, from_currency, to_currency, amount)
        .map_err(|_| "conversion not possible for provided values")
        .err_to_js()?;

    Ok(serde_wasm_bindgen::to_value(&converted_amount)?)
}

/// This function can be used by the frontend to get all the two letter country codes
/// along with their country names.
#[wasm_bindgen(js_name=getTwoLetterCountryCode)]
pub fn get_two_letter_country_code() -> JsResult {
    let country_code_with_name = CountryAlpha2::iter()
        .map(|country_code| CountryCodeWithName {
            code: country_code,
            name: common_enums::Country::from_alpha2(country_code),
        })
        .collect::<Vec<_>>();

    Ok(serde_wasm_bindgen::to_value(&country_code_with_name)?)
}

/// This function can be used by the frontend to get all the merchant category codes
/// along with their names.
#[wasm_bindgen(js_name=getMerchantCategoryCodeWithName)]
pub fn get_merchant_category_code_with_name() -> JsResult {
    let merchant_category_codes_with_name = vec![5411, 7011, 763, 8111, 5021, 4816, 5661]
        .into_iter()
        .filter_map(|mcc_value| match MerchantCategoryCode::new(mcc_value) {
            Ok(mcc) => match mcc.get_category_name() {
                Ok(mcc_name) => Some(MerchantCategoryCodeWithName {
                    code: mcc.clone(),
                    name: mcc_name.to_string(),
                }),
                Err(err) => {
                    console_error(&format!(
                        "Failed to get category name for MCC {}: {:?}",
                        mcc_value, err
                    ));
                    None
                }
            },
            Err(err) => {
                console_error(&format!(
                    "Failed to create MCC for value {}: {:?}",
                    mcc_value, err
                ));