Files
movies-diary/crates/application/src/movies/request_enrichment.rs
Gabriel Kaszewski d867a14b28
Some checks failed
CI / Check / Test (push) Has been cancelled
add 400+ unit tests for domain and application layers
Extract ReviewLogger trait to decouple import/integrations
from diary::log_review (cross-module coupling smell).

Add in-memory fakes for all repository ports, enabling
isolated testing of every use case module without a database.

Coverage: domain+application 22% → 80%, 427 tests.
2026-06-09 02:07:26 +02:00

49 lines
1.3 KiB
Rust

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),
}
}
#[cfg(test)]
#[path = "tests/request_enrichment.rs"]
mod tests;