GetBalanceByIndicator retrieves a balance from the database using the specified indicator and currency. The function scans the query result into a Balance object and converts various fields from int64 to big.Int. It returns the balance if found, or an error if the balance does not exist. Parameters
(indicator, currency string)
| 567 | // - *model.Balance: The retrieved balance object or an empty Balance object if not found. |
| 568 | // - error: An error if any issues occur during the query execution or data retrieval. |
| 569 | func (d Datasource) GetBalanceByIndicator(indicator, currency string) (*model.Balance, error) { |
| 570 | var balance model.Balance |
| 571 | // Change to string variables to handle large numbers |
| 572 | var balanceValue, creditBalanceValue, debitBalanceValue string |
| 573 | var inflightBalanceValue, inflightCreditBalanceValue, inflightDebitBalanceValue string |
| 574 | var allocationStrategy sql.NullString |
| 575 | |
| 576 | // Execute query to find the balance with the given indicator and currency |
| 577 | row := d.Conn.QueryRowContext(context.Background(), ` |
| 578 | SELECT balance_id, indicator, currency, currency_multiplier, ledger_id, balance, credit_balance, debit_balance, inflight_balance, inflight_credit_balance, inflight_debit_balance, created_at, version, track_fund_lineage, COALESCE(allocation_strategy, 'FIFO') as allocation_strategy, COALESCE(identity_id, '') as identity_id |
| 579 | FROM ledgerforge.balances |
| 580 | WHERE indicator = $1 AND currency = $2 |
| 581 | `, indicator, currency) |
| 582 | |
| 583 | // Scan the result into the Balance object |
| 584 | err := row.Scan( |
| 585 | &balance.BalanceID, |
| 586 | &balance.Indicator, |
| 587 | &balance.Currency, |
| 588 | &balance.CurrencyMultiplier, |
| 589 | &balance.LedgerID, |
| 590 | &balanceValue, |
| 591 | &creditBalanceValue, |
| 592 | &debitBalanceValue, |
| 593 | &inflightBalanceValue, |
| 594 | &inflightCreditBalanceValue, |
| 595 | &inflightDebitBalanceValue, |
| 596 | &balance.CreatedAt, |
| 597 | &balance.Version, |
| 598 | &balance.TrackFundLineage, |
| 599 | &allocationStrategy, |
| 600 | &balance.IdentityID, |
| 601 | ) |
| 602 | if err != nil { |
| 603 | if err == sql.ErrNoRows { |
| 604 | // Handle the case where no balance was found with the given indicator and currency |
| 605 | return &model.Balance{}, fmt.Errorf("balance with indicator '%s' not found", indicator) |
| 606 | } |
| 607 | // Return other types of errors, such as query execution failures |
| 608 | return nil, err |
| 609 | } |
| 610 | |
| 611 | // Handle null allocation_strategy field |
| 612 | if allocationStrategy.Valid { |
| 613 | balance.AllocationStrategy = allocationStrategy.String |
| 614 | } else { |
| 615 | balance.AllocationStrategy = "FIFO" |
| 616 | } |
| 617 | |
| 618 | // Initialize big.Int values |
| 619 | balance.Balance = new(big.Int) |
| 620 | balance.CreditBalance = new(big.Int) |
| 621 | balance.DebitBalance = new(big.Int) |
| 622 | balance.InflightBalance = new(big.Int) |
| 623 | balance.InflightCreditBalance = new(big.Int) |
| 624 | balance.InflightDebitBalance = new(big.Int) |
| 625 | |
| 626 | // Parse string values to big.Int |