Skip to content

Commit

Permalink
contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
zupzup committed Feb 12, 2025
1 parent 1ef2eda commit b7b49f0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 51 deletions.
74 changes: 56 additions & 18 deletions src/service/contact_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ pub trait ContactServiceApi: Send + Sync {
name: Option<String>,
email: Option<String>,
postal_address: OptionalPostalAddress,
date_of_birth_or_registration: Option<String>,
country_of_birth_or_registration: Option<String>,
city_of_birth_or_registration: Option<String>,
identification_number: Option<String>,
avatar_file_upload_id: Option<String>,
proof_document_file_upload_id: Option<String>,
) -> Result<()>;

/// Adds a new contact
Expand Down Expand Up @@ -177,7 +182,12 @@ impl ContactServiceApi for ContactService {
name: Option<String>,
email: Option<String>,
postal_address: OptionalPostalAddress,
date_of_birth_or_registration: Option<String>,
country_of_birth_or_registration: Option<String>,
city_of_birth_or_registration: Option<String>,
identification_number: Option<String>,
avatar_file_upload_id: Option<String>,
proof_document_file_upload_id: Option<String>,
) -> Result<()> {
let mut contact = match self.store.get(node_id).await? {
Some(contact) => contact,
Expand Down Expand Up @@ -209,30 +219,42 @@ impl ContactServiceApi for ContactService {
changed = true;
}

match contact.postal_address.zip {
Some(_) => {
if let Some(ref postal_address_zip_to_set) = postal_address.zip {
contact.postal_address.zip = Some(postal_address_zip_to_set.clone());
changed = true;
} else {
contact.postal_address.zip = None;
changed = true;
}
}
None => {
if let Some(ref postal_address_zip_to_set) = postal_address.zip {
contact.postal_address.zip = Some(postal_address_zip_to_set.clone());
changed = true;
}
}
};
util::update_optional_field(
&mut contact.postal_address.zip,
&postal_address.zip,
&mut changed,
);

if let Some(ref postal_address_address_to_set) = postal_address.address {
contact.postal_address.address = postal_address_address_to_set.clone();
changed = true;
}

if !changed && avatar_file_upload_id.is_none() {
util::update_optional_field(
&mut contact.date_of_birth_or_registration,
&date_of_birth_or_registration,
&mut changed,
);

util::update_optional_field(
&mut contact.country_of_birth_or_registration,
&country_of_birth_or_registration,
&mut changed,
);

util::update_optional_field(
&mut contact.city_of_birth_or_registration,
&city_of_birth_or_registration,
&mut changed,
);

util::update_optional_field(
&mut contact.identification_number,
&identification_number,
&mut changed,
);

if !changed && avatar_file_upload_id.is_none() && proof_document_file_upload_id.is_none() {
return Ok(());
}

Expand All @@ -243,6 +265,17 @@ impl ContactServiceApi for ContactService {
if avatar_file.is_some() {
contact.avatar_file = avatar_file;
}
let proof_document_file = self
.process_upload_file(
&proof_document_file_upload_id,
node_id,
&identity_public_key,
)
.await?;
// only override the document, if there is a new one
if proof_document_file.is_some() {
contact.proof_document_file = proof_document_file;
}

self.store.update(node_id, contact).await?;

Expand Down Expand Up @@ -612,6 +645,11 @@ pub mod tests {
None,
OptionalPostalAddress::new_empty(),
None,
None,
None,
None,
None,
None,
)
.await;
assert!(result.is_ok());
Expand Down
41 changes: 8 additions & 33 deletions src/service/identity_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,6 @@ impl IdentityService {
hash: file_hash,
})
}

fn update_optional_field(
&self,
identity_field: &mut Option<String>,
field: &Option<String>,
changed: &mut bool,
) {
match identity_field {
Some(_) => {
if let Some(ref field_to_set) = field {
*identity_field = Some(field_to_set.clone());
*changed = true;
} else {
*identity_field = None;
*changed = true;
}
}
None => {
if let Some(ref field_to_set) = field {
*identity_field = Some(field_to_set.clone());
*changed = true;
}
}
};
}
}

#[async_trait]
Expand Down Expand Up @@ -222,41 +197,41 @@ impl IdentityServiceApi for IdentityService {
}
}

self.update_optional_field(
util::update_optional_field(
&mut identity.postal_address.country,
&postal_address.country,
&mut changed,
);

self.update_optional_field(
util::update_optional_field(
&mut identity.postal_address.city,
&postal_address.city,
&mut changed,
);

self.update_optional_field(
util::update_optional_field(
&mut identity.postal_address.zip,
&postal_address.zip,
&mut changed,
);

self.update_optional_field(
util::update_optional_field(
&mut identity.postal_address.address,
&postal_address.address,
&mut changed,
);

self.update_optional_field(&mut identity.date_of_birth, &date_of_birth, &mut changed);
util::update_optional_field(&mut identity.date_of_birth, &date_of_birth, &mut changed);

self.update_optional_field(
util::update_optional_field(
&mut identity.country_of_birth,
&country_of_birth,
&mut changed,
);

self.update_optional_field(&mut identity.city_of_birth, &city_of_birth, &mut changed);
util::update_optional_field(&mut identity.city_of_birth, &city_of_birth, &mut changed);

self.update_optional_field(
util::update_optional_field(
&mut identity.identification_number,
&identification_number,
&mut changed,
Expand Down
59 changes: 59 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,62 @@ pub fn base58_decode(input: &str) -> std::result::Result<Vec<u8>, Error> {
let result = bs58::decode(input).into_vec()?;
Ok(result)
}

pub fn update_optional_field(
field_to_update: &mut Option<String>,
field: &Option<String>,
changed: &mut bool,
) {
match field_to_update {
Some(_) => {
if let Some(ref field_to_set) = field {
*field_to_update = Some(field_to_set.clone());
*changed = true;
} else {
*field_to_update = None;
*changed = true;
}
}
None => {
if let Some(ref field_to_set) = field {
*field_to_update = Some(field_to_set.clone());
*changed = true;
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn update_optional_field_baseline() {
let mut field_to_update = Some(String::from("hi"));
let mut changed = false;
update_optional_field(
&mut field_to_update,
&Some(String::from("hello")),
&mut changed,
);
assert!(changed);
assert_eq!(Some(String::from("hello")), field_to_update);
}

#[test]
fn update_optional_field_none() {
let mut field_to_update = None;
let mut changed = false;
update_optional_field(&mut field_to_update, &None, &mut changed);
assert!(!changed);
assert_eq!(None, field_to_update);
}

#[test]
fn update_optional_field_some_none() {
let mut field_to_update = Some(String::from("hi"));
let mut changed = false;
update_optional_field(&mut field_to_update, &None, &mut changed);
assert!(changed);
assert_eq!(None, field_to_update);
}
}
5 changes: 5 additions & 0 deletions src/web/handlers/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ pub async fn edit_contact(
payload.name,
payload.email,
payload.postal_address,
payload.date_of_birth_or_registration,
payload.country_of_birth_or_registration,
payload.city_of_birth_or_registration,
payload.identification_number,
payload.avatar_file_upload_id,
payload.proof_document_file_upload_id,
)
.await?;
Ok(Json(SuccessResponse::new()))
Expand Down

0 comments on commit b7b49f0

Please sign in to comment.