Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: conditionally add reverse lookup in update mandate #4387

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/diesel_models/src/mandate.rs
Expand Up @@ -177,6 +177,10 @@ impl MandateUpdateInternal {
..source
}
}

pub fn get_connector_mandate_id(&self) -> Option<&String> {
self.connector_mandate_id.as_ref()
}
}

impl From<&MandateNew> for Mandate {
Expand Down
66 changes: 52 additions & 14 deletions crates/router/src/db/mandate.rs
Expand Up @@ -207,23 +207,39 @@ mod storage {
let field = format!("mandate_{}", mandate_id);
let key_str = key.to_string();

if let diesel_models::MandateUpdate::ConnectorMandateIdUpdate {
connector_mandate_id: Some(val),
..
} = &mandate_update
{
let rev_lookup = diesel_models::ReverseLookupNew {
sk_id: field.clone(),
pk_id: key_str.clone(),
lookup_id: format!("mid_{}_conn_mandate_{}", merchant_id, val),
source: "mandate".to_string(),
updated_by: storage_scheme.to_string(),
};
self.insert_reverse_lookup(rev_lookup, storage_scheme)
let m_update = diesel_models::MandateUpdateInternal::from(mandate_update);

match (
mandate.connector_mandate_id.as_ref(),
m_update.get_connector_mandate_id(),
) {
(None, Some(val)) => {
add_connector_mandate_id_reverse_lookup(
self,
key_str.as_str(),
field.as_str(),
merchant_id,
val.as_str(),
storage_scheme,
)
.await?;
}
(Some(old_val), Some(new_val)) => {
if old_val.ne(new_val) {
add_connector_mandate_id_reverse_lookup(
self,
key_str.as_str(),
field.as_str(),
merchant_id,
new_val.as_str(),
storage_scheme,
)
.await?;
}
}
_ => (),
}

let m_update = diesel_models::MandateUpdateInternal::from(mandate_update);
let updated_mandate = m_update.clone().apply_changeset(mandate.clone());

let redis_value = serde_json::to_string(&updated_mandate)
Expand Down Expand Up @@ -342,6 +358,28 @@ mod storage {
}
}
}

#[inline]
#[instrument(skip_all)]
async fn add_connector_mandate_id_reverse_lookup(
store: &Store,
key: &str,
field: &str,
merchant_id: &str,
connector_mandate_id: &str,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<diesel_models::ReverseLookup, errors::StorageError> {
let rev_lookup = diesel_models::ReverseLookupNew {
sk_id: field.to_string(),
pk_id: key.to_string(),
lookup_id: format!("mid_{}_conn_mandate_{}", merchant_id, connector_mandate_id),
source: "mandate".to_string(),
updated_by: storage_scheme.to_string(),
};
store
.insert_reverse_lookup(rev_lookup, storage_scheme)
.await
}
}

#[cfg(not(feature = "kv_store"))]
Expand Down