Compare commits

...

2 Commits

3 changed files with 179 additions and 155 deletions

View File

@ -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
}

View File

@ -0,0 +1,74 @@
package application
import (
"context"
"fmt"
"strings"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
commonlang "git.grassecon.net/grassrootseconomics/common/lang"
)
// SetLanguage sets the language across the menu.
func (h *MenuHandlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
symbol, _ := h.st.Where()
code := strings.Split(symbol, "_")[1]
// TODO: Use defaultlanguage from config
if !commonlang.IsValidISO639(code) {
//Fallback to english instead?
code = "eng"
}
err := h.persistLanguageCode(ctx, code)
if err != nil {
return res, err
}
res.Content = code
res.FlagSet = append(res.FlagSet, state.FLAG_LANG)
languageSetFlag, err := h.flagManager.GetFlag("flag_language_set")
if err != nil {
logg.ErrorCtxf(ctx, "Error setting the languageSetFlag", "error", err)
return res, err
}
res.FlagSet = append(res.FlagSet, languageSetFlag)
return res, nil
}
// persistLanguageCode persists the selected ISO 639 language code
func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) error {
store := h.userdataStore
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return fmt.Errorf("missing session")
}
err := store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code))
if err != nil {
logg.ErrorCtxf(ctx, "failed to persist language code", "key", storedb.DATA_SELECTED_LANGUAGE_CODE, "value", code, "error", err)
return err
}
return h.persistInitialLanguageCode(ctx, sessionId, code)
}
// 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
_, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE)
if err == nil {
return nil
}
if !db.IsNotFound(err) {
return err
}
err = store.WriteEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE, []byte(code))
if err != nil {
logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err)
return err
}
return nil
}

View File

@ -5,7 +5,6 @@ import (
"fmt"
"path"
"strconv"
"strings"
"gopkg.in/leonelquinteros/gotext.v1"
@ -17,7 +16,6 @@ import (
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
commonlang "git.grassecon.net/grassrootseconomics/common/lang"
"git.grassecon.net/grassrootseconomics/common/pin"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
"git.grassecon.net/grassrootseconomics/sarafu-vise/internal/sms"
@ -158,34 +156,6 @@ func codeFromCtx(ctx context.Context) string {
return code
}
// SetLanguage sets the language across the menu.
func (h *MenuHandlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
symbol, _ := h.st.Where()
code := strings.Split(symbol, "_")[1]
// TODO: Use defaultlanguage from config
if !commonlang.IsValidISO639(code) {
//Fallback to english instead?
code = "eng"
}
err := h.persistLanguageCode(ctx, code)
if err != nil {
return res, err
}
res.Content = code
res.FlagSet = append(res.FlagSet, state.FLAG_LANG)
languageSetFlag, err := h.flagManager.GetFlag("flag_language_set")
if err != nil {
logg.ErrorCtxf(ctx, "Error setting the languageSetFlag", "error", err)
return res, err
}
res.FlagSet = append(res.FlagSet, languageSetFlag)
return res, nil
}
func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
flag_language_set, _ := h.flagManager.GetFlag("flag_language_set")
@ -463,131 +433,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
_, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE)
if err == nil {
return nil
}
if !db.IsNotFound(err) {
return err
}
err = store.WriteEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE, []byte(code))
if err != nil {
logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err)
return err
}
return nil
}
// persistLanguageCode persists the selected ISO 639 language code
func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) error {
store := h.userdataStore
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return fmt.Errorf("missing session")
}
err := store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code))
if err != nil {
logg.ErrorCtxf(ctx, "failed to persist language code", "key", storedb.DATA_SELECTED_LANGUAGE_CODE, "value", code, "error", err)
return err
}
return h.persistInitialLanguageCode(ctx, sessionId, code)
}
// ClearTemporaryValue empties the DATA_TEMPORARY_VALUE at the main menu to prevent
// previously stored data from being accessed
func (h *MenuHandlers) ClearTemporaryValue(ctx context.Context, sym string, input []byte) (resource.Result, error) {