Skip to content

Commit

Permalink
patch replay
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Dec 6, 2024
1 parent ef7ef82 commit 79f719c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/analytics/enrich_account_funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl BalanceTracker {
) {
let ul = self.0.entry(user_id).or_default();

let has_history = !ul.0.is_empty();

let most_recent_date = *ul.0.keys().max_by(|x, y| x.cmp(y)).unwrap_or(&date);

// NOTE the previous record may be today's record from a previous transaction. Need to take care in the aggregation below
Expand All @@ -99,47 +101,51 @@ impl BalanceTracker {

let today = ul.0.entry(date).or_default();

// roll over from previous
if has_history {
today.current_balance = previous.current_balance;
today.total_funded = previous.total_funded;
today.total_inflows = previous.total_inflows;
today.total_outflows = previous.total_outflows;
}

if credit {
today.current_balance = previous.current_balance + amount;
today.total_inflows = previous.total_inflows + amount;
today.current_balance += amount;
today.total_inflows += amount;
// there are records from today
if most_recent_date == date {
today.daily_inflows = previous.daily_inflows + amount;
} else {
// today's first record
today.daily_inflows = amount;
}
// no change from on totals
today.total_outflows = previous.total_outflows;
} else {
// debit
today.current_balance = previous.current_balance - amount;
today.total_outflows = previous.total_outflows + amount;
today.current_balance += -amount;
today.total_outflows += amount;

if most_recent_date == date {
today.daily_outflows = previous.daily_outflows + amount;
} else {
today.daily_outflows = amount;
}

// no change from on totals
today.total_inflows = previous.total_inflows;
}

// find out if the outflows created a funding requirement on the account
if today.current_balance < 0.0 {
let negative_balance = today.current_balance.abs();
// funding was needed
today.total_funded = previous.total_funded + negative_balance;
today.total_funded += negative_balance;

// if the previous record is from today
if most_recent_date == date {
today.daily_funding += negative_balance;
today.daily_funding = previous.daily_funding + negative_balance;
} else {
today.daily_funding = negative_balance;
}
// reset to zero
today.current_balance = 0.0;
}
// no changes to funding
today.total_funded = previous.total_funded;

}

/// Save the balance tracker to a JSON file
Expand Down

0 comments on commit 79f719c

Please sign in to comment.