From 6c6af5ec210a0b5782c9d17ca3b2d4900dc270cc Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 15:46:02 +0300 Subject: [PATCH] set a default voucher as the active sym if none exists --- internal/handlers/handlerservice.go | 1 + internal/handlers/ussd/menuhandler.go | 93 ++++++++++++++++++--------- services/registration/main.vis | 2 + 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 0d891f1..129d851 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -91,6 +91,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin) ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange) ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp) + ls.DbRs.AddLocalFunc("set_default_voucher", ussdHandlers.SetDefaultVoucher) ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers) ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList) ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 3e164af..af3b99f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -633,41 +633,13 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( } store := h.userdataStore - publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) - if err != nil { - return res, err - } - // check if the user has an active sym + // get the active sym and active balance activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) if err != nil { - if db.IsNotFound(err) { - logg.Printf(logging.LVL_INFO, "Using the default sym to fetch balance") - balance, err := h.accountService.CheckBalance(string(publicKey)) - if err != nil { - return res, err - } - - res.Content = balance - - return res, nil - } - return res, err } - if len(activeSym) == 0 { - logg.Printf(logging.LVL_INFO, "Using the default sym to fetch balance") - balance, err := h.accountService.CheckBalance(string(publicKey)) - if err != nil { - return res, err - } - - res.Content = balance - - return res, nil - } - activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) if err != nil { return res, err @@ -1004,6 +976,69 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) return res, nil } +// SetDefaultVoucher retrieves the current vouchers +// and sets the first as the default voucher, if no active voucher is set +func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + var err error + store := h.userdataStore + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + fmt.Println("Running SetDefaultVoucher") + + // check if the user has an active sym + _, err = store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) + + if err != nil { + if db.IsNotFound(err) { + publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) + + if err != nil { + return res, nil + } + + // Fetch vouchers from the API using the public key + vouchersResp, err := h.accountService.FetchVouchers(string(publicKey)) + if err != nil { + return res, nil + } + + // Ensure there is at least one voucher + if len(vouchersResp.Result.Holdings) == 0 { + return res, err + } + + // Use only the first voucher + firstVoucher := vouchersResp.Result.Holdings[0] + defaultSym := firstVoucher.TokenSymbol + defaultBal := firstVoucher.Balance + + // set the active symbol + err = store.WriteEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM, []byte(defaultSym)) + if err != nil { + return res, err + } + // set the active balance + err = store.WriteEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL, []byte(defaultBal)) + if err != nil { + return res, err + } + + return res, nil + } + + fmt.Println("Nothing will happen as the error in not 404") + + return res, err + } + + return res, nil +} + // CheckVouchers retrieves the token holdings from the API using the "PublicKey" and stores // them to gdbm func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) { diff --git a/services/registration/main.vis b/services/registration/main.vis index 88f8a42..b809ece 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -1,3 +1,5 @@ +LOAD set_default_voucher 8 +RELOAD set_default_voucher LOAD check_balance 64 RELOAD check_balance LOAD check_vouchers 10