Compare commits
3 Commits
master
...
add-balanc
Author | SHA1 | Date | |
---|---|---|---|
9234bfd579 | |||
706b6fe629 | |||
a543569236 |
@ -2141,17 +2141,25 @@ func (h *MenuHandlers) GetVoucherList(ctx context.Context, sym string, input []b
|
||||
|
||||
// Read vouchers from the store
|
||||
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
||||
logg.InfoCtxf(ctx, "reading GetVoucherList entries for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
|
||||
logg.InfoCtxf(ctx, "reading voucherData in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
formattedData := h.ReplaceSeparatorFunc(string(voucherData))
|
||||
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES)
|
||||
logg.InfoCtxf(ctx, "reading voucherBalances in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_BALANCES, "voucherBalances", voucherBalances)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_BALANCES, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
logg.InfoCtxf(ctx, "final output for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "formattedData", formattedData)
|
||||
formattedVoucherList := store.FormatVoucherList(ctx, string(voucherData), string(voucherBalances))
|
||||
finalOutput := strings.Join(formattedVoucherList, "\n")
|
||||
|
||||
res.Content = string(formattedData)
|
||||
logg.InfoCtxf(ctx, "final output for GetVoucherList", "sessionId", sessionId, "finalOutput", finalOutput)
|
||||
|
||||
res.Content = finalOutput
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
@ -2208,20 +2208,25 @@ func TestGetVoucherList(t *testing.T) {
|
||||
ReplaceSeparatorFunc: mockReplaceSeparator,
|
||||
}
|
||||
|
||||
mockSyms := []byte("1:SRF\n2:MILO")
|
||||
mockSymbols := []byte("1:SRF\n2:MILO")
|
||||
mockBalances := []byte("1:10.099999\n2:40.7")
|
||||
|
||||
// Put voucher sym data from the store
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSyms)
|
||||
// Put voucher symnols and balances data to the store
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSymbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES, mockBalances)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedSyms := []byte("1: SRF\n2: MILO")
|
||||
expectedList := []byte("1: SRF 10.09\n2: MILO 40.70")
|
||||
|
||||
res, err := h.GetVoucherList(ctx, "", []byte(""))
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, res.Content, string(expectedSyms))
|
||||
assert.Equal(t, res.Content, string(expectedList))
|
||||
}
|
||||
|
||||
func TestViewVoucher(t *testing.T) {
|
||||
|
@ -182,3 +182,30 @@ func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, d
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FormatVoucherList combines the voucher symbols with their balances (SRF 0.11)
|
||||
func FormatVoucherList(ctx context.Context, symbolsData, balancesData string) []string {
|
||||
symbols := strings.Split(symbolsData, "\n")
|
||||
balances := strings.Split(balancesData, "\n")
|
||||
|
||||
var combined []string
|
||||
for i := 0; i < len(symbols) && i < len(balances); i++ {
|
||||
symbolParts := strings.SplitN(symbols[i], ":", 2)
|
||||
balanceParts := strings.SplitN(balances[i], ":", 2)
|
||||
|
||||
if len(symbolParts) == 2 && len(balanceParts) == 2 {
|
||||
index := strings.TrimSpace(symbolParts[0])
|
||||
symbol := strings.TrimSpace(symbolParts[1])
|
||||
rawBalance := strings.TrimSpace(balanceParts[1])
|
||||
|
||||
formattedBalance, err := TruncateDecimalString(rawBalance, 2)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to format balance", "balance", rawBalance, "error", err)
|
||||
formattedBalance = rawBalance
|
||||
}
|
||||
|
||||
combined = append(combined, fmt.Sprintf("%s: %s %s", index, symbol, formattedBalance))
|
||||
}
|
||||
}
|
||||
return combined
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user