From 7c08a0f0afad4f01b52321f17e4ef4a7f458a635 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 10 Oct 2024 14:48:23 +0300 Subject: [PATCH] add temp and active balance --- internal/handlers/ussd/menuhandler.go | 19 +++++++++++++++++++ internal/utils/db.go | 2 ++ 2 files changed, 21 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index fbb254b..e459975 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1123,6 +1123,10 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r if err != nil { return res, err } + err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_BAL, []byte(matchedBalance)) + if err != nil { + return res, err + } res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance) res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) } else { @@ -1149,18 +1153,33 @@ func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (re if err != nil { return res, err } + // get the current temporary balance + temporaryBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_BAL) + 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 } + // set the active balance + err = store.WriteEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL, []byte(temporaryBal)) + 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 } + // reset the temporary balance + err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_BAL, []byte("")) + if err != nil { + return res, err + } res.Content = string(temporarySym) diff --git a/internal/utils/db.go b/internal/utils/db.go index f70e08b..45e7681 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -26,6 +26,8 @@ const ( DATA_VOUCHER_LIST DATA_TEMPORARY_SYM DATA_ACTIVE_SYM + DATA_TEMPORARY_BAL + DATA_ACTIVE_BAL ) func typToBytes(typ DataTyp) []byte {