From a5435692365cdb0a7bd459a9a926d12528d7cdee Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 30 Jun 2025 13:14:34 +0300 Subject: [PATCH 1/3] add a helper function FormatVoucherList to combine the voucher symbols with their balances --- store/vouchers.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/store/vouchers.go b/store/vouchers.go index 120358c..a8f4504 100644 --- a/store/vouchers.go +++ b/store/vouchers.go @@ -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 +} From 706b6fe6299bc7208916e781feb01dbbf435077a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 30 Jun 2025 13:31:55 +0300 Subject: [PATCH 2/3] include the balance next to each voucher --- handlers/application/menuhandler.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 77abdd2..94748eb 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -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 } From 9234bfd57982740fd39e1f4071ffdb13754e743a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 30 Jun 2025 13:36:07 +0300 Subject: [PATCH 3/3] update the TestGetVoucherList to the expected content --- handlers/application/menuhandler_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index f7a23ba..48ee27b 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -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) {