Skip to content

Commit

Permalink
fix: current_balance error when executing template (#58)
Browse files Browse the repository at this point in the history
* fix: current_balance error when executing templates

* fix: clippy error
  • Loading branch information
thevaibhav-dixit committed Oct 18, 2023
1 parent c80e477 commit 3269bf6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
8 changes: 5 additions & 3 deletions ledger/src/balance/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sqlx::{PgPool, Postgres, QueryBuilder, Row, Transaction};
use tracing::instrument;
use uuid::Uuid;

use std::collections::HashMap;
use std::{collections::HashMap, str::FromStr};

use super::entity::*;
use crate::{error::*, primitives::*};
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Balances {
journal_id: JournalId,
ids: Vec<(AccountId, &Currency)>,
tx: &mut Transaction<'a, Postgres>,
) -> Result<HashMap<AccountId, BalanceDetails>, SqlxLedgerError> {
) -> Result<HashMap<(AccountId, Currency), BalanceDetails>, SqlxLedgerError> {
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
r#"SELECT
b.journal_id, b.account_id, entry_id, b.currency,
Expand All @@ -165,8 +165,10 @@ impl Balances {
let mut ret = HashMap::new();
for r in records {
let account_id = AccountId::from(r.get::<Uuid, _>("account_id"));
let currency =
Currency::from_str(r.get("currency")).expect("currency code should be valid");
ret.insert(
account_id,
(account_id, currency),
BalanceDetails {
account_id,
journal_id: JournalId::from(r.get::<Uuid, _>("journal_id")),
Expand Down
12 changes: 7 additions & 5 deletions ledger/src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ impl SqlxLedger {
HashMap::new();
let mut new_balances = Vec::new();
for entry in entries.iter() {
let key = (entry.account_id, &entry.currency);
let balance = match (
latest_balances.remove(&key),
balances.remove(&entry.account_id),
latest_balances.remove(&(entry.account_id, &entry.currency)),
balances.remove(&(entry.account_id, entry.currency)),
) {
(Some(latest), _) => {
new_balances.push(latest.clone());
latest
}
(_, Some(balance)) => balance,
_ => {
latest_balances.insert(key, BalanceDetails::init(journal_id, entry));
latest_balances.insert(
(entry.account_id, &entry.currency),
BalanceDetails::init(journal_id, entry),
);
continue;
}
};
latest_balances.insert(key, balance.update(entry));
latest_balances.insert((entry.account_id, &entry.currency), balance.update(entry));
}
new_balances.extend(latest_balances.into_values());

Expand Down
43 changes: 43 additions & 0 deletions ledger/tests/post_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,49 @@ async fn post_transaction() -> anyhow::Result<()> {
get_balance(&ledger, journal_id, sender_account_id, Currency::Iso(usd)).await?;
assert_eq!(usd_credit_balance.settled(), Decimal::from(-100));

let external_id = uuid::Uuid::new_v4().to_string();
params = TxParams::new();
params.insert("journal_id", journal_id);
params.insert("sender", sender_account_id);
params.insert("recipient", recipient_account_id);
params.insert("external_id", external_id.clone());

ledger
.post_transaction(TransactionId::new(), &tx_code, Some(params))
.await
.unwrap();

let usd_credit_balance = get_balance(
&ledger,
journal_id,
recipient_account_id,
Currency::Iso(usd),
)
.await?;
assert_eq!(usd_credit_balance.settled(), Decimal::from(200));

let btc_credit_balance = get_balance(
&ledger,
journal_id,
recipient_account_id,
Currency::Crypto(btc),
)
.await?;
assert_eq!(btc_credit_balance.settled(), Decimal::from(2580));

let btc_debit_balance = get_balance(
&ledger,
journal_id,
sender_account_id,
Currency::Crypto(btc),
)
.await?;
assert_eq!(btc_debit_balance.settled(), Decimal::from(-2580));

let usd_credit_balance =
get_balance(&ledger, journal_id, sender_account_id, Currency::Iso(usd)).await?;
assert_eq!(usd_credit_balance.settled(), Decimal::from(-200));

Ok(())
}

Expand Down

0 comments on commit 3269bf6

Please sign in to comment.