menu-voucherlist #101
@ -3,7 +3,6 @@ package ussd
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -251,20 +250,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal the stored JSON data into the correct struct
|
res.Content = string(voucherData)
|
||||||
var vouchers []struct {
|
|
||||||
TokenSymbol string `json:"tokenSymbol"`
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(voucherData, &vouchers)
|
|
||||||
if err != nil {
|
|
||||||
return res, fmt.Errorf("failed to unmarshal vouchers: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var numberedVouchers []string
|
|
||||||
for i, voucher := range vouchers {
|
|
||||||
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol))
|
|
||||||
}
|
|
||||||
res.Content = strings.Join(numberedVouchers, "\n")
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -1027,8 +1013,6 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
|
|||||||
// them to gdbm
|
// them to gdbm
|
||||||
func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
var err error
|
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
@ -1040,20 +1024,20 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch vouchers from the API
|
// Fetch vouchers from the API using the public key
|
||||||
vouchersResp, err := h.accountService.FetchVouchers(string(publicKey))
|
vouchersResp, err := h.accountService.FetchVouchers(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
Alfred-mk marked this conversation as resolved
Outdated
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert only the list of holdings (vouchers) to JSON
|
var numberedVouchers []string
|
||||||
voucherBytes, err := json.Marshal(vouchersResp.Result.Holdings)
|
for i, voucher := range vouchersResp.Result.Holdings {
|
||||||
if err != nil {
|
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.TokenSymbol))
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the voucher symbols in the userdataStore
|
voucherList := strings.Join(numberedVouchers, "\n")
|
||||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, voucherBytes)
|
|
||||||
|
err = store.WriteEntry(ctx, sessionId, utils.DATA_VOUCHER_LIST, []byte(voucherList))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,11 @@ func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func(s *SubPrefixDb) toKey(k []byte) []byte {
|
func (s *SubPrefixDb) toKey(k []byte) []byte {
|
||||||
return append(s.pfx, k...)
|
return append(s.pfx, k...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
s.store.SetPrefix(DATATYPE_USERSUB)
|
||||||
key = s.toKey(key)
|
key = s.toKey(key)
|
||||||
v, err := s.store.Get(ctx, key)
|
v, err := s.store.Get(ctx, key)
|
||||||
@ -36,7 +36,7 @@ func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
|||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func(s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
|
func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
|
||||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
s.store.SetPrefix(DATATYPE_USERSUB)
|
||||||
key = s.toKey(key)
|
key = s.toKey(key)
|
||||||
return s.store.Put(ctx, key, val)
|
return s.store.Put(ctx, key, val)
|
||||||
|
Loading…
Reference in New Issue
Block a user
Can we put this in a separate method please (that can be unit-tested with data only)