feat: add macro to generate ToEncryptable trait (#6313)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com>
This commit is contained in:
Kartikeya Hegde
2024-11-04 11:24:13 +05:30
committed by GitHub
parent adc5262f13
commit 19cf0f7437
19 changed files with 949 additions and 738 deletions

View File

@ -1,12 +1,5 @@
use common_utils::{
crypto, custom_serde,
encryption::Encryption,
id_type,
pii::{self, EmailStrategy},
types::{keymanager::ToEncryptable, Description},
};
use masking::{ExposeInterface, Secret, SwitchStrategy};
use rustc_hash::FxHashMap;
use common_utils::{crypto, custom_serde, id_type, pii, types::Description};
use masking::Secret;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
@ -129,85 +122,6 @@ impl CustomerRequest {
}
}
pub struct CustomerRequestWithEmail {
pub name: Option<Secret<String>>,
pub email: Option<pii::Email>,
pub phone: Option<Secret<String>>,
}
pub struct CustomerRequestWithEncryption {
pub name: Option<Encryption>,
pub phone: Option<Encryption>,
pub email: Option<Encryption>,
}
pub struct EncryptableCustomer {
pub name: crypto::OptionalEncryptableName,
pub phone: crypto::OptionalEncryptablePhone,
pub email: crypto::OptionalEncryptableEmail,
}
impl ToEncryptable<EncryptableCustomer, Secret<String>, Encryption>
for CustomerRequestWithEncryption
{
fn to_encryptable(self) -> FxHashMap<String, Encryption> {
let mut map = FxHashMap::with_capacity_and_hasher(3, Default::default());
self.name.map(|x| map.insert("name".to_string(), x));
self.phone.map(|x| map.insert("phone".to_string(), x));
self.email.map(|x| map.insert("email".to_string(), x));
map
}
fn from_encryptable(
mut hashmap: FxHashMap<String, crypto::Encryptable<Secret<String>>>,
) -> common_utils::errors::CustomResult<EncryptableCustomer, common_utils::errors::ParsingError>
{
Ok(EncryptableCustomer {
name: hashmap.remove("name"),
phone: hashmap.remove("phone"),
email: hashmap.remove("email").map(|email| {
let encryptable: crypto::Encryptable<Secret<String, EmailStrategy>> =
crypto::Encryptable::new(
email.clone().into_inner().switch_strategy(),
email.into_encrypted(),
);
encryptable
}),
})
}
}
impl ToEncryptable<EncryptableCustomer, Secret<String>, Secret<String>>
for CustomerRequestWithEmail
{
fn to_encryptable(self) -> FxHashMap<String, Secret<String>> {
let mut map = FxHashMap::with_capacity_and_hasher(3, Default::default());
self.name.map(|x| map.insert("name".to_string(), x));
self.phone.map(|x| map.insert("phone".to_string(), x));
self.email
.map(|x| map.insert("email".to_string(), x.expose().switch_strategy()));
map
}
fn from_encryptable(
mut hashmap: FxHashMap<String, crypto::Encryptable<Secret<String>>>,
) -> common_utils::errors::CustomResult<EncryptableCustomer, common_utils::errors::ParsingError>
{
Ok(EncryptableCustomer {
name: hashmap.remove("name"),
email: hashmap.remove("email").map(|email| {
let encryptable: crypto::Encryptable<Secret<String, EmailStrategy>> =
crypto::Encryptable::new(
email.clone().into_inner().switch_strategy(),
email.into_encrypted(),
);
encryptable
}),
phone: hashmap.remove("phone"),
})
}
}
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "customer_v2")))]
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct CustomerResponse {

View File

@ -12,13 +12,12 @@ use common_utils::{
ext_traits::{ConfigExt, Encode, ValueExt},
hashing::HashedString,
id_type,
pii::{self, Email, EmailStrategy},
types::{keymanager::ToEncryptable, MinorUnit, StringMajorUnit},
pii::{self, Email},
types::{MinorUnit, StringMajorUnit},
};
use error_stack::ResultExt;
use masking::{ExposeInterface, PeekInterface, Secret, SwitchStrategy, WithType};
use masking::{PeekInterface, Secret, WithType};
use router_derive::Setter;
use rustc_hash::FxHashMap;
use serde::{de, ser::Serializer, Deserialize, Deserializer, Serialize};
use strum::Display;
use time::{Date, PrimitiveDateTime};
@ -3818,54 +3817,6 @@ pub struct EncryptableAddressDetails {
pub email: crypto::OptionalEncryptableEmail,
}
impl ToEncryptable<EncryptableAddressDetails, Secret<String>, Secret<String>>
for AddressDetailsWithPhone
{
fn from_encryptable(
mut hashmap: FxHashMap<String, crypto::Encryptable<Secret<String>>>,
) -> common_utils::errors::CustomResult<
EncryptableAddressDetails,
common_utils::errors::ParsingError,
> {
Ok(EncryptableAddressDetails {
line1: hashmap.remove("line1"),
line2: hashmap.remove("line2"),
line3: hashmap.remove("line3"),
state: hashmap.remove("state"),
zip: hashmap.remove("zip"),
first_name: hashmap.remove("first_name"),
last_name: hashmap.remove("last_name"),
phone_number: hashmap.remove("phone_number"),
email: hashmap.remove("email").map(|x| {
let inner: Secret<String, EmailStrategy> = x.clone().into_inner().switch_strategy();
crypto::Encryptable::new(inner, x.into_encrypted())
}),
})
}
fn to_encryptable(self) -> FxHashMap<String, Secret<String>> {
let mut map = FxHashMap::with_capacity_and_hasher(9, Default::default());
self.address.map(|address| {
address.line1.map(|x| map.insert("line1".to_string(), x));
address.line2.map(|x| map.insert("line2".to_string(), x));
address.line3.map(|x| map.insert("line3".to_string(), x));
address.state.map(|x| map.insert("state".to_string(), x));
address.zip.map(|x| map.insert("zip".to_string(), x));
address
.first_name
.map(|x| map.insert("first_name".to_string(), x));
address
.last_name
.map(|x| map.insert("last_name".to_string(), x));
});
self.email
.map(|x| map.insert("email".to_string(), x.expose().switch_strategy()));
self.phone_number
.map(|x| map.insert("phone_number".to_string(), x));
map
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq, ToSchema, serde::Deserialize, serde::Serialize)]
pub struct PhoneDetails {
/// The contact number