Skip to content

Commit

Permalink
Merge pull request 'trigger-balance-reload' (#193) from trigger-balan…
Browse files Browse the repository at this point in the history
…ce-reload into master

Reviewed-on: https://git.grassecon.net/urdt/ussd/pulls/193
Reviewed-by: lash <[email protected]>
  • Loading branch information
lash committed Dec 2, 2024
2 parents da46234 + 22e870b commit b615c27
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion common/vouchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 33 additions & 3 deletions internal/handlers/ussd/menuhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,14 +24,13 @@ import (
"gopkg.in/leonelquinteros/gotext.v1"

"git.grassecon.net/urdt/ussd/internal/storage"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
)

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
Expand Down Expand Up @@ -1536,6 +1534,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
Expand Down
6 changes: 3 additions & 3 deletions services/registration/main.vis
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b615c27

Please sign in to comment.