MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / CreateBalance

Method CreateBalance

database/balance.go:216–291  ·  view source on GitHub ↗

CreateBalance inserts a new balance record into the `ledgerforge.balances` table in the database. It handles the generation of a unique balance ID, default values for fields, and any necessary error handling. Parameters: - balance: A model.Balance object containing the balance information to be cre

(balance model.Balance)

Source from the content-addressed store, hash-verified

214// - model.Balance: The created balance with its ID and timestamp populated.
215// - error: Returns an APIError in case of failures such as database conflicts or other issues.
216func (d Datasource) CreateBalance(balance model.Balance) (model.Balance, error) {
217 // Marshal metadata into JSON
218 metaDataJSON, err := json.Marshal(balance.MetaData)
219 if err != nil {
220 return model.Balance{}, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to marshal metadata", err)
221 }
222
223 // Generate a unique balance ID and set the creation timestamp
224 balance.BalanceID = model.GenerateUUIDWithSuffix("bln")
225 balance.CreatedAt = time.Now()
226
227 // Handle nullable fields
228 var identityID interface{} = balance.IdentityID
229 if balance.IdentityID == "" {
230 identityID = nil
231 }
232
233 var indicator interface{} = balance.Indicator
234 if balance.Indicator == "" {
235 indicator = nil
236 }
237
238 // Set default values for balance fields if they are nil
239 if balance.Balance == nil {
240 balance.Balance = big.NewInt(0)
241 }
242 if balance.CreditBalance == nil {
243 balance.CreditBalance = big.NewInt(0)
244 }
245 if balance.DebitBalance == nil {
246 balance.DebitBalance = big.NewInt(0)
247 }
248 if balance.InflightBalance == nil {
249 balance.InflightBalance = big.NewInt(0)
250 }
251 if balance.InflightCreditBalance == nil {
252 balance.InflightCreditBalance = big.NewInt(0)
253 }
254 if balance.InflightDebitBalance == nil {
255 balance.InflightDebitBalance = big.NewInt(0)
256 }
257
258 // Default allocation strategy to FIFO if not set
259 allocationStrategy := balance.AllocationStrategy
260 if allocationStrategy == "" {
261 allocationStrategy = "FIFO"
262 }
263
264 // Insert the balance into the database
265 _, err = d.Conn.ExecContext(context.Background(), `
266 INSERT INTO ledgerforge.balances (balance_id, balance, credit_balance, debit_balance, currency, currency_multiplier, ledger_id, identity_id, indicator, created_at, meta_data, track_fund_lineage, allocation_strategy)
267 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
268 `, balance.BalanceID, balance.Balance.String(), balance.CreditBalance.String(), balance.DebitBalance.String(), balance.Currency, balance.CurrencyMultiplier, balance.LedgerID, identityID, indicator, balance.CreatedAt, &metaDataJSON, balance.TrackFundLineage, allocationStrategy)
269 if err != nil {
270 // Handle specific PostgreSQL errors (e.g., unique or foreign key violations)
271 pqErr, ok := err.(*pq.Error)
272 if ok {
273 switch pqErr.Code.Name() {

Callers 2

Calls 2

NewAPIErrorFunction · 0.92
GenerateUUIDWithSuffixFunction · 0.92

Tested by 2