forked from urdt/ussd
added model and func to fetch vouchers from the API
This commit is contained in:
parent
3a46fda769
commit
31aea6b807
@ -13,6 +13,7 @@ type AccountServiceInterface interface {
|
||||
CheckBalance(publicKey string) (string, error)
|
||||
CreateAccount() (*models.AccountResponse, error)
|
||||
CheckAccountStatus(trackingId string) (string, error)
|
||||
FetchVouchersFromAPI() ([]models.VoucherHolding, error)
|
||||
}
|
||||
|
||||
type AccountService struct {
|
||||
@ -110,3 +111,31 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||
|
||||
return &accountResp, nil
|
||||
}
|
||||
|
||||
// fetchVouchersFromAPI calls the API to get the list of vouchers belonging to the user
|
||||
func (as *AccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) {
|
||||
// TODO replace with the actual request once ready
|
||||
mockJSON := `[
|
||||
{
|
||||
"symbol": "MUMO",
|
||||
"address": "0x078b3a26596218507781722A4e8825BFB9570Fba"
|
||||
},
|
||||
{
|
||||
"symbol": "SRF",
|
||||
"address": "0x45d747172e77d55575c197CbA9451bC2CD8F4958"
|
||||
},
|
||||
{
|
||||
"symbol": "HALGAN",
|
||||
"address": "0x12169Fb5931A599ad1283bb8311Dad54Feb51A28"
|
||||
}
|
||||
]`
|
||||
|
||||
// Unmarshal the JSON response
|
||||
var holdings []models.VoucherHolding
|
||||
err := json.Unmarshal([]byte(mockJSON), &holdings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return holdings, nil
|
||||
}
|
||||
|
@ -231,36 +231,23 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetVoucherList fetches the list of vouchers and formats them
|
||||
// checks whether they are synced internally before calling the API
|
||||
func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
vouchers := []string{
|
||||
"SRF",
|
||||
"CRF",
|
||||
"VCF",
|
||||
"VSAPA",
|
||||
"FSTMP",
|
||||
"FSAW",
|
||||
"PTAQ",
|
||||
"VCRXT",
|
||||
"VSGAQ",
|
||||
"QPWIQQ",
|
||||
"FSTMP",
|
||||
"FSAW",
|
||||
"PTAQ",
|
||||
"VCRXT",
|
||||
"VSGAQ",
|
||||
"QPWIQQ",
|
||||
"FSTMP",
|
||||
"FSAW",
|
||||
"PTAQ",
|
||||
"VCRXT",
|
||||
"VSGAQ",
|
||||
"QPWIQQ",
|
||||
|
||||
// check if the vouchers exist internally and if not
|
||||
// fetch from the API
|
||||
|
||||
// Fetch vouchers from API
|
||||
vouchers, err := h.accountService.FetchVouchersFromAPI()
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("error fetching vouchers: %w", err)
|
||||
}
|
||||
|
||||
var numberedVouchers []string
|
||||
for i, voucher := range vouchers {
|
||||
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher))
|
||||
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher.Symbol))
|
||||
}
|
||||
res.Content = strings.Join(numberedVouchers, "\n")
|
||||
|
||||
|
@ -23,4 +23,9 @@ func (m *MockAccountService) CheckBalance(publicKey string) (string, error) {
|
||||
func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) {
|
||||
args := m.Called(trackingId)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockAccountService) FetchVouchersFromAPI() ([]models.VoucherHolding, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).([]models.VoucherHolding), args.Error(1)
|
||||
}
|
||||
|
7
internal/models/vouchersresponse.go
Normal file
7
internal/models/vouchersresponse.go
Normal file
@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// VoucherHolding represents a single voucher holding
|
||||
type VoucherHolding struct {
|
||||
Symbol string `json:"symbol"`
|
||||
Address string `json:"address"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user