app: person enrichment use case + staleness checks

This commit is contained in:
2026-06-11 13:36:43 +02:00
parent 517a18da8a
commit 371a3cdc46
10 changed files with 70 additions and 8 deletions

View File

@@ -0,0 +1,13 @@
use crate::context::AppContext;
use domain::{
errors::DomainError,
models::{PersonEnrichmentData, PersonId},
};
pub async fn execute(
ctx: &AppContext,
person_id: PersonId,
data: PersonEnrichmentData,
) -> Result<(), DomainError> {
ctx.repos.person_command.update_enrichment(&person_id, &data).await
}

View File

@@ -1,11 +1,33 @@
use crate::context::AppContext;
use chrono::Utc;
use domain::{
errors::DomainError,
events::DomainEvent,
models::{Person, PersonId},
};
const ENRICHMENT_TTL_DAYS: i64 = 90;
pub async fn execute(ctx: &AppContext, id: PersonId) -> Result<Option<Person>, DomainError> {
ctx.repos.person_query.get_by_id(&id).await
let person = ctx.repos.person_query.get_by_id(&id).await?;
if let Some(ref p) = person {
if should_enrich(p) {
let _ = ctx.services.event_publisher.publish(
&DomainEvent::PersonEnrichmentRequested {
person_id: id,
external_person_id: p.external_id().value().to_string(),
},
).await;
}
}
Ok(person)
}
fn should_enrich(p: &Person) -> bool {
match p.enriched_at() {
None => true,
Some(at) => (Utc::now() - at).num_days() >= ENRICHMENT_TTL_DAYS,
}
}
#[cfg(test)]

View File

@@ -1,11 +1,31 @@
use crate::context::AppContext;
use chrono::Utc;
use domain::{
errors::DomainError,
models::{PersonCredits, PersonId},
events::DomainEvent,
models::{Person, PersonCredits, PersonId},
};
const ENRICHMENT_TTL_DAYS: i64 = 90;
pub async fn execute(ctx: &AppContext, id: PersonId) -> Result<PersonCredits, DomainError> {
ctx.repos.person_query.get_credits(&id).await
let credits = ctx.repos.person_query.get_credits(&id).await?;
if should_enrich(&credits.person) {
let _ = ctx.services.event_publisher.publish(
&DomainEvent::PersonEnrichmentRequested {
person_id: id,
external_person_id: credits.person.external_id().value().to_string(),
},
).await;
}
Ok(credits)
}
fn should_enrich(p: &Person) -> bool {
match p.enriched_at() {
None => true,
Some(at) => (Utc::now() - at).num_days() >= ENRICHMENT_TTL_DAYS,
}
}
#[cfg(test)]

View File

@@ -1,2 +1,3 @@
pub mod enrich;
pub mod get;
pub mod get_credits;