Skip to content

Commit

Permalink
fix: handle no balance found error
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 24, 2025
1 parent 94248c6 commit ba78b62
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lana/app/src/trial_balance/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod statement;

use cala_ledger::{
account_set::{AccountSet, AccountSetMemberId, AccountSetsByCreatedAtCursor, NewAccountSet},
balance::error::BalanceError,
AccountSetId, CalaLedger, Currency, DebitOrCredit, JournalId, LedgerOperation,
};

Expand Down Expand Up @@ -88,26 +89,36 @@ impl TrialBalanceLedger {

let btc_currency =
Currency::try_from("BTC".to_string()).expect("Cannot deserialize 'BTC' as Currency");
let btc_balance = self
let btc_balance = match self
.cala
.balances()
.find_in_op(op, self.journal_id, id, btc_currency)
.await?;
.await
{
Ok(balance) => balance.try_into()?,
Err(BalanceError::NotFound(_, _, _)) => BtcStatementAccountSetBalance::ZERO,
Err(e) => return Err(e.into()),
};

let usd_currency =
Currency::try_from("BTC".to_string()).expect("Cannot deserialize 'BTC' as Currency");
let usd_balance = self
Currency::try_from("USD".to_string()).expect("Cannot deserialize 'USD' as Currency");
let usd_balance = match self
.cala
.balances()
.find_in_op(op, self.journal_id, id, usd_currency)
.await?;
.await
{
Ok(balance) => balance.try_into()?,
Err(BalanceError::NotFound(_, _, _)) => UsdStatementAccountSetBalance::ZERO,
Err(e) => return Err(e.into()),
};

Ok(StatementAccountSet {
id: values.id,
name: values.name,
description: values.description,
btc_balance: btc_balance.try_into()?,
usd_balance: usd_balance.try_into()?,
btc_balance,
usd_balance,
})
}

Expand Down
34 changes: 34 additions & 0 deletions lana/app/src/trial_balance/ledger/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl TryFrom<AccountBalance> for BtcStatementAccountSetBalance {
}
}

impl BtcStatementAccountSetBalance {
pub const ZERO: Self = Self {
all: BtcStatementBalanceAmount::ZERO,
settled: BtcStatementBalanceAmount::ZERO,
pending: BtcStatementBalanceAmount::ZERO,
encumbrance: BtcStatementBalanceAmount::ZERO,
};
}

#[derive(Clone)]
pub struct UsdStatementAccountSetBalance {
pub all: UsdStatementBalanceAmount,
Expand Down Expand Up @@ -121,16 +130,41 @@ impl TryFrom<AccountBalance> for UsdStatementAccountSetBalance {
}
}

impl UsdStatementAccountSetBalance {
pub const ZERO: Self = Self {
all: UsdStatementBalanceAmount::ZERO,
settled: UsdStatementBalanceAmount::ZERO,
pending: UsdStatementBalanceAmount::ZERO,
encumbrance: UsdStatementBalanceAmount::ZERO,
};
}

#[derive(Clone)]
pub struct BtcStatementBalanceAmount {
pub normal_balance: Satoshis,
pub dr_balance: Satoshis,
pub cr_balance: Satoshis,
}

impl BtcStatementBalanceAmount {
pub const ZERO: Self = Self {
normal_balance: Satoshis::ZERO,
dr_balance: Satoshis::ZERO,
cr_balance: Satoshis::ZERO,
};
}

#[derive(Clone)]
pub struct UsdStatementBalanceAmount {
pub normal_balance: UsdCents,
pub dr_balance: UsdCents,
pub cr_balance: UsdCents,
}

impl UsdStatementBalanceAmount {
pub const ZERO: Self = Self {
normal_balance: UsdCents::ZERO,
dr_balance: UsdCents::ZERO,
cr_balance: UsdCents::ZERO,
};
}

0 comments on commit ba78b62

Please sign in to comment.