feat: wrapup date validation, delete endpoint, failed record cleanup
Some checks failed
CI / Check / Test (push) Failing after 41s
Some checks failed
CI / Check / Test (push) Failing after 41s
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use chrono::Utc;
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::models::wrapup::WrapUpStatus;
|
||||
@@ -7,16 +8,37 @@ 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(),
|
||||
));
|
||||
}
|
||||
if cmd.end_date > Utc::now().date_naive() {
|
||||
return Err(DomainError::ValidationError(
|
||||
"end_date cannot be in the future".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let existing = ctx
|
||||
.repos
|
||||
.wrapup_repo
|
||||
.find_existing(cmd.user_id, cmd.start_date, cmd.end_date)
|
||||
.await?;
|
||||
|
||||
if let Some(ref rec) = existing
|
||||
&& (rec.status == WrapUpStatus::Ready || rec.status == WrapUpStatus::Generating)
|
||||
{
|
||||
return Ok(rec.id.clone());
|
||||
if let Some(ref rec) = existing {
|
||||
match rec.status {
|
||||
WrapUpStatus::Ready | WrapUpStatus::Generating => return Ok(rec.id.clone()),
|
||||
WrapUpStatus::Failed => {
|
||||
ctx.repos.wrapup_repo.delete(&rec.id).await?;
|
||||
}
|
||||
WrapUpStatus::Pending => return Ok(rec.id.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
let id = WrapUpId::generate();
|
||||
@@ -28,7 +50,7 @@ pub async fn execute(ctx: &AppContext, cmd: RequestWrapUpCommand) -> Result<Wrap
|
||||
status: WrapUpStatus::Pending,
|
||||
report_json: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
ctx.repos.wrapup_repo.create(&record).await?;
|
||||
|
||||
Reference in New Issue
Block a user