Compare commits
2 Commits
9ccb6cc066
...
9ad7d5a522
Author | SHA1 | Date | |
---|---|---|---|
9ad7d5a522 | |||
6c904a8b3f |
@ -95,6 +95,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
|
|||||||
ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers)
|
ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers)
|
||||||
ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList)
|
ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList)
|
||||||
ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher)
|
ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher)
|
||||||
|
ls.DbRs.AddLocalFunc("set_voucher", ussdHandlers.SetVoucher)
|
||||||
|
|
||||||
return ussdHandlers, nil
|
return ussdHandlers, nil
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
var err error
|
var err error
|
||||||
|
store := h.userdataStore
|
||||||
|
|
||||||
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
|
if !ok {
|
||||||
|
return res, fmt.Errorf("missing session")
|
||||||
|
}
|
||||||
|
|
||||||
inputStr := string(input)
|
inputStr := string(input)
|
||||||
|
|
||||||
if inputStr == "0" || inputStr == "99" {
|
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")
|
flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher")
|
||||||
|
|
||||||
// Initialize the store and prefix database
|
// Initialize the store and prefix database
|
||||||
store := h.userdataStore
|
|
||||||
prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
|
prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings"))
|
||||||
|
|
||||||
// Retrieve the voucher symbol list
|
// 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 != "" {
|
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.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance)
|
||||||
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
res.FlagReset = append(res.FlagReset, flag_incorrect_voucher)
|
||||||
} else {
|
} else {
|
||||||
@ -1121,3 +1131,38 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
|
|||||||
|
|
||||||
return res, nil
|
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
|
||||||
|
}
|
||||||
|
@ -24,6 +24,8 @@ const (
|
|||||||
DATA_AMOUNT
|
DATA_AMOUNT
|
||||||
DATA_TEMPORARY_PIN
|
DATA_TEMPORARY_PIN
|
||||||
DATA_VOUCHER_LIST
|
DATA_VOUCHER_LIST
|
||||||
|
DATA_TEMPORARY_SYM
|
||||||
|
DATA_ACTIVE_SYM
|
||||||
)
|
)
|
||||||
|
|
||||||
func typToBytes(typ DataTyp) []byte {
|
func typToBytes(typ DataTyp) []byte {
|
||||||
|
@ -1 +1 @@
|
|||||||
Success! symbol is now your active voucher.
|
Success! {{.set_voucher}} is now your active voucher.
|
@ -1,3 +1,5 @@
|
|||||||
|
LOAD set_voucher 12
|
||||||
|
MAP set_voucher
|
||||||
MOUT back 0
|
MOUT back 0
|
||||||
MOUT quit 9
|
MOUT quit 9
|
||||||
HALT
|
HALT
|
||||||
|
@ -1 +1 @@
|
|||||||
Hongera! symbol ni Sarafu inayotumika sasa.
|
Hongera! {{.set_voucher}} ni Sarafu inayotumika sasa.
|
Loading…
Reference in New Issue
Block a user