mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 00:49:42 +08:00
fix(customers): insert/update address in database when address is provided in customers request (#250)
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
use common_utils::ext_traits::ValueExt;
|
||||
use error_stack::ResultExt;
|
||||
use router_env::{tracing, tracing::instrument};
|
||||
|
||||
@ -7,6 +8,7 @@ use crate::{
|
||||
payment_methods::cards,
|
||||
},
|
||||
db::StorageInterface,
|
||||
pii::PeekInterface,
|
||||
routes::AppState,
|
||||
services,
|
||||
types::{
|
||||
@ -22,13 +24,39 @@ pub async fn create_customer(
|
||||
customer_data: customers::CustomerRequest,
|
||||
) -> RouterResponse<customers::CustomerResponse> {
|
||||
let mut customer_data = customer_data.validate()?;
|
||||
let customer_id = customer_data.customer_id.to_owned();
|
||||
let merchant_id = merchant_account.merchant_id.to_owned();
|
||||
let customer_id = &customer_data.customer_id;
|
||||
let merchant_id = &merchant_account.merchant_id;
|
||||
customer_data.merchant_id = merchant_id.to_owned();
|
||||
|
||||
if let Some(addr) = &customer_data.address {
|
||||
let customer_address: api_models::payments::AddressDetails = addr
|
||||
.peek()
|
||||
.clone()
|
||||
.parse_value("AddressDetails")
|
||||
.change_context(errors::ApiErrorResponse::AddressNotFound)?;
|
||||
db.insert_address(storage::AddressNew {
|
||||
city: customer_address.city,
|
||||
country: customer_address.country,
|
||||
line1: customer_address.line1,
|
||||
line2: customer_address.line2,
|
||||
line3: customer_address.line3,
|
||||
zip: customer_address.zip,
|
||||
state: customer_address.state,
|
||||
first_name: customer_address.first_name,
|
||||
last_name: customer_address.last_name,
|
||||
phone_number: customer_data.phone.clone(),
|
||||
country_code: customer_data.phone_country_code.clone(),
|
||||
customer_id: customer_id.to_string(),
|
||||
merchant_id: merchant_id.to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)?;
|
||||
};
|
||||
|
||||
let new_customer = storage::CustomerNew {
|
||||
customer_id: customer_id.clone(),
|
||||
merchant_id: merchant_id.clone(),
|
||||
customer_id: customer_id.to_string(),
|
||||
merchant_id: merchant_id.to_string(),
|
||||
name: customer_data.name,
|
||||
email: customer_data.email,
|
||||
phone: customer_data.phone,
|
||||
@ -41,7 +69,7 @@ pub async fn create_customer(
|
||||
Ok(customer) => customer,
|
||||
Err(error) => {
|
||||
if error.current_context().is_db_unique_violation() {
|
||||
db.find_customer_by_customer_id_merchant_id(&customer_id, &merchant_id)
|
||||
db.find_customer_by_customer_id_merchant_id(customer_id, merchant_id)
|
||||
.await
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)?
|
||||
} else {
|
||||
@ -49,8 +77,10 @@ pub async fn create_customer(
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut customer_response: customers::CustomerResponse = customer.into();
|
||||
customer_response.address = customer_data.address;
|
||||
|
||||
Ok(services::BachResponse::Json(customer.into()))
|
||||
Ok(services::BachResponse::Json(customer_response))
|
||||
}
|
||||
|
||||
#[instrument(skip(db))]
|
||||
@ -171,6 +201,34 @@ pub async fn update_customer(
|
||||
) -> RouterResponse<customers::CustomerResponse> {
|
||||
let update_customer = update_customer.validate()?;
|
||||
|
||||
if let Some(addr) = &update_customer.address {
|
||||
let customer_address: api_models::payments::AddressDetails = addr
|
||||
.peek()
|
||||
.clone()
|
||||
.parse_value("AddressDetails")
|
||||
.change_context(errors::ApiErrorResponse::AddressNotFound)?;
|
||||
let update_address = storage::AddressUpdate::Update {
|
||||
city: customer_address.city,
|
||||
country: customer_address.country,
|
||||
line1: customer_address.line1,
|
||||
line2: customer_address.line2,
|
||||
line3: customer_address.line3,
|
||||
zip: customer_address.zip,
|
||||
state: customer_address.state,
|
||||
first_name: customer_address.first_name,
|
||||
last_name: customer_address.last_name,
|
||||
phone_number: update_customer.phone.clone(),
|
||||
country_code: update_customer.phone_country_code.clone(),
|
||||
};
|
||||
db.update_address_by_merchant_id_customer_id(
|
||||
&update_customer.customer_id,
|
||||
&merchant_account.merchant_id,
|
||||
update_address,
|
||||
)
|
||||
.await
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)?;
|
||||
};
|
||||
|
||||
let response = db
|
||||
.update_customer_by_customer_id_merchant_id(
|
||||
update_customer.customer_id.to_owned(),
|
||||
@ -187,5 +245,7 @@ pub async fn update_customer(
|
||||
.await
|
||||
.map_err(|error| error.to_not_found_response(errors::ApiErrorResponse::CustomerNotFound))?;
|
||||
|
||||
Ok(services::BachResponse::Json(response.into()))
|
||||
let mut customer_update_response: customers::CustomerResponse = response.into();
|
||||
customer_update_response.address = update_customer.address;
|
||||
Ok(services::BachResponse::Json(customer_update_response))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user