refactor: move profile-field count validation into domain UserProfile
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
use domain::{errors::DomainError, events::DomainEvent, value_objects::UserId};
|
||||
use domain::{errors::DomainError, events::DomainEvent, models::UserProfile, value_objects::UserId};
|
||||
|
||||
use crate::{context::AppContext, users::commands::UpdateProfileFieldsCommand};
|
||||
|
||||
pub async fn execute(ctx: &AppContext, cmd: UpdateProfileFieldsCommand) -> Result<(), DomainError> {
|
||||
if cmd.fields.len() > 4 {
|
||||
return Err(DomainError::ValidationError(
|
||||
"Maximum 4 profile fields allowed".into(),
|
||||
));
|
||||
}
|
||||
UserProfile::validate_custom_fields(&cmd.fields)?;
|
||||
let user_id = UserId::from_uuid(cmd.user_id);
|
||||
ctx.repos
|
||||
.profile_fields
|
||||
|
||||
@@ -230,3 +230,26 @@ fn movie_is_manual_match_different_director_fails() {
|
||||
let year = crate::value_objects::ReleaseYear::new(1982).unwrap();
|
||||
assert!(!m.is_manual_match(&title, &year, Some("Denis Villeneuve")));
|
||||
}
|
||||
|
||||
// ── UserProfile field validation ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn profile_fields_validates_max_count() {
|
||||
let fields: Vec<ProfileField> = (0..5)
|
||||
.map(|i| ProfileField { name: format!("f{i}"), value: format!("v{i}") })
|
||||
.collect();
|
||||
assert!(UserProfile::validate_custom_fields(&fields).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_fields_allows_four() {
|
||||
let fields: Vec<ProfileField> = (0..4)
|
||||
.map(|i| ProfileField { name: format!("f{i}"), value: format!("v{i}") })
|
||||
.collect();
|
||||
assert!(UserProfile::validate_custom_fields(&fields).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_fields_allows_zero() {
|
||||
assert!(UserProfile::validate_custom_fields(&[]).is_ok());
|
||||
}
|
||||
|
||||
@@ -32,6 +32,20 @@ pub struct UserProfile {
|
||||
pub profile_fields: Vec<ProfileField>,
|
||||
}
|
||||
|
||||
impl UserProfile {
|
||||
pub const MAX_CUSTOM_FIELDS: usize = 4;
|
||||
|
||||
pub fn validate_custom_fields(fields: &[ProfileField]) -> Result<(), crate::errors::DomainError> {
|
||||
if fields.len() > Self::MAX_CUSTOM_FIELDS {
|
||||
Err(crate::errors::DomainError::ValidationError(
|
||||
"Maximum 4 profile fields allowed".into(),
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct User {
|
||||
id: UserId,
|
||||
|
||||
Reference in New Issue
Block a user