structural refactor and codebase improvements
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::ports::WrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
pub async fn execute(
|
||||
wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
id: WrapUpId,
|
||||
) -> Result<(), DomainError> {
|
||||
wrapup_repo
|
||||
use crate::wrapup::deps::DeleteWrapUpDeps;
|
||||
|
||||
pub async fn execute(deps: &DeleteWrapUpDeps, id: WrapUpId) -> Result<(), DomainError> {
|
||||
deps.wrapup_repo
|
||||
.get_by_id(&id)
|
||||
.await?
|
||||
.ok_or_else(|| DomainError::NotFound("wrap-up not found".into()))?;
|
||||
|
||||
wrapup_repo.delete(&id).await
|
||||
deps.wrapup_repo.delete(&id).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -7,3 +7,24 @@ pub struct HandleWrapUpRequestedDeps {
|
||||
pub event_publisher: Arc<dyn EventPublisher>,
|
||||
pub wrapup_stats: Arc<dyn WrapUpStatsQuery>,
|
||||
}
|
||||
|
||||
pub struct GetReadyReportDeps {
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
}
|
||||
|
||||
pub struct DeleteWrapUpDeps {
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
}
|
||||
|
||||
pub struct GenerateWrapUpDeps {
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
pub event_publisher: Arc<dyn EventPublisher>,
|
||||
}
|
||||
|
||||
pub struct GetWrapUpDeps {
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
}
|
||||
|
||||
pub struct ListWrapUpsDeps {
|
||||
pub wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::Utc;
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::models::wrapup::{DateRange, WrapUpStatus};
|
||||
use domain::ports::{EventPublisher, WrapUpRepository};
|
||||
use domain::value_objects::{UserId, WrapUpId};
|
||||
|
||||
use crate::wrapup::commands::RequestWrapUpCommand;
|
||||
use crate::wrapup::deps::GenerateWrapUpDeps;
|
||||
|
||||
pub async fn execute(
|
||||
wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
event_publisher: Arc<dyn EventPublisher>,
|
||||
deps: &GenerateWrapUpDeps,
|
||||
cmd: RequestWrapUpCommand,
|
||||
) -> Result<WrapUpId, DomainError> {
|
||||
let date_range = DateRange::new(cmd.start_date, cmd.end_date)?;
|
||||
@@ -22,7 +19,8 @@ pub async fn execute(
|
||||
));
|
||||
}
|
||||
|
||||
let existing = wrapup_repo
|
||||
let existing = deps
|
||||
.wrapup_repo
|
||||
.find_existing(cmd.user_id, date_range.start(), date_range.end())
|
||||
.await?;
|
||||
|
||||
@@ -30,7 +28,7 @@ pub async fn execute(
|
||||
match rec.status {
|
||||
WrapUpStatus::Ready | WrapUpStatus::Generating => return Ok(rec.id.clone()),
|
||||
WrapUpStatus::Failed => {
|
||||
wrapup_repo.delete(&rec.id).await?;
|
||||
deps.wrapup_repo.delete(&rec.id).await?;
|
||||
}
|
||||
WrapUpStatus::Pending => return Ok(rec.id.clone()),
|
||||
}
|
||||
@@ -48,9 +46,9 @@ pub async fn execute(
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
wrapup_repo.create(&record).await?;
|
||||
deps.wrapup_repo.create(&record).await?;
|
||||
|
||||
event_publisher
|
||||
deps.event_publisher
|
||||
.publish(&DomainEvent::WrapUpRequested {
|
||||
wrapup_id: id.clone(),
|
||||
user_id: cmd.user_id.map(UserId::from_uuid),
|
||||
|
||||
53
crates/application/src/wrapup/get_ready_report.rs
Normal file
53
crates/application/src/wrapup/get_ready_report.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use chrono::NaiveDate;
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::wrapup::{WrapUpReport, WrapUpScope, WrapUpStatus},
|
||||
};
|
||||
|
||||
use crate::wrapup::deps::GetReadyReportDeps;
|
||||
|
||||
/// Absorbs the year -> date-range conversion and the `status == Ready` +
|
||||
/// `report.is_some()` gating both HTML wrap-up handlers used to do by hand.
|
||||
///
|
||||
/// Takes `domain::models::wrapup::WrapUpScope` directly (the same type
|
||||
/// `wrapup::handle_requested` already matches on) rather than a second,
|
||||
/// use-case-local `WrapUpScope` — there is exactly one concept here, one type.
|
||||
/// `None` can no longer be silently passed for "global": callers must pick
|
||||
/// `WrapUpScope::User(uid)` or `WrapUpScope::Global` explicitly.
|
||||
///
|
||||
/// Error shape mirrors the deleted handlers exactly:
|
||||
/// - An unrepresentable `year` (can't form a valid `NaiveDate`) is
|
||||
/// `ValidationError` — the handlers used this to return a bare 400.
|
||||
/// - Everything else — no record, a record whose `status` isn't `Ready`, a
|
||||
/// `Ready` record with `report: None`, or even a repository error — collapses
|
||||
/// to `NotFound`, matching the old handlers' `_ => return StatusCode::NOT_FOUND`
|
||||
/// catch-all (which really did swallow repo errors into a 404, not just the
|
||||
/// "not ready yet" cases; preserved here rather than tidied).
|
||||
pub async fn execute(
|
||||
deps: &GetReadyReportDeps,
|
||||
scope: WrapUpScope,
|
||||
year: i32,
|
||||
) -> Result<WrapUpReport, DomainError> {
|
||||
let start = NaiveDate::from_ymd_opt(year, 1, 1)
|
||||
.ok_or_else(|| DomainError::ValidationError("invalid year".into()))?;
|
||||
let end = NaiveDate::from_ymd_opt(year + 1, 1, 1)
|
||||
.ok_or_else(|| DomainError::ValidationError("invalid year".into()))?;
|
||||
|
||||
let user_id = match scope {
|
||||
WrapUpScope::User(uid) => Some(uid),
|
||||
WrapUpScope::Global => None,
|
||||
};
|
||||
|
||||
let record = match deps.wrapup_repo.find_existing(user_id, start, end).await {
|
||||
Ok(Some(r)) if r.status == WrapUpStatus::Ready => r,
|
||||
_ => return Err(DomainError::NotFound("wrap-up report".into())),
|
||||
};
|
||||
|
||||
record
|
||||
.report
|
||||
.ok_or_else(|| DomainError::NotFound("wrap-up report".into()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tests/get_ready_report.rs"]
|
||||
mod tests;
|
||||
@@ -1,15 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::WrapUpRecord;
|
||||
use domain::ports::WrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::wrapup::deps::GetWrapUpDeps;
|
||||
|
||||
pub async fn execute(
|
||||
wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
deps: &GetWrapUpDeps,
|
||||
id: WrapUpId,
|
||||
) -> Result<Option<WrapUpRecord>, DomainError> {
|
||||
wrapup_repo.get_by_id(&id).await
|
||||
deps.wrapup_repo.get_by_id(&id).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::WrapUpRecord;
|
||||
use domain::ports::WrapUpRepository;
|
||||
|
||||
use crate::wrapup::deps::ListWrapUpsDeps;
|
||||
|
||||
pub struct ListWrapUpsQuery {
|
||||
pub user_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
pub async fn execute(
|
||||
wrapup_repo: Arc<dyn WrapUpRepository>,
|
||||
deps: &ListWrapUpsDeps,
|
||||
query: ListWrapUpsQuery,
|
||||
) -> Result<Vec<WrapUpRecord>, DomainError> {
|
||||
match query.user_id {
|
||||
Some(uid) => wrapup_repo.list_for_user(uid).await,
|
||||
None => wrapup_repo.list_global().await,
|
||||
Some(uid) => deps.wrapup_repo.list_for_user(uid).await,
|
||||
None => deps.wrapup_repo.list_global().await,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ pub mod delete;
|
||||
pub mod deps;
|
||||
pub mod event_handler;
|
||||
pub mod generate;
|
||||
pub mod get_ready_report;
|
||||
pub mod get_wrapup;
|
||||
pub mod handle_requested;
|
||||
pub mod list_wrapups;
|
||||
|
||||
@@ -4,6 +4,7 @@ use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::wrapup::delete;
|
||||
use crate::wrapup::deps::DeleteWrapUpDeps;
|
||||
|
||||
#[tokio::test]
|
||||
async fn deletes_existing_wrapup() {
|
||||
@@ -21,13 +22,19 @@ async fn deletes_existing_wrapup() {
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
delete::execute(repo.clone(), id).await.unwrap();
|
||||
let deps = DeleteWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
delete::execute(&deps, id).await.unwrap();
|
||||
assert_eq!(repo.store.lock().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_not_found() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let result = delete::execute(repo.clone(), WrapUpId::generate()).await;
|
||||
let deps = DeleteWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = delete::execute(&deps, WrapUpId::generate()).await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
@@ -5,8 +5,21 @@ use domain::testing::{InMemoryWrapUpRepository, NoopEventPublisher};
|
||||
use domain::value_objects::WrapUpId;
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::wrapup::deps::GenerateWrapUpDeps;
|
||||
use crate::wrapup::{commands::RequestWrapUpCommand, generate};
|
||||
|
||||
fn deps(
|
||||
repo: &Arc<InMemoryWrapUpRepository>,
|
||||
events: &Arc<NoopEventPublisher>,
|
||||
) -> GenerateWrapUpDeps {
|
||||
GenerateWrapUpDeps {
|
||||
wrapup_repo: Arc::clone(repo) as _,
|
||||
event_publisher: Arc::clone(events) as _,
|
||||
}
|
||||
}
|
||||
|
||||
fn past_cmd() -> RequestWrapUpCommand {
|
||||
RequestWrapUpCommand {
|
||||
user_id: Some(Uuid::nil()),
|
||||
@@ -20,7 +33,7 @@ async fn creates_pending_record_and_emits_event() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
|
||||
let id = generate::execute(repo.clone(), events.clone(), past_cmd())
|
||||
let id = generate::execute(&deps(&repo, &events), past_cmd())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -54,7 +67,7 @@ async fn reuses_existing_ready_wrapup() {
|
||||
});
|
||||
let events = NoopEventPublisher::new();
|
||||
|
||||
let id = generate::execute(repo.clone(), events.clone(), past_cmd())
|
||||
let id = generate::execute(&deps(&repo, &events), past_cmd())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(id, existing_id);
|
||||
@@ -78,7 +91,7 @@ async fn replaces_failed_wrapup() {
|
||||
|
||||
let events = NoopEventPublisher::new();
|
||||
|
||||
let id = generate::execute(repo.clone(), events.clone(), past_cmd())
|
||||
let id = generate::execute(&deps(&repo, &events), past_cmd())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -93,8 +106,7 @@ async fn rejects_future_end_date() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let err = generate::execute(
|
||||
repo.clone(),
|
||||
events.clone(),
|
||||
&deps(&repo, &events),
|
||||
RequestWrapUpCommand {
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2030, 1, 1).unwrap(),
|
||||
|
||||
152
crates/application/src/wrapup/tests/get_ready_report.rs
Normal file
152
crates/application/src/wrapup/tests/get_ready_report.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
use chrono::NaiveDate;
|
||||
use uuid::Uuid;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::{DateRange, WrapUpRecord, WrapUpScope, WrapUpStatus};
|
||||
use domain::ports::WrapUpRepository;
|
||||
use domain::services::wrapup_analyzer;
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::wrapup::deps::GetReadyReportDeps;
|
||||
use crate::wrapup::get_ready_report;
|
||||
|
||||
fn year_range(year: i32) -> DateRange {
|
||||
DateRange::new(
|
||||
NaiveDate::from_ymd_opt(year, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn record(
|
||||
user_id: Option<Uuid>,
|
||||
year: i32,
|
||||
status: WrapUpStatus,
|
||||
with_report: bool,
|
||||
) -> WrapUpRecord {
|
||||
let range = year_range(year);
|
||||
let domain_scope = match user_id {
|
||||
Some(u) => WrapUpScope::User(u),
|
||||
None => WrapUpScope::Global,
|
||||
};
|
||||
let report = if with_report {
|
||||
Some(wrapup_analyzer::build_report(
|
||||
domain_scope,
|
||||
range.clone(),
|
||||
&[],
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
WrapUpRecord {
|
||||
id: WrapUpId::generate(),
|
||||
user_id,
|
||||
start_date: range.start(),
|
||||
end_date: range.end(),
|
||||
status,
|
||||
report,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_returns_report_when_ready() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
repo.create(&record(Some(user_id), 2024, WrapUpStatus::Ready, true))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let report = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.total_movies, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_status_not_ready() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
repo.create(&record(
|
||||
Some(user_id),
|
||||
2024,
|
||||
WrapUpStatus::Generating,
|
||||
false,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_report_absent() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
// Status is Ready but `report` is None — an inconsistent-but-representable
|
||||
// state the old handler guarded against explicitly (`match record.report {
|
||||
// Some(r) => ..., None => 404 }`), separate from the status gate above.
|
||||
repo.create(&record(Some(user_id), 2024, WrapUpStatus::Ready, false))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_no_record_exists() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(Uuid::new_v4()), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_global_scope_ignores_user_id() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
repo.create(&record(None, 2024, WrapUpStatus::Ready, true))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
get_ready_report::execute(&deps, WrapUpScope::Global, 2024)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// The old handlers guarded `NaiveDate::from_ymd_opt` up front and returned a bare
|
||||
/// 400 for a year that can't form a valid date — that guard moved into the use
|
||||
/// case along with the rest of the year->date-range conversion. A distinguishable
|
||||
/// error (ValidationError, not NotFound) lets the handler still map it to 400.
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_validation_error_for_unrepresentable_year() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::Global, 999_999)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(err, DomainError::ValidationError(_)),
|
||||
"got {err:?}"
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use crate::wrapup::deps::GetWrapUpDeps;
|
||||
use crate::wrapup::get_wrapup;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -21,7 +22,10 @@ async fn returns_record_when_exists() {
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
let result = get_wrapup::execute(repo.clone(), id).await.unwrap();
|
||||
let deps = GetWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = get_wrapup::execute(&deps, id).await.unwrap();
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap().status, WrapUpStatus::Pending);
|
||||
}
|
||||
@@ -29,7 +33,10 @@ async fn returns_record_when_exists() {
|
||||
#[tokio::test]
|
||||
async fn returns_none_when_missing() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let result = get_wrapup::execute(repo.clone(), WrapUpId::generate())
|
||||
let deps = GetWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = get_wrapup::execute(&deps, WrapUpId::generate())
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(result.is_none());
|
||||
|
||||
@@ -4,6 +4,7 @@ use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::wrapup::deps::ListWrapUpsDeps;
|
||||
use crate::wrapup::list_wrapups::{self, ListWrapUpsQuery};
|
||||
|
||||
fn make_record(user_id: Option<Uuid>) -> WrapUpRecord {
|
||||
@@ -31,7 +32,10 @@ async fn filters_by_user() {
|
||||
store.push(make_record(None));
|
||||
}
|
||||
|
||||
let result = list_wrapups::execute(repo.clone(), ListWrapUpsQuery { user_id: Some(uid) })
|
||||
let deps = ListWrapUpsDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = list_wrapups::execute(&deps, ListWrapUpsQuery { user_id: Some(uid) })
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
@@ -48,7 +52,10 @@ async fn returns_global_when_no_user() {
|
||||
store.push(make_record(Some(Uuid::new_v4())));
|
||||
}
|
||||
|
||||
let result = list_wrapups::execute(repo.clone(), ListWrapUpsQuery { user_id: None })
|
||||
let deps = ListWrapUpsDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = list_wrapups::execute(&deps, ListWrapUpsQuery { user_id: None })
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result.len(), 2);
|
||||
|
||||
Reference in New Issue
Block a user