Compare commits

...

2 Commits

2 changed files with 39 additions and 4 deletions

View File

@ -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
}

View File

@ -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
}