Skip to content

Commit

Permalink
chore: read balance in op for account
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jun 17, 2024
1 parent 14bacb5 commit f461385
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cala-server/src/graphql/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::app::CalaApp;

use super::{
account_set::*, balance::Balance, convert::ToGlobalId, loader::LedgerDataLoader, primitives::*,
schema::DbOp,
};

#[derive(Clone, SimpleObject)]
Expand Down Expand Up @@ -37,12 +38,26 @@ impl Account {
journal_id: UUID,
currency: CurrencyCode,
) -> async_graphql::Result<Option<Balance>> {
let loader = ctx.data_unchecked::<DataLoader<LedgerDataLoader>>();
let journal_id = JournalId::from(journal_id);
let account_id = AccountId::from(self.account_id);
let currency = Currency::from(currency);
let balance: Option<AccountBalance> =
loader.load_one((journal_id, account_id, currency)).await?;

let balance: Option<AccountBalance> = match ctx.data_opt::<DbOp>() {
Some(op) => {
let app = ctx.data_unchecked::<CalaApp>();
let mut op = op.try_lock().expect("Lock held concurrently");
Some(
app.ledger()
.balances()
.find_in_op(&mut op, journal_id, account_id, currency)
.await?,
)
}
None => {
let loader = ctx.data_unchecked::<DataLoader<LedgerDataLoader>>();
loader.load_one((journal_id, account_id, currency)).await?
}
};
Ok(balance.map(Balance::from))
}

Expand Down

0 comments on commit f461385

Please sign in to comment.