set the active symbol

This commit is contained in:
Alfred Kamanda 2024-10-09 15:39:06 +03:00
parent 6c904a8b3f
commit 9ad7d5a522
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
5 changed files with 52 additions and 4 deletions

View File

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

View File

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

View File

@ -1 +1 @@
Success! symbol is now your active voucher.
Success! {{.set_voucher}} is now your active voucher.

View File

@ -1,3 +1,5 @@
LOAD set_voucher 12
MAP set_voucher
MOUT back 0
MOUT quit 9
HALT

View File

@ -1 +1 @@
Hongera! symbol ni Sarafu inayotumika sasa.
Hongera! {{.set_voucher}} ni Sarafu inayotumika sasa.