Allow duplicate labels in extra profile fields

This commit is contained in:
silverpill 2022-04-12 15:09:58 +00:00
parent 4a1b75969f
commit f914702de9
2 changed files with 43 additions and 7 deletions

View file

@ -392,6 +392,7 @@ pub async fn search_profile_by_wallet_address(
Ok(profiles) Ok(profiles)
} }
/// Get wallet address corresponding to local profile
pub async fn get_wallet_address( pub async fn get_wallet_address(
db_client: &impl GenericClient, db_client: &impl GenericClient,
profile_id: &Uuid, profile_id: &Uuid,
@ -472,6 +473,10 @@ pub async fn update_post_count(
mod tests { mod tests {
use serial_test::serial; use serial_test::serial;
use crate::database::test_utils::create_test_database; use crate::database::test_utils::create_test_database;
use crate::models::profiles::queries::create_profile;
use crate::models::profiles::types::{ExtraField, ProfileCreateData};
use crate::models::users::queries::create_user;
use crate::models::users::types::UserCreateData;
use super::*; use super::*;
#[tokio::test] #[tokio::test]
@ -496,4 +501,42 @@ mod tests {
assert_eq!(deletion_queue.files.len(), 0); assert_eq!(deletion_queue.files.len(), 0);
assert_eq!(deletion_queue.ipfs_objects.len(), 0); assert_eq!(deletion_queue.ipfs_objects.len(), 0);
} }
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_local() {
let db_client = &mut create_test_database().await;
let wallet_address = "0x1234abcd";
let user_data = UserCreateData {
wallet_address: Some(wallet_address.to_string()),
..Default::default()
};
let user = create_user(db_client, user_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
db_client, "ETH", wallet_address).await.unwrap();
assert_eq!(profiles.len(), 1);
assert_eq!(profiles[0].id, user.profile.id);
}
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_remote() {
let db_client = &mut create_test_database().await;
let extra_field = ExtraField {
name: "$eth".to_string(),
value: "0x1234aBcD".to_string(),
value_source: None,
};
let profile_data = ProfileCreateData {
extra_fields: vec![extra_field],
..Default::default()
};
let profile = create_profile(db_client, profile_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
db_client, "ETH", "0x1234abcd").await.unwrap();
assert_eq!(profiles.len(), 1);
assert_eq!(profiles[0].id, profile.id);
}
} }

View file

@ -59,13 +59,6 @@ pub fn clean_extra_fields(extra_fields: &[ExtraField])
if cleaned_extra_fields.len() >= 10 { if cleaned_extra_fields.len() >= 10 {
return Err(ValidationError("at most 10 fields are allowed")); return Err(ValidationError("at most 10 fields are allowed"));
}; };
let mut unique_labels: Vec<String> = cleaned_extra_fields.iter()
.map(|field| field.name.clone()).collect();
unique_labels.sort();
unique_labels.dedup();
if unique_labels.len() < cleaned_extra_fields.len() {
return Err(ValidationError("duplicate labels"));
};
Ok(cleaned_extra_fields) Ok(cleaned_extra_fields)
} }