Skip to content

Commit

Permalink
add locale in SessionState
Browse files Browse the repository at this point in the history
  • Loading branch information
cookieg13 committed Jan 2, 2025
1 parent b7b57f0 commit 66d841f
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 150 deletions.
4 changes: 2 additions & 2 deletions crates/router/src/bin/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub async fn deep_health_check(
let app_state = Arc::clone(&state.into_inner());
let service_name = service.into_inner();
for (tenant, _) in stores {
let session_state_res = app_state.clone().get_session_state(&tenant, || {
let session_state_res = app_state.clone().get_session_state(&tenant, None, || {
errors::ApiErrorResponse::MissingRequiredField {
field_name: "tenant_id",
}
Expand Down Expand Up @@ -397,7 +397,7 @@ async fn start_scheduler(
WorkflowRunner {},
|state, tenant| {
Arc::new(state.clone())
.get_session_state(tenant, || ProcessTrackerError::TenantNotFound.into())
.get_session_state(tenant, None, || ProcessTrackerError::TenantNotFound.into())
},
)
.await
Expand Down
68 changes: 20 additions & 48 deletions crates/router/src/core/payment_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use api_models::{
};
use common_utils::{
consts::{DEFAULT_LOCALE, DEFAULT_SESSION_EXPIRY},
ext_traits::{AsyncExt, OptionExt, ValueExt},
ext_traits::{OptionExt, ValueExt},
types::{AmountConvertor, StringMajorUnitForCore},
};
use error_stack::{report, ResultExt};
Expand All @@ -28,9 +28,8 @@ use crate::{
},
errors::RouterResponse,
get_payment_link_config_value, get_payment_link_config_value_based_on_priority,
headers::ACCEPT_LANGUAGE,
routes::SessionState,
services::{self, authentication::get_header_value_by_key},
services,
types::{
api::payment_link::PaymentLinkResponseExt,
domain,
Expand Down Expand Up @@ -70,7 +69,6 @@ pub async fn form_payment_link_data(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: common_utils::id_type::PaymentId,
locale: Option<String>,
) -> RouterResult<(PaymentLink, PaymentLinkData, PaymentLinkConfig)> {
todo!()
}
Expand All @@ -82,7 +80,6 @@ pub async fn form_payment_link_data(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: common_utils::id_type::PaymentId,
locale: Option<String>,
) -> RouterResult<(PaymentLink, PaymentLinkData, PaymentLinkConfig)> {
let db = &*state.store;
let key_manager_state = &state.into();
Expand Down Expand Up @@ -242,7 +239,7 @@ pub async fn form_payment_link_data(
redirect: false,
theme: payment_link_config.theme.clone(),
return_url: return_url.clone(),
locale: locale.clone(),
locale: Some(state.clone().locale),
transaction_details: payment_link_config.transaction_details.clone(),
unified_code: payment_attempt.unified_code,
unified_message: payment_attempt.unified_message,
Expand Down Expand Up @@ -273,7 +270,7 @@ pub async fn form_payment_link_data(
display_sdk_only: payment_link_config.display_sdk_only,
hide_card_nickname_field: payment_link_config.hide_card_nickname_field,
show_card_form_by_default: payment_link_config.show_card_form_by_default,
locale,
locale: Some(state.clone().locale),
transaction_details: payment_link_config.transaction_details.clone(),
background_image: payment_link_config.background_image.clone(),
details_layout: payment_link_config.details_layout,
Expand All @@ -296,17 +293,9 @@ pub async fn initiate_secure_payment_link_flow(
payment_id: common_utils::id_type::PaymentId,
request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let locale = get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers)?
.map(|val| val.to_string());
let (payment_link, payment_link_details, payment_link_config) = form_payment_link_data(
&state,
merchant_account,
key_store,
merchant_id,
payment_id,
locale,
)
.await?;
let (payment_link, payment_link_details, payment_link_config) =
form_payment_link_data(&state, merchant_account, key_store, merchant_id, payment_id)
.await?;

validator::validate_secure_payment_link_render_request(
request_headers,
Expand Down Expand Up @@ -396,19 +385,10 @@ pub async fn initiate_payment_link_flow(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: common_utils::id_type::PaymentId,
request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let locale = get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers)?
.map(|val| val.to_string());
let (_, payment_details, payment_link_config) = form_payment_link_data(
&state,
merchant_account,
key_store,
merchant_id,
payment_id,
locale,
)
.await?;
let (_, payment_details, payment_link_config) =
form_payment_link_data(&state, merchant_account, key_store, merchant_id, payment_id)
.await?;

let css_script = get_color_scheme_css(&payment_link_config);
let js_script = get_js_script(&payment_details)?;
Expand Down Expand Up @@ -727,7 +707,6 @@ pub async fn get_payment_link_status(
_key_store: domain::MerchantKeyStore,
_merchant_id: common_utils::id_type::MerchantId,
_payment_id: common_utils::id_type::PaymentId,
_request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
todo!()
}
Expand All @@ -739,10 +718,8 @@ pub async fn get_payment_link_status(
key_store: domain::MerchantKeyStore,
merchant_id: common_utils::id_type::MerchantId,
payment_id: common_utils::id_type::PaymentId,
request_headers: &header::HeaderMap,
_request_headers: &header::HeaderMap,
) -> RouterResponse<services::PaymentLinkFormData> {
let locale = get_header_value_by_key(ACCEPT_LANGUAGE.into(), request_headers)?
.map(|val| val.to_string());
let db = &*state.store;
let key_manager_state = &(&state).into();

Expand Down Expand Up @@ -858,19 +835,14 @@ pub async fn get_payment_link_status(
consts::DEFAULT_UNIFIED_ERROR_MESSAGE.to_owned(),
)
};
let unified_translated_message = locale
.as_ref()
.async_and_then(|locale_str| async {
helpers::get_unified_translation(
&state,
unified_code.to_owned(),
unified_message.to_owned(),
locale_str.to_owned(),
)
.await
})
.await
.or(Some(unified_message));
let unified_translated_message = helpers::get_unified_translation(
&state,
unified_code.to_owned(),
unified_message.to_owned(),
state.locale.clone(),
)
.await
.or(Some(unified_message));

let payment_details = api_models::payments::PaymentLinkStatusDetails {
amount,
Expand All @@ -885,7 +857,7 @@ pub async fn get_payment_link_status(
redirect: true,
theme: payment_link_config.theme.clone(),
return_url,
locale,
locale: Some(state.locale.clone()),
transaction_details: payment_link_config.transaction_details,
unified_code: Some(unified_code),
unified_message: unified_translated_message,
Expand Down
11 changes: 5 additions & 6 deletions crates/router/src/core/payout_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub async fn initiate_payout_link(
key_store: domain::MerchantKeyStore,
req: payouts::PayoutLinkInitiateRequest,
request_headers: &header::HeaderMap,
locale: String,
) -> RouterResponse<services::GenericLinkFormData> {
let db: &dyn StorageInterface = &*state.store;
let merchant_id = merchant_account.get_id();
Expand Down Expand Up @@ -128,7 +127,7 @@ pub async fn initiate_payout_link(
GenericLinks {
allowed_domains,
data: GenericLinksData::ExpiredLink(expired_link_data),
locale,
locale: state.locale,
},
)))
}
Expand Down Expand Up @@ -245,7 +244,7 @@ pub async fn initiate_payout_link(
enabled_payment_methods_with_required_fields,
amount,
currency: payout.destination_currency,
locale: locale.clone(),
locale: state.locale.clone(),
form_layout: link_data.form_layout,
test_mode: link_data.test_mode.unwrap_or(false),
};
Expand All @@ -270,7 +269,7 @@ pub async fn initiate_payout_link(
GenericLinks {
allowed_domains,
data: GenericLinksData::PayoutLink(generic_form_data),
locale,
locale: state.locale.clone(),
},
)))
}
Expand All @@ -282,7 +281,7 @@ pub async fn initiate_payout_link(
&state,
payout_attempt.unified_code.as_ref(),
payout_attempt.unified_message.as_ref(),
&locale,
&state.locale.clone(),
)
.await?;
let js_data = payouts::PayoutLinkStatusDetails {
Expand Down Expand Up @@ -322,7 +321,7 @@ pub async fn initiate_payout_link(
GenericLinks {
allowed_domains,
data: GenericLinksData::PayoutLinkStatus(generic_status_data),
locale,
locale: state.locale.clone(),
},
)))
}
Expand Down
22 changes: 6 additions & 16 deletions crates/router/src/core/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ pub async fn payouts_create_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutCreateRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
// Validate create request
let (payout_id, payout_method_data, profile_id, customer) =
Expand All @@ -332,7 +331,7 @@ pub async fn payouts_create_core(
&payout_id,
&profile_id,
payout_method_data.as_ref(),
locale,
&state.locale,
customer.as_ref(),
)
.await?;
Expand Down Expand Up @@ -382,15 +381,14 @@ pub async fn payouts_confirm_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutCreateRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
let mut payout_data = make_payout_data(
&state,
&merchant_account,
None,
&key_store,
&payouts::PayoutRequest::PayoutCreateRequest(Box::new(req.to_owned())),
locale,
&state.locale,
)
.await?;
let payout_attempt = payout_data.payout_attempt.to_owned();
Expand Down Expand Up @@ -454,7 +452,6 @@ pub async fn payouts_update_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutCreateRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
let payout_id = req.payout_id.clone().get_required_value("payout_id")?;
let mut payout_data = make_payout_data(
Expand All @@ -463,7 +460,7 @@ pub async fn payouts_update_core(
None,
&key_store,
&payouts::PayoutRequest::PayoutCreateRequest(Box::new(req.to_owned())),
locale,
&state.locale,
)
.await?;

Expand Down Expand Up @@ -539,15 +536,14 @@ pub async fn payouts_retrieve_core(
profile_id: Option<common_utils::id_type::ProfileId>,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutRetrieveRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
let mut payout_data = make_payout_data(
&state,
&merchant_account,
profile_id,
&key_store,
&payouts::PayoutRequest::PayoutRetrieveRequest(req.to_owned()),
locale,
&state.locale,
)
.await?;
let payout_attempt = payout_data.payout_attempt.to_owned();
Expand Down Expand Up @@ -584,15 +580,14 @@ pub async fn payouts_cancel_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutActionRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
let mut payout_data = make_payout_data(
&state,
&merchant_account,
None,
&key_store,
&payouts::PayoutRequest::PayoutActionRequest(req.to_owned()),
locale,
&state.locale,
)
.await?;

Expand Down Expand Up @@ -678,15 +673,14 @@ pub async fn payouts_fulfill_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutActionRequest,
locale: &str,
) -> RouterResponse<payouts::PayoutCreateResponse> {
let mut payout_data = make_payout_data(
&state,
&merchant_account,
None,
&key_store,
&payouts::PayoutRequest::PayoutActionRequest(req.to_owned()),
locale,
&state.locale,
)
.await?;

Expand Down Expand Up @@ -773,7 +767,6 @@ pub async fn payouts_list_core(
_profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,
_key_store: domain::MerchantKeyStore,
_constraints: payouts::PayoutListConstraints,
_locale: &str,
) -> RouterResponse<payouts::PayoutListResponse> {
todo!()
}
Expand All @@ -789,7 +782,6 @@ pub async fn payouts_list_core(
profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,
key_store: domain::MerchantKeyStore,
constraints: payouts::PayoutListConstraints,
_locale: &str,
) -> RouterResponse<payouts::PayoutListResponse> {
validator::validate_payout_list_request(&constraints)?;
let merchant_id = merchant_account.get_id();
Expand Down Expand Up @@ -910,7 +902,6 @@ pub async fn payouts_filtered_list_core(
profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,
key_store: domain::MerchantKeyStore,
filters: payouts::PayoutListFilterConstraints,
_locale: &str,
) -> RouterResponse<payouts::PayoutListResponse> {
let limit = &filters.limit;
validator::validate_payout_list_request_for_joins(*limit)?;
Expand Down Expand Up @@ -1014,7 +1005,6 @@ pub async fn payouts_list_available_filters_core(
merchant_account: domain::MerchantAccount,
profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,
time_range: common_utils::types::TimeRange,
_locale: &str,
) -> RouterResponse<api::PayoutListFilters> {
let db = state.store.as_ref();
let payouts = db
Expand Down
6 changes: 2 additions & 4 deletions crates/router/src/core/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,14 @@ pub async fn refund_retrieve_core(
})
.transpose()?;

let locale = state.get_locale();
let unified_translated_message = if let (Some(unified_code), Some(unified_message)) =
(refund.unified_code.clone(), refund.unified_message.clone())
{
helpers::get_unified_translation(
&state,
unified_code,
unified_message.clone(),
locale.to_owned(),
state.locale.to_string(),
)
.await
.or(Some(unified_message))
Expand Down Expand Up @@ -957,15 +956,14 @@ pub async fn validate_and_create_refund(
}
}
};
let locale = state.get_locale();
let unified_translated_message = if let (Some(unified_code), Some(unified_message)) =
(refund.unified_code.clone(), refund.unified_message.clone())
{
helpers::get_unified_translation(
state,
unified_code,
unified_message.clone(),
locale.to_owned(),
state.locale.to_string(),
)
.await
.or(Some(unified_message))
Expand Down
Loading

0 comments on commit 66d841f

Please sign in to comment.