Compare commits
No commits in common. "3c66a2ded2440fda52fd64db8e00ffae36012ac6" and "ee649dc79e7af6a7c2d2e01ce25ac44f07ec4269" have entirely different histories.
3c66a2ded2
...
ee649dc79e
150
cmd/main.go
150
cmd/main.go
@ -6,6 +6,8 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -17,8 +19,6 @@ import (
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.grassecon.net/urdt/ussd/internal/server/handlers"
|
||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -38,6 +38,42 @@ const (
|
||||
USERFLAG_INCORRECTDATEFORMAT
|
||||
)
|
||||
|
||||
const (
|
||||
createAccountURL = "https://custodial.sarafu.africa/api/account/create"
|
||||
trackStatusURL = "https://custodial.sarafu.africa/api/track/"
|
||||
checkBalanceURL = "https://custodial.sarafu.africa/api/account/status/"
|
||||
)
|
||||
|
||||
type accountResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
CustodialId json.Number `json:"custodialId"`
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
type trackStatusResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Transaction struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
TransferValue json.Number `json:"transferValue"`
|
||||
TxHash string `json:"txHash"`
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
type balanceResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Balance string `json:"balance"`
|
||||
Nonce json.Number `json:"nonce"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
type fsData struct {
|
||||
path string
|
||||
st *state.State
|
||||
@ -264,7 +300,9 @@ func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte)
|
||||
return res, err
|
||||
}
|
||||
f.Close()
|
||||
accountResp, err := handlers.CreateAccount()
|
||||
|
||||
accountResp, err := createAccount()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Failed to create account:", err)
|
||||
return res, err
|
||||
@ -361,6 +399,8 @@ func (fsd *fsData) reset_incorrect_pin(ctx context.Context, sym string, input []
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (fsd *fsData) check_account_status(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
fp := fsd.path + "_data"
|
||||
@ -376,8 +416,7 @@ func (fsd *fsData) check_account_status(ctx context.Context, sym string, input [
|
||||
return res, err
|
||||
}
|
||||
|
||||
//status, err := checkAccountStatus(accountData["TrackingId"])
|
||||
status, err := handlers.CheckAccountStatus(accountData["TrackingId"])
|
||||
status, err := checkAccountStatus(accountData["TrackingId"])
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error checking account status:", err)
|
||||
@ -407,6 +446,50 @@ func (fsd *fsData) check_account_status(ctx context.Context, sym string, input [
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func createAccount() (*accountResponse, error) {
|
||||
resp, err := http.Post(createAccountURL, "application/json", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var accountResp accountResponse
|
||||
err = json.Unmarshal(body, &accountResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accountResp, nil
|
||||
}
|
||||
|
||||
func checkAccountStatus(trackingId string) (string, error) {
|
||||
resp, err := http.Get(trackStatusURL + trackingId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var trackResp trackStatusResponse
|
||||
err = json.Unmarshal(body, &trackResp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
status := trackResp.Result.Transaction.Status
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
switch codeFromCtx(ctx) {
|
||||
@ -444,19 +527,37 @@ func (fsd *fsData) check_balance(ctx context.Context, sym string, input []byte)
|
||||
res := resource.Result{}
|
||||
|
||||
fp := fsd.path + "_data"
|
||||
|
||||
jsonData, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
var accountData map[string]string
|
||||
err = json.Unmarshal(jsonData, &accountData)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
balance, err := handlers.CheckBalance(accountData["PublicKey"])
|
||||
|
||||
resp, err := http.Get(checkBalanceURL + accountData["PublicKey"])
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var balanceResp balanceResponse
|
||||
err = json.Unmarshal(body, &balanceResp)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
balance := balanceResp.Result.Balance
|
||||
|
||||
res.Content = balance
|
||||
|
||||
return res, nil
|
||||
@ -659,28 +760,12 @@ func (fsd *fsData) get_profile_info(ctx context.Context, sym string, input []byt
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
name := accountData["FirstName"] + " " + accountData["FamilyName"]
|
||||
name := accountData["FirstName"]
|
||||
gender := accountData["Gender"]
|
||||
yob := accountData["YOB"]
|
||||
age := accountData["YOB"]
|
||||
location := accountData["Location"]
|
||||
offerings := accountData["Offerings"]
|
||||
|
||||
layout := "02/01/2006"
|
||||
|
||||
birthdate, err := time.Parse(layout, yob)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
currentDate := time.Now()
|
||||
formattedDate := currentDate.Format(layout)
|
||||
today, err := time.Parse(layout, formattedDate)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
age := utils.CalculateAge(birthdate, today)
|
||||
|
||||
formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n", name, gender, age, location, offerings)
|
||||
formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %s\nLocation: %s\n", name, gender, age, location)
|
||||
res.Content = formattedData
|
||||
|
||||
return res, nil
|
||||
@ -720,10 +805,23 @@ func (fsd *fsData) quit_with_balance(ctx context.Context, sym string, input []by
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
balance, err := handlers.CheckBalance(accountData["PublicKey"])
|
||||
resp, err := http.Get(checkBalanceURL + accountData["PublicKey"])
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var balanceResp balanceResponse
|
||||
err = json.Unmarshal(body, &balanceResp)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
balance := balanceResp.Result.Balance
|
||||
res.Content = fmt.Sprintf("Your account balance is: %s", balance)
|
||||
res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED)
|
||||
return res, nil
|
||||
|
@ -2,9 +2,5 @@ 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/"
|
||||
)
|
||||
|
||||
//We define our endpoints here?
|
||||
|
@ -2,14 +2,19 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type AccountResponse struct {
|
||||
|
||||
type trackStatusResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
CustodialId json.Number `json:"custodialId"`
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
Transaction struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
TransferValue json.Number `json:"transferValue"`
|
||||
TxHash string `json:"txHash"`
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package models
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
|
||||
type BalanceResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Balance string `json:"balance"`
|
||||
Nonce json.Number `json:"nonce"`
|
||||
} `json:"result"`
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type TrackStatusResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Transaction struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
TransferValue json.Number `json:"transferValue"`
|
||||
TxHash string `json:"txHash"`
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
)
|
||||
|
||||
|
||||
func CheckAccountStatus(trackingId string) (string, error) {
|
||||
resp, err := http.Get(config.TrackStatusURL + trackingId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var trackResp models.TrackStatusResponse
|
||||
err = json.Unmarshal(body, &trackResp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
status := trackResp.Result.Transaction.Status
|
||||
|
||||
return status, nil
|
||||
}
|
@ -1,33 +1,4 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
)
|
||||
|
||||
func CheckBalance(publicKey string) (string, error) {
|
||||
|
||||
resp, err := http.Get(config.BalanceURL + publicKey)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
}
|
||||
|
||||
var balanceResp models.BalanceResponse
|
||||
err = json.Unmarshal(body, &balanceResp)
|
||||
if err != nil {
|
||||
return "0.0", err
|
||||
}
|
||||
|
||||
balance := balanceResp.Result.Balance
|
||||
return balance, nil
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
)
|
||||
|
||||
|
||||
func CreateAccount() (*models.AccountResponse, error) {
|
||||
resp, err := http.Post(config.CreateAccountURL, "application/json", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var accountResp models.AccountResponse
|
||||
err = json.Unmarshal(body, &accountResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accountResp, nil
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package utils
|
||||
|
||||
import "time"
|
||||
|
||||
|
||||
func CalculateAge(birthdate, today time.Time) int {
|
||||
today = today.In(birthdate.Location())
|
||||
ty, tm, td := today.Date()
|
||||
today = time.Date(ty, tm, td, 0, 0, 0, 0, time.UTC)
|
||||
by, bm, bd := birthdate.Date()
|
||||
birthdate = time.Date(by, bm, bd, 0, 0, 0, 0, time.UTC)
|
||||
if today.Before(birthdate) {
|
||||
return 0
|
||||
}
|
||||
age := ty - by
|
||||
anniversary := birthdate.AddDate(age, 0, 0)
|
||||
if anniversary.After(today) {
|
||||
age--
|
||||
}
|
||||
return age
|
||||
}
|
Loading…
Reference in New Issue
Block a user