From 188cb573dd713278b417d8fab9ea1bdc0ec6f9cb Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 25 Sep 2024 13:27:13 +0300 Subject: [PATCH 01/52] add dummy vouchers list --- internal/handlers/handlerservice.go | 1 + internal/handlers/ussd/menuhandler.go | 32 ++++++++++++++++++++++++ services/registration/main.vis | 2 +- services/registration/select_voucher | 2 ++ services/registration/select_voucher.vis | 9 +++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 services/registration/select_voucher create mode 100644 services/registration/select_voucher.vis diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 1d6f5fd..b733efe 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -94,6 +94,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("get_vouchers",ussdHandlers.GetVoucherList) return ussdHandlers, nil } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index ff8d8bd..7902cd2 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -254,6 +254,38 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt return res, nil } + +func (h *Handlers) GetVoucherList(ctx context.Context,sym string,input []byte) (resource.Result,error){ + var res resource.Result + vouchers := []string{ + "SRF", + "CRF", + "VCF", + "VSAPA", + "FSTMP", + "FSAW", + "PTAQ", + "VCRXT", + "VSGAQ", + "QPWIQQ", + "FSTMP", + "FSAW", + "PTAQ", + "VCRXT", + "VSGAQ", + "QPWIQQ", + "FSTMP", + "FSAW", + "PTAQ", + "VCRXT", + "VSGAQ", + "QPWIQQ", + } + res.Content = strings.Join(vouchers,"\n") + + return res,nil +} + func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result sessionId, ok := ctx.Value("SessionId").(string) diff --git a/services/registration/main.vis b/services/registration/main.vis index d883dca..b5d791d 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -8,7 +8,7 @@ MOUT help 4 MOUT quit 9 HALT INCMP send 1 -INCMP quit 2 +INCMP select_voucher 2 INCMP my_account 3 INCMP help 4 INCMP quit 9 diff --git a/services/registration/select_voucher b/services/registration/select_voucher new file mode 100644 index 0000000..084b9b8 --- /dev/null +++ b/services/registration/select_voucher @@ -0,0 +1,2 @@ +Select number or symbol from your vouchers: +{{.get_vouchers}} \ No newline at end of file diff --git a/services/registration/select_voucher.vis b/services/registration/select_voucher.vis new file mode 100644 index 0000000..3919f71 --- /dev/null +++ b/services/registration/select_voucher.vis @@ -0,0 +1,9 @@ +LOAD get_vouchers 0 +MAP get_vouchers +MNEXT next 11 +MPREV back 22 +HALT +INCMP > 11 +INCMP < 22 +INCMP _* + -- 2.45.2 From 7aa44caea27c8e45f5ac2a02e20c123430ccf0fa Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 25 Sep 2024 15:57:23 +0300 Subject: [PATCH 02/52] add voucher nodes --- services/registration/main.vis | 2 +- services/registration/my_vouchers | 1 + services/registration/my_vouchers.vis | 9 +++++++++ services/registration/select_voucher_menu | 1 + services/registration/voucher_details_menu | 1 + 5 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 services/registration/my_vouchers create mode 100644 services/registration/my_vouchers.vis create mode 100644 services/registration/select_voucher_menu create mode 100644 services/registration/voucher_details_menu diff --git a/services/registration/main.vis b/services/registration/main.vis index b5d791d..1009039 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -8,7 +8,7 @@ MOUT help 4 MOUT quit 9 HALT INCMP send 1 -INCMP select_voucher 2 +INCMP my_vouchers 2 INCMP my_account 3 INCMP help 4 INCMP quit 9 diff --git a/services/registration/my_vouchers b/services/registration/my_vouchers new file mode 100644 index 0000000..548de9c --- /dev/null +++ b/services/registration/my_vouchers @@ -0,0 +1 @@ +My vouchers \ No newline at end of file diff --git a/services/registration/my_vouchers.vis b/services/registration/my_vouchers.vis new file mode 100644 index 0000000..b70dbfa --- /dev/null +++ b/services/registration/my_vouchers.vis @@ -0,0 +1,9 @@ +MOUT select_voucher 1 +MOUT voucher_details 2 +MOUT back 0 +HALT +INCMP _ 0 +INCMP select_voucher 1 + + + diff --git a/services/registration/select_voucher_menu b/services/registration/select_voucher_menu new file mode 100644 index 0000000..8ee06df --- /dev/null +++ b/services/registration/select_voucher_menu @@ -0,0 +1 @@ +Select voucher \ No newline at end of file diff --git a/services/registration/voucher_details_menu b/services/registration/voucher_details_menu new file mode 100644 index 0000000..a588f23 --- /dev/null +++ b/services/registration/voucher_details_menu @@ -0,0 +1 @@ +Voucher details \ No newline at end of file -- 2.45.2 From 0e376e0d9e0bc7164bff7e4314c8ffb74ce5306a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 25 Sep 2024 16:03:08 +0300 Subject: [PATCH 03/52] include back and quit --- services/registration/select_voucher.vis | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/registration/select_voucher.vis b/services/registration/select_voucher.vis index 3919f71..4fb1d40 100644 --- a/services/registration/select_voucher.vis +++ b/services/registration/select_voucher.vis @@ -1,9 +1,11 @@ LOAD get_vouchers 0 MAP get_vouchers +MOUT back 0 +MOUT quit 9 MNEXT next 11 -MPREV back 22 +MPREV prev 22 HALT +INCMP _ 0 +INCMP quit 9 INCMP > 11 INCMP < 22 -INCMP _* - -- 2.45.2 From 221db4e998ce4b5bcda91e6f99ec3aec18f28fbb Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 25 Sep 2024 16:06:06 +0300 Subject: [PATCH 04/52] return a numbered list of vouchers --- internal/handlers/ussd/menuhandler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 7902cd2..e998bd5 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -281,7 +281,12 @@ func (h *Handlers) GetVoucherList(ctx context.Context,sym string,input []byte) ( "VSGAQ", "QPWIQQ", } - res.Content = strings.Join(vouchers,"\n") + + var numberedVouchers []string + for i, voucher := range vouchers { + numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher)) + } + res.Content = strings.Join(numberedVouchers,"\n") return res,nil } -- 2.45.2 From 4c339450810a8ee212b2fe464fac847f567f7ad8 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 27 Sep 2024 19:10:08 +0300 Subject: [PATCH 05/52] use latest go-vise update --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c4c5167..e3a441a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index ed5636f..3f7a262 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb h1:6P4kxihcwMjDKzvUFC6t2zGNb7MDW+l/ACGlSAN1N8Y= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac h1:D4KI22KWXT8S66sHIjWhTBX6SXRfnd7j8VErq3PPbok= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= -- 2.45.2 From 3a46fda769941e196b0e62ad12a4924782de9dd1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 28 Sep 2024 12:26:37 +0300 Subject: [PATCH 06/52] use save_temporary_pin and updated tests --- internal/handlers/handlerservice.go | 7 +-- internal/handlers/ussd/menuhandler.go | 59 +++++++------------- internal/handlers/ussd/menuhandler_test.go | 51 +++-------------- services/registration/account_creation.vis | 2 +- services/registration/confirm_create_pin.vis | 4 +- services/registration/create_pin.vis | 4 +- 6 files changed, 38 insertions(+), 89 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 3dffe16..1a556cc 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -60,8 +60,8 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ussdHandlers = ussdHandlers.WithPersister(ls.Pe) ls.DbRs.AddLocalFunc("set_language", ussdHandlers.SetLanguage) ls.DbRs.AddLocalFunc("create_account", ussdHandlers.CreateAccount) - ls.DbRs.AddLocalFunc("save_pin", ussdHandlers.SavePin) - ls.DbRs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin) + ls.DbRs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin) + ls.DbRs.AddLocalFunc("verify_create_pin", ussdHandlers.VerifyCreatePin) ls.DbRs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier) ls.DbRs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus) ls.DbRs.AddLocalFunc("authorize_account", ussdHandlers.Authorize) @@ -89,11 +89,10 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ls.DbRs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob) ls.DbRs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob) ls.DbRs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction) - ls.DbRs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin) 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("get_vouchers",ussdHandlers.GetVoucherList) + ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList) return ussdHandlers, nil } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 5ebdaa5..165a456 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -180,34 +180,6 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) return res, nil } -// SavePin persists the user's PIN choice into the filesystem -func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - var err error - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - - flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") - - accountPIN := string(input) - // Validate that the PIN is a 4-digit number - if !isValidPIN(accountPIN) { - res.FlagSet = append(res.FlagSet, flag_incorrect_pin) - return res, nil - } - - res.FlagReset = append(res.FlagReset, flag_incorrect_pin) - store := h.userdataStore - err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(accountPIN)) - if err != nil { - return res, err - } - return res, nil -} - func (h *Handlers) VerifyNewPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} _, ok := ctx.Value("SessionId").(string) @@ -226,6 +198,9 @@ func (h *Handlers) VerifyNewPin(ctx context.Context, sym string, input []byte) ( return res, nil } +// SaveTemporaryPin saves the valid PIN input to the DATA_TEMPORARY_PIN +// during the account creation process +// and during the change PIN process func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result var err error @@ -234,6 +209,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt if !ok { return res, fmt.Errorf("missing session") } + flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") accountPIN := string(input) @@ -243,16 +219,19 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt res.FlagSet = append(res.FlagSet, flag_incorrect_pin) return res, nil } + + res.FlagReset = append(res.FlagReset, flag_incorrect_pin) + store := h.userdataStore err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(accountPIN)) if err != nil { return res, err } + return res, nil } - -func (h *Handlers) GetVoucherList(ctx context.Context,sym string,input []byte) (resource.Result,error){ +func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result vouchers := []string{ "SRF", @@ -283,9 +262,9 @@ func (h *Handlers) GetVoucherList(ctx context.Context,sym string,input []byte) ( for i, voucher := range vouchers { numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher)) } - res.Content = strings.Join(numberedVouchers,"\n") + res.Content = strings.Join(numberedVouchers, "\n") - return res,nil + return res, nil } func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -313,10 +292,10 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt return res, nil } -// VerifyPin checks whether the confirmation PIN is similar to the account PIN -// If similar, it sets the USERFLAG_PIN_SET flag allowing the user +// VerifyCreatePin checks whether the confirmation PIN is similar to the temporary PIN +// If similar, it sets the USERFLAG_PIN_SET flag and writes the account PIN allowing the user // to access the main menu -func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { +func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result flag_valid_pin, _ := h.flagManager.GetFlag("flag_valid_pin") @@ -328,14 +307,13 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res return res, fmt.Errorf("missing session") } - //AccountPin, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN) store := h.userdataStore - AccountPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN) + temporaryPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN) if err != nil { return res, err } - if bytes.Equal(input, AccountPin) { + if bytes.Equal(input, temporaryPin) { res.FlagSet = []uint32{flag_valid_pin} res.FlagReset = []uint32{flag_pin_mismatch} res.FlagSet = append(res.FlagSet, flag_pin_set) @@ -343,6 +321,11 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res res.FlagSet = []uint32{flag_pin_mismatch} } + err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(temporaryPin)) + if err != nil { + return res, err + } + return res, nil } diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 83e6f0c..1ce8b51 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -171,7 +171,7 @@ func TestSaveFamilyname(t *testing.T) { mockStore.AssertExpectations(t) } -func TestSavePin(t *testing.T) { +func TestSaveTemporaryPIn(t *testing.T) { fm, err := NewFlagManager(flagsPath) mockStore := new(mocks.MockUserDataStore) if err != nil { @@ -213,10 +213,10 @@ func TestSavePin(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // Set up the expected behavior of the mock - mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(tt.input)).Return(nil) + mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(tt.input)).Return(nil) // Call the method - res, err := h.SavePin(ctx, "save_pin", tt.input) + res, err := h.SaveTemporaryPin(ctx, "save_pin", tt.input) if err != nil { t.Error(err) @@ -937,7 +937,7 @@ func TestVerifyYob(t *testing.T) { } } -func TestVerifyPin(t *testing.T) { +func TestVerifyCreatePin(t *testing.T) { fm, err := NewFlagManager(flagsPath) if err != nil { @@ -986,7 +986,7 @@ func TestVerifyPin(t *testing.T) { }, } - typ := utils.DATA_ACCOUNT_PIN + typ := utils.DATA_TEMPORARY_PIN for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -994,8 +994,11 @@ func TestVerifyPin(t *testing.T) { // Define expected interactions with the mock mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return([]byte(firstSetPin), nil) + // Set up the expected behavior of the mock + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(firstSetPin)).Return(nil) + // Call the method under test - res, err := h.VerifyPin(ctx, "verify_pin", []byte(tt.input)) + res, err := h.VerifyCreatePin(ctx, "verify_create_pin", []byte(tt.input)) // Assert that no errors occurred assert.NoError(t, err) @@ -1693,42 +1696,6 @@ func TestVerifyNewPin(t *testing.T) { } -func TestSaveTemporaryPIn(t *testing.T) { - - fm, err := NewFlagManager(flagsPath) - - if err != nil { - t.Logf(err.Error()) - } - - // Create a new instance of UserDataStore - mockStore := new(mocks.MockUserDataStore) - - // Define test data - sessionId := "session123" - PIN := "1234" - ctx := context.WithValue(context.Background(), "SessionId", sessionId) - - // Set up the expected behavior of the mock - mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(PIN)).Return(nil) - - // Create the Handlers instance with the mock store - h := &Handlers{ - userdataStore: mockStore, - flagManager: fm.parser, - } - - // Call the method - res, err := h.SaveTemporaryPin(ctx, "save_temporary_pin", []byte(PIN)) - - // Assert results - assert.NoError(t, err) - assert.Equal(t, resource.Result{}, res) - - // Assert all expectations were met - mockStore.AssertExpectations(t) -} - func TestConfirmPin(t *testing.T) { sessionId := "session123" diff --git a/services/registration/account_creation.vis b/services/registration/account_creation.vis index f4f326b..380fe6d 100644 --- a/services/registration/account_creation.vis +++ b/services/registration/account_creation.vis @@ -1,4 +1,4 @@ -RELOAD verify_pin +RELOAD verify_create_pin CATCH create_pin_mismatch flag_pin_mismatch 1 LOAD quit 0 HALT diff --git a/services/registration/confirm_create_pin.vis b/services/registration/confirm_create_pin.vis index 1235916..1a3173c 100644 --- a/services/registration/confirm_create_pin.vis +++ b/services/registration/confirm_create_pin.vis @@ -1,4 +1,4 @@ -LOAD save_pin 0 +LOAD save_temporary_pin 0 HALT -LOAD verify_pin 8 +LOAD verify_create_pin 8 INCMP account_creation * diff --git a/services/registration/create_pin.vis b/services/registration/create_pin.vis index e0e330f..c76e1bf 100644 --- a/services/registration/create_pin.vis +++ b/services/registration/create_pin.vis @@ -2,8 +2,8 @@ LOAD create_account 0 CATCH account_creation_failed flag_account_creation_failed 1 MOUT exit 0 HALT -LOAD save_pin 0 -RELOAD save_pin +LOAD save_temporary_pin 0 +RELOAD save_temporary_pin CATCH . flag_incorrect_pin 1 INCMP quit 0 INCMP confirm_create_pin * -- 2.45.2 From 31aea6b807ed352b44e7640a794bab87269dbe4e Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 28 Sep 2024 14:07:37 +0300 Subject: [PATCH 07/52] added model and func to fetch vouchers from the API --- internal/handlers/server/accountservice.go | 29 ++++++++++++++++++ internal/handlers/ussd/menuhandler.go | 35 +++++++--------------- internal/mocks/servicemock.go | 7 ++++- internal/models/vouchersresponse.go | 7 +++++ 4 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 internal/models/vouchersresponse.go diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index f4375a1..ca460c7 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -13,6 +13,7 @@ type AccountServiceInterface interface { CheckBalance(publicKey string) (string, error) CreateAccount() (*models.AccountResponse, error) CheckAccountStatus(trackingId string) (string, error) + FetchVouchersFromAPI() ([]models.VoucherHolding, error) } type AccountService struct { @@ -110,3 +111,31 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) { return &accountResp, nil } + +// fetchVouchersFromAPI calls the API to get the list of vouchers belonging to the user +func (as *AccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) { + // TODO replace with the actual request once ready + mockJSON := `[ + { + "symbol": "MUMO", + "address": "0x078b3a26596218507781722A4e8825BFB9570Fba" + }, + { + "symbol": "SRF", + "address": "0x45d747172e77d55575c197CbA9451bC2CD8F4958" + }, + { + "symbol": "HALGAN", + "address": "0x12169Fb5931A599ad1283bb8311Dad54Feb51A28" + } + ]` + + // Unmarshal the JSON response + var holdings []models.VoucherHolding + err := json.Unmarshal([]byte(mockJSON), &holdings) + if err != nil { + return nil, err + } + + return holdings, nil +} diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 165a456..19c7d1a 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -231,36 +231,23 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt return res, nil } +// GetVoucherList fetches the list of vouchers and formats them +// checks whether they are synced internally before calling the API func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - vouchers := []string{ - "SRF", - "CRF", - "VCF", - "VSAPA", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", + + // check if the vouchers exist internally and if not + // fetch from the API + + // Fetch vouchers from API + vouchers, err := h.accountService.FetchVouchersFromAPI() + if err != nil { + return res, fmt.Errorf("error fetching vouchers: %w", err) } var numberedVouchers []string for i, voucher := range vouchers { - numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher)) + numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.Symbol)) } res.Content = strings.Join(numberedVouchers, "\n") diff --git a/internal/mocks/servicemock.go b/internal/mocks/servicemock.go index 9fb6d3e..30386b3 100644 --- a/internal/mocks/servicemock.go +++ b/internal/mocks/servicemock.go @@ -23,4 +23,9 @@ func (m *MockAccountService) CheckBalance(publicKey string) (string, error) { func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) { args := m.Called(trackingId) return args.String(0), args.Error(1) -} \ No newline at end of file +} + +func (m *MockAccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) { + args := m.Called() + return args.Get(0).([]models.VoucherHolding), args.Error(1) +} diff --git a/internal/models/vouchersresponse.go b/internal/models/vouchersresponse.go new file mode 100644 index 0000000..08967b7 --- /dev/null +++ b/internal/models/vouchersresponse.go @@ -0,0 +1,7 @@ +package models + +// VoucherHolding represents a single voucher holding +type VoucherHolding struct { + Symbol string `json:"symbol"` + Address string `json:"address"` +} \ No newline at end of file -- 2.45.2 From 517f9806643b4012cf6aaedfbe3450f4c781c0c6 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 5 Oct 2024 16:56:49 +0300 Subject: [PATCH 08/52] get the vouchers list and store in gdbm --- internal/handlers/handlerservice.go | 1 + internal/handlers/server/accountservice.go | 100 +++++++++++++++------ internal/handlers/ussd/menuhandler.go | 63 +++++++++++-- internal/mocks/servicemock.go | 4 +- internal/models/vouchersresponse.go | 18 ++-- internal/utils/db.go | 1 + services/registration/main.vis | 2 + 7 files changed, 152 insertions(+), 37 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 1a556cc..8f0ff16 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -92,6 +92,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("check_vouchers", ussdHandlers.CheckVouchers) ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList) return ussdHandlers, nil diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index ca460c7..d53db57 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -13,14 +13,12 @@ type AccountServiceInterface interface { CheckBalance(publicKey string) (string, error) CreateAccount() (*models.AccountResponse, error) CheckAccountStatus(trackingId string) (string, error) - FetchVouchersFromAPI() ([]models.VoucherHolding, error) + FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) } type AccountService struct { } - - // CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID. // // Parameters: @@ -28,12 +26,10 @@ type AccountService struct { // CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the // AccountResponse struct can be used here to check the account status during a transaction. // -// // Returns: // - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string. // - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data. // If no error occurs, this will be nil. -// func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) { resp, err := http.Get(config.TrackStatusURL + trackingId) if err != nil { @@ -57,7 +53,6 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) return status, nil } - // CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint. // Parameters: // - publicKey: The public key associated with the account whose balance needs to be checked. @@ -84,8 +79,7 @@ func (as *AccountService) CheckBalance(publicKey string) (string, error) { return balance, nil } - -//CreateAccount creates a new account in the custodial system. +// CreateAccount creates a new account in the custodial system. // Returns: // - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account. // If there is an error during the request or processing, this will be nil. @@ -112,30 +106,86 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) { return &accountResp, nil } -// fetchVouchersFromAPI calls the API to get the list of vouchers belonging to the user -func (as *AccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) { +// FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint +// Parameters: +// - publicKey: The public key associated with the account. +func (as *AccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { // TODO replace with the actual request once ready - mockJSON := `[ - { - "symbol": "MUMO", - "address": "0x078b3a26596218507781722A4e8825BFB9570Fba" - }, - { - "symbol": "SRF", - "address": "0x45d747172e77d55575c197CbA9451bC2CD8F4958" - }, - { - "symbol": "HALGAN", - "address": "0x12169Fb5931A599ad1283bb8311Dad54Feb51A28" + mockJSON := `{ + "ok": true, + "description": "Token holdings with current balances", + "result": { + "holdings": [ + { + "contractAddress": "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", + "tokenSymbol": "FSPTST", + "tokenDecimals": "6", + "balance": "8869964242" + }, + { + "contractAddress": "0x724F2910D790B54A39a7638282a45B1D83564fFA", + "tokenSymbol": "GEO", + "tokenDecimals": "6", + "balance": "9884" + }, + { + "contractAddress": "0x2105a206B7bec31E2F90acF7385cc8F7F5f9D273", + "tokenSymbol": "MFNK", + "tokenDecimals": "6", + "balance": "19788697" + }, + { + "contractAddress": "0x63DE2Ac8D1008351Cc69Fb8aCb94Ba47728a7E83", + "tokenSymbol": "MILO", + "tokenDecimals": "6", + "balance": "75" + }, + { + "contractAddress": "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", + "tokenSymbol": "SOHAIL", + "tokenDecimals": "6", + "balance": "27874115" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SRQIF", + "tokenDecimals": "6", + "balance": "2745987" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SRFI", + "tokenDecimals": "6", + "balance": "2745987" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SRFU", + "tokenDecimals": "6", + "balance": "2745987" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SRQF", + "tokenDecimals": "6", + "balance": "2745987" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SREF", + "tokenDecimals": "6", + "balance": "2745987" + } + ] } - ]` + }` // Unmarshal the JSON response - var holdings []models.VoucherHolding + var holdings models.VoucherHoldingResponse err := json.Unmarshal([]byte(mockJSON), &holdings) if err != nil { return nil, err } - return holdings, nil + return &holdings, nil } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 19c7d1a..d55abaf 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -3,6 +3,7 @@ package ussd import ( "bytes" "context" + "encoding/json" "fmt" "path" "regexp" @@ -232,22 +233,36 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt } // GetVoucherList fetches the list of vouchers and formats them -// checks whether they are synced internally before calling the API +// checks whether they are stored internally before calling the API func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } // check if the vouchers exist internally and if not // fetch from the API - // Fetch vouchers from API - vouchers, err := h.accountService.FetchVouchersFromAPI() + // Read vouchers from the store + store := h.userdataStore + voucherData, err := store.ReadEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST) if err != nil { - return res, fmt.Errorf("error fetching vouchers: %w", err) + return res, err + } + + // Unmarshal the stored JSON data into the correct struct + var vouchers []struct { + TokenSymbol string `json:"tokenSymbol"` + } + err = json.Unmarshal(voucherData, &vouchers) + if err != nil { + return res, fmt.Errorf("failed to unmarshal vouchers: %v", err) } var numberedVouchers []string for i, voucher := range vouchers { - numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.Symbol)) + numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) } res.Content = strings.Join(numberedVouchers, "\n") @@ -1007,3 +1022,41 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) 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) { + var res resource.Result + var err error + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + store := h.userdataStore + publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) + if err != nil { + return res, nil + } + + // Fetch vouchers from the API + vouchersResp, err := h.accountService.FetchVouchers(string(publicKey)) + if err != nil { + return res, nil + } + + // Convert only the list of holdings (vouchers) to JSON + voucherBytes, err := json.Marshal(vouchersResp.Result.Holdings) + if err != nil { + return res, nil + } + + // Store the voucher symbols in the userdataStore + err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, voucherBytes) + if err != nil { + return res, nil + } + + return res, nil +} diff --git a/internal/mocks/servicemock.go b/internal/mocks/servicemock.go index 30386b3..8fbde0f 100644 --- a/internal/mocks/servicemock.go +++ b/internal/mocks/servicemock.go @@ -25,7 +25,7 @@ func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, erro return args.String(0), args.Error(1) } -func (m *MockAccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) { +func (m *MockAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { args := m.Called() - return args.Get(0).([]models.VoucherHolding), args.Error(1) + return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1) } diff --git a/internal/models/vouchersresponse.go b/internal/models/vouchersresponse.go index 08967b7..010730f 100644 --- a/internal/models/vouchersresponse.go +++ b/internal/models/vouchersresponse.go @@ -1,7 +1,15 @@ package models -// VoucherHolding represents a single voucher holding -type VoucherHolding struct { - Symbol string `json:"symbol"` - Address string `json:"address"` -} \ No newline at end of file +// VoucherHoldingResponse represents a single voucher holding +type VoucherHoldingResponse struct { + Ok bool `json:"ok"` + Description string `json:"description"` + Result struct { + Holdings []struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` + } `json:"holdings"` + } `json:"result"` +} diff --git a/internal/utils/db.go b/internal/utils/db.go index 410da68..3080c1b 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -23,6 +23,7 @@ const ( DATA_RECIPIENT DATA_AMOUNT DATA_TEMPORARY_PIN + DATA_VOUCHER_LIST ) func typToBytes(typ DataTyp) []byte { diff --git a/services/registration/main.vis b/services/registration/main.vis index 1009039..88f8a42 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -1,5 +1,7 @@ LOAD check_balance 64 RELOAD check_balance +LOAD check_vouchers 10 +RELOAD check_vouchers MAP check_balance MOUT send 1 MOUT vouchers 2 -- 2.45.2 From 755899be4e6a1e0dc5c6b731910ade18d40284ad Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 7 Oct 2024 08:59:15 +0300 Subject: [PATCH 09/52] store the pre-rendered vouchers list --- internal/handlers/ussd/menuhandler.go | 32 +++++++-------------------- internal/storage/db.go | 28 +++++++++++------------ 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index d55abaf..4c09acb 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -3,7 +3,6 @@ package ussd import ( "bytes" "context" - "encoding/json" "fmt" "path" "regexp" @@ -251,20 +250,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) return res, err } - // Unmarshal the stored JSON data into the correct struct - var vouchers []struct { - TokenSymbol string `json:"tokenSymbol"` - } - err = json.Unmarshal(voucherData, &vouchers) - if err != nil { - return res, fmt.Errorf("failed to unmarshal vouchers: %v", err) - } - - var numberedVouchers []string - for i, voucher := range vouchers { - numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) - } - res.Content = strings.Join(numberedVouchers, "\n") + res.Content = string(voucherData) return res, nil } @@ -1027,8 +1013,6 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) // them to gdbm func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - var err error - sessionId, ok := ctx.Value("SessionId").(string) if !ok { return res, fmt.Errorf("missing session") @@ -1040,20 +1024,20 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } - // Fetch vouchers from the API + // Fetch vouchers from the API using the public key vouchersResp, err := h.accountService.FetchVouchers(string(publicKey)) if err != nil { return res, nil } - // Convert only the list of holdings (vouchers) to JSON - voucherBytes, err := json.Marshal(vouchersResp.Result.Holdings) - if err != nil { - return res, nil + var numberedVouchers []string + for i, voucher := range vouchersResp.Result.Holdings { + numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) } - // Store the voucher symbols in the userdataStore - err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, voucherBytes) + voucherList := strings.Join(numberedVouchers, "\n") + + err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, []byte(voucherList)) if err != nil { return res, nil } diff --git a/internal/storage/db.go b/internal/storage/db.go index b2ac6a9..060f0c0 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -12,32 +12,32 @@ const ( type SubPrefixDb struct { store db.Db - pfx []byte + pfx []byte } func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb { return &SubPrefixDb{ store: store, - pfx: pfx, + pfx: pfx, } } -func(s *SubPrefixDb) toKey(k []byte) []byte { - return append(s.pfx, k...) +func (s *SubPrefixDb) toKey(k []byte) []byte { + return append(s.pfx, k...) } -func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) { - s.store.SetPrefix(DATATYPE_USERSUB) +func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) { + s.store.SetPrefix(DATATYPE_USERSUB) key = s.toKey(key) - v, err := s.store.Get(ctx, key) - if err != nil { - return nil, err - } - return v, nil + v, err := s.store.Get(ctx, key) + if err != nil { + return nil, err + } + return v, nil } -func(s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error { - s.store.SetPrefix(DATATYPE_USERSUB) +func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error { + s.store.SetPrefix(DATATYPE_USERSUB) key = s.toKey(key) - return s.store.Put(ctx, key, val) + return s.store.Put(ctx, key, val) } -- 2.45.2 From e4ed9a65bbc777bf98232a0b887fab73530ae291 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 7 Oct 2024 13:49:12 +0300 Subject: [PATCH 10/52] store the symbols and balances --- internal/handlers/server/accountservice.go | 36 ---------------------- internal/handlers/ussd/menuhandler.go | 29 +++++++++++------ 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index d53db57..9f6ce03 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -139,42 +139,6 @@ func (as *AccountService) FetchVouchers(publicKey string) (*models.VoucherHoldin "tokenSymbol": "MILO", "tokenDecimals": "6", "balance": "75" - }, - { - "contractAddress": "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", - "tokenSymbol": "SOHAIL", - "tokenDecimals": "6", - "balance": "27874115" - }, - { - "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", - "tokenSymbol": "SRQIF", - "tokenDecimals": "6", - "balance": "2745987" - }, - { - "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", - "tokenSymbol": "SRFI", - "tokenDecimals": "6", - "balance": "2745987" - }, - { - "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", - "tokenSymbol": "SRFU", - "tokenDecimals": "6", - "balance": "2745987" - }, - { - "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", - "tokenSymbol": "SRQF", - "tokenDecimals": "6", - "balance": "2745987" - }, - { - "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", - "tokenSymbol": "SREF", - "tokenDecimals": "6", - "balance": "2745987" } ] } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 4c09acb..0ff1017 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -21,6 +21,8 @@ import ( "git.grassecon.net/urdt/ussd/internal/handlers/server" "git.grassecon.net/urdt/ussd/internal/utils" "gopkg.in/leonelquinteros/gotext.v1" + + "git.grassecon.net/urdt/ussd/internal/storage" ) var ( @@ -235,19 +237,17 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt // checks whether they are stored internally before calling the API func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } // check if the vouchers exist internally and if not // fetch from the API // Read vouchers from the store store := h.userdataStore - voucherData, err := store.ReadEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST) + prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) + + voucherData, err := prefixdb.Get(ctx, []byte("tokens")) if err != nil { - return res, err + return res, nil } res.Content = string(voucherData) @@ -1030,14 +1030,23 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } - var numberedVouchers []string + var numberedSymbols []string + var numberedBalances []string for i, voucher := range vouchersResp.Result.Holdings { - numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) + numberedSymbols = append(numberedSymbols, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) + numberedBalances = append(numberedBalances, fmt.Sprintf("%d:%s", i+1, voucher.Balance)) } - voucherList := strings.Join(numberedVouchers, "\n") + voucherSymbolList := strings.Join(numberedSymbols, "\n") + voucherBalanceList := strings.Join(numberedBalances, "\n") - err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, []byte(voucherList)) + prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) + err = prefixdb.Put(ctx, []byte("tokens"), []byte(voucherSymbolList)) + if err != nil { + return res, nil + } + + err = prefixdb.Put(ctx, []byte(voucherSymbolList), []byte(voucherBalanceList)) if err != nil { return res, nil } -- 2.45.2 From 06ebcc0f07886f05ece7c0c04e3a71ad6312c5e5 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 7 Oct 2024 16:15:13 +0300 Subject: [PATCH 11/52] added swahili template --- services/registration/select_voucher_swa | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 services/registration/select_voucher_swa diff --git a/services/registration/select_voucher_swa b/services/registration/select_voucher_swa new file mode 100644 index 0000000..b4720bf --- /dev/null +++ b/services/registration/select_voucher_swa @@ -0,0 +1,2 @@ +Chagua nambari au ishara kutoka kwa salio zako: +{{.get_vouchers}} \ No newline at end of file -- 2.45.2 From cb4a52e4f245bddf9755434004a4cdb7fffd9f65 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 7 Oct 2024 16:16:57 +0300 Subject: [PATCH 12/52] view a selected voucher and verify the PIN --- internal/handlers/handlerservice.go | 1 + internal/handlers/ussd/menuhandler.go | 68 ++++++++++++++++++++++++ services/registration/my_vouchers.vis | 3 -- services/registration/pp.csv | 2 +- services/registration/select_voucher.vis | 6 ++- services/registration/view_voucher | 2 + services/registration/view_voucher.vis | 11 ++++ services/registration/view_voucher_swa | 2 + services/registration/voucher_set | 1 + services/registration/voucher_set.vis | 5 ++ services/registration/voucher_set_swa | 1 + 11 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 services/registration/view_voucher create mode 100644 services/registration/view_voucher.vis create mode 100644 services/registration/view_voucher_swa create mode 100644 services/registration/voucher_set create mode 100644 services/registration/voucher_set.vis create mode 100644 services/registration/voucher_set_swa diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 8f0ff16..4c152b9 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -94,6 +94,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp) ls.DbRs.AddLocalFunc("check_vouchers", ussdHandlers.CheckVouchers) ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList) + ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher) return ussdHandlers, nil } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 0ff1017..c254401 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1053,3 +1053,71 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } + +// ViewVoucher retrieves the token holding and balance from the subprefixDB +func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + var err error + inputStr := string(input) + + if inputStr == "0" || inputStr == "00" { + return res, nil + } + + 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 + voucherSymbolList, err := prefixdb.Get(ctx, []byte("tokens")) + if err != nil { + return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err) + } + + // Retrieve the voucher balance list + voucherBalanceList, err := prefixdb.Get(ctx, []byte(voucherSymbolList)) + if err != nil { + return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err) + } + + // Convert the symbol and balance lists from byte arrays to strings + voucherSymbols := string(voucherSymbolList) + voucherBalances := string(voucherBalanceList) + + // Split the lists into slices for processing + symbols := strings.Split(voucherSymbols, "\n") + balances := strings.Split(voucherBalances, "\n") + + var matchedSymbol, matchedBalance string + + for i, symbol := range symbols { + symbolParts := strings.SplitN(symbol, ":", 2) + if len(symbolParts) != 2 { + continue + } + voucherNum := symbolParts[0] + voucherSymbol := symbolParts[1] + + // Check if input matches either the number or the symbol + if inputStr == voucherNum || strings.EqualFold(inputStr, voucherSymbol) { + matchedSymbol = voucherSymbol + // Ensure there's a corresponding balance + if i < len(balances) { + matchedBalance = strings.SplitN(balances[i], ":", 2)[1] // Extract balance after the "x:balance" format + } + break + } + } + + // If a match is found, return the symbol and balance + if matchedSymbol != "" && matchedBalance != "" { + res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance) + res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) + } else { + res.FlagSet = append(res.FlagSet, flag_incorrect_voucher) + } + + return res, nil +} diff --git a/services/registration/my_vouchers.vis b/services/registration/my_vouchers.vis index b70dbfa..9702573 100644 --- a/services/registration/my_vouchers.vis +++ b/services/registration/my_vouchers.vis @@ -4,6 +4,3 @@ MOUT back 0 HALT INCMP _ 0 INCMP select_voucher 1 - - - diff --git a/services/registration/pp.csv b/services/registration/pp.csv index fd552e4..2a7bb21 100644 --- a/services/registration/pp.csv +++ b/services/registration/pp.csv @@ -12,5 +12,5 @@ flag,flag_invalid_amount,18,this is set when the given transaction amount is inv flag,flag_incorrect_pin,19,this is set when the provided PIN is invalid or does not match the current account's PIN flag,flag_valid_pin,20,this is set when the given PIN is valid flag,flag_allow_update,21,this is set to allow a user to update their profile data -flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth +flag,flag_incorrect_voucher,22,this is set when the selected voucher is invalid flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid diff --git a/services/registration/select_voucher.vis b/services/registration/select_voucher.vis index 4fb1d40..50b99ad 100644 --- a/services/registration/select_voucher.vis +++ b/services/registration/select_voucher.vis @@ -1,11 +1,15 @@ LOAD get_vouchers 0 MAP get_vouchers MOUT back 0 -MOUT quit 9 +MOUT quit 00 MNEXT next 11 MPREV prev 22 HALT +LOAD view_voucher 80 +RELOAD view_voucher +CATCH . flag_incorrect_voucher 1 INCMP _ 0 INCMP quit 9 INCMP > 11 INCMP < 22 +INCMP view_voucher * diff --git a/services/registration/view_voucher b/services/registration/view_voucher new file mode 100644 index 0000000..3940982 --- /dev/null +++ b/services/registration/view_voucher @@ -0,0 +1,2 @@ +Enter PIN to confirm selection: +{{.view_voucher}} \ No newline at end of file diff --git a/services/registration/view_voucher.vis b/services/registration/view_voucher.vis new file mode 100644 index 0000000..ee8bf85 --- /dev/null +++ b/services/registration/view_voucher.vis @@ -0,0 +1,11 @@ +RELOAD view_voucher +MAP view_voucher +MOUT back 0 +MOUT quit 9 +LOAD authorize_account 6 +HALT +RELOAD authorize_account +CATCH incorrect_pin flag_incorrect_pin 1 +INCMP _ 0 +INCMP quit 9 +INCMP voucher_set * diff --git a/services/registration/view_voucher_swa b/services/registration/view_voucher_swa new file mode 100644 index 0000000..485e2ef --- /dev/null +++ b/services/registration/view_voucher_swa @@ -0,0 +1,2 @@ +Weka PIN ili kuthibitisha chaguo: +{{.view_voucher}} \ No newline at end of file diff --git a/services/registration/voucher_set b/services/registration/voucher_set new file mode 100644 index 0000000..838c1ef --- /dev/null +++ b/services/registration/voucher_set @@ -0,0 +1 @@ +Success! symbol 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 new file mode 100644 index 0000000..832ef22 --- /dev/null +++ b/services/registration/voucher_set.vis @@ -0,0 +1,5 @@ +MOUT back 0 +MOUT quit 9 +HALT +INCMP ^ 0 +INCMP quit 9 diff --git a/services/registration/voucher_set_swa b/services/registration/voucher_set_swa new file mode 100644 index 0000000..320a315 --- /dev/null +++ b/services/registration/voucher_set_swa @@ -0,0 +1 @@ +Hongera! symbol ni Sarafu inayotumika sasa. \ No newline at end of file -- 2.45.2 From 7b374ad801a37e1b88f2c45ce3b7d92852a68880 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 8 Oct 2024 13:41:09 +0300 Subject: [PATCH 13/52] define importable token list --- internal/handlers/server/accountservice.go | 22 ++++--- internal/handlers/ussd/menuhandler.go | 68 +++++++++++++--------- sample_tokens.json | 44 ++++++++++++++ 3 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 sample_tokens.json diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index f4375a1..88c3354 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io" "net/http" + "os" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/internal/models" @@ -18,8 +19,6 @@ type AccountServiceInterface interface { type AccountService struct { } - - // CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID. // // Parameters: @@ -27,12 +26,10 @@ type AccountService struct { // CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the // AccountResponse struct can be used here to check the account status during a transaction. // -// // Returns: // - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string. // - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data. // If no error occurs, this will be nil. -// func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) { resp, err := http.Get(config.TrackStatusURL + trackingId) if err != nil { @@ -56,7 +53,6 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) return status, nil } - // CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint. // Parameters: // - publicKey: The public key associated with the account whose balance needs to be checked. @@ -83,8 +79,7 @@ func (as *AccountService) CheckBalance(publicKey string) (string, error) { return balance, nil } - -//CreateAccount creates a new account in the custodial system. +// CreateAccount creates a new account in the custodial system. // Returns: // - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account. // If there is an error during the request or processing, this will be nil. @@ -110,3 +105,16 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) { return &accountResp, nil } + +func GetTokenList() (*models.ApiResponse, error) { + file, err := os.Open("sample_tokens.json") + if err != nil { + return nil, err + } + defer file.Close() + var apiResponse models.ApiResponse + if err := json.NewDecoder(file).Decode(&apiResponse); err != nil { + return nil, err + } + return &apiResponse, nil +} diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index e998bd5..a7a58bf 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -256,36 +256,50 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt func (h *Handlers) GetVoucherList(ctx context.Context,sym string,input []byte) (resource.Result,error){ - var res resource.Result - vouchers := []string{ - "SRF", - "CRF", - "VCF", - "VSAPA", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", - "FSTMP", - "FSAW", - "PTAQ", - "VCRXT", - "VSGAQ", - "QPWIQQ", + res := resource.Result{} + //as := h.accountService.(*server.AccountService) + tokenList,err := server.GetTokenList() + fmt.Println("Error here:",err) + if err != nil { + return res,err } - var numberedVouchers []string - for i, voucher := range vouchers { - numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher)) + holdings := tokenList.Result.Holdings + fmt.Println("TokenList:",tokenList.Result.Holdings) + + // vouchers := []string{ + // "SRF", + // "CRF", + // "VCF", + // "VSAPA", + // "FSTMP", + // "FSAW", + // "PTAQ", + // "VCRXT", + // "VSGAQ", + // "QPWIQQ", + // "FSTMP", + // "FSAW", + // "PTAQ", + // "VCRXT", + // "VSGAQ", + // "QPWIQQ", + // "FSTMP", + // "FSAW", + // "PTAQ", + // "VCRXT", + // "VSGAQ", + // "QPWIQQ", + // } + var numberedVouchers []string + for i,token := range holdings { + numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, token.TokenSymbol)) } + + // var numberedVouchers []string + // for i, voucher := range vouchers { + // numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher)) + // } res.Content = strings.Join(numberedVouchers,"\n") return res,nil diff --git a/sample_tokens.json b/sample_tokens.json new file mode 100644 index 0000000..07126ed --- /dev/null +++ b/sample_tokens.json @@ -0,0 +1,44 @@ +{ + "ok": true, + "description": "Token holdings with current balances", + "result": { + "holdings": [ + { + "contractAddress": "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", + "tokenSymbol": "FSPTST", + "tokenDecimals": "6", + "balance": "8869964242" + }, + { + "contractAddress": "0x724F2910D790B54A39a7638282a45B1D83564fFA", + "tokenSymbol": "GEO", + "tokenDecimals": "6", + "balance": "9884" + }, + { + "contractAddress": "0x2105a206B7bec31E2F90acF7385cc8F7F5f9D273", + "tokenSymbol": "MFNK", + "tokenDecimals": "6", + "balance": "19788697" + }, + { + "contractAddress": "0x63DE2Ac8D1008351Cc69Fb8aCb94Ba47728a7E83", + "tokenSymbol": "MILO", + "tokenDecimals": "6", + "balance": "75" + }, + { + "contractAddress": "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", + "tokenSymbol": "SOHAIL", + "tokenDecimals": "6", + "balance": "27874115" + }, + { + "contractAddress": "0x45d747172e77d55575c197CbA9451bC2CD8F4958", + "tokenSymbol": "SRF", + "tokenDecimals": "6", + "balance": "2745987" + } + ] + } + } -- 2.45.2 From 4b6fd35e7a3c66ea3f28f03b6abd212b38bd0007 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 8 Oct 2024 13:43:57 +0300 Subject: [PATCH 14/52] define importable voucher response --- internal/models/tokenresponse.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 internal/models/tokenresponse.go diff --git a/internal/models/tokenresponse.go b/internal/models/tokenresponse.go new file mode 100644 index 0000000..d243d93 --- /dev/null +++ b/internal/models/tokenresponse.go @@ -0,0 +1,18 @@ +package models + +type ApiResponse struct { + OK bool `json:"ok"` + Description string `json:"description"` + Result Result `json:"result"` +} + +type Result struct { + Holdings []Holding `json:"holdings"` +} + +type Holding struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` +} -- 2.45.2 From 2c98a8e1331861a0c1754ed62ea72baec1853af3 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 8 Oct 2024 14:34:21 +0300 Subject: [PATCH 15/52] read token list from json file --- internal/handlers/server/accountservice.go | 53 ++-------------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index 2b0efc0..b83507f 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -111,59 +111,16 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) { // Parameters: // - publicKey: The public key associated with the account. func (as *AccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { - // TODO replace with the actual request once ready - mockJSON := `{ - "ok": true, - "description": "Token holdings with current balances", - "result": { - "holdings": [ - { - "contractAddress": "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", - "tokenSymbol": "FSPTST", - "tokenDecimals": "6", - "balance": "8869964242" - }, - { - "contractAddress": "0x724F2910D790B54A39a7638282a45B1D83564fFA", - "tokenSymbol": "GEO", - "tokenDecimals": "6", - "balance": "9884" - }, - { - "contractAddress": "0x2105a206B7bec31E2F90acF7385cc8F7F5f9D273", - "tokenSymbol": "MFNK", - "tokenDecimals": "6", - "balance": "19788697" - }, - { - "contractAddress": "0x63DE2Ac8D1008351Cc69Fb8aCb94Ba47728a7E83", - "tokenSymbol": "MILO", - "tokenDecimals": "6", - "balance": "75" - } - ] - } - }` - - // Unmarshal the JSON response - var holdings models.VoucherHoldingResponse - err := json.Unmarshal([]byte(mockJSON), &holdings) - if err != nil { - return nil, err - } - - return &holdings, nil -} - -func GetTokenList() (*models.ApiResponse, error) { file, err := os.Open("sample_tokens.json") if err != nil { return nil, err } defer file.Close() - var apiResponse models.ApiResponse - if err := json.NewDecoder(file).Decode(&apiResponse); err != nil { + var holdings models.VoucherHoldingResponse + + if err := json.NewDecoder(file).Decode(&holdings); err != nil { return nil, err } - return &apiResponse, nil + return &holdings, nil } + -- 2.45.2 From 9ccb6cc066cbbb1f0da585d6d1f6f0941901b6e3 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 9 Oct 2024 15:05:59 +0300 Subject: [PATCH 16/52] use 99 as the quit --- internal/handlers/ussd/menuhandler.go | 2 +- services/registration/select_voucher.vis | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index c254401..b634f91 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1060,7 +1060,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r var err error inputStr := string(input) - if inputStr == "0" || inputStr == "00" { + if inputStr == "0" || inputStr == "99" { return res, nil } diff --git a/services/registration/select_voucher.vis b/services/registration/select_voucher.vis index 50b99ad..08aa434 100644 --- a/services/registration/select_voucher.vis +++ b/services/registration/select_voucher.vis @@ -1,7 +1,7 @@ LOAD get_vouchers 0 MAP get_vouchers MOUT back 0 -MOUT quit 00 +MOUT quit 99 MNEXT next 11 MPREV prev 22 HALT @@ -9,7 +9,7 @@ LOAD view_voucher 80 RELOAD view_voucher CATCH . flag_incorrect_voucher 1 INCMP _ 0 -INCMP quit 9 +INCMP quit 99 INCMP > 11 INCMP < 22 INCMP view_voucher * -- 2.45.2 From 6c904a8b3fab516bb49d1a950cda05785883fc0a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 9 Oct 2024 15:38:19 +0300 Subject: [PATCH 17/52] add the temporary and active symbol --- internal/utils/db.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/utils/db.go b/internal/utils/db.go index 3080c1b..f70e08b 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -24,6 +24,8 @@ const ( DATA_AMOUNT DATA_TEMPORARY_PIN DATA_VOUCHER_LIST + DATA_TEMPORARY_SYM + DATA_ACTIVE_SYM ) func typToBytes(typ DataTyp) []byte { -- 2.45.2 From 9ad7d5a5226619eac07e99bcd4ffe1f3ec89548b Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 9 Oct 2024 15:39:06 +0300 Subject: [PATCH 18/52] 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 -- 2.45.2 From 7c08a0f0afad4f01b52321f17e4ef4a7f458a635 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 10 Oct 2024 14:48:23 +0300 Subject: [PATCH 19/52] 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 { -- 2.45.2 From ea2df552958f5b9fb53dcc916f4ac9a635f28547 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 10 Oct 2024 15:18:13 +0300 Subject: [PATCH 20/52] use go-vise v0.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index e3a441a..cdadce5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac + git.defalsify.org/vise.git v0.2.0 github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index 3f7a262..d8a1553 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb h1:6P4kxihc git.defalsify.org/vise.git v0.1.0-rc.3.0.20240923162317-c20d557a3dbb/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac h1:D4KI22KWXT8S66sHIjWhTBX6SXRfnd7j8VErq3PPbok= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240926120105-89b0529cf7ac/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.2.0 h1:X2ZgiGRq4C+9qOlDMP0b/oE5QHjVQNT4aEFZB88ST0Q= +git.defalsify.org/vise.git v0.2.0/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= -- 2.45.2 From 6c93fb76b11eacfbd44a27436ae0d6ea1a0bbcb8 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 11 Oct 2024 09:36:43 +0300 Subject: [PATCH 21/52] correctly added the flag and the flag count --- cmd/africastalking/main.go | 2 +- cmd/async/main.go | 2 +- cmd/http/main.go | 2 +- cmd/main.go | 2 +- services/registration/pp.csv | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index c24c4b1..4dbf555 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -87,7 +87,7 @@ func main() { cfg := engine.Config{ Root: "root", OutputSize: uint32(size), - FlagCount: uint32(16), + FlagCount: uint32(17), } if engineDebug { diff --git a/cmd/async/main.go b/cmd/async/main.go index 09236fd..eda54f8 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -60,7 +60,7 @@ func main() { cfg := engine.Config{ Root: "root", OutputSize: uint32(size), - FlagCount: uint32(16), + FlagCount: uint32(17), } if engineDebug { diff --git a/cmd/http/main.go b/cmd/http/main.go index 6b868ed..3708f92 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -48,7 +48,7 @@ func main() { cfg := engine.Config{ Root: "root", OutputSize: uint32(size), - FlagCount: uint32(16), + FlagCount: uint32(17), } if engineDebug { diff --git a/cmd/main.go b/cmd/main.go index 9db5e0a..b0cdb80 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -40,7 +40,7 @@ func main() { Root: "root", SessionId: sessionId, OutputSize: uint32(size), - FlagCount: uint32(16), + FlagCount: uint32(17), } resourceDir := scriptDir diff --git a/services/registration/pp.csv b/services/registration/pp.csv index 2a7bb21..1391771 100644 --- a/services/registration/pp.csv +++ b/services/registration/pp.csv @@ -12,5 +12,6 @@ flag,flag_invalid_amount,18,this is set when the given transaction amount is inv flag,flag_incorrect_pin,19,this is set when the provided PIN is invalid or does not match the current account's PIN flag,flag_valid_pin,20,this is set when the given PIN is valid flag,flag_allow_update,21,this is set to allow a user to update their profile data -flag,flag_incorrect_voucher,22,this is set when the selected voucher is invalid +flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid +flag,flag_incorrect_voucher,24,this is set when the selected voucher is invalid -- 2.45.2 From 8f834b3d76e44a8c854eefccfa19bb8be397d1da Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 11 Oct 2024 09:41:47 +0300 Subject: [PATCH 22/52] ensure that a user is authorized --- services/registration/voucher_set.vis | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/registration/voucher_set.vis b/services/registration/voucher_set.vis index e3e81f5..e75c693 100644 --- a/services/registration/voucher_set.vis +++ b/services/registration/voucher_set.vis @@ -1,3 +1,6 @@ +LOAD reset_incorrect 6 +CATCH incorrect_pin flag_incorrect_pin 1 +CATCH _ flag_account_authorized 0 LOAD set_voucher 12 MAP set_voucher MOUT back 0 -- 2.45.2 From 672eebb8fb43ac8947730532a5d0830c1da93587 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 11 Oct 2024 09:42:08 +0300 Subject: [PATCH 23/52] display the balance based on the symbol --- internal/handlers/ussd/menuhandler.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index e459975..fb1db3f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -660,11 +660,28 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( return res, err } - balance, err := h.accountService.CheckBalance(string(publicKey)) + // check if the user has an active sym + activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) if err != nil { - return res, 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 + } } - res.Content = balance + + activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) + if err != nil { + return res, err + } + + res.Content = fmt.Sprintf("%s %s", activeBal, activeSym) return res, nil } -- 2.45.2 From a5b1c5b74e9c7da4f650a9c6d1b77f4d1ae4f379 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 14:32:40 +0300 Subject: [PATCH 24/52] cleaned up the code --- internal/handlers/ussd/menuhandler.go | 65 +++++++++++++-------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index fb1db3f..4196325 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -233,28 +233,6 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt return res, nil } -// GetVoucherList fetches the list of vouchers and formats them -// checks whether they are stored internally before calling the API -func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - - // check if the vouchers exist internally and if not - // fetch from the API - - // Read vouchers from the store - store := h.userdataStore - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) - - voucherData, err := prefixdb.Get(ctx, []byte("tokens")) - if err != nil { - return res, nil - } - - res.Content = string(voucherData) - - return res, nil -} - func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result sessionId, ok := ctx.Value("SessionId").(string) @@ -1057,13 +1035,13 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) voucherSymbolList := strings.Join(numberedSymbols, "\n") voucherBalanceList := strings.Join(numberedBalances, "\n") - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) - err = prefixdb.Put(ctx, []byte("tokens"), []byte(voucherSymbolList)) + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList)) if err != nil { return res, nil } - err = prefixdb.Put(ctx, []byte(voucherSymbolList), []byte(voucherBalanceList)) + err = prefixdb.Put(ctx, []byte("bal"), []byte(voucherBalanceList)) if err != nil { return res, nil } @@ -1071,6 +1049,24 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } +// GetVoucherList fetches the list of vouchers and formats them +func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + + // Read vouchers from the store + store := h.userdataStore + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + + voucherData, err := prefixdb.Get(ctx, []byte("sym")) + if err != nil { + return res, nil + } + + res.Content = string(voucherData) + + return res, nil +} + // ViewVoucher retrieves the token holding and balance from the subprefixDB func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -1082,25 +1078,25 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r return res, fmt.Errorf("missing session") } + flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher") + inputStr := string(input) if inputStr == "0" || inputStr == "99" { + res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) return res, nil } - flag_incorrect_voucher, _ := h.flagManager.GetFlag("flag_incorrect_voucher") - - // Initialize the store and prefix database - prefixdb := storage.NewSubPrefixDb(store, []byte("token_holdings")) + prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) // Retrieve the voucher symbol list - voucherSymbolList, err := prefixdb.Get(ctx, []byte("tokens")) + voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym")) if err != nil { return res, fmt.Errorf("failed to retrieve voucher symbol list: %v", err) } // Retrieve the voucher balance list - voucherBalanceList, err := prefixdb.Get(ctx, []byte(voucherSymbolList)) + voucherBalanceList, err := prefixdb.Get(ctx, []byte("bal")) if err != nil { return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err) } @@ -1134,7 +1130,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r } } - // If a match is found, write the temporary sym , then 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 { @@ -1153,8 +1149,9 @@ 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 +// SetVoucher retrieves the temporary sym and balance, +// sets them as the active data and +// clears the temporary data func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result var err error -- 2.45.2 From e9684fcf45b4fc3873d84dda5d932bf7721b193b Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 16:28:02 +0300 Subject: [PATCH 25/52] check whether the active symbol is empty --- internal/handlers/ussd/menuhandler.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 4196325..d6d8102 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -652,6 +652,20 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( 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) -- 2.45.2 From b6d24bf92938fce74eada07f3e454fcf71e32fa0 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 16:29:12 +0300 Subject: [PATCH 26/52] update the TestCheckBalance --- internal/handlers/ussd/menuhandler_test.go | 83 ++++++++++++++++------ 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 1ce8b51..21edb3d 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1560,33 +1560,72 @@ func TestValidateRecipient(t *testing.T) { } func TestCheckBalance(t *testing.T) { - - mockDataStore := new(mocks.MockUserDataStore) - sessionId := "session123" - publicKey := "0X13242618721" - balance := "0.003 CELO" - - expectedResult := resource.Result{ - Content: "0.003 CELO", + tests := []struct { + name string + sessionId string + publicKey string + activeSym string + activeBal string + expectedResult resource.Result + expectError bool + }{ + { + name: "User with active sym", + sessionId: "session456", + publicKey: "0X98765432109", + activeSym: "ETH", + activeBal: "1.5", + expectedResult: resource.Result{Content: "1.5 ETH"}, + expectError: false, + }, + { + name: "User without active sym", + sessionId: "session123", + publicKey: "0X13242618721", + activeSym: "", + activeBal: "", + expectedResult: resource.Result{Content: "0.003 CELO"}, + expectError: false, + }, } - mockCreateAccountService := new(mocks.MockAccountService) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockDataStore := new(mocks.MockUserDataStore) + mockAccountService := new(mocks.MockAccountService) + ctx := context.WithValue(context.Background(), "SessionId", tt.sessionId) - ctx := context.WithValue(context.Background(), "SessionId", sessionId) + h := &Handlers{ + userdataStore: mockDataStore, + accountService: mockAccountService, + } - h := &Handlers{ - userdataStore: mockDataStore, - accountService: mockCreateAccountService, - //flagManager: fm.parser, + // Mock calls for public key + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(tt.publicKey), nil) + + if tt.activeSym == "" { + // Mock for user without active sym + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte{}, db.ErrNotFound{}) + mockAccountService.On("CheckBalance", tt.publicKey).Return("0.003 CELO", nil) + } else { + // Mock for user with active sym + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil) + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil) + } + + res, err := h.CheckBalance(ctx, "check_balance", []byte("123456")) + + if tt.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.expectedResult, res, "Result should match expected output") + } + + mockDataStore.AssertExpectations(t) + mockAccountService.AssertExpectations(t) + }) } - //mock call operations - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) - mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(balance, nil) - - res, _ := h.CheckBalance(ctx, "check_balance", []byte("123456")) - - assert.Equal(t, res, expectedResult, "Result should contain flag(s) that have been reset") - } func TestGetProfile(t *testing.T) { -- 2.45.2 From a9f986797677d6430ad7d783dc0241b35ca62674 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 17:36:00 +0300 Subject: [PATCH 27/52] added the TestSetVoucher --- internal/handlers/ussd/menuhandler_test.go | 35 +++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 21edb3d..9159722 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1782,5 +1782,38 @@ func TestConfirmPin(t *testing.T) { }) } - +} + +func TestSetVoucher(t *testing.T) { + mockDataStore := new(mocks.MockUserDataStore) + + sessionId := "session123" + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + + temporarySym := []byte("tempSym") + temporaryBal := []byte("tempBal") + + // Set expectations for the mock data store + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM).Return(temporarySym, nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL).Return(temporaryBal, nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM, temporarySym).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL, temporaryBal).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte("")).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL, []byte("")).Return(nil) + + h := &Handlers{ + userdataStore: mockDataStore, + } + + // Call the method under test + res, err := h.SetVoucher(ctx, "someSym", []byte{}) + + // Assert that no errors occurred + assert.NoError(t, err) + + // Assert that the result content is correct + assert.Equal(t, string(temporarySym), res.Content) + + // Assert that expectations were met + mockDataStore.AssertExpectations(t) } -- 2.45.2 From 7fe8f0b7d578d1541207a8604a909c08b8124a68 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 18:03:13 +0300 Subject: [PATCH 28/52] updated the args under FetchVouchers --- internal/mocks/servicemock.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/mocks/servicemock.go b/internal/mocks/servicemock.go index 8fbde0f..f8f638a 100644 --- a/internal/mocks/servicemock.go +++ b/internal/mocks/servicemock.go @@ -26,6 +26,6 @@ func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, erro } func (m *MockAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { - args := m.Called() + args := m.Called(publicKey) return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1) } -- 2.45.2 From f5dbfe553d90ee2e57cf7371ff66486828ccfbea Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 20:06:00 +0300 Subject: [PATCH 29/52] use the check_balance for the max_amount --- internal/handlers/handlerservice.go | 1 - services/registration/amount | 2 +- services/registration/amount.vis | 4 ++-- services/registration/amount_swa | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 3b79ea2..0d891f1 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -69,7 +69,6 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) { ls.DbRs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance) ls.DbRs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient) ls.DbRs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset) - ls.DbRs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount) ls.DbRs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount) ls.DbRs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount) ls.DbRs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient) diff --git a/services/registration/amount b/services/registration/amount index 9142aba..d3e8602 100644 --- a/services/registration/amount +++ b/services/registration/amount @@ -1,2 +1,2 @@ -Maximum amount: {{.max_amount}} +Maximum amount: {{.check_balance}} Enter amount: \ No newline at end of file diff --git a/services/registration/amount.vis b/services/registration/amount.vis index b491fab..5a02ff9 100644 --- a/services/registration/amount.vis +++ b/services/registration/amount.vis @@ -1,6 +1,6 @@ LOAD reset_transaction_amount 0 -LOAD max_amount 10 -MAP max_amount +LOAD check_balance 48 +MAP check_balance MOUT back 0 HALT LOAD validate_amount 64 diff --git a/services/registration/amount_swa b/services/registration/amount_swa index 0c8cf01..d071f63 100644 --- a/services/registration/amount_swa +++ b/services/registration/amount_swa @@ -1,2 +1,2 @@ -Kiwango cha juu: {{.max_amount}} +Kiwango cha juu: {{.check_balance}} Weka kiwango: \ No newline at end of file -- 2.45.2 From 7df77a134307823e058ca05541a004283189d0e1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 12 Oct 2024 20:07:06 +0300 Subject: [PATCH 30/52] updated the ValidateAmount to also check the active symbol, updated tests --- internal/handlers/ussd/menuhandler.go | 88 +++++++++------------- internal/handlers/ussd/menuhandler_test.go | 76 +++++++++---------- 2 files changed, 72 insertions(+), 92 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index d6d8102..3e164af 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -761,74 +761,60 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input return res, nil } -// MaxAmount gets the current balance from the API and sets it as -// the result content. -func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - var err error - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - store := h.userdataStore - publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) - - balance, err := h.accountService.CheckBalance(string(publicKey)) - if err != nil { - return res, nil - } - - res.Content = balance - - return res, nil -} - // ValidateAmount ensures that the given input is a valid amount and that // it is not more than the current balance. func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - var err error sessionId, ok := ctx.Value("SessionId").(string) if !ok { return res, fmt.Errorf("missing session") } - flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount") - store := h.userdataStore - publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) - - amountStr := string(input) - - balanceStr, err := h.accountService.CheckBalance(string(publicKey)) + publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) if err != nil { return res, err } - res.Content = balanceStr - // Parse the balance - balanceParts := strings.Split(balanceStr, " ") - if len(balanceParts) != 2 { - return res, fmt.Errorf("unexpected balance format: %s", balanceStr) - } - balanceValue, err := strconv.ParseFloat(balanceParts[0], 64) - if err != nil { - return res, fmt.Errorf("failed to parse balance: %v", err) + // retrieve the active symbol + activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) + useActiveSymbol := err == nil && len(activeSym) > 0 + + var balanceValue float64 + if useActiveSymbol { + // If active symbol is set, retrieve its balance + activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) + if err != nil { + return res, fmt.Errorf("failed to get active balance: %v", err) + } + balanceValue, err = strconv.ParseFloat(string(activeBal), 64) + if err != nil { + return res, fmt.Errorf("failed to parse active balance: %v", err) + } + } else { + // If no active symbol, use the current balance from the API + balanceStr, err := h.accountService.CheckBalance(string(publicKey)) + if err != nil { + return res, fmt.Errorf("failed to check balance: %v", err) + } + res.Content = balanceStr + + // Parse the balance string + balanceParts := strings.Split(balanceStr, " ") + if len(balanceParts) != 2 { + return res, fmt.Errorf("unexpected balance format: %s", balanceStr) + } + balanceValue, err = strconv.ParseFloat(balanceParts[0], 64) + if err != nil { + return res, fmt.Errorf("failed to parse balance: %v", err) + } } - // Extract numeric part from input - re := regexp.MustCompile(`^(\d+(\.\d+)?)\s*(?:CELO)?$`) - matches := re.FindStringSubmatch(strings.TrimSpace(amountStr)) - if len(matches) < 2 { - res.FlagSet = append(res.FlagSet, flag_invalid_amount) - res.Content = amountStr - return res, nil - } - - inputAmount, err := strconv.ParseFloat(matches[1], 64) + // Extract numeric part from the input amount + amountStr := strings.TrimSpace(string(input)) + inputAmount, err := strconv.ParseFloat(amountStr, 64) if err != nil { res.FlagSet = append(res.FlagSet, flag_invalid_amount) res.Content = amountStr @@ -841,12 +827,12 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) return res, nil } - res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places err = store.WriteEntry(ctx, sessionId, utils.DATA_AMOUNT, []byte(amountStr)) if err != nil { return res, err } + res.Content = fmt.Sprintf("%.3f", inputAmount) return res, nil } diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 9159722..4fd9d22 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -434,34 +434,6 @@ func TestCheckIdentifier(t *testing.T) { } } -func TestMaxAmount(t *testing.T) { - mockStore := new(mocks.MockUserDataStore) - mockCreateAccountService := new(mocks.MockAccountService) - - // Define test data - sessionId := "session123" - ctx := context.WithValue(context.Background(), "SessionId", sessionId) - publicKey := "0xcasgatweksalw1018221" - expectedBalance := "0.003CELO" - - // Set up the expected behavior of the mock - mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) - mockCreateAccountService.On("CheckBalance", publicKey).Return(expectedBalance, nil) - - // Create the Handlers instance with the mock store - h := &Handlers{ - userdataStore: mockStore, - accountService: mockCreateAccountService, - } - - // Call the method - res, _ := h.MaxAmount(ctx, "max_amount", []byte("check_balance")) - - //Assert that the balance that was set as the result content is what was returned by Check Balance - assert.Equal(t, expectedBalance, res.Content) - -} - func TestGetSender(t *testing.T) { mockStore := new(mocks.MockUserDataStore) @@ -1444,59 +1416,81 @@ func TestValidateAmount(t *testing.T) { name string input []byte publicKey []byte + activeSym []byte + activeBal []byte balance string expectedResult resource.Result }{ { - name: "Test with valid amount", + name: "Test with valid amount and active symbol", input: []byte("0.001"), - balance: "0.003 CELO", publicKey: []byte("0xrqeqrequuq"), + activeSym: []byte("CELO"), + activeBal: []byte("0.003"), expectedResult: resource.Result{ Content: "0.001", }, }, { - name: "Test with amount larger than balance", + name: "Test with amount larger than active balance", input: []byte("0.02"), - balance: "0.003 CELO", publicKey: []byte("0xrqeqrequuq"), + activeSym: []byte("CELO"), + activeBal: []byte("0.003"), expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, Content: "0.02", }, }, { - name: "Test with invalid amount", + name: "Test with invalid amount format", input: []byte("0.02ms"), - balance: "0.003 CELO", publicKey: []byte("0xrqeqrequuq"), + balance: "0.003 CELO", expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, Content: "0.02ms", }, }, + { + name: "Test fallback to current balance without active symbol", + input: []byte("0.001"), + publicKey: []byte("0xrqeqrequuq"), + balance: "0.003 CELO", + expectedResult: resource.Result{ + Content: "0.001", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - + // Mock behavior for public key retrieval mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil) - mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil) + + // Mock behavior for active symbol and balance retrieval (if present) + if len(tt.activeSym) > 0 { + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(tt.activeSym, nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL).Return(tt.activeBal, nil) + } else { + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(nil, fmt.Errorf("not found")) + mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil) + } + + // Mock behavior for storing the amount (if valid) mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe() // Call the method under test res, _ := h.ValidateAmount(ctx, "test_validate_amount", tt.input) - // Assert that no errors occurred + // Assert no errors occurred assert.NoError(t, err) - //Assert that the account created flag has been set to the result - assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result") + // Assert the result matches the expected result + assert.Equal(t, tt.expectedResult, res, "Expected result should match actual result") - // Assert that expectations were met + // Assert all expectations were met mockDataStore.AssertExpectations(t) - }) } } -- 2.45.2 From 8ed98b3e6cc3e3e0d2baf6eda6a26ff919a333b5 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 14:53:00 +0300 Subject: [PATCH 31/52] resolve case issue --- internal/handlers/ussd/menuhandler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 4fd9d22..f36814a 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -171,7 +171,7 @@ func TestSaveFamilyname(t *testing.T) { mockStore.AssertExpectations(t) } -func TestSaveTemporaryPIn(t *testing.T) { +func TestSaveTemporaryPin(t *testing.T) { fm, err := NewFlagManager(flagsPath) mockStore := new(mocks.MockUserDataStore) if err != nil { -- 2.45.2 From 6c6af5ec210a0b5782c9d17ca3b2d4900dc270cc Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 15:46:02 +0300 Subject: [PATCH 32/52] 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 -- 2.45.2 From 859e203a006159da26aa784fa2f87ae4a0772b57 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 15:54:43 +0300 Subject: [PATCH 33/52] removed debug comments --- internal/handlers/ussd/menuhandler.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index af3b99f..c3d1870 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -988,8 +988,6 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by 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) @@ -1031,8 +1029,6 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by return res, nil } - fmt.Println("Nothing will happen as the error in not 404") - return res, err } -- 2.45.2 From b2fb9faf6c450d5afe1e79f9a6a6220c4a9b2a6d Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 16:17:33 +0300 Subject: [PATCH 34/52] use the active balance to validate the amount --- internal/handlers/ussd/menuhandler.go | 41 +++++---------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index c3d1870..f263e4b 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -745,43 +745,16 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount") store := h.userdataStore - publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) + var balanceValue float64 + + // retrieve the active balance + activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) if err != nil { return res, err } - - // retrieve the active symbol - activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) - useActiveSymbol := err == nil && len(activeSym) > 0 - - var balanceValue float64 - if useActiveSymbol { - // If active symbol is set, retrieve its balance - activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) - if err != nil { - return res, fmt.Errorf("failed to get active balance: %v", err) - } - balanceValue, err = strconv.ParseFloat(string(activeBal), 64) - if err != nil { - return res, fmt.Errorf("failed to parse active balance: %v", err) - } - } else { - // If no active symbol, use the current balance from the API - balanceStr, err := h.accountService.CheckBalance(string(publicKey)) - if err != nil { - return res, fmt.Errorf("failed to check balance: %v", err) - } - res.Content = balanceStr - - // Parse the balance string - balanceParts := strings.Split(balanceStr, " ") - if len(balanceParts) != 2 { - return res, fmt.Errorf("unexpected balance format: %s", balanceStr) - } - balanceValue, err = strconv.ParseFloat(balanceParts[0], 64) - if err != nil { - return res, fmt.Errorf("failed to parse balance: %v", err) - } + balanceValue, err = strconv.ParseFloat(string(activeBal), 64) + if err != nil { + return res, err } // Extract numeric part from the input amount -- 2.45.2 From f378f154223349108e953f2a2c0edeb76b9c2680 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 16:29:51 +0300 Subject: [PATCH 35/52] updated tests --- internal/handlers/ussd/menuhandler_test.go | 61 ++++------------------ 1 file changed, 9 insertions(+), 52 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index f36814a..901a15a 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1415,17 +1415,13 @@ func TestValidateAmount(t *testing.T) { tests := []struct { name string input []byte - publicKey []byte - activeSym []byte activeBal []byte balance string expectedResult resource.Result }{ { - name: "Test with valid amount and active symbol", + name: "Test with valid amount", input: []byte("0.001"), - publicKey: []byte("0xrqeqrequuq"), - activeSym: []byte("CELO"), activeBal: []byte("0.003"), expectedResult: resource.Result{ Content: "0.001", @@ -1434,8 +1430,6 @@ func TestValidateAmount(t *testing.T) { { name: "Test with amount larger than active balance", input: []byte("0.02"), - publicKey: []byte("0xrqeqrequuq"), - activeSym: []byte("CELO"), activeBal: []byte("0.003"), expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, @@ -1445,37 +1439,18 @@ func TestValidateAmount(t *testing.T) { { name: "Test with invalid amount format", input: []byte("0.02ms"), - publicKey: []byte("0xrqeqrequuq"), balance: "0.003 CELO", expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, Content: "0.02ms", }, }, - { - name: "Test fallback to current balance without active symbol", - input: []byte("0.001"), - publicKey: []byte("0xrqeqrequuq"), - balance: "0.003 CELO", - expectedResult: resource.Result{ - Content: "0.001", - }, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Mock behavior for public key retrieval - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil) - - // Mock behavior for active symbol and balance retrieval (if present) - if len(tt.activeSym) > 0 { - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(tt.activeSym, nil) - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL).Return(tt.activeBal, nil) - } else { - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(nil, fmt.Errorf("not found")) - mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil) - } + // Mock behavior for active balance retrieval + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL).Return(tt.activeBal, nil) // Mock behavior for storing the amount (if valid) mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe() @@ -1572,15 +1547,6 @@ func TestCheckBalance(t *testing.T) { expectedResult: resource.Result{Content: "1.5 ETH"}, expectError: false, }, - { - name: "User without active sym", - sessionId: "session123", - publicKey: "0X13242618721", - activeSym: "", - activeBal: "", - expectedResult: resource.Result{Content: "0.003 CELO"}, - expectError: false, - }, } for _, tt := range tests { @@ -1593,21 +1559,12 @@ func TestCheckBalance(t *testing.T) { userdataStore: mockDataStore, accountService: mockAccountService, } - - // Mock calls for public key - mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(tt.publicKey), nil) - - if tt.activeSym == "" { - // Mock for user without active sym - mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte{}, db.ErrNotFound{}) - mockAccountService.On("CheckBalance", tt.publicKey).Return("0.003 CELO", nil) - } else { - // Mock for user with active sym - mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil) - mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil) - } - - res, err := h.CheckBalance(ctx, "check_balance", []byte("123456")) + + // Mock for user with active sym + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil) + mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil) + + res, err := h.CheckBalance(ctx, "check_balance", []byte("")) if tt.expectError { assert.Error(t, err) -- 2.45.2 From a336856c9b8fc4b028c29cce967d043a609468c3 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 15 Oct 2024 17:02:51 +0300 Subject: [PATCH 36/52] use separate functions to process voucher data --- internal/handlers/ussd/menuhandler.go | 79 ++++++++++++++++----------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index f263e4b..8041bf7 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1029,15 +1029,8 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } - var numberedSymbols []string - var numberedBalances []string - for i, voucher := range vouchersResp.Result.Holdings { - numberedSymbols = append(numberedSymbols, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) - numberedBalances = append(numberedBalances, fmt.Sprintf("%d:%s", i+1, voucher.Balance)) - } - - voucherSymbolList := strings.Join(numberedSymbols, "\n") - voucherBalanceList := strings.Join(numberedBalances, "\n") + // process voucher data + voucherSymbolList, voucherBalanceList := ProcessVouchers(vouchersResp.Result.Holdings) prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList)) @@ -1053,6 +1046,26 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } +// ProcessVouchers formats the holdings into symbol and balance lists. +func ProcessVouchers(holdings []struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` +}) (string, string) { + var numberedSymbols, numberedBalances []string + + for i, voucher := range holdings { + numberedSymbols = append(numberedSymbols, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol)) + numberedBalances = append(numberedBalances, fmt.Sprintf("%d:%s", i+1, voucher.Balance)) + } + + voucherSymbolList := strings.Join(numberedSymbols, "\n") + voucherBalanceList := strings.Join(numberedBalances, "\n") + + return voucherSymbolList, voucherBalanceList +} + // GetVoucherList fetches the list of vouchers and formats them func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -1074,7 +1087,6 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) // ViewVoucher retrieves the token holding and balance from the subprefixDB 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) @@ -1105,10 +1117,31 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r return res, fmt.Errorf("failed to retrieve voucher balance list: %v", err) } - // Convert the symbol and balance lists from byte arrays to strings - voucherSymbols := string(voucherSymbolList) - voucherBalances := string(voucherBalanceList) + // match the voucher symbol and balance with the input + matchedSymbol, matchedBalance := MatchVoucher(inputStr, string(voucherSymbolList), string(voucherBalanceList)) + // If a match is found, write the temporary sym and balance + if matchedSymbol != "" && matchedBalance != "" { + err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte(matchedSymbol)) + if err != nil { + return res, err + } + err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_BAL, []byte(matchedBalance)) + if err != nil { + return res, err + } + + res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) + res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance) + } else { + res.FlagSet = append(res.FlagSet, flag_incorrect_voucher) + } + + return res, nil +} + +// MatchVoucher finds the matching voucher symbol and balance based on the input. +func MatchVoucher(inputStr string, voucherSymbols, voucherBalances string) (string, string) { // Split the lists into slices for processing symbols := strings.Split(voucherSymbols, "\n") balances := strings.Split(voucherBalances, "\n") @@ -1128,29 +1161,13 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r matchedSymbol = voucherSymbol // Ensure there's a corresponding balance if i < len(balances) { - matchedBalance = strings.SplitN(balances[i], ":", 2)[1] // Extract balance after the "x:balance" format + matchedBalance = strings.SplitN(balances[i], ":", 2)[1] } break } } - // 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 - } - 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 { - res.FlagSet = append(res.FlagSet, flag_incorrect_voucher) - } - - return res, nil + return matchedSymbol, matchedBalance } // SetVoucher retrieves the temporary sym and balance, -- 2.45.2 From eea3be3a39c1fe5ee1be7ea2f7c7ad7a5a8e3125 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 17 Oct 2024 13:49:56 +0300 Subject: [PATCH 37/52] resolve failing test --- internal/handlers/ussd/menuhandler_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index c624e0f..499c426 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1440,7 +1440,6 @@ func TestValidateAmount(t *testing.T) { t.Logf(err.Error()) } flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") - flag_api_error, _ := fm.GetFlag("flag_api_call_error") mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) @@ -1466,7 +1465,6 @@ func TestValidateAmount(t *testing.T) { activeBal: []byte("0.003"), expectedResult: resource.Result{ Content: "0.001", - FlagReset: []uint32{flag_api_error}, }, }, { @@ -1475,7 +1473,6 @@ func TestValidateAmount(t *testing.T) { activeBal: []byte("0.003"), expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, - FlagReset: []uint32{flag_api_error}, Content: "0.02", }, }, @@ -1485,7 +1482,6 @@ func TestValidateAmount(t *testing.T) { balance: "0.003 CELO", expectedResult: resource.Result{ FlagSet: []uint32{flag_invalid_amount}, - FlagReset: []uint32{flag_api_error}, Content: "0.02ms", }, }, -- 2.45.2 From 25bc7006a4d743f0a74e9d64b3703b59e025d8ae Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 18 Oct 2024 18:31:40 +0300 Subject: [PATCH 38/52] show a balance of 0 and prevent sending when no voucher exists --- internal/handlers/ussd/menuhandler.go | 18 ++++++++++++------ services/registration/no_voucher | 1 + services/registration/no_voucher.vis | 5 +++++ services/registration/no_voucher_swa | 1 + services/registration/pp.csv | 3 ++- services/registration/send.vis | 1 + 6 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 services/registration/no_voucher create mode 100644 services/registration/no_voucher.vis create mode 100644 services/registration/no_voucher_swa diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 4f010d6..93acc02 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -624,7 +624,7 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by return res, nil } -// CheckBalance retrieves the balance from the API using the "PublicKey" and sets +// CheckBalance retrieves the balance of the active voucher and sets // the balance as the result content func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -640,12 +640,13 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( // get the active sym and active balance activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) if err != nil { - return res, err + res.Content = "0.00" + return res, nil } activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) if err != nil { - return res, err + return res, nil } res.Content = fmt.Sprintf("%s %s", activeBal, activeSym) @@ -991,6 +992,8 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by return res, fmt.Errorf("missing session") } + flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher") + // check if the user has an active sym _, err = store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) @@ -1008,9 +1011,10 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by return res, nil } - // Ensure there is at least one voucher + // Return if there is no voucher if len(vouchersResp.Result.Holdings) == 0 { - return res, err + res.FlagSet = append(res.FlagSet, flag_no_active_voucher) + return res, nil } // Use only the first voucher @@ -1035,6 +1039,8 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by return res, err } + res.FlagReset = append(res.FlagReset, flag_no_active_voucher) + return res, nil } @@ -1160,7 +1166,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r if err != nil { return res, err } - + res.FlagReset = append(res.FlagReset, flag_incorrect_voucher) res.Content = fmt.Sprintf("%s\n%s", matchedSymbol, matchedBalance) } else { diff --git a/services/registration/no_voucher b/services/registration/no_voucher new file mode 100644 index 0000000..332f00e --- /dev/null +++ b/services/registration/no_voucher @@ -0,0 +1 @@ +You need a voucher to send \ No newline at end of file diff --git a/services/registration/no_voucher.vis b/services/registration/no_voucher.vis new file mode 100644 index 0000000..832ef22 --- /dev/null +++ b/services/registration/no_voucher.vis @@ -0,0 +1,5 @@ +MOUT back 0 +MOUT quit 9 +HALT +INCMP ^ 0 +INCMP quit 9 diff --git a/services/registration/no_voucher_swa b/services/registration/no_voucher_swa new file mode 100644 index 0000000..66e8f26 --- /dev/null +++ b/services/registration/no_voucher_swa @@ -0,0 +1 @@ +Unahitaji sarafu kutuma \ No newline at end of file diff --git a/services/registration/pp.csv b/services/registration/pp.csv index 96d3e8a..ec0d8c1 100644 --- a/services/registration/pp.csv +++ b/services/registration/pp.csv @@ -15,4 +15,5 @@ flag,flag_allow_update,21,this is set to allow a user to update their profile da flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid flag,flag_incorrect_voucher,24,this is set when the selected voucher is invalid -flag,flag_api_call_error,25,this is set when communication to an external service fails +flag,flag_api_call_error,25,this is set when communication to an external service fails +flag,flag_no_active_voucher,26,this is set when a user does not have an active voucher diff --git a/services/registration/send.vis b/services/registration/send.vis index e120302..0ff0927 100644 --- a/services/registration/send.vis +++ b/services/registration/send.vis @@ -1,4 +1,5 @@ LOAD transaction_reset 0 +CATCH no_voucher flag_no_active_voucher 1 MOUT back 0 HALT LOAD validate_recipient 20 -- 2.45.2 From 3bf2045f1552e365113b90861779ac358a55a361 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 19 Oct 2024 16:07:40 +0300 Subject: [PATCH 39/52] added FetchVouchers to the TestAccountService --- internal/handlers/server/accountservice.go | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index e38e13d..ebe825e 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -132,7 +132,6 @@ func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error) } func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) { - balanceResponse := &models.BalanceResponse{ Ok: true, Result: struct { @@ -170,3 +169,31 @@ func (tas *TestAccountService) CheckAccountStatus(trackingId string) (*models.Tr } return trackResponse, nil } + +func (tas *TestAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { + return &models.VoucherHoldingResponse{ + Ok: true, + Result: struct { + Holdings []struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` + } `json:"holdings"` + }{ + Holdings: []struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` + }{ + { + ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", + TokenSymbol: "SRF", + TokenDecimals: "6", + Balance: "2745987", + }, + }, + }, + }, nil +} -- 2.45.2 From 5909659fa96708b52a1e21a244b7794002c37090 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 21 Oct 2024 16:52:07 +0300 Subject: [PATCH 40/52] Use dynamic balance --- menutraversal_test/group_test.json | 44 +++++++++++------------ menutraversal_test/menu_traversal_test.go | 38 ++++++++++++++++++-- menutraversal_test/test_setup.json | 8 ++--- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/menutraversal_test/group_test.json b/menutraversal_test/group_test.json index 203ff08..8b765f5 100644 --- a/menutraversal_test/group_test.json +++ b/menutraversal_test/group_test.json @@ -5,7 +5,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -33,7 +33,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -42,7 +42,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -70,7 +70,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -79,7 +79,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -116,7 +116,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -125,7 +125,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -162,7 +162,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -171,7 +171,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -203,7 +203,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -212,7 +212,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -244,7 +244,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] @@ -254,7 +254,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -286,7 +286,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -295,7 +295,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -327,7 +327,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -336,7 +336,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -368,7 +368,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -377,7 +377,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -409,7 +409,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] }, @@ -418,7 +418,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", @@ -446,7 +446,7 @@ }, { "input": "0", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } ] } diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 5eb1ef8..a32e17a 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -43,6 +43,17 @@ func extractPublicKey(response []byte) string { return "" } +// Extracts the balance value from the engine response. +func extractBalance(response []byte) string { + // Regex to match "Balance: " followed by a newline + re := regexp.MustCompile(`(?m)^Balance:\s+(\d+(\.\d+)?)\s+([A-Z]+)`) + match := re.FindSubmatch(response) + if match != nil && len(match) > 0 { + return string(match[1]) + " " + string(match[3]) // " " + } + return "" +} + func TestMain(m *testing.M) { sessionID = GenerateSessionId() defer func() { @@ -154,6 +165,12 @@ func TestMainMenuHelp(t *testing.T) { } b := w.Bytes() + balance := extractBalance(b) + + expectedContent := []byte(step.ExpectedContent) + expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) + + step.ExpectedContent = string(expectedContent) match, err := step.MatchesExpectedContent(b) if err != nil { t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) @@ -189,6 +206,12 @@ func TestMainMenuQuit(t *testing.T) { } b := w.Bytes() + balance := extractBalance(b) + + expectedContent := []byte(step.ExpectedContent) + expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) + + step.ExpectedContent = string(expectedContent) match, err := step.MatchesExpectedContent(b) if err != nil { t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) @@ -225,8 +248,13 @@ func TestMyAccount_MyAddress(t *testing.T) { } b := w.Bytes() + balance := extractBalance(b) publicKey := extractPublicKey(b) - expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) + + expectedContent := []byte(step.ExpectedContent) + expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) + expectedContent = bytes.Replace(expectedContent, []byte("{public_key}"), []byte(publicKey), -1) + step.ExpectedContent = string(expectedContent) match, err := step.MatchesExpectedContent(b) if err != nil { @@ -265,6 +293,13 @@ func TestGroups(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", tt.Name, err) } b := w.Bytes() + balance := extractBalance(b) + + expectedContent := []byte(tt.ExpectedContent) + expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) + + tt.ExpectedContent = string(expectedContent) + match, err := tt.MatchesExpectedContent(b) if err != nil { t.Fatalf("Error compiling regex for step '%s': %v", tt.Input, err) @@ -272,7 +307,6 @@ func TestGroups(t *testing.T) { if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", tt.ExpectedContent, b) } - }) } } diff --git a/menutraversal_test/test_setup.json b/menutraversal_test/test_setup.json index 56c0278..619744b 100644 --- a/menutraversal_test/test_setup.json +++ b/menutraversal_test/test_setup.json @@ -57,7 +57,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "1", @@ -106,7 +106,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "4", @@ -119,7 +119,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "9", @@ -132,7 +132,7 @@ "steps": [ { "input": "", - "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" }, { "input": "3", -- 2.45.2 From 02cb75f97a61150830bfbdcb58860c7b52c96c68 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 21 Oct 2024 17:14:59 +0300 Subject: [PATCH 41/52] increase the size to resolve sink error --- services/registration/confirm_create_pin.vis | 2 +- services/registration/create_pin.vis | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/registration/confirm_create_pin.vis b/services/registration/confirm_create_pin.vis index 1a3173c..02279dc 100644 --- a/services/registration/confirm_create_pin.vis +++ b/services/registration/confirm_create_pin.vis @@ -1,4 +1,4 @@ -LOAD save_temporary_pin 0 +LOAD save_temporary_pin 6 HALT LOAD verify_create_pin 8 INCMP account_creation * diff --git a/services/registration/create_pin.vis b/services/registration/create_pin.vis index c76e1bf..40989ec 100644 --- a/services/registration/create_pin.vis +++ b/services/registration/create_pin.vis @@ -2,7 +2,7 @@ LOAD create_account 0 CATCH account_creation_failed flag_account_creation_failed 1 MOUT exit 0 HALT -LOAD save_temporary_pin 0 +LOAD save_temporary_pin 6 RELOAD save_temporary_pin CATCH . flag_incorrect_pin 1 INCMP quit 0 -- 2.45.2 From 0547bc7e5fe0b2c35d9973553b05d48d853fd082 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 22 Oct 2024 14:24:22 +0300 Subject: [PATCH 42/52] added send menu test with dynamic max_amount --- menutraversal_test/menu_traversal_test.go | 57 +++++++++++++++++++++++ menutraversal_test/test_setup.json | 8 ++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index a32e17a..8d028c9 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -54,6 +54,17 @@ func extractBalance(response []byte) string { return "" } +// Extracts the Maximum amount value from the engine response. +func extractMaxAmount(response []byte) string { + // Regex to match "Maximum amount: " followed by a newline + re := regexp.MustCompile(`(?m)^Maximum amount:\s+(\d+(\.\d+)?)\s+([A-Z]+)`) + match := re.FindSubmatch(response) + if match != nil && len(match) > 0 { + return string(match[1]) + " " + string(match[3]) // " " + } + return "" +} + func TestMain(m *testing.M) { sessionID = GenerateSessionId() defer func() { @@ -268,6 +279,52 @@ func TestMyAccount_MyAddress(t *testing.T) { } } +func TestMainMenuSend(t *testing.T) { + en, fn, _ := testutil.TestEngine(sessionID) + defer fn() + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "send_with_invalid_inputs") + for _, group := range groups { + for _, step := range group.Steps { + cont, err := en.Exec(ctx, []byte(step.Input)) + if err != nil { + t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) + return + } + if !cont { + break + } + w := bytes.NewBuffer(nil) + if _, err := en.Flush(ctx, w); err != nil { + t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) + } + + b := w.Bytes() + balance := extractBalance(b) + max_amount := extractMaxAmount(b) + publicKey := extractPublicKey(b) + + expectedContent := []byte(step.ExpectedContent) + expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) + expectedContent = bytes.Replace(expectedContent, []byte("{max_amount}"), []byte(max_amount), -1) + expectedContent = bytes.Replace(expectedContent, []byte("{public_key}"), []byte(publicKey), -1) + + step.ExpectedContent = string(expectedContent) + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + } + } + } +} + + func TestGroups(t *testing.T) { groups, err := driver.LoadTestGroups(groupTestFile) if err != nil { diff --git a/menutraversal_test/test_setup.json b/menutraversal_test/test_setup.json index 619744b..736d4de 100644 --- a/menutraversal_test/test_setup.json +++ b/menutraversal_test/test_setup.json @@ -73,15 +73,15 @@ }, { "input": "065656", - "expectedContent": "Maximum amount: 0.003 CELO\nEnter amount:\n0:Back" + "expectedContent": "{max_amount}\nEnter amount:\n0:Back" }, { - "input": "0.1", - "expectedContent": "Amount 0.1 is invalid, please try again:\n1:retry\n9:Quit" + "input": "10000000", + "expectedContent": "Amount 10000000 is invalid, please try again:\n1:retry\n9:Quit" }, { "input": "1", - "expectedContent": "Maximum amount: 0.003 CELO\nEnter amount:\n0:Back" + "expectedContent": "{max_amount}\nEnter amount:\n0:Back" }, { "input": "0.001", -- 2.45.2 From f37ec13c757fce0e3cc3db308e95fd695b9b396f Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 22 Oct 2024 16:31:29 +0300 Subject: [PATCH 43/52] polish code --- menutraversal_test/menu_traversal_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 8d028c9..2b2a093 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -48,7 +48,7 @@ func extractBalance(response []byte) string { // Regex to match "Balance: " followed by a newline re := regexp.MustCompile(`(?m)^Balance:\s+(\d+(\.\d+)?)\s+([A-Z]+)`) match := re.FindSubmatch(response) - if match != nil && len(match) > 0 { + if match != nil { return string(match[1]) + " " + string(match[3]) // " " } return "" @@ -59,7 +59,7 @@ func extractMaxAmount(response []byte) string { // Regex to match "Maximum amount: " followed by a newline re := regexp.MustCompile(`(?m)^Maximum amount:\s+(\d+(\.\d+)?)\s+([A-Z]+)`) match := re.FindSubmatch(response) - if match != nil && len(match) > 0 { + if match != nil { return string(match[1]) + " " + string(match[3]) // " " } return "" @@ -324,7 +324,6 @@ func TestMainMenuSend(t *testing.T) { } } - func TestGroups(t *testing.T) { groups, err := driver.LoadTestGroups(groupTestFile) if err != nil { -- 2.45.2 From 5f1ee396d84d069c25a5ca5d71bccbad855e373f Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 23 Oct 2024 06:35:19 +0300 Subject: [PATCH 44/52] Fix PIN being requested twice --- services/registration/my_vouchers.vis | 2 ++ services/registration/view_voucher.vis | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/registration/my_vouchers.vis b/services/registration/my_vouchers.vis index 9702573..b59441a 100644 --- a/services/registration/my_vouchers.vis +++ b/services/registration/my_vouchers.vis @@ -1,3 +1,5 @@ +LOAD reset_account_authorized 16 +RELOAD reset_account_authorized MOUT select_voucher 1 MOUT voucher_details 2 MOUT back 0 diff --git a/services/registration/view_voucher.vis b/services/registration/view_voucher.vis index ee8bf85..1480099 100644 --- a/services/registration/view_voucher.vis +++ b/services/registration/view_voucher.vis @@ -1,4 +1,3 @@ -RELOAD view_voucher MAP view_voucher MOUT back 0 MOUT quit 9 -- 2.45.2 From 176473aa264cc0c353e2a78f0c69f8cb3bc31aa2 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 23 Oct 2024 13:54:42 +0300 Subject: [PATCH 45/52] Rename prefix to vouchers --- internal/handlers/ussd/menuhandler.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 621a43e..d464ca3 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1068,7 +1068,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) // process voucher data voucherSymbolList, voucherBalanceList := ProcessVouchers(vouchersResp.Result.Holdings) - prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers")) err = prefixdb.Put(ctx, []byte("sym"), []byte(voucherSymbolList)) if err != nil { return res, nil @@ -1108,7 +1108,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) // Read vouchers from the store store := h.userdataStore - prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers")) voucherData, err := prefixdb.Get(ctx, []byte("sym")) if err != nil { @@ -1139,7 +1139,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r return res, nil } - prefixdb := storage.NewSubPrefixDb(store, []byte("pfx")) + prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers")) // Retrieve the voucher symbol list voucherSymbolList, err := prefixdb.Get(ctx, []byte("sym")) -- 2.45.2 From 4011597d9c9d6653c35cb569970aba62066b59e9 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 23 Oct 2024 14:02:13 +0300 Subject: [PATCH 46/52] Check specific db error --- internal/handlers/ussd/menuhandler.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index d464ca3..009d3cc 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -640,8 +640,12 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( // get the active sym and active balance activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) if err != nil { - res.Content = "0.00" - return res, nil + if db.IsNotFound(err) { + res.Content = "0.00" + return res, nil + } + + return res, err } activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) -- 2.45.2 From a553731f02f3fb8bb53150a6982eca1629372a9b Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 23 Oct 2024 17:45:41 +0300 Subject: [PATCH 47/52] Cleaned up code --- internal/handlers/ussd/menuhandler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 009d3cc..f57b4e7 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1004,7 +1004,6 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by if err != nil { if db.IsNotFound(err) { publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) - if err != nil { return res, nil } -- 2.45.2 From ee9a683eb0f6824e9e342d1f7dd85714a597200d Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 24 Oct 2024 16:30:44 +0300 Subject: [PATCH 48/52] Merge branch 'master' into menu-voucherlist --- internal/handlers/ussd/menuhandler.go | 10 +++----- internal/handlers/ussd/menuhandler_test.go | 27 +++++++--------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index f178231..a9cb5b7 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -849,7 +849,7 @@ func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) ( return res, nil } -// GetSender retrieves the public key from the Gdbm Db +// GetSender returns the sessionId (phoneNumber) func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -858,10 +858,7 @@ func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (res return res, fmt.Errorf("missing session") } - store := h.userdataStore - publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) - - res.Content = string(publicKey) + res.Content = string(sessionId) return res, nil } @@ -898,13 +895,12 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input [] // TODO // Use the amount, recipient and sender to call the API and initialize the transaction store := h.userdataStore - publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) amount, _ := store.ReadEntry(ctx, sessionId, utils.DATA_AMOUNT) recipient, _ := store.ReadEntry(ctx, sessionId, utils.DATA_RECIPIENT) - res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey)) + res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(sessionId)) account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized") if err != nil { diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index e201531..e86167b 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -478,12 +478,8 @@ func TestGetSender(t *testing.T) { mockStore := new(mocks.MockUserDataStore) // Define test data - sessionId := "session123" + sessionId := "254712345678" ctx := context.WithValue(context.Background(), "SessionId", sessionId) - publicKey := "0xcasgatweksalw1018221" - - // Set up the expected behavior of the mock - mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) // Create the Handlers instance with the mock store h := &Handlers{ @@ -491,11 +487,10 @@ func TestGetSender(t *testing.T) { } // Call the method - res, _ := h.GetSender(ctx, "max_amount", []byte("check_balance")) - - //Assert that the public key from readentry operation is what was set as the result content. - assert.Equal(t, publicKey, res.Content) + res, _ := h.GetSender(ctx, "get_sender", []byte("")) + //Assert that the sessionId is what was set as the result content. + assert.Equal(t, sessionId, res.Content) } func TestGetAmount(t *testing.T) { @@ -1256,7 +1251,7 @@ func TestResetInvalidAmount(t *testing.T) { } func TestInitiateTransaction(t *testing.T) { - sessionId := "session123" + sessionId := "254712345678" fm, err := NewFlagManager(flagsPath) @@ -1279,30 +1274,26 @@ func TestInitiateTransaction(t *testing.T) { tests := []struct { name string input []byte - PublicKey []byte Recipient []byte Amount []byte status string expectedResult resource.Result }{ { - name: "Test amount reset", - PublicKey: []byte("0x1241527192"), - Amount: []byte("0.002CELO"), + name: "Test initiate transaction", + Amount: []byte("0.002 CELO"), Recipient: []byte("0x12415ass27192"), expectedResult: resource.Result{ FlagReset: []uint32{account_authorized_flag}, - Content: "Your request has been sent. 0x12415ass27192 will receive 0.002CELO from 0x1241527192.", + Content: "Your request has been sent. 0x12415ass27192 will receive 0.002 CELO from 254712345678.", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Define expected interactions with the mock - mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.PublicKey, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return(tt.Amount, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return(tt.Recipient, nil) - //mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, []byte("")).Return(nil) // Call the method under test res, _ := h.InitiateTransaction(ctx, "transaction_reset_amount", tt.input) @@ -1315,10 +1306,8 @@ func TestInitiateTransaction(t *testing.T) { // Assert that expectations were met mockDataStore.AssertExpectations(t) - }) } - } func TestQuit(t *testing.T) { -- 2.45.2 From 728815f0c6780022524d17e0c0f6a916f394cd58 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 24 Oct 2024 17:50:37 +0300 Subject: [PATCH 49/52] Include the active symbol in the send menu --- internal/handlers/handlerservice.go | 1 + internal/handlers/ussd/menuhandler.go | 41 +++++++++++++++++++-- services/registration/amount | 2 +- services/registration/amount.vis | 7 ++-- services/registration/amount_swa | 2 +- services/registration/locale/swa/default.po | 4 +- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index d14f7a7..311e694 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -70,6 +70,7 @@ func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceIn ls.DbRs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance) ls.DbRs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient) ls.DbRs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset) + ls.DbRs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount) ls.DbRs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount) ls.DbRs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount) ls.DbRs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index a9cb5b7..56080bb 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -785,6 +785,28 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input return res, nil } +// MaxAmount gets the current balance from the API and sets it as +// the result content. +func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + var err error + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + store := h.userdataStore + + activeBal, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_BAL) + if err != nil { + return res, err + } + + res.Content = string(activeBal) + + return res, nil +} + // ValidateAmount ensures that the given input is a valid amount and that // it is not more than the current balance. func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -824,12 +846,14 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) return res, nil } - err = store.WriteEntry(ctx, sessionId, utils.DATA_AMOUNT, []byte(amountStr)) + // Format the amount with 2 decimal places before saving + formattedAmount := fmt.Sprintf("%.2f", inputAmount) + err = store.WriteEntry(ctx, sessionId, utils.DATA_AMOUNT, []byte(formattedAmount)) if err != nil { return res, err } - res.Content = fmt.Sprintf("%.3f", inputAmount) + res.Content = fmt.Sprintf("%s", formattedAmount) return res, nil } @@ -872,9 +896,16 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res return res, fmt.Errorf("missing session") } store := h.userdataStore + + // retrieve the active symbol + activeSym, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) + if err != nil { + return res, err + } + amount, _ := store.ReadEntry(ctx, sessionId, utils.DATA_AMOUNT) - res.Content = string(amount) + res.Content = fmt.Sprintf("%s %s", string(amount), string(activeSym)) return res, nil } @@ -900,7 +931,9 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input [] recipient, _ := store.ReadEntry(ctx, sessionId, utils.DATA_RECIPIENT) - res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(sessionId)) + activeSym, _ := store.ReadEntry(ctx, sessionId, utils.DATA_ACTIVE_SYM) + + res.Content = l.Get("Your request has been sent. %s will receive %s %s from %s.", string(recipient), string(amount), string(activeSym), string(sessionId)) account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized") if err != nil { diff --git a/services/registration/amount b/services/registration/amount index d3e8602..9142aba 100644 --- a/services/registration/amount +++ b/services/registration/amount @@ -1,2 +1,2 @@ -Maximum amount: {{.check_balance}} +Maximum amount: {{.max_amount}} Enter amount: \ No newline at end of file diff --git a/services/registration/amount.vis b/services/registration/amount.vis index 7c2a0a7..82e1fd4 100644 --- a/services/registration/amount.vis +++ b/services/registration/amount.vis @@ -1,6 +1,7 @@ LOAD reset_transaction_amount 0 -LOAD check_balance 48 -MAP check_balance +LOAD max_amount 10 +RELOAD max_amount +MAP max_amount MOUT back 0 HALT LOAD validate_amount 64 @@ -10,5 +11,5 @@ CATCH invalid_amount flag_invalid_amount 1 INCMP _ 0 LOAD get_recipient 12 LOAD get_sender 64 -LOAD get_amount 12 +LOAD get_amount 32 INCMP transaction_pin * diff --git a/services/registration/amount_swa b/services/registration/amount_swa index d071f63..0c8cf01 100644 --- a/services/registration/amount_swa +++ b/services/registration/amount_swa @@ -1,2 +1,2 @@ -Kiwango cha juu: {{.check_balance}} +Kiwango cha juu: {{.max_amount}} Weka kiwango: \ No newline at end of file diff --git a/services/registration/locale/swa/default.po b/services/registration/locale/swa/default.po index 0a3909b..ba9a9bb 100644 --- a/services/registration/locale/swa/default.po +++ b/services/registration/locale/swa/default.po @@ -1,8 +1,8 @@ msgid "Your account balance is %s" msgstr "Salio lako ni %s" -msgid "Your request has been sent. %s will receive %s from %s." -msgstr "Ombi lako limetumwa. %s atapokea %s kutoka kwa %s." +msgid "Your request has been sent. %s will receive %s %s from %s." +msgstr "Ombi lako limetumwa. %s atapokea %s %s kutoka kwa %s." msgid "Thank you for using Sarafu. Goodbye!" msgstr "Asante kwa kutumia huduma ya Sarafu. Kwaheri!" -- 2.45.2 From a92c640cb7ab15c16507d1ff5ae7019cc8a7e52d Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 24 Oct 2024 18:15:54 +0300 Subject: [PATCH 50/52] Updated tests --- internal/handlers/ussd/menuhandler_test.go | 53 ++++++++++++---------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index e86167b..97df63e 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -494,26 +494,30 @@ func TestGetSender(t *testing.T) { } func TestGetAmount(t *testing.T) { - mockStore := new(mocks.MockUserDataStore) + mockDataStore := new(mocks.MockUserDataStore) // Define test data sessionId := "session123" ctx := context.WithValue(context.Background(), "SessionId", sessionId) - Amount := "0.03CELO" + amount := "0.03" + activeSym := "SRF" // Set up the expected behavior of the mock - mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return([]byte(Amount), nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(activeSym), nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return([]byte(amount), nil) // Create the Handlers instance with the mock store h := &Handlers{ - userdataStore: mockStore, + userdataStore: mockDataStore, } // Call the method - res, _ := h.GetAmount(ctx, "get_amount", []byte("Getting amount...")) + res, _ := h.GetAmount(ctx, "get_amount", []byte("")) + + formattedAmount := fmt.Sprintf("%s %s", amount, activeSym) //Assert that the retrieved amount is what was set as the content - assert.Equal(t, Amount, res.Content) + assert.Equal(t, formattedAmount, res.Content) } @@ -1276,16 +1280,18 @@ func TestInitiateTransaction(t *testing.T) { input []byte Recipient []byte Amount []byte + ActiveSym []byte status string expectedResult resource.Result }{ { name: "Test initiate transaction", - Amount: []byte("0.002 CELO"), + Amount: []byte("0.002"), + ActiveSym: []byte("SRF"), Recipient: []byte("0x12415ass27192"), expectedResult: resource.Result{ FlagReset: []uint32{account_authorized_flag}, - Content: "Your request has been sent. 0x12415ass27192 will receive 0.002 CELO from 254712345678.", + Content: "Your request has been sent. 0x12415ass27192 will receive 0.002 SRF from 254712345678.", }, }, } @@ -1294,6 +1300,7 @@ func TestInitiateTransaction(t *testing.T) { // Define expected interactions with the mock mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return(tt.Amount, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return(tt.Recipient, nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(tt.ActiveSym, nil) // Call the method under test res, _ := h.InitiateTransaction(ctx, "transaction_reset_amount", tt.input) @@ -1444,28 +1451,28 @@ func TestValidateAmount(t *testing.T) { }{ { name: "Test with valid amount", - input: []byte("0.001"), - activeBal: []byte("0.003"), + input: []byte("4.10"), + activeBal: []byte("5"), expectedResult: resource.Result{ - Content: "0.001", + Content: "4.10", }, }, { name: "Test with amount larger than active balance", - input: []byte("0.02"), - activeBal: []byte("0.003"), + input: []byte("5.02"), + activeBal: []byte("5"), expectedResult: resource.Result{ - FlagSet: []uint32{flag_invalid_amount}, - Content: "0.02", + FlagSet: []uint32{flag_invalid_amount}, + Content: "5.02", }, }, { - name: "Test with invalid amount format", - input: []byte("0.02ms"), - balance: "0.003 CELO", + name: "Test with invalid amount format", + input: []byte("0.02ms"), + activeBal: []byte("5"), expectedResult: resource.Result{ - FlagSet: []uint32{flag_invalid_amount}, - Content: "0.02ms", + FlagSet: []uint32{flag_invalid_amount}, + Content: "0.02ms", }, }, } @@ -1567,7 +1574,7 @@ func TestCheckBalance(t *testing.T) { publicKey: "0X98765432109", activeSym: "ETH", activeBal: "1.5", - expectedResult: resource.Result{Content: "1.5 ETH"}, + expectedResult: resource.Result{Content: "Balance: 1.5 ETH\n"}, expectError: false, }, } @@ -1582,11 +1589,11 @@ func TestCheckBalance(t *testing.T) { userdataStore: mockDataStore, accountService: mockAccountService, } - + // Mock for user with active sym mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil) mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil) - + res, err := h.CheckBalance(ctx, "check_balance", []byte("")) if tt.expectError { -- 2.45.2 From d113ea82fdd41b844bcef380b0288946ac3679be Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 24 Oct 2024 20:21:28 +0300 Subject: [PATCH 51/52] Add dynamic send_amount and session_id --- menutraversal_test/menu_traversal_test.go | 22 +++++++++++++++++----- menutraversal_test/test_setup.json | 8 ++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 2b2a093..900ebcc 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -56,11 +56,22 @@ func extractBalance(response []byte) string { // Extracts the Maximum amount value from the engine response. func extractMaxAmount(response []byte) string { - // Regex to match "Maximum amount: " followed by a newline - re := regexp.MustCompile(`(?m)^Maximum amount:\s+(\d+(\.\d+)?)\s+([A-Z]+)`) + // Regex to match "Maximum amount: " followed by a newline + re := regexp.MustCompile(`(?m)^Maximum amount:\s+(\d+(\.\d+)?)`) match := re.FindSubmatch(response) if match != nil { - return string(match[1]) + " " + string(match[3]) // " " + return string(match[1]) // "" + } + return "" +} + +// Extracts the send amount value from the engine response. +func extractSendAmount(response []byte) string { + // Regex to match the pattern "will receive X.XX SYM from" + re := regexp.MustCompile(`will receive (\d+\.\d{2}\s+[A-Z]+) from`) + match := re.FindSubmatch(response) + if match != nil { + return string(match[1]) // Returns "X.XX SYM" } return "" } @@ -304,12 +315,13 @@ func TestMainMenuSend(t *testing.T) { b := w.Bytes() balance := extractBalance(b) max_amount := extractMaxAmount(b) - publicKey := extractPublicKey(b) + send_amount := extractSendAmount(b) expectedContent := []byte(step.ExpectedContent) expectedContent = bytes.Replace(expectedContent, []byte("{balance}"), []byte(balance), -1) expectedContent = bytes.Replace(expectedContent, []byte("{max_amount}"), []byte(max_amount), -1) - expectedContent = bytes.Replace(expectedContent, []byte("{public_key}"), []byte(publicKey), -1) + expectedContent = bytes.Replace(expectedContent, []byte("{send_amount}"), []byte(send_amount), -1) + expectedContent = bytes.Replace(expectedContent, []byte("{session_id}"), []byte(sessionID), -1) step.ExpectedContent = string(expectedContent) match, err := step.MatchesExpectedContent(b) diff --git a/menutraversal_test/test_setup.json b/menutraversal_test/test_setup.json index 736d4de..13166a4 100644 --- a/menutraversal_test/test_setup.json +++ b/menutraversal_test/test_setup.json @@ -84,8 +84,8 @@ "expectedContent": "{max_amount}\nEnter amount:\n0:Back" }, { - "input": "0.001", - "expectedContent": "065656 will receive 0.001 from {public_key}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + "input": "1.00", + "expectedContent": "065656 will receive {send_amount} from {session_id}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" }, { "input": "1222", @@ -93,11 +93,11 @@ }, { "input": "1", - "expectedContent": "065656 will receive 0.001 from {public_key}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + "expectedContent": "065656 will receive {send_amount} from {session_id}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" }, { "input": "1234", - "expectedContent": "Your request has been sent. 065656 will receive 0.001 from {public_key}." + "expectedContent": "Your request has been sent. 065656 will receive {send_amount} from {session_id}." } ] }, -- 2.45.2 From b4454f7517070d1f1f3a121e5b8769df5243e974 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 24 Oct 2024 20:44:29 +0300 Subject: [PATCH 52/52] Added context to FetchVouchers --- internal/handlers/server/accountservice.go | 6 +++--- internal/handlers/ussd/menuhandler.go | 4 ++-- internal/mocks/servicemock.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index 58774ce..3e2f91d 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -25,7 +25,7 @@ type AccountServiceInterface interface { CreateAccount(ctx context.Context) (*api.OKResponse, error) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) - FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) + FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) } type AccountService struct { @@ -174,7 +174,7 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, e // FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint // Parameters: // - publicKey: The public key associated with the account. -func (as *AccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { +func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) { file, err := os.Open("sample_tokens.json") if err != nil { return nil, err @@ -245,7 +245,7 @@ func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingI return trackResponse, nil } -func (tas *TestAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { +func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) { return &models.VoucherHoldingResponse{ Ok: true, Result: struct { diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index ec0a5b3..f4d279b 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1035,7 +1035,7 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by } // Fetch vouchers from the API using the public key - vouchersResp, err := h.accountService.FetchVouchers(string(publicKey)) + vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey)) if err != nil { return res, nil } @@ -1089,7 +1089,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) } // Fetch vouchers from the API using the public key - vouchersResp, err := h.accountService.FetchVouchers(string(publicKey)) + vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey)) if err != nil { return res, nil } diff --git a/internal/mocks/servicemock.go b/internal/mocks/servicemock.go index 6a217ce..de0e99a 100644 --- a/internal/mocks/servicemock.go +++ b/internal/mocks/servicemock.go @@ -34,7 +34,7 @@ func (m *MockAccountService) TrackAccountStatus(ctx context.Context,publicKey st } -func (m *MockAccountService) FetchVouchers(publicKey string) (*models.VoucherHoldingResponse, error) { +func (m *MockAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) { args := m.Called(publicKey) return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1) } -- 2.45.2