Skip to main content

headless_lms_server/domain/credit_registration_phases/
ledger_snapshot.rs

1//! The `ledger-snapshot` phase: the day's queue-depth snapshot for every ledger state.
2//!
3//! Database-only. Its own phase rather than folded into `config-validation`'s per-module check: a
4//! scoped run must never write a snapshot that claims to cover every course, and `ScopeSupport::NONE`
5//! only enforces that if the write has no other job sharing its dispatch.
6
7use chrono::{Duration, NaiveTime, Utc};
8use headless_lms_models::credit_registration_daily_snapshots::{
9    count_states_for_day, write_snapshot_for_date,
10};
11use headless_lms_models::credit_registration_phase_state::PhaseRunOutcome;
12
13use super::{PhaseContext, PhaseScope};
14
15pub async fn run(ctx: &PhaseContext<'_>, _scope: &PhaseScope) -> anyhow::Result<PhaseRunOutcome> {
16    let mut conn = ctx.pool.acquire().await?;
17    let today = Utc::now().date_naive();
18    let day_start = today.and_time(NaiveTime::MIN).and_utc();
19    let counts = count_states_for_day(&mut conn, day_start, day_start + Duration::days(1)).await?;
20    write_snapshot_for_date(&mut conn, today, &counts).await?;
21    Ok(PhaseRunOutcome::processed(counts.len() as i64))
22}