From d1e9340ea9888acd1bbc7dbb0dc4f9e71b6c6004 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 21 Nov 2024 13:03:43 +0300 Subject: [PATCH 01/10] add voucher details --- models/voucher_data_result.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/models/voucher_data_result.go b/models/voucher_data_result.go index c9f0b74..9a10831 100644 --- a/models/voucher_data_result.go +++ b/models/voucher_data_result.go @@ -1,8 +1,10 @@ package models type VoucherDataResult struct { - TokenName string `json:"tokenName"` - TokenSymbol string `json:"tokenSymbol"` - TokenDecimals int `json:"tokenDecimals"` - SinkAddress string `json:"sinkAddress"` + TokenName string `json:"tokenName"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals int `json:"tokenDecimals"` + SinkAddress string `json:"sinkAddress"` + TokenCommodity string `json:"tokenCommodity"` + TokenLocation string `json:"tokenLocation"` } From b8d938d3aa99ddecd999a2a68cf6b326b8f9b71f Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 21 Nov 2024 13:04:19 +0300 Subject: [PATCH 02/10] add voucher details --- internal/handlers/ussd/menuhandler.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index d8e6fa0..8f206e3 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1626,10 +1626,9 @@ func (h *Handlers) GetVoucherDetails(ctx context.Context, sym string, input []by return res, nil } - tokenSymbol := voucherData.TokenSymbol - tokenName := voucherData.TokenName - - res.Content = fmt.Sprintf("%s %s", tokenSymbol, tokenName) + res.Content = fmt.Sprintf( + "name: %s\nsymbol: %s\ncommodity: %s\nlocation: %s", voucherData.TokenName, voucherData.TokenSymbol, voucherData.TokenCommodity, voucherData.TokenLocation, + ) return res, nil } From 1174500e3fce4d0621924d936370eac2d63ea9ad Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 21 Nov 2024 15:19:36 +0300 Subject: [PATCH 03/10] add test for voucher details --- internal/handlers/ussd/menuhandler_test.go | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 51693df..2559a54 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -2024,3 +2024,44 @@ func TestSetVoucher(t *testing.T) { assert.Equal(t, string(tempData.TokenSymbol), res.Content) } + +func TestGetVoucherDetails(t *testing.T) { + + ctx, store := InitializeTestStore(t) + fm, err := NewFlagManager(flagsPath) + if err != nil { + t.Logf(err.Error()) + } + mockAccountService := new(mocks.MockAccountService) + + sessionId := "session123" + ctx = context.WithValue(ctx, "SessionId", sessionId) + expectedResult := resource.Result{} + + tokA_AAddress := "0x0000000000000000000000000000000000000000" + + h := &Handlers{ + userdataStore: store, + flagManager: fm.parser, + accountService: mockAccountService, + } + err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_ADDRESS, []byte(tokA_AAddress)) + if err != nil { + t.Fatal(err) + } + tokenDetails := &models.VoucherDataResult{ + TokenName: "Token A", + TokenSymbol: "TOKA", + TokenLocation: "Kilifi,Kenya", + TokenCommodity: "Farming", + } + expectedResult.Content = fmt.Sprintf( + "name: %s\nsymbol: %s\ncommodity: %s\nlocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation, + ) + mockAccountService.On("VoucherData", string(tokA_AAddress)).Return(tokenDetails, nil) + + res, err := h.GetVoucherDetails(ctx, "SessionId", []byte("")) + assert.NoError(t, err) + assert.Equal(t, expectedResult, res) + +} From 6b23c284e5c72bf7f1e517b31a3cb46fa256b1cd Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 30 Nov 2024 15:24:14 +0300 Subject: [PATCH 04/10] check vouchers before checking the balance --- services/registration/main.vis | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/registration/main.vis b/services/registration/main.vis index 7e1c9bf..beb2d7c 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -1,10 +1,10 @@ LOAD set_default_voucher 8 RELOAD set_default_voucher -LOAD check_balance 64 -RELOAD check_balance LOAD check_vouchers 10 RELOAD check_vouchers -CATCH api_failure flag_api_call_error 1 +LOAD check_balance 64 +RELOAD check_balance +CATCH api_failure flag_api_call_error 1 MAP check_balance MOUT send 1 MOUT vouchers 2 From 7a86b2ad3bb6b310b13f3faeda7bde9f587a16ea Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 30 Nov 2024 15:26:13 +0300 Subject: [PATCH 05/10] updated the UpdateVoucherData description --- common/vouchers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/vouchers.go b/common/vouchers.go index 81ca55f..c542084 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -151,7 +151,7 @@ func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId str return data, nil } -// UpdateVoucherData sets the active voucher data in the DataStore. +// UpdateVoucherData updates the active voucher data in the DataStore. func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error { logg.TraceCtxf(ctx, "dtal", "data", data) // Active voucher data entries From 54c1fe51ef66e66a5a927b50c7bc99bc48b0aec6 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 30 Nov 2024 15:28:21 +0300 Subject: [PATCH 06/10] update the active voucher data when checking the current vouchers --- internal/handlers/ussd/menuhandler.go | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 8d3b928..4b7fc7a 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -25,6 +25,7 @@ import ( "gopkg.in/leonelquinteros/gotext.v1" "git.grassecon.net/urdt/ussd/internal/storage" + dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) var ( @@ -1534,6 +1535,38 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) return res, nil } + // check the current active sym and update the data + activeSym, _ := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_SYM) + if activeSym != nil { + activeSymStr := string(activeSym) + + // Find the matching voucher data + var activeData *dataserviceapi.TokenHoldings + for _, voucher := range vouchersResp { + if voucher.TokenSymbol == activeSymStr { + activeData = &voucher + break + } + } + + if activeData == nil { + logg.ErrorCtxf(ctx, "activeSym not found in vouchers", "activeSym", activeSymStr) + return res, fmt.Errorf("activeSym %s not found in vouchers", activeSymStr) + } + + // Scale down the balance + scaledBalance := common.ScaleDownBalance(activeData.Balance, activeData.TokenDecimals) + + // Update the balance field with the scaled value + activeData.Balance = scaledBalance + + // Pass the matching voucher data to UpdateVoucherData + if err := common.UpdateVoucherData(ctx, h.userdataStore, sessionId, activeData); err != nil { + logg.ErrorCtxf(ctx, "failed on UpdateVoucherData", "error", err) + return res, err + } + } + data := common.ProcessVouchers(vouchersResp) // Store all voucher data From aa7497573e7c0d07eebd6d1e278c7af3ea34825d Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Sat, 30 Nov 2024 15:29:28 +0300 Subject: [PATCH 07/10] removed unused code --- internal/handlers/ussd/menuhandler.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 4b7fc7a..ff9d11e 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -10,7 +10,6 @@ import ( "strings" "git.defalsify.org/vise.git/asm" - "github.com/grassrootseconomics/eth-custodial/pkg/api" "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/db" @@ -32,8 +31,6 @@ var ( logg = logging.NewVanilla().WithDomain("ussdmenuhandler") scriptDir = path.Join("services", "registration") translationDir = path.Join(scriptDir, "locale") - okResponse *api.OKResponse - errResponse *api.ErrResponse ) // Define the regex patterns as constants From c2019267d1a77f55a4ae6f373416a5c80a93ef36 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 2 Dec 2024 13:26:46 +0300 Subject: [PATCH 08/10] capitalize the voucher descriptions --- internal/handlers/ussd/menuhandler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 8f206e3..5bc95bc 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1627,7 +1627,7 @@ func (h *Handlers) GetVoucherDetails(ctx context.Context, sym string, input []by } res.Content = fmt.Sprintf( - "name: %s\nsymbol: %s\ncommodity: %s\nlocation: %s", voucherData.TokenName, voucherData.TokenSymbol, voucherData.TokenCommodity, voucherData.TokenLocation, + "Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", voucherData.TokenName, voucherData.TokenSymbol, voucherData.TokenCommodity, voucherData.TokenLocation, ) return res, nil From ef3a3d6717c98a17516281e04f271c8fb6e712fd Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 2 Dec 2024 13:30:33 +0300 Subject: [PATCH 09/10] updated the TestGetVoucherDetails --- internal/handlers/ussd/menuhandler_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 2559a54..6f1d57a 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -2026,7 +2026,6 @@ func TestSetVoucher(t *testing.T) { } func TestGetVoucherDetails(t *testing.T) { - ctx, store := InitializeTestStore(t) fm, err := NewFlagManager(flagsPath) if err != nil { @@ -2056,12 +2055,11 @@ func TestGetVoucherDetails(t *testing.T) { TokenCommodity: "Farming", } expectedResult.Content = fmt.Sprintf( - "name: %s\nsymbol: %s\ncommodity: %s\nlocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation, + "Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation, ) mockAccountService.On("VoucherData", string(tokA_AAddress)).Return(tokenDetails, nil) res, err := h.GetVoucherDetails(ctx, "SessionId", []byte("")) assert.NoError(t, err) assert.Equal(t, expectedResult, res) - } From 406bd84875c7828e67942a0330ff5ecd7383b42c Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 2 Dec 2024 14:53:18 +0000 Subject: [PATCH 10/10] Always reset persister in handler --- internal/handlers/ussd/menuhandler.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 87760bd..a93d637 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -114,6 +114,9 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource logg.WarnCtxf(ctx, "handler init called before it is ready or more than once", "state", h.st, "cache", h.ca) return r, nil } + defer func() { + h.pe = nil + }() h.st = h.pe.GetState() h.ca = h.pe.GetMemory() @@ -133,7 +136,6 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource logg.ErrorCtxf(ctx, "perister fail in handler", "state", h.st, "cache", h.ca) return r, fmt.Errorf("cannot get state and memory for handler") } - h.pe = nil logg.DebugCtxf(ctx, "handler has been initialized", "state", h.st, "cache", h.ca)