|
28 | 28 | key_store: &domain::MerchantKeyStore,
|
29 | 29 | ) -> CustomResult<domain::Address, errors::StorageError>;
|
30 | 30 |
|
| 31 | + async fn update_address_for_payments( |
| 32 | + &self, |
| 33 | + this: domain::Address, |
| 34 | + address: domain::AddressUpdate, |
| 35 | + payment_id: String, |
| 36 | + key_store: &domain::MerchantKeyStore, |
| 37 | + storage_scheme: MerchantStorageScheme, |
| 38 | + ) -> CustomResult<domain::Address, errors::StorageError>; |
| 39 | + |
31 | 40 | async fn find_address_by_address_id(
|
32 | 41 | &self,
|
33 | 42 | address_id: &str,
|
@@ -155,6 +164,32 @@ mod storage {
|
155 | 164 | .await
|
156 | 165 | }
|
157 | 166 |
|
| 167 | + async fn update_address_for_payments( |
| 168 | + &self, |
| 169 | + this: domain::Address, |
| 170 | + address_update: domain::AddressUpdate, |
| 171 | + _payment_id: String, |
| 172 | + key_store: &domain::MerchantKeyStore, |
| 173 | + _storage_scheme: MerchantStorageScheme, |
| 174 | + ) -> CustomResult<domain::Address, errors::StorageError> { |
| 175 | + let conn = connection::pg_connection_write(self).await?; |
| 176 | + let address = Conversion::convert(this) |
| 177 | + .await |
| 178 | + .change_context(errors::StorageError::EncryptionError)?; |
| 179 | + address |
| 180 | + .update(&conn, address_update.into()) |
| 181 | + .await |
| 182 | + .map_err(Into::into) |
| 183 | + .into_report() |
| 184 | + .async_and_then(|address| async { |
| 185 | + address |
| 186 | + .convert(key_store.key.get_inner()) |
| 187 | + .await |
| 188 | + .change_context(errors::StorageError::DecryptionError) |
| 189 | + }) |
| 190 | + .await |
| 191 | + } |
| 192 | + |
158 | 193 | async fn insert_address_for_payments(
|
159 | 194 | &self,
|
160 | 195 | _payment_id: &str,
|
@@ -241,6 +276,7 @@ mod storage {
|
241 | 276 | mod storage {
|
242 | 277 | use common_utils::ext_traits::AsyncExt;
|
243 | 278 | use data_models::MerchantStorageScheme;
|
| 279 | + use diesel_models::AddressUpdateInternal; |
244 | 280 | use error_stack::{IntoReport, ResultExt};
|
245 | 281 | use redis_interface::HsetnxReply;
|
246 | 282 | use router_env::{instrument, tracing};
|
@@ -348,6 +384,79 @@ mod storage {
|
348 | 384 | .await
|
349 | 385 | }
|
350 | 386 |
|
| 387 | + async fn update_address_for_payments( |
| 388 | + &self, |
| 389 | + this: domain::Address, |
| 390 | + address_update: domain::AddressUpdate, |
| 391 | + payment_id: String, |
| 392 | + key_store: &domain::MerchantKeyStore, |
| 393 | + storage_scheme: MerchantStorageScheme, |
| 394 | + ) -> CustomResult<domain::Address, errors::StorageError> { |
| 395 | + let conn = connection::pg_connection_write(self).await?; |
| 396 | + let address = Conversion::convert(this) |
| 397 | + .await |
| 398 | + .change_context(errors::StorageError::EncryptionError)?; |
| 399 | + match storage_scheme { |
| 400 | + MerchantStorageScheme::PostgresOnly => { |
| 401 | + address |
| 402 | + .update(&conn, address_update.into()) |
| 403 | + .await |
| 404 | + .map_err(Into::into) |
| 405 | + .into_report() |
| 406 | + .async_and_then(|address| async { |
| 407 | + address |
| 408 | + .convert(key_store.key.get_inner()) |
| 409 | + .await |
| 410 | + .change_context(errors::StorageError::DecryptionError) |
| 411 | + }) |
| 412 | + .await |
| 413 | + } |
| 414 | + MerchantStorageScheme::RedisKv => { |
| 415 | + let key = format!("mid_{}_pid_{}", address.merchant_id.clone(), payment_id); |
| 416 | + let field = format!("add_{}", address.address_id); |
| 417 | + let updated_address = AddressUpdateInternal::from(address_update.clone()) |
| 418 | + .create_address(address.clone()); |
| 419 | + let redis_value = serde_json::to_string(&updated_address) |
| 420 | + .into_report() |
| 421 | + .change_context(errors::StorageError::KVError)?; |
| 422 | + kv_wrapper::<(), _, _>( |
| 423 | + self, |
| 424 | + KvOperation::Hset::<storage_types::Address>((&field, redis_value)), |
| 425 | + &key, |
| 426 | + ) |
| 427 | + .await |
| 428 | + .change_context(errors::StorageError::KVError)? |
| 429 | + .try_into_hset() |
| 430 | + .change_context(errors::StorageError::KVError)?; |
| 431 | + |
| 432 | + let redis_entry = kv::TypedSql { |
| 433 | + op: kv::DBOperation::Update { |
| 434 | + updatable: kv::Updateable::AddressUpdate(Box::new( |
| 435 | + kv::AddressUpdateMems { |
| 436 | + orig: address, |
| 437 | + update_data: address_update.into(), |
| 438 | + }, |
| 439 | + )), |
| 440 | + }, |
| 441 | + }; |
| 442 | + |
| 443 | + self.push_to_drainer_stream::<storage_types::Address>( |
| 444 | + redis_entry, |
| 445 | + PartitionKey::MerchantIdPaymentId { |
| 446 | + merchant_id: &updated_address.merchant_id, |
| 447 | + payment_id: &payment_id, |
| 448 | + }, |
| 449 | + ) |
| 450 | + .await |
| 451 | + .change_context(errors::StorageError::KVError)?; |
| 452 | + updated_address |
| 453 | + .convert(key_store.key.get_inner()) |
| 454 | + .await |
| 455 | + .change_context(errors::StorageError::DecryptionError) |
| 456 | + } |
| 457 | + } |
| 458 | + } |
| 459 | + |
351 | 460 | async fn insert_address_for_payments(
|
352 | 461 | &self,
|
353 | 462 | payment_id: &str,
|
@@ -584,6 +693,37 @@ impl AddressInterface for MockDb {
|
584 | 693 | }
|
585 | 694 | }
|
586 | 695 |
|
| 696 | + async fn update_address_for_payments( |
| 697 | + &self, |
| 698 | + this: domain::Address, |
| 699 | + address_update: domain::AddressUpdate, |
| 700 | + _payment_id: String, |
| 701 | + key_store: &domain::MerchantKeyStore, |
| 702 | + _storage_scheme: MerchantStorageScheme, |
| 703 | + ) -> CustomResult<domain::Address, errors::StorageError> { |
| 704 | + match self |
| 705 | + .addresses |
| 706 | + .lock() |
| 707 | + .await |
| 708 | + .iter_mut() |
| 709 | + .find(|address| address.address_id == this.address_id) |
| 710 | + .map(|a| { |
| 711 | + let address_updated = |
| 712 | + AddressUpdateInternal::from(address_update).create_address(a.clone()); |
| 713 | + *a = address_updated.clone(); |
| 714 | + address_updated |
| 715 | + }) { |
| 716 | + Some(address_updated) => address_updated |
| 717 | + .convert(key_store.key.get_inner()) |
| 718 | + .await |
| 719 | + .change_context(errors::StorageError::DecryptionError), |
| 720 | + None => Err(errors::StorageError::ValueNotFound( |
| 721 | + "cannot find address to update".to_string(), |
| 722 | + ) |
| 723 | + .into()), |
| 724 | + } |
| 725 | + } |
| 726 | + |
587 | 727 | async fn insert_address_for_payments(
|
588 | 728 | &self,
|
589 | 729 | _payment_id: &str,
|
|
0 commit comments