Skip to content

Commit

Permalink
refactor: #3059 group db update calls (#2788)
Browse files Browse the repository at this point in the history
Co-authored-by: akshay.s <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 7, 2023
1 parent 8af150e commit 35173a1
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions crates/router/src/core/payments/operations/payment_response.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;

use async_trait::async_trait;
use data_models::payments::payment_attempt::PaymentAttempt;
use error_stack::ResultExt;
use futures::FutureExt;
use router_derive;
use router_env::{instrument, tracing};
use storage_impl::DataModelExt;
use tracing_futures::Instrument;

use super::{Operation, PostUpdateTracker};
Expand Down Expand Up @@ -589,6 +591,17 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
let m_db = state.clone().store;
let m_payment_attempt_update = payment_attempt_update.clone();
let m_payment_attempt = payment_attempt.clone();

let payment_attempt = payment_attempt_update
.map(|payment_attempt_update| {
PaymentAttempt::from_storage_model(
payment_attempt_update
.to_storage_model()
.apply_changeset(payment_attempt.clone().to_storage_model()),
)
})
.unwrap_or_else(|| payment_attempt);

let payment_attempt_fut = tokio::spawn(
async move {
Box::pin(async move {
Expand All @@ -602,7 +615,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?,
None => payment_attempt,
None => m_payment_attempt,
},
)
})
Expand All @@ -614,6 +627,13 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
let m_db = state.clone().store;
let m_connector_response_update = connector_response_update.clone();
let m_connector_response = connector_response.clone();

let connector_response = connector_response_update
.map(|connector_response_update| {
connector_response_update.apply_changeset(connector_response.clone())
})
.unwrap_or_else(|| connector_response);

let connector_response_fut = tokio::spawn(
async move {
Box::pin(async move {
Expand All @@ -627,7 +647,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
)
.await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?,
None => connector_response,
None => m_connector_response,
},
)
})
Expand All @@ -636,10 +656,6 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.in_current_span(),
);

let (payment_attempt, connector_response) = futures::try_join!(
utils::flatten_join_error(payment_attempt_fut),
utils::flatten_join_error(connector_response_fut)
)?;
payment_data.payment_attempt = payment_attempt;
payment_data.connector_response = connector_response;

Expand Down Expand Up @@ -701,12 +717,14 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.in_current_span(),
);

let (payment_intent, _) = futures::try_join!(
let (payment_intent, _, _, _) = futures::try_join!(
utils::flatten_join_error(payment_intent_fut),
utils::flatten_join_error(mandate_update_fut)
utils::flatten_join_error(mandate_update_fut),
utils::flatten_join_error(payment_attempt_fut),
utils::flatten_join_error(connector_response_fut)
)?;
payment_data.payment_intent = payment_intent;

payment_data.payment_intent = payment_intent;
Ok(payment_data)
}

Expand Down

0 comments on commit 35173a1

Please sign in to comment.