From b40ad782949f682e02eb90f6347e6c122476a8f1 Mon Sep 17 00:00:00 2001
From: alfred-mk <alfredmwaik@gmail.com>
Date: Fri, 15 Nov 2024 21:03:57 +0300
Subject: [PATCH] add the GetVoucherDetails function

---
 internal/handlers/handlerservice.go   |  1 +
 internal/handlers/ussd/menuhandler.go | 33 +++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go
index 358d492..e0cad8f 100644
--- a/internal/handlers/handlerservice.go
+++ b/internal/handlers/handlerservice.go
@@ -109,6 +109,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountServiceIn
 	ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList)
 	ls.DbRs.AddLocalFunc("view_voucher", ussdHandlers.ViewVoucher)
 	ls.DbRs.AddLocalFunc("set_voucher", ussdHandlers.SetVoucher)
+	ls.DbRs.AddLocalFunc("get_voucher_details", ussdHandlers.GetVoucherDetails)
 	ls.DbRs.AddLocalFunc("reset_valid_pin", ussdHandlers.ResetValidPin)
 	ls.DbRs.AddLocalFunc("check_pin_mismatch", ussdHandlers.CheckPinMisMatch)
 	ls.DbRs.AddLocalFunc("validate_blocked_number", ussdHandlers.ValidateBlockedNumber)
diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go
index 0b9360f..8a3e8cf 100644
--- a/internal/handlers/ussd/menuhandler.go
+++ b/internal/handlers/ussd/menuhandler.go
@@ -1624,3 +1624,36 @@ func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (re
 	res.Content = tempData.TokenSymbol
 	return res, nil
 }
+
+// GetVoucherDetails retrieves the voucher details
+func (h *Handlers) GetVoucherDetails(ctx context.Context, sym string, input []byte) (resource.Result, error) {
+	var res resource.Result
+	store := h.userdataStore
+	sessionId, ok := ctx.Value("SessionId").(string)
+	if !ok {
+		return res, fmt.Errorf("missing session")
+	}
+
+	flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
+
+	// get the active address
+	activeAddress, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_ADDRESS)
+	if err != nil {
+		logg.ErrorCtxf(ctx, "failed to read activeAddress entry with", "key", common.DATA_ACTIVE_ADDRESS, "error", err)
+		return res, err
+	}
+
+	// use the voucher contract address to get the data from the API
+	voucherData, err := h.accountService.VoucherData(ctx, string(activeAddress))
+	if err != nil {
+		res.FlagSet = append(res.FlagSet, flag_api_error)
+		return res, nil
+	}
+
+	tokenSymbol := voucherData.TokenSymbol
+	tokenName := voucherData.TokenName
+
+	res.Content = fmt.Sprintf("%s %s", tokenSymbol, tokenName)
+
+	return res, nil
+}