refactor(wrapup): scoped deps — HandleWrapUpRequestedDeps, flat-Arc jobs

This commit is contained in:
2026-06-11 22:29:30 +02:00
parent cdff0de53d
commit ddf100cfc2
18 changed files with 185 additions and 205 deletions

View File

@@ -1,18 +1,30 @@
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use chrono::Datelike;
use domain::{errors::DomainError, ports::PeriodicJob};
use crate::context::AppContext;
use domain::{
errors::DomainError,
ports::{EventPublisher, PeriodicJob, UserRepository, WrapUpRepository},
};
pub struct WrapUpAutoGenerateJob {
ctx: AppContext,
user: Arc<dyn UserRepository>,
wrapup_repo: Arc<dyn WrapUpRepository>,
event_publisher: Arc<dyn EventPublisher>,
}
impl WrapUpAutoGenerateJob {
pub fn new(ctx: AppContext) -> Self {
Self { ctx }
pub fn new(
user: Arc<dyn UserRepository>,
wrapup_repo: Arc<dyn WrapUpRepository>,
event_publisher: Arc<dyn EventPublisher>,
) -> Self {
Self {
user,
wrapup_repo,
event_publisher,
}
}
}
@@ -33,12 +45,10 @@ impl PeriodicJob for WrapUpAutoGenerateJob {
let end = chrono::NaiveDate::from_ymd_opt(year + 1, 1, 1)
.ok_or_else(|| DomainError::ValidationError("invalid date".into()))?;
let users = self.ctx.repos.user.list_with_stats().await?;
let users = self.user.list_with_stats().await?;
for user in &users {
if user.total_movies > 0 {
let existing = self
.ctx
.repos
.wrapup_repo
.find_existing(Some(user.user_id.value()), start, end)
.await?;
@@ -48,7 +58,13 @@ impl PeriodicJob for WrapUpAutoGenerateJob {
start_date: start,
end_date: end,
};
if let Err(e) = crate::wrapup::generate::execute(&self.ctx, cmd).await {
if let Err(e) = crate::wrapup::generate::execute(
self.wrapup_repo.clone(),
self.event_publisher.clone(),
cmd,
)
.await
{
tracing::warn!(
"auto-generate wrapup for user {} failed: {e}",
user.user_id.value()
@@ -58,19 +74,20 @@ impl PeriodicJob for WrapUpAutoGenerateJob {
}
}
let existing = self
.ctx
.repos
.wrapup_repo
.find_existing(None, start, end)
.await?;
let existing = self.wrapup_repo.find_existing(None, start, end).await?;
if existing.is_none() {
let cmd = crate::wrapup::commands::RequestWrapUpCommand {
user_id: None,
start_date: start,
end_date: end,
};
if let Err(e) = crate::wrapup::generate::execute(&self.ctx, cmd).await {
if let Err(e) = crate::wrapup::generate::execute(
self.wrapup_repo.clone(),
self.event_publisher.clone(),
cmd,
)
.await
{
tracing::warn!("auto-generate global wrapup failed: {e}");
}
}
@@ -80,12 +97,12 @@ impl PeriodicJob for WrapUpAutoGenerateJob {
}
pub struct WrapUpCleanupJob {
ctx: AppContext,
wrapup_repo: Arc<dyn WrapUpRepository>,
}
impl WrapUpCleanupJob {
pub fn new(ctx: AppContext) -> Self {
Self { ctx }
pub fn new(wrapup_repo: Arc<dyn WrapUpRepository>) -> Self {
Self { wrapup_repo }
}
}
@@ -98,8 +115,6 @@ impl PeriodicJob for WrapUpCleanupJob {
async fn run(&self) -> Result<(), DomainError> {
let cutoff = chrono::Utc::now().naive_utc() - chrono::Duration::days(7);
let n = self
.ctx
.repos
.wrapup_repo
.delete_failed_older_than(cutoff)
.await?;