From 9ad7d5a5226619eac07e99bcd4ffe1f3ec89548b Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 9 Oct 2024 15:39:06 +0300 Subject: [PATCH] set the active symbol --- internal/handlers/handlerservice.go | 1 + internal/handlers/ussd/menuhandler.go | 49 +++++++++++++++++++++++++-- services/registration/voucher_set | 2 +- services/registration/voucher_set.vis | 2 ++ services/registration/voucher_set_swa | 2 +- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 4c152b9..3b79ea2 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -95,6 +95,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers) ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList) ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher) + ls.DbRs.AddLocalFunc("set_voucher", ussdHandlers.SetVoucher) return ussdHandlers, nil } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index b634f91..fbb254b 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1058,6 +1058,13 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) func (h *Handlers) ViewVoucher(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") + } + inputStr := string(input) if inputStr == "0" || inputStr == "99" { @@ -1067,7 +1074,6 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher") // Initialize the store and prefix database - store := h.userdataStore prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) // Retrieve the voucher symbol list @@ -1111,8 +1117,12 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r } } - // If a match is found, 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 { + return res, err + } res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance) res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) } else { @@ -1121,3 +1131,38 @@ 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 +func (h *Handlers) SetVoucher(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") + } + + // get the current temporary symbol + temporarySym, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM) + if err != nil { + return res, err + } + + // set the active symbol + err = store.WriteEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM, []byte(temporarySym)) + if err != nil { + return res, err + } + + // reset the temporary symbol + err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte("")) + if err != nil { + return res, err + } + + res.Content = string(temporarySym) + + return res, nil +} diff --git a/services/registration/voucher_set b/services/registration/voucher_set index 838c1ef..e90d2e5 100644 --- a/services/registration/voucher_set +++ b/services/registration/voucher_set @@ -1 +1 @@ -Success! symbol is now your active voucher. \ No newline at end of file +Success! {{.set_voucher}} is now your active voucher. \ No newline at end of file diff --git a/services/registration/voucher_set.vis b/services/registration/voucher_set.vis index 832ef22..e3e81f5 100644 --- a/services/registration/voucher_set.vis +++ b/services/registration/voucher_set.vis @@ -1,3 +1,5 @@ +LOAD set_voucher 12 +MAP set_voucher MOUT back 0 MOUT quit 9 HALT diff --git a/services/registration/voucher_set_swa b/services/registration/voucher_set_swa index 320a315..97d3fde 100644 --- a/services/registration/voucher_set_swa +++ b/services/registration/voucher_set_swa @@ -1 +1 @@ -Hongera! symbol ni Sarafu inayotumika sasa. \ No newline at end of file +Hongera! {{.set_voucher}} ni Sarafu inayotumika sasa. \ No newline at end of file