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

Method GetBalanceAtTime

api/balance.go:394–440  ·  view source on GitHub ↗

GetBalanceAtTime retrieves a balance's state at a specific point in time. It extracts the balance ID from the route parameters and the timestamp from query parameters. The timestamp should be provided in ISO 8601 format (e.g., "2024-01-01T15:04:05Z"). Optionally accepts a "from_source" query paramet

(c *gin.Context)

Source from the content-addressed store, hash-verified

392// - 400 Bad Request: If the balance ID is missing, timestamp is invalid, or there's an error retrieving the balance.
393// - 200 OK: If the historical balance state is successfully retrieved.
394func (a Api) GetBalanceAtTime(c *gin.Context) {
395 balanceID, exists := c.Params.Get("id")
396 if !exists {
397 c.JSON(http.StatusBadRequest, gin.H{"error": "balance ID is required"})
398 return
399 }
400
401 var timestamp time.Time
402 timestampStr := c.Query("timestamp")
403 if timestampStr == "" {
404 // Use current time if no timestamp is provided
405 timestamp = time.Now().UTC()
406 } else {
407 var err error
408 timestamp, err = time.Parse(time.RFC3339, timestampStr)
409 if err != nil {
410 c.JSON(http.StatusBadRequest, gin.H{
411 "error": "invalid timestamp format. Please use ISO 8601 format (e.g., 2024-01-01T15:04:05Z)",
412 })
413 return
414 }
415 }
416
417 // Check if the request specifies to calculate from source transactions
418 fromSourceStr := c.Query("from_source")
419 fromSource := fromSourceStr == "true" || fromSourceStr == "1"
420
421 balance, err := a.ledgerforge.GetBalanceAtTime(c.Request.Context(), balanceID, timestamp, fromSource)
422 if err != nil {
423 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
424 return
425 }
426
427 balanceResult := map[string]interface{}{
428 "balance": balance.Balance,
429 "debit_balance": balance.DebitBalance,
430 "credit_balance": balance.CreditBalance,
431 "currency": balance.Currency,
432 "balance_id": balance.BalanceID,
433 }
434
435 c.JSON(http.StatusOK, gin.H{
436 "balance": balanceResult,
437 "timestamp": timestamp.Format(time.RFC3339),
438 "from_source": fromSource,
439 })
440}
441
442// GetBalanceByIndicator retrieves a balance by its indicator and currency.
443// It extracts the indicator and currency from the route parameters.

Callers

nothing calls this directly

Calls 3

GetMethod · 0.65
GetBalanceAtTimeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected