Compare commits
No commits in common. "c2a4efde2bff51006ce5dcc4b5f6035640899899" and "c9deca11805f82c023d7026e8b37978ab319566c" have entirely different histories.
c2a4efde2b
...
c9deca1180
@ -151,7 +151,7 @@ func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId str
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateVoucherData updates the active voucher data in the DataStore.
|
// UpdateVoucherData sets the active voucher data in the DataStore.
|
||||||
func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||||
logg.TraceCtxf(ctx, "dtal", "data", data)
|
logg.TraceCtxf(ctx, "dtal", "data", data)
|
||||||
// Active voucher data entries
|
// Active voucher data entries
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/asm"
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/cache"
|
"git.defalsify.org/vise.git/cache"
|
||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
@ -24,13 +25,14 @@ import (
|
|||||||
"gopkg.in/leonelquinteros/gotext.v1"
|
"gopkg.in/leonelquinteros/gotext.v1"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
translationDir = path.Join(scriptDir, "locale")
|
translationDir = path.Join(scriptDir, "locale")
|
||||||
|
okResponse *api.OKResponse
|
||||||
|
errResponse *api.ErrResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define the regex patterns as constants
|
// Define the regex patterns as constants
|
||||||
@ -112,9 +114,6 @@ 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)
|
logg.WarnCtxf(ctx, "handler init called before it is ready or more than once", "state", h.st, "cache", h.ca)
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
h.pe = nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
h.st = h.pe.GetState()
|
h.st = h.pe.GetState()
|
||||||
h.ca = h.pe.GetMemory()
|
h.ca = h.pe.GetMemory()
|
||||||
@ -134,6 +133,7 @@ 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)
|
logg.ErrorCtxf(ctx, "perister fail in handler", "state", h.st, "cache", h.ca)
|
||||||
return r, fmt.Errorf("cannot get state and memory for handler")
|
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)
|
logg.DebugCtxf(ctx, "handler has been initialized", "state", h.st, "cache", h.ca)
|
||||||
|
|
||||||
@ -1534,38 +1534,6 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
|||||||
return res, nil
|
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)
|
data := common.ProcessVouchers(vouchersResp)
|
||||||
|
|
||||||
// Store all voucher data
|
// Store all voucher data
|
||||||
@ -1690,9 +1658,10 @@ func (h *Handlers) GetVoucherDetails(ctx context.Context, sym string, input []by
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = fmt.Sprintf(
|
tokenSymbol := voucherData.TokenSymbol
|
||||||
"Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", voucherData.TokenName, voucherData.TokenSymbol, voucherData.TokenCommodity, voucherData.TokenLocation,
|
tokenName := voucherData.TokenName
|
||||||
)
|
|
||||||
|
res.Content = fmt.Sprintf("%s %s", tokenSymbol, tokenName)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@ -2024,42 +2024,3 @@ func TestSetVoucher(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, string(tempData.TokenSymbol), res.Content)
|
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)
|
|
||||||
}
|
|
||||||
|
@ -5,6 +5,4 @@ type VoucherDataResult struct {
|
|||||||
TokenSymbol string `json:"tokenSymbol"`
|
TokenSymbol string `json:"tokenSymbol"`
|
||||||
TokenDecimals int `json:"tokenDecimals"`
|
TokenDecimals int `json:"tokenDecimals"`
|
||||||
SinkAddress string `json:"sinkAddress"`
|
SinkAddress string `json:"sinkAddress"`
|
||||||
TokenCommodity string `json:"tokenCommodity"`
|
|
||||||
TokenLocation string `json:"tokenLocation"`
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
LOAD set_default_voucher 8
|
LOAD set_default_voucher 8
|
||||||
RELOAD set_default_voucher
|
RELOAD set_default_voucher
|
||||||
LOAD check_vouchers 10
|
|
||||||
RELOAD check_vouchers
|
|
||||||
LOAD check_balance 64
|
LOAD check_balance 64
|
||||||
RELOAD check_balance
|
RELOAD check_balance
|
||||||
|
LOAD check_vouchers 10
|
||||||
|
RELOAD check_vouchers
|
||||||
CATCH api_failure flag_api_call_error 1
|
CATCH api_failure flag_api_call_error 1
|
||||||
MAP check_balance
|
MAP check_balance
|
||||||
MOUT send 1
|
MOUT send 1
|
||||||
|
Loading…
Reference in New Issue
Block a user