//! What Home's Today deck reads ahead (`cards.birthdays`, `cards.moneyDue`) //! and the words a face's facts are labelled with (`cards.factLabels`). Each //! read is today and the days after it, in the reader's zone: a birthday //! comes round on its day whatever year it was kept with; money is due on an //! expected transaction's day and a subscription's next charge. use crate::definitions::{Form, TableDefinition}; use crate::records::page::{form_words, years_completed, DateAge, Moment, RelativeDay, Words}; use crate::rules::shell_lines::{self, LineRow}; use crate::transforms::zoned_clock::{civil_from_days, day_key, day_number, next_yearly_occurrence, yearly_month_day}; use serde_json::{json, Map, Value}; /// How far ahead the deck reads when the caller names no reach. pub const DAYS_AHEAD: i64 = 7; /// The people whose birthday comes round from `today` to `days` after it, /// soonest first: each with the day, how far off it is in words, and the /// age they turn where the year is kept. pub fn birthdays(people: &[(String, Map)], today: i64, days: i64, zone: &str, words: &Words) -> Vec { let on = civil_from_days(today); let mut found: Vec<(i64, Value)> = people .iter() .filter_map(|(id, person)| { let kept = person.get("birthday")?.as_str()?.trim(); let (month, day) = yearly_month_day(kept)?; let next = next_yearly_occurrence(month, day, on); let ahead = day_number(next.0, next.1, next.2) - today; if ahead > days { return None; } let born = (!kept.starts_with("--")).then(|| Moment::parse(kept, zone)).flatten().map(|at| civil_from_days(at.day)); let turns = born.and_then(|born| years_completed(born, next)).map(|age| DateAge::Turns(age).words(&words.dates)); let mut said = json!({ "table": "person", "id": id, "name": person.get("full_name").cloned().unwrap_or(Value::Null), "photo": person.get("avatar_key").cloned().unwrap_or(Value::Null), "date": day_key(today + ahead), "in_days": ahead, "when": when(today + ahead, today, days, words), }); if let Some(turns) = turns { said["turns"] = Value::String(turns); } if let Some(wishes) = person.iter().find(|(key, _)| key.as_str() == "wish" || key.ends_with("__wish")).map(|(_, value)| value) { said["wishes"] = Value::Bool(wishes.as_bool().unwrap_or(false)); } Some((ahead, said)) }) .collect(); found.sort_by(|(a, left), (b, right)| a.cmp(b).then_with(|| left["name"].as_str().cmp(&right["name"].as_str()))); found.into_iter().map(|(_, said)| said).collect() } /// One table's rows as the money read takes them. pub struct MoneyRows<'a> { pub definition: &'a TableDefinition, pub rows: &'a [(String, Map)], } /// The money due from `today` to `days` after it, soonest first: expected /// transactions on their day (the due date where one is kept), and active /// subscriptions on their next charge. Each says its amount in its /// currency; a transaction says which way it moves. pub fn money_due( transactions: Option>, subscriptions: Option>, today: i64, days: i64, zone: &str, now: chrono::DateTime, words: &Words, ) -> Vec { let mut found: Vec<(i64, Value)> = Vec::new(); let mut take = |rows: &MoneyRows<'_>, holds: &dyn Fn(&Map) -> bool, day_of: &[&str], title: &str, amount: &str| { for (id, row) in rows.rows.iter().filter(|(_, row)| holds(row)) { let Some(day) = day_of.iter().find_map(|key| Moment::parse(row.get(*key)?.as_str()?, zone)).map(|at| at.day) else { continue; }; let ahead = day - today; if !(0..=days).contains(&ahead) { continue; } let said = rows .definition .column(amount) .and_then(|column| form_words(rows.definition, column, row.get(amount)?, row, Some(zone), Some(now), Form::Money, words)); let mut due = json!({ "table": rows.definition.id, "id": id, "title": row.get(title).cloned().unwrap_or(Value::Null), "amount": said, "amount_minor": row.get(amount).cloned().unwrap_or(Value::Null), "currency": row.get("currency").cloned().unwrap_or(Value::Null), "date": day_key(day), "in_days": ahead, "when": when(day, today, days, words), }); if let Some(direction) = row.get("direction").filter(|direction| !direction.is_null()) { due["direction"] = direction.clone(); } found.push((ahead, due)); } }; if let Some(rows) = &transactions { take(rows, &|row| row.get("status").and_then(Value::as_str) == Some("expected"), &["due_date", "occurred_on"], "title", "amount_minor"); } if let Some(rows) = &subscriptions { take(rows, &|row| row.get("status").and_then(Value::as_str) == Some("active"), &["next_renewal"], "name", "amount"); } found.sort_by_key(|(ahead, _)| *ahead); found.into_iter().map(|(_, due)| due).collect() } /// The labels a face's facts are drawn under, in the database's `cards` /// words: `slot` for every face, and `face` for a face that says one in its /// own words ("Last used" on a password's date). A face's own label stands /// over the slot's. pub fn fact_labels(rows: &[LineRow]) -> Value { shell_lines::resolved("cards", rows).and_then(|cards| cards.get("label").cloned()).unwrap_or_else(|| json!({})) } fn when(day: i64, today: i64, days: i64, words: &Words) -> String { RelativeDay::caption(day, today, days, true, false).words(&words.dates) } #[cfg(test)] #[path = "today_deck_tests.rs"] mod tests;