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:
19
crates/application/src/wrapup/delete.rs
Normal file
19
crates/application/src/wrapup/delete.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use domain::errors::DomainError;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::context::AppContext;
|
||||
|
||||
pub async fn execute(ctx: &AppContext, id: WrapUpId) -> Result<(), DomainError> {
|
||||
let record = ctx
|
||||
.repos
|
||||
.wrapup_repo
|
||||
.get_by_id(&id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("wrap-up not found".into()))?;
|
||||
|
||||
let wrapup_key = format!("wrapups/{}", id.value());
|
||||
let video_key = format!("{wrapup_key}/video.mp4");
|
||||
let _ = ctx.services.image_storage.delete(&video_key).await;
|
||||
|
||||
ctx.repos.wrapup_repo.delete(&record.id).await
|
||||
}
|
||||
@@ -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?;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod commands;
|
||||
pub mod compute;
|
||||
pub mod delete;
|
||||
pub mod event_handler;
|
||||
pub mod generate;
|
||||
pub mod get_wrapup;
|
||||
|
||||
Reference in New Issue
Block a user