From 701f15840a9656fa9f9fdd32caa677a51356ddea Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 3 Nov 2026 23:27:26 +0100 Subject: [PATCH] [insight] Household duplicates from shared subscription names --- crates/fiscus-insight/src/household.rs | 36 ++++++++++++++++++++++++++ crates/fiscus-insight/src/lib.rs | 1 + 2 files changed, 37 insertions(+) create mode 100644 crates/fiscus-insight/src/household.rs diff --git a/crates/fiscus-insight/src/household.rs b/crates/fiscus-insight/src/household.rs new file mode 100644 index 0000000..5ec3aae --- /dev/null +++ b/crates/fiscus-insight/src/household.rs @@ -0,0 +1,36 @@ +//! Household duplicates: two or more members paying for the same service ("You both pay Spotify"). Runs only +//! on the subscription names members chose to share into the household budget. + +use fiscus_ledger::merchant::merchant_key; +use serde::Serialize; +use std::collections::{BTreeMap, BTreeSet}; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +pub struct Duplicate { + /// The service: the first word of the merchant key (`PAYPAL *SPOTIFY`, `Spotify AB` → `spotify`). + pub key: String, + /// Who pays it, sorted. + pub members: Vec, +} + +/// The first word of the merchant key, if it is at least three letters long. +pub fn service_key(name: &str) -> Option { + let key = merchant_key(name); + let first = key.split(' ').next()?; + (first.chars().count() >= 3).then(|| first.to_string()) +} + +/// `(member, the subscription names they shared)` → every service two or more members pay, by key. +pub fn household_duplicates(shared: &[(String, Vec)]) -> Vec { + let mut by_key: BTreeMap> = BTreeMap::new(); + for (member, names) in shared { + for key in names.iter().filter_map(|n| service_key(n)) { + by_key.entry(key).or_default().insert(member.clone()); + } + } + by_key + .into_iter() + .filter(|(_, m)| m.len() >= 2) + .map(|(key, m)| Duplicate { key, members: m.into_iter().collect() }) + .collect() +} diff --git a/crates/fiscus-insight/src/lib.rs b/crates/fiscus-insight/src/lib.rs index df8622e..1ba1533 100644 --- a/crates/fiscus-insight/src/lib.rs +++ b/crates/fiscus-insight/src/lib.rs @@ -3,4 +3,5 @@ pub mod cancel; pub mod forecast; +pub mod household; pub mod subscriptions;