forked from urdt/ussd
Export voucher related code
This commit is contained in:
parent
33bba73a65
commit
4bf56c525f
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
@ -96,13 +95,13 @@ func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol,
|
||||
return
|
||||
}
|
||||
|
||||
// StoreTemporaryVoucher saves voucher metadata as temporary entries in the common.DataStore.
|
||||
func StoreTemporaryVoucher(ctx context.Context, store common.DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
entries := map[common.DataTyp][]byte{
|
||||
common.DATA_TEMPORARY_SYM: []byte(data.TokenSymbol),
|
||||
common.DATA_TEMPORARY_BAL: []byte(data.Balance),
|
||||
common.DATA_TEMPORARY_DECIMAL: []byte(data.TokenDecimals),
|
||||
common.DATA_TEMPORARY_ADDRESS: []byte(data.ContractAddress),
|
||||
// StoreTemporaryVoucher saves voucher metadata as temporary entries in the DataStore.
|
||||
func StoreTemporaryVoucher(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
entries := map[DataTyp][]byte{
|
||||
DATA_TEMPORARY_SYM: []byte(data.TokenSymbol),
|
||||
DATA_TEMPORARY_BAL: []byte(data.Balance),
|
||||
DATA_TEMPORARY_DECIMAL: []byte(data.TokenDecimals),
|
||||
DATA_TEMPORARY_ADDRESS: []byte(data.ContractAddress),
|
||||
}
|
||||
|
||||
for key, value := range entries {
|
||||
@ -113,13 +112,13 @@ func StoreTemporaryVoucher(ctx context.Context, store common.DataStore, sessionI
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTemporaryVoucherData retrieves temporary voucher metadata from the common.DataStore.
|
||||
func GetTemporaryVoucherData(ctx context.Context, store common.DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []common.DataTyp{
|
||||
common.DATA_TEMPORARY_SYM,
|
||||
common.DATA_TEMPORARY_BAL,
|
||||
common.DATA_TEMPORARY_DECIMAL,
|
||||
common.DATA_TEMPORARY_ADDRESS,
|
||||
// GetTemporaryVoucherData retrieves temporary voucher metadata from the DataStore.
|
||||
func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []DataTyp{
|
||||
DATA_TEMPORARY_SYM,
|
||||
DATA_TEMPORARY_BAL,
|
||||
DATA_TEMPORARY_DECIMAL,
|
||||
DATA_TEMPORARY_ADDRESS,
|
||||
}
|
||||
|
||||
data := &dataserviceapi.TokenHoldings{}
|
||||
@ -141,14 +140,14 @@ func GetTemporaryVoucherData(ctx context.Context, store common.DataStore, sessio
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// UpdateVoucherData sets the active voucher data and clears the temporary voucher data in the common.DataStore.
|
||||
func UpdateVoucherData(ctx context.Context, store common.DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
// UpdateVoucherData sets the active voucher data and clears the temporary voucher data in the DataStore.
|
||||
func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
|
||||
// Active voucher data entries
|
||||
activeEntries := map[common.DataTyp][]byte{
|
||||
common.DATA_ACTIVE_SYM: []byte(data.TokenSymbol),
|
||||
common.DATA_ACTIVE_BAL: []byte(data.Balance),
|
||||
common.DATA_ACTIVE_DECIMAL: []byte(data.TokenDecimals),
|
||||
common.DATA_ACTIVE_ADDRESS: []byte(data.ContractAddress),
|
||||
activeEntries := map[DataTyp][]byte{
|
||||
DATA_ACTIVE_SYM: []byte(data.TokenSymbol),
|
||||
DATA_ACTIVE_BAL: []byte(data.Balance),
|
||||
DATA_ACTIVE_DECIMAL: []byte(data.TokenDecimals),
|
||||
DATA_ACTIVE_ADDRESS: []byte(data.ContractAddress),
|
||||
}
|
||||
|
||||
// Write active data
|
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
@ -1117,7 +1117,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
data := utils.ProcessVouchers(vouchersResp)
|
||||
data := common.ProcessVouchers(vouchersResp)
|
||||
|
||||
// Store all voucher data
|
||||
dataMap := map[string]string{
|
||||
@ -1167,7 +1167,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
|
||||
return res, nil
|
||||
}
|
||||
|
||||
metadata, err := utils.GetVoucherData(ctx, h.prefixDb, inputStr)
|
||||
metadata, err := common.GetVoucherData(ctx, h.prefixDb, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve voucher data: %v", err)
|
||||
}
|
||||
@ -1177,7 +1177,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
|
||||
return res, nil
|
||||
}
|
||||
|
||||
if err := utils.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
|
||||
if err := common.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
@ -1198,13 +1198,13 @@ func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (re
|
||||
|
||||
|
||||
// Get temporary data
|
||||
tempData, err := utils.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
||||
tempData, err := common.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Set as active and clear temporary data
|
||||
if err := utils.UpdateVoucherData(ctx, h.userdataStore, sessionId, tempData); err != nil {
|
||||
if err := common.UpdateVoucherData(ctx, h.userdataStore, sessionId, tempData); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user