refactor: move DateRange validation to value object, add delete/cleanup
Some checks failed
CI / Check / Test (push) Failing after 40s
Some checks failed
CI / Check / Test (push) Failing after 40s
This commit is contained in:
@@ -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?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user