test_support/test_helpers -> tests/support; watched_at unified w/ domain test_watched_at (2024-03-15). Kept inline: users::diary_filter tests (feed_sort_to_direction_all_variants, paged_user_filter_builds_correctly) - pub(super) helpers in private module. Dropped stale never-compiled users/tests/get_users.rs (old execute signature).
45 lines
1.3 KiB
Rust
45 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),
|
|
}
|
|
}
|