refactor: extract reindex + enrichment logic from handlers into use cases
Some checks failed
CI / Check / Test (push) Failing after 6m45s

This commit is contained in:
2026-06-04 16:09:35 +02:00
parent 81a377cbb2
commit 5334312d64
5 changed files with 188 additions and 158 deletions

View File

@@ -0,0 +1,44 @@
use std::sync::Arc;
use chrono::Utc;
use domain::{
errors::DomainError,
models::MovieProfile,
ports::{MovieEnrichmentClient, MovieProfileRepository},
value_objects::MovieId,
};
const STALENESS_DAYS: i64 = 30;
pub async fn fetch_if_stale(
enrichment_client: &dyn MovieEnrichmentClient,
profile_repo: &Arc<dyn MovieProfileRepository>,
movie_id: MovieId,
external_metadata_id: &str,
) -> Result<Option<MovieProfile>, DomainError> {
if let Ok(Some(existing)) = profile_repo.get_by_movie_id(&movie_id).await {
let age = Utc::now() - existing.enriched_at;
if age.num_days() < STALENESS_DAYS {
tracing::debug!(
movie_id = %movie_id.value(),
"skipping enrichment — profile is {} days old",
age.num_days()
);
return Ok(None);
}
}
tracing::info!(movie_id = %movie_id.value(), external_id = %external_metadata_id, "enriching movie");
match enrichment_client
.fetch_profile(movie_id, external_metadata_id)
.await
{
Ok(profile) => Ok(Some(profile)),
Err(DomainError::NotFound(msg)) => {
tracing::warn!("TMDb lookup found nothing: {msg}");
Ok(None)
}
Err(e) => Err(e),
}
}