From a5b1c5b74e9c7da4f650a9c6d1b77f4d1ae4f379 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 14:32:40 +0300 Subject: [PATCH] cleaned up the code --- internal/handlers/ussd/menuhandler.go | 65 +++++++++++++-------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index fb1db3f..4196325 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -233,28 +233,6 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt return res, nil } -// GetVoucherList fetches the list of vouchers and formats them -// checks whether they are stored internally before calling the API -func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - - // check if the vouchers exist internally and if not - // fetch from the API - - // Read vouchers from the store - store := h.userdataStore - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) - - voucherData, err := prefixdb.Get(ctx, []byte("tokens")) - if err != nil { - return res, nil - } - - res.Content = string(voucherData) - - return res, nil -} - func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result sessionId, ok := ctx.Value("SessionId").(string) @@ -1057,13 +1035,13 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) voucherSymbolList := strings.Join(numberedSymbols, "\n") voucherBalanceList := strings.Join(numberedBalances, "\n") - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) - err = prefixdb.Put(ctx, []byte("tokens"), []byte(voucherSymbolList)) + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList)) if err != nil { return res, nil } - err = prefixdb.Put(ctx, []byte(voucherSymbolList), []byte(voucherBalanceList)) + err = prefixdb.Put(ctx, []byte("bal"), []byte(voucherBalanceList)) if err != nil { return res, nil } @@ -1071,6 +1049,24 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } +// GetVoucherList fetches the list of vouchers and formats them +func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + + // Read vouchers from the store + store := h.userdataStore + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + + voucherData, err := prefixdb.Get(ctx, []byte("sym")) + if err != nil { + return res, nil + } + + res.Content = string(voucherData) + + return res, nil +} + // ViewVoucher retrieves the token holding and balance from the subprefixDB func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -1082,25 +1078,25 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r return res, fmt.Errorf("missing session") } + flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher") + inputStr := string(input) if inputStr == "0" || inputStr == "99" { + res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) return res, nil } - flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher") - - // Initialize the store and prefix database - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) // Retrieve the voucher symbol list - voucherSymbolList, err := prefixdb.Get(ctx, []byte("tokens")) + voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym")) if err != nil { return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err) } // Retrieve the voucher balance list - voucherBalanceList, err := prefixdb.Get(ctx, []byte(voucherSymbolList)) + voucherBalanceList, err := prefixdb.Get(ctx, []byte("bal")) if err != nil { return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err) } @@ -1134,7 +1130,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r } } - // If a match is found, write the temporary sym , then return the symbol and balance + // If a match is found, write the temporary sym, then return the symbol and balance if matchedSymbol != "" && matchedBalance != "" { err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte(matchedSymbol)) if err != nil { @@ -1153,8 +1149,9 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r return res, nil } -// SetVoucher retrieves the temporary voucher, sets it as the active voucher and -// clears the temporary voucher/sym +// SetVoucher retrieves the temporary sym and balance, +// sets them as the active data and +// clears the temporary data func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result var err error