From 8297f012f47624674a10b8288d4e758650e70bba Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 3 Jul 2025 16:39:44 +0300 Subject: [PATCH] added balance.go for balance related functions --- handlers/application/balance.go | 105 ++++++++++++++++++++++++++++ handlers/application/menuhandler.go | 92 ------------------------ 2 files changed, 105 insertions(+), 92 deletions(-) create mode 100644 handlers/application/balance.go diff --git a/handlers/application/balance.go b/handlers/application/balance.go new file mode 100644 index 0000000..f36a6a3 --- /dev/null +++ b/handlers/application/balance.go @@ -0,0 +1,105 @@ +package application + +import ( + "context" + "fmt" + "strings" + + "git.defalsify.org/vise.git/db" + "git.defalsify.org/vise.git/resource" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" + storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" + "gopkg.in/leonelquinteros/gotext.v1" +) + +// CheckBalance retrieves the balance of the active voucher and sets +// the balance as the result content. +func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var ( + res resource.Result + err error + alias string + content string + ) + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + store := h.userdataStore + + // get the active sym and active balance + activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM) + if err != nil { + logg.InfoCtxf(ctx, "could not find the activeSym in checkBalance:", "err", err) + if !db.IsNotFound(err) { + logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err) + return res, err + } + } + + activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL) + if err != nil { + if !db.IsNotFound(err) { + logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err) + return res, err + } + } + + accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS) + if err != nil { + if !db.IsNotFound(err) { + logg.ErrorCtxf(ctx, "failed to read account alias entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "error", err) + return res, err + } + } else { + alias = strings.Split(string(accAlias), ".")[0] + } + + content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias) + if err != nil { + return res, err + } + res.Content = content + + return res, nil +} + +// loadUserContent loads the main user content in the main menu: the alias,balance associated with active voucher +func loadUserContent(ctx context.Context, activeSym string, balance string, alias string) (string, error) { + var content string + + code := codeFromCtx(ctx) + l := gotext.NewLocale(translationDir, code) + l.AddDomain("default") + + // Format the balance to 2 decimal places or default to 0.00 + formattedAmount, err := store.TruncateDecimalString(balance, 2) + if err != nil { + formattedAmount = "0.00" + } + + // format the final output + balStr := fmt.Sprintf("%s %s", formattedAmount, activeSym) + if alias != "" { + content = l.Get("%s balance: %s\n", alias, balStr) + } else { + content = l.Get("Balance: %s\n", balStr) + } + return content, nil +} + +// FetchCommunityBalance retrieves and displays the balance for community accounts in user's preferred language. +func (h *MenuHandlers) FetchCommunityBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + // retrieve the language code from the context + code := codeFromCtx(ctx) + // Initialize the localization system with the appropriate translation directory + l := gotext.NewLocale(translationDir, code) + l.AddDomain("default") + //TODO: + //Check if the address is a community account,if then,get the actual balance + res.Content = l.Get("Community Balance: 0.00") + return res, nil +} diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 36b5258..4ab6a8e 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -463,98 +463,6 @@ func (h *MenuHandlers) ShowBlockedAccount(ctx context.Context, sym string, input return res, nil } -// loadUserContent loads the main user content in the main menu: the alias,balance associated with active voucher -func loadUserContent(ctx context.Context, activeSym string, balance string, alias string) (string, error) { - var content string - - code := codeFromCtx(ctx) - l := gotext.NewLocale(translationDir, code) - l.AddDomain("default") - - // Format the balance to 2 decimal places or default to 0.00 - formattedAmount, err := store.TruncateDecimalString(balance, 2) - if err != nil { - formattedAmount = "0.00" - } - - // format the final output - balStr := fmt.Sprintf("%s %s", formattedAmount, activeSym) - if alias != "" { - content = l.Get("%s balance: %s\n", alias, balStr) - } else { - content = l.Get("Balance: %s\n", balStr) - } - return content, nil -} - -// CheckBalance retrieves the balance of the active voucher and sets -// the balance as the result content. -func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var ( - res resource.Result - err error - alias string - content string - ) - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - - store := h.userdataStore - - // get the active sym and active balance - activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM) - if err != nil { - logg.InfoCtxf(ctx, "could not find the activeSym in checkBalance:", "err", err) - if !db.IsNotFound(err) { - logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err) - return res, err - } - } - - activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL) - if err != nil { - if !db.IsNotFound(err) { - logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err) - return res, err - } - } - - accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS) - if err != nil { - if !db.IsNotFound(err) { - logg.ErrorCtxf(ctx, "failed to read account alias entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "error", err) - return res, err - } - } else { - alias = strings.Split(string(accAlias), ".")[0] - } - - content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias) - if err != nil { - return res, err - } - res.Content = content - - return res, nil -} - -// FetchCommunityBalance retrieves and displays the balance for community accounts in user's preferred language. -func (h *MenuHandlers) FetchCommunityBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - // retrieve the language code from the context - code := codeFromCtx(ctx) - // Initialize the localization system with the appropriate translation directory - l := gotext.NewLocale(translationDir, code) - l.AddDomain("default") - //TODO: - //Check if the address is a community account,if then,get the actual balance - res.Content = l.Get("Community Balance: 0.00") - return res, nil -} - // persistInitialLanguageCode receives an initial language code and persists it to the store func (h *MenuHandlers) persistInitialLanguageCode(ctx context.Context, sessionId string, code string) error { store := h.userdataStore