refactor: move DateRange validation to value object, add delete/cleanup
Some checks failed
CI / Check / Test (push) Failing after 40s

This commit is contained in:
2026-06-03 00:58:07 +02:00
parent 3a66f89609
commit fc086de7f7
8 changed files with 46 additions and 33 deletions

View File

@@ -1,24 +1,15 @@
use chrono::Utc;
use domain::errors::DomainError;
use domain::events::DomainEvent;
use domain::models::wrapup::WrapUpStatus;
use domain::models::wrapup::{DateRange, WrapUpStatus};
use domain::value_objects::{UserId, WrapUpId};
use crate::context::AppContext;
use crate::wrapup::commands::RequestWrapUpCommand;
pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<WrapUpId, DomainError> {
if cmd.end_date <= cmd.start_date {
return Err(DomainError::ValidationError(
"end_date must be after start_date".into(),
));
}
let days = (cmd.end_date - cmd.start_date).num_days();
if days > 366 {
return Err(DomainError::ValidationError(
"date range cannot exceed 366 days".into(),
));
}
let date_range = DateRange::new(cmd.start_date, cmd.end_date)?;
if cmd.end_date > Utc::now().date_naive() {
return Err(DomainError::ValidationError(
"end_date cannot be in the future".into(),
@@ -28,7 +19,7 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
let existing = ctx
.repos
.wrapup_repo
.find_existing(cmd.user_id, cmd.start_date, cmd.end_date)
.find_existing(cmd.user_id, date_range.start(), date_range.end())
.await?;
if let Some(ref rec) = existing {
@@ -45,8 +36,8 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
let record = domain::models::wrapup::WrapUpRecord {
id: id.clone(),
user_id: cmd.user_id,
start_date: cmd.start_date,
end_date: cmd.end_date,
start_date: date_range.start(),
end_date: date_range.end(),
status: WrapUpStatus::Pending,
report_json: None,
error_message: None,
@@ -60,8 +51,8 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
.publish(&DomainEvent::WrapUpRequested {
wrapup_id: id.clone(),
user_id: cmd.user_id.map(UserId::from_uuid),
start_date: cmd.start_date,
end_date: cmd.end_date,
start_date: date_range.start(),
end_date: date_range.end(),
})
.await?;

View File

@@ -24,10 +24,7 @@ pub async fn execute(
};
let query = ComputeWrapUpQuery {
scope,
date_range: DateRange {
start: start_date,
end: end_date,
},
date_range: DateRange::new(start_date, end_date)?,
};
match compute::execute(ctx, query).await {

View File

@@ -32,10 +32,11 @@ fn make_row(title: &str, rating: u8, watched_at: &str) -> WrapUpMovieRow {
}
fn year_2024_range() -> DateRange {
DateRange {
start: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
end: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
}
DateRange::new(
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
)
.unwrap()
}
#[tokio::test]