forked from urdt/ussd
Compare commits
47 Commits
menu-profi
...
menu-api-e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
353e24de33
|
||
|
|
1c57c95d93
|
||
|
|
128a354b34
|
||
|
|
81c4189c8e
|
||
|
|
b8e12e5215
|
||
|
|
4f04362835
|
||
|
|
f73b7a8b04
|
||
|
|
0be570ae2d
|
||
|
|
6a36bc43b5
|
||
|
|
1d27a88908
|
||
|
|
4889e6d18b
|
||
|
|
368c25125a
|
||
|
|
283793a2ae
|
||
|
|
bec7e5c69f
|
||
|
|
d638aba85e
|
||
|
|
df7788dd0b
|
||
|
|
4a62773098
|
||
|
|
26d315b032
|
||
|
|
1927544533
|
||
|
|
c641a0c669
|
||
|
|
952da86931
|
||
|
|
be6391686f
|
||
|
|
65794c1b20
|
||
| b058f9d770 | |||
|
|
2a93ea7a0c
|
||
|
|
f89b1acc6c | ||
| ddeafe015b | |||
|
|
1dc8b054eb
|
||
|
|
a3ff3be5b1
|
||
|
|
ea52a7cf0a
|
||
|
|
a993026380
|
||
|
|
4ee241714b
|
||
|
|
d7dc743fa2 | ||
|
|
c220cbb767
|
||
| 0dc322729f | |||
| 726d6dd338 | |||
|
|
ad1f9233ca | ||
|
|
c18c40b1b8 | ||
|
|
a07fce527f | ||
|
|
ddc8f6dad1
|
||
|
|
c2b68231f5
|
||
|
|
3f3dbf414c
|
||
|
|
e4c3e9f015
|
||
|
|
9b71244391 | ||
|
|
ada1f26b68
|
||
|
|
935b777e57
|
||
|
|
19372c17f4
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,3 +4,5 @@ go.work*
|
||||
**/*/*.bin
|
||||
**/*/.state/
|
||||
cmd/.state/
|
||||
id_*
|
||||
*.gdbm
|
||||
|
||||
@@ -87,7 +87,7 @@ func main() {
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
OutputSize: uint32(size),
|
||||
FlagCount: uint32(16),
|
||||
FlagCount: uint32(128),
|
||||
}
|
||||
|
||||
if engineDebug {
|
||||
|
||||
@@ -60,7 +60,7 @@ func main() {
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
OutputSize: uint32(size),
|
||||
FlagCount: uint32(16),
|
||||
FlagCount: uint32(128),
|
||||
}
|
||||
|
||||
if engineDebug {
|
||||
|
||||
@@ -48,7 +48,7 @@ func main() {
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
OutputSize: uint32(size),
|
||||
FlagCount: uint32(16),
|
||||
FlagCount: uint32(128),
|
||||
}
|
||||
|
||||
if engineDebug {
|
||||
|
||||
@@ -40,7 +40,7 @@ func main() {
|
||||
Root: "root",
|
||||
SessionId: sessionId,
|
||||
OutputSize: uint32(size),
|
||||
FlagCount: uint32(16),
|
||||
FlagCount: uint32(128),
|
||||
}
|
||||
|
||||
resourceDir := scriptDir
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package config
|
||||
|
||||
|
||||
|
||||
const (
|
||||
CreateAccountURL = "https://custodial.sarafu.africa/api/account/create"
|
||||
TrackStatusURL = "https://custodial.sarafu.africa/api/track/"
|
||||
BalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
||||
CreateAccountURL = "http://localhost:5003/api/v2/account/create"
|
||||
BalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
||||
TrackURL = "http://localhost:5003/api/v2/account/status"
|
||||
)
|
||||
|
||||
|
||||
@@ -82,7 +82,6 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
|
||||
ls.DbRs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
||||
ls.DbRs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
||||
ls.DbRs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
||||
ls.DbRs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
||||
ls.DbRs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
||||
ls.DbRs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
||||
ls.DbRs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
||||
@@ -93,6 +92,7 @@ func (ls *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
|
||||
ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||
ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||
ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
|
||||
ls.DbRs.AddLocalFunc("fetch_custodial_balances", ussdHandlers.FetchCustodialBalances)
|
||||
|
||||
return ussdHandlers, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -9,104 +10,175 @@ import (
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
)
|
||||
|
||||
var apiResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type AccountServiceInterface interface {
|
||||
CheckBalance(publicKey string) (string, error)
|
||||
CreateAccount() (*models.AccountResponse, error)
|
||||
CheckAccountStatus(trackingId string) (string, error)
|
||||
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
||||
CreateAccount() (*OKResponse, *ErrResponse)
|
||||
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
||||
TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse)
|
||||
}
|
||||
|
||||
type AccountService struct {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CheckAccountStatus retrieves the status of an account transaction based on the provided tracking ID.
|
||||
//
|
||||
// Parameters:
|
||||
// - trackingId: A unique identifier for the account.This should be obtained from a previous call to
|
||||
// CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the
|
||||
// AccountResponse struct can be used here to check the account status during a transaction.
|
||||
//
|
||||
//
|
||||
// Returns:
|
||||
// - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string.
|
||||
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
|
||||
// If no error occurs, this will be nil.
|
||||
//
|
||||
func (as *AccountService) CheckAccountStatus(trackingId string) (string, error) {
|
||||
resp, err := http.Get(config.TrackStatusURL + trackingId)
|
||||
// If no error occurs, this will be nil
|
||||
func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
||||
resp, err := http.Get(config.BalanceURL + trackingId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var trackResp models.TrackStatusResponse
|
||||
err = json.Unmarshal(body, &trackResp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return &trackResp, nil
|
||||
|
||||
status := trackResp.Result.Transaction.Status
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
||||
var errResponse ErrResponse
|
||||
var okResponse OKResponse
|
||||
var err error
|
||||
// Construct the URL with the path parameter
|
||||
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
// Set headers
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-GE-KEY", "xd")
|
||||
|
||||
// Send the request
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read the response body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
|
||||
// Step 2: Unmarshal into the generic struct
|
||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
if apiResponse.Ok {
|
||||
err = json.Unmarshal([]byte(body), &okResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
return &okResponse, nil
|
||||
} else {
|
||||
err := json.Unmarshal([]byte(body), &errResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
return nil, &errResponse
|
||||
}
|
||||
}
|
||||
|
||||
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||
func (as *AccountService) CheckBalance(publicKey string) (string, error) {
|
||||
|
||||
func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
||||
resp, err := http.Get(config.BalanceURL + publicKey)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var balanceResp models.BalanceResponse
|
||||
err = json.Unmarshal(body, &balanceResp)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
balance := balanceResp.Result.Balance
|
||||
return balance, nil
|
||||
return &balanceResp, nil
|
||||
}
|
||||
|
||||
|
||||
//CreateAccount creates a new account in the custodial system.
|
||||
// CreateAccount creates a new account in the custodial system.
|
||||
// Returns:
|
||||
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
||||
// If there is an error during the request or processing, this will be nil.
|
||||
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
|
||||
// If no error occurs, this will be nil.
|
||||
func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||
resp, err := http.Post(config.CreateAccountURL, "application/json", nil)
|
||||
func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
||||
|
||||
var errResponse ErrResponse
|
||||
var okResponse OKResponse
|
||||
var err error
|
||||
|
||||
// Create a new request
|
||||
req, err := http.NewRequest("POST", config.CreateAccountURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-GE-KEY", "xd")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
|
||||
var accountResp models.AccountResponse
|
||||
err = json.Unmarshal(body, &accountResp)
|
||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, &errResponse
|
||||
}
|
||||
if apiResponse.Ok {
|
||||
err = json.Unmarshal([]byte(body), &okResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
return &okResponse, nil
|
||||
} else {
|
||||
err := json.Unmarshal([]byte(body), &errResponse)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
return nil, &errResponse
|
||||
}
|
||||
return nil, &errResponse
|
||||
}
|
||||
|
||||
return &accountResp, nil
|
||||
}
|
||||
|
||||
60
internal/handlers/server/api.go
Normal file
60
internal/handlers/server/api.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package server
|
||||
|
||||
type (
|
||||
OKResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result map[string]any `json:"result"`
|
||||
}
|
||||
|
||||
ErrResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
ErrCode string `json:"errorCode"`
|
||||
}
|
||||
|
||||
TransferRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
To string `json:"to" validate:"required,eth_addr_checksum"`
|
||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
PoolSwapRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
FromTokenAddress string `json:"fromTokenAddress" validate:"required,eth_addr_checksum"`
|
||||
ToTokenAddress string `json:"toTokenAddress" validate:"required,eth_addr_checksum"`
|
||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
PoolDepositRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr_checksum"`
|
||||
TokenAddress string `json:"tokenAddress" validate:"required,eth_addr_checksum"`
|
||||
PoolAddress string `json:"poolAddress" validate:"required,eth_addr_checksum"`
|
||||
Amount string `json:"amount" validate:"required,number,gt=0"`
|
||||
}
|
||||
|
||||
AccountAddressParam struct {
|
||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
||||
}
|
||||
|
||||
TrackingIDParam struct {
|
||||
TrackingID string `param:"trackingId" validate:"required,uuid"`
|
||||
}
|
||||
|
||||
OTXByAccountRequest struct {
|
||||
Address string `param:"address" validate:"required,eth_addr_checksum"`
|
||||
PerPage int `query:"perPage" validate:"required,number,gt=0"`
|
||||
Cursor int `query:"cursor" validate:"number"`
|
||||
Next bool `query:"next"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
ErrCodeInternalServerError = "E01"
|
||||
ErrCodeInvalidJSON = "E02"
|
||||
ErrCodeInvalidAPIKey = "E03"
|
||||
ErrCodeValidationFailed = "E04"
|
||||
ErrCodeAccountNotExists = "E05"
|
||||
)
|
||||
@@ -27,6 +27,8 @@ var (
|
||||
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
|
||||
scriptDir = path.Join("services", "registration")
|
||||
translationDir = path.Join(scriptDir, "locale")
|
||||
okResponse *server.OKResponse
|
||||
errResponse *server.ErrResponse
|
||||
)
|
||||
|
||||
// FlagManager handles centralized flag management
|
||||
@@ -136,11 +138,16 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
||||
}
|
||||
|
||||
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||
accountResp, err := h.accountService.CreateAccount()
|
||||
okResponse, errResponse := h.accountService.CreateAccount()
|
||||
if errResponse != nil {
|
||||
return nil
|
||||
}
|
||||
trackingId := okResponse.Result["trackingId"].(string)
|
||||
publicKey := okResponse.Result["publicKey"].(string)
|
||||
|
||||
data := map[utils.DataTyp]string{
|
||||
utils.DATA_TRACKING_ID: accountResp.Result.TrackingId,
|
||||
utils.DATA_PUBLIC_KEY: accountResp.Result.PublicKey,
|
||||
utils.DATA_CUSTODIAL_ID: accountResp.Result.CustodialId.String(),
|
||||
utils.DATA_TRACKING_ID: trackingId,
|
||||
utils.DATA_PUBLIC_KEY: publicKey,
|
||||
}
|
||||
|
||||
for key, value := range data {
|
||||
@@ -152,7 +159,7 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r
|
||||
}
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||
return err
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
@@ -191,7 +198,6 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
|
||||
}
|
||||
|
||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||
|
||||
accountPIN := string(input)
|
||||
// Validate that the PIN is a 4-digit number
|
||||
if !isValidPIN(accountPIN) {
|
||||
@@ -290,8 +296,6 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
//AccountPin, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN)
|
||||
store := h.userdataStore
|
||||
AccountPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN)
|
||||
if err != nil {
|
||||
@@ -370,7 +374,6 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
if len(input) == 4 {
|
||||
yob := string(input)
|
||||
store := h.userdataStore
|
||||
@@ -413,7 +416,6 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
gender := strings.Split(symbol, "_")[1]
|
||||
store := h.userdataStore
|
||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_GENDER, []byte(gender))
|
||||
@@ -432,7 +434,6 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
if len(input) > 0 {
|
||||
offerings := string(input)
|
||||
store := h.userdataStore
|
||||
@@ -458,7 +459,6 @@ func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byt
|
||||
// ResetAccountAuthorized resets the account authorization flag after a successful PIN entry.
|
||||
func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
|
||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
@@ -468,12 +468,10 @@ func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input
|
||||
// CheckIdentifier retrieves the PublicKey from the JSON data file.
|
||||
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
store := h.userdataStore
|
||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
|
||||
@@ -487,12 +485,10 @@ func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte
|
||||
func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var err error
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||
flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
|
||||
@@ -525,9 +521,7 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res
|
||||
// ResetIncorrectPin resets the incorrect pin flag after a new PIN attempt.
|
||||
func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
|
||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
||||
return res, nil
|
||||
}
|
||||
@@ -539,29 +533,27 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
||||
|
||||
flag_account_success, _ := h.flagManager.GetFlag("flag_account_success")
|
||||
flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
store := h.userdataStore
|
||||
trackingId, err := store.ReadEntry(ctx, sessionId, utils.DATA_TRACKING_ID)
|
||||
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
status, err := h.accountService.CheckAccountStatus(string(trackingId))
|
||||
if err != nil {
|
||||
fmt.Println("Error checking account status:", err)
|
||||
okResponse, errResponse = h.accountService.TrackAccountStatus(string(publicKey))
|
||||
if errResponse != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
err = store.WriteEntry(ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(status))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
isActive := okResponse.Result["active"].(bool)
|
||||
if !ok {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if status == "SUCCESS" {
|
||||
if isActive {
|
||||
res.FlagSet = append(res.FlagSet, flag_account_success)
|
||||
res.FlagReset = append(res.FlagReset, flag_account_pending)
|
||||
} else {
|
||||
@@ -641,6 +633,8 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
||||
var res resource.Result
|
||||
var err error
|
||||
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
@@ -652,15 +646,60 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
||||
return res, err
|
||||
}
|
||||
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
if !balanceResponse.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
return res, nil
|
||||
}
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
balance := balanceResponse.Result.Balance
|
||||
res.Content = balance
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
symbol, _ := h.st.Where()
|
||||
balanceType := strings.Split(symbol, "_")[0]
|
||||
|
||||
store := h.userdataStore
|
||||
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
if !balanceResponse.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
return res, nil
|
||||
}
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
balance := balanceResponse.Result.Balance
|
||||
|
||||
switch balanceType {
|
||||
case "my":
|
||||
res.Content = fmt.Sprintf("Your balance is %s", balance)
|
||||
case "community":
|
||||
res.Content = fmt.Sprintf("Your community balance is %s", balance)
|
||||
default:
|
||||
break
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ValidateRecipient validates that the given input is a valid phone number.
|
||||
func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
@@ -757,10 +796,11 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
|
||||
store := h.userdataStore
|
||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
balanceResp, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
balance := balanceResp.Result.Balance
|
||||
|
||||
res.Content = balance
|
||||
|
||||
@@ -779,18 +819,25 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
||||
}
|
||||
|
||||
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
store := h.userdataStore
|
||||
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
|
||||
amountStr := string(input)
|
||||
|
||||
balanceStr, err := h.accountService.CheckBalance(string(publicKey))
|
||||
balanceRes, err := h.accountService.CheckBalance(string(publicKey))
|
||||
balanceStr := balanceRes.Result.Balance
|
||||
|
||||
if !balanceRes.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
return res, nil
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Content = balanceStr
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
|
||||
// Parse the balance
|
||||
balanceParts := strings.Split(balanceStr, " ")
|
||||
@@ -882,36 +929,6 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// QuickWithBalance retrieves the balance for a given public key from the custodial balance API endpoint before
|
||||
// gracefully exiting the session.
|
||||
func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var err error
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
|
||||
store := h.userdataStore
|
||||
publicKey, err := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
res.Content = l.Get("Your account balance is %s", balance)
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// InitiateTransaction returns a confirmation and resets the transaction data
|
||||
// on the gdbm store.
|
||||
func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
@@ -945,16 +962,23 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetProfileInfo retrieves and formats the profile information of a user from a Gdbm backed storage.
|
||||
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var defaultValue string
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
// Default value when an entry is not found
|
||||
defaultValue := "Not Provided"
|
||||
language, ok := ctx.Value("Language").(lang.Language)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("value for 'Language' is not of type lang.Language")
|
||||
}
|
||||
code := language.Code
|
||||
if code == "swa" {
|
||||
defaultValue = "Haipo"
|
||||
} else {
|
||||
defaultValue = "Not Provided"
|
||||
}
|
||||
|
||||
// Helper function to handle nil byte slices and convert them to string
|
||||
getEntryOrDefault := func(entry []byte, err error) string {
|
||||
@@ -991,12 +1015,23 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
|
||||
return res, fmt.Errorf("invalid year of birth: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Format the result
|
||||
res.Content = fmt.Sprintf(
|
||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||
name, gender, age, location, offerings,
|
||||
)
|
||||
switch language.Code {
|
||||
case "eng":
|
||||
res.Content = fmt.Sprintf(
|
||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||
name, gender, age, location, offerings,
|
||||
)
|
||||
case "swa":
|
||||
res.Content = fmt.Sprintf(
|
||||
"Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n",
|
||||
name, gender, age, location, offerings,
|
||||
)
|
||||
default:
|
||||
res.Content = fmt.Sprintf(
|
||||
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||
name, gender, age, location, offerings,
|
||||
)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
@@ -10,17 +11,40 @@ type MockAccountService struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) {
|
||||
func (m *MockAccountService) CreateAccount() (*server.OKResponse, *server.ErrResponse) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(*models.AccountResponse), args.Error(1)
|
||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
||||
|
||||
if ok {
|
||||
return okResponse, nil
|
||||
}
|
||||
|
||||
if err {
|
||||
return nil, errResponse
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CheckBalance(publicKey string) (string, error) {
|
||||
func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
|
||||
args := m.Called(publicKey)
|
||||
return args.String(0), args.Error(1)
|
||||
return args.Get(0).(*models.BalanceResponse), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) {
|
||||
func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
|
||||
args := m.Called(trackingId)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) TrackAccountStatus(publicKey string) (*server.OKResponse, *server.ErrResponse) {
|
||||
args := m.Called(publicKey)
|
||||
okResponse, ok := args.Get(0).(*server.OKResponse)
|
||||
errResponse, err := args.Get(1).(*server.ErrResponse)
|
||||
if ok {
|
||||
return okResponse, nil
|
||||
}
|
||||
if err {
|
||||
return nil, errResponse
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
)
|
||||
|
||||
type AccountResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
CustodialId json.Number `json:"custodialId"`
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"` // Include the description field
|
||||
Result struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
} `json:"result"`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type TrackStatusResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
@@ -17,4 +16,4 @@ type TrackStatusResponse struct {
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
||||
}
|
||||
|
||||
43
internal/storage/db.go
Normal file
43
internal/storage/db.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
)
|
||||
|
||||
const (
|
||||
DATATYPE_USERSUB = 64
|
||||
)
|
||||
|
||||
type SubPrefixDb struct {
|
||||
store db.Db
|
||||
pfx []byte
|
||||
}
|
||||
|
||||
func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
|
||||
return &SubPrefixDb{
|
||||
store: store,
|
||||
pfx: pfx,
|
||||
}
|
||||
}
|
||||
|
||||
func(s *SubPrefixDb) toKey(k []byte) []byte {
|
||||
return append(s.pfx, k...)
|
||||
}
|
||||
|
||||
func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
||||
key = s.toKey(key)
|
||||
v, err := s.store.Get(ctx, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func(s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {
|
||||
s.store.SetPrefix(DATATYPE_USERSUB)
|
||||
key = s.toKey(key)
|
||||
return s.store.Put(ctx, key, val)
|
||||
}
|
||||
54
internal/storage/db_test.go
Normal file
54
internal/storage/db_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
memdb "git.defalsify.org/vise.git/db/mem"
|
||||
)
|
||||
|
||||
func TestSubPrefix(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := memdb.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sdba := NewSubPrefixDb(db, []byte("tinkywinky"))
|
||||
err = sdba.Put(ctx, []byte("foo"), []byte("dipsy"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := sdba.Get(ctx, []byte("foo"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(r, []byte("dipsy")) {
|
||||
t.Fatalf("expected 'dipsy', got %s", r)
|
||||
}
|
||||
|
||||
sdbb := NewSubPrefixDb(db, []byte("lala"))
|
||||
r, err = sdbb.Get(ctx, []byte("foo"))
|
||||
if err == nil {
|
||||
t.Fatal("expected not found")
|
||||
}
|
||||
|
||||
err = sdbb.Put(ctx, []byte("foo"), []byte("pu"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err = sdbb.Get(ctx, []byte("foo"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(r, []byte("pu")) {
|
||||
t.Fatalf("expected 'pu', got %s", r)
|
||||
}
|
||||
|
||||
r, err = sdba.Get(ctx, []byte("foo"))
|
||||
if !bytes.Equal(r, []byte("dipsy")) {
|
||||
t.Fatalf("expected 'dipsy', got %s", r)
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,6 @@ import (
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
)
|
||||
|
||||
const (
|
||||
DATATYPE_CUSTOM = 128
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
Persister *persist.Persister
|
||||
UserdataDb db.Db
|
||||
|
||||
@@ -5,6 +5,7 @@ MOUT back 0
|
||||
HALT
|
||||
LOAD validate_amount 64
|
||||
RELOAD validate_amount
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
CATCH invalid_amount flag_invalid_amount 1
|
||||
INCMP _ 0
|
||||
LOAD get_recipient 12
|
||||
|
||||
1
services/registration/api_failure
Normal file
1
services/registration/api_failure
Normal file
@@ -0,0 +1 @@
|
||||
Failed to connect to the custodial service.Please try again.
|
||||
5
services/registration/api_failure.vis
Normal file
5
services/registration/api_failure.vis
Normal file
@@ -0,0 +1,5 @@
|
||||
MOUT retry 0
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP quit 9
|
||||
@@ -1,4 +1,5 @@
|
||||
LOAD reset_account_authorized 0
|
||||
RELOAD reset_account_authorized
|
||||
MOUT my_balance 1
|
||||
MOUT community_balance 2
|
||||
MOUT back 0
|
||||
|
||||
@@ -1 +1 @@
|
||||
Your community balance is: 0.00SRF
|
||||
{{.fetch_custodial_balances}}
|
||||
@@ -1,5 +1,11 @@
|
||||
LOAD reset_incorrect 0
|
||||
LOAD reset_incorrect 6
|
||||
LOAD fetch_custodial_balances 0
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
MAP fetch_custodial_balances
|
||||
CATCH incorrect_pin flag_incorrect_pin 1
|
||||
CATCH pin_entry flag_account_authorized 0
|
||||
LOAD quit_with_balance 0
|
||||
MOUT back 0
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP quit 9
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
LOAD check_balance 64
|
||||
RELOAD check_balance
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
MAP check_balance
|
||||
MOUT send 1
|
||||
MOUT vouchers 2
|
||||
|
||||
@@ -1 +1 @@
|
||||
Your balance is: 0.00 SRF
|
||||
{{.fetch_custodial_balances}}
|
||||
@@ -1,5 +1,11 @@
|
||||
LOAD reset_incorrect 0
|
||||
LOAD reset_incorrect 6
|
||||
LOAD fetch_custodial_balances 0
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
MAP fetch_custodial_balances
|
||||
CATCH incorrect_pin flag_incorrect_pin 1
|
||||
CATCH pin_entry flag_account_authorized 0
|
||||
LOAD quit_with_balance 0
|
||||
MOUT back 0
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP quit 9
|
||||
|
||||
@@ -14,3 +14,4 @@ flag,flag_valid_pin,20,this is set when the given PIN is valid
|
||||
flag,flag_allow_update,21,this is set to allow a user to update their profile data
|
||||
flag,flag_single_edit,22,this is set to allow a user to edit a single profile item such as year of birth
|
||||
flag,flag_incorrect_date_format,23,this is set when the given year of birth is invalid
|
||||
flag,flag_api_call_error,25,this is set when communication to an external service fails
|
||||
|
||||
|
@@ -1,6 +1,8 @@
|
||||
CATCH select_language flag_language_set 0
|
||||
CATCH terms flag_account_created 0
|
||||
LOAD check_account_status 0
|
||||
RELOAD check_account_status
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
CATCH account_pending flag_account_pending 1
|
||||
CATCH create_pin flag_pin_set 0
|
||||
CATCH main flag_account_success 1
|
||||
|
||||
1
services/registration/view_menu_swa
Normal file
1
services/registration/view_menu_swa
Normal file
@@ -0,0 +1 @@
|
||||
Angalia Wasifu
|
||||
Reference in New Issue
Block a user