Compare commits
5 Commits
31be1fa221
...
2b511aba4c
Author | SHA1 | Date | |
---|---|---|---|
2b511aba4c | |||
626fca1a55 | |||
0c96831378 | |||
fe4df78f80 | |||
ba4d0abcda |
@ -83,9 +83,9 @@ func main() {
|
|||||||
|
|
||||||
fp := path.Join(dp, sessionId)
|
fp := path.Join(dp, sessionId)
|
||||||
|
|
||||||
ussdHandlers,err := ussd.NewHandlers(fp, &st)
|
ussdHandlers, err := ussd.NewHandlers(fp, &st)
|
||||||
|
|
||||||
if(err != nil){
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "handler setup failed with error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "handler setup failed with error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package ussd
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -16,46 +17,74 @@ import (
|
|||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
|
"github.com/graygnuorg/go-gdbm"
|
||||||
"gopkg.in/leonelquinteros/gotext.v1"
|
"gopkg.in/leonelquinteros/gotext.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
translationDir = path.Join(scriptDir, "locale")
|
translationDir = path.Join(scriptDir, "locale")
|
||||||
|
dbFile = path.Join(scriptDir, "vise.gdbm")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TrackingIdKey = "TRACKINGID"
|
||||||
|
PublicKeyKey = "PUBLICKEY"
|
||||||
|
CustodialIdKey = "CUSTODIALID"
|
||||||
|
AccountPin = "ACCOUNTPIN"
|
||||||
|
AccountStatus = "ACCOUNTSTATUS"
|
||||||
|
FirstName = "FIRSTNAME"
|
||||||
|
FamilyName = "FAMILYNAME"
|
||||||
|
YearOfBirth = "YOB"
|
||||||
|
Location = "LOCATION"
|
||||||
|
Gender = "GENDER"
|
||||||
|
Offerings = "OFFERINGS"
|
||||||
|
Recipient = "RECIPIENT"
|
||||||
|
Amount = "AMOUNT"
|
||||||
|
)
|
||||||
|
|
||||||
|
func toBytes(s string) []byte {
|
||||||
|
return []byte(s)
|
||||||
|
}
|
||||||
|
|
||||||
type FSData struct {
|
type FSData struct {
|
||||||
Path string
|
Path string
|
||||||
St *state.State
|
St *state.State
|
||||||
}
|
}
|
||||||
|
|
||||||
type FlagParserInterface interface {
|
type FlagParserInterface interface {
|
||||||
GetFlag(key string) (uint32, error)
|
GetFlag(key string) (uint32, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handlers struct {
|
type Handlers struct {
|
||||||
fs *FSData
|
fs *FSData
|
||||||
|
db *gdbm.Database
|
||||||
parser FlagParserInterface
|
parser FlagParserInterface
|
||||||
accountFileHandler utils.AccountFileHandlerInterface
|
accountFileHandler utils.AccountFileHandlerInterface
|
||||||
accountService server.AccountServiceInterface
|
accountService server.AccountServiceInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandlers(dir string, st *state.State) (*Handlers, error) {
|
func NewHandlers(dir string, st *state.State) (*Handlers, error) {
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
db, err := gdbm.Open(dbFile, gdbm.ModeWrcreat)
|
||||||
parser := asm.NewFlagParser()
|
if err != nil {
|
||||||
_, err := parser.Load(pfp)
|
panic(err)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
}
|
parser := asm.NewFlagParser()
|
||||||
return &Handlers{
|
_, err = parser.Load(pfp)
|
||||||
fs: &FSData{
|
if err != nil {
|
||||||
Path: dir,
|
return nil, err
|
||||||
St: st,
|
}
|
||||||
},
|
return &Handlers{
|
||||||
parser: parser,
|
db: db,
|
||||||
accountFileHandler: utils.NewAccountFileHandler(dir + "_data"),
|
fs: &FSData{
|
||||||
accountService: &server.AccountService{},
|
Path: dir,
|
||||||
}, nil
|
St: st,
|
||||||
|
},
|
||||||
|
parser: parser,
|
||||||
|
accountFileHandler: utils.NewAccountFileHandler(dir + "_data"),
|
||||||
|
accountService: &server.AccountService{},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the regex pattern as a constant
|
// Define the regex pattern as a constant
|
||||||
@ -135,22 +164,18 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
|
|||||||
res.FlagSet = append(res.FlagSet, flags["flag_account_creation_failed"])
|
res.FlagSet = append(res.FlagSet, flags["flag_account_creation_failed"])
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
data := map[string]string{
|
||||||
accountData := map[string]string{
|
TrackingIdKey: accountResp.Result.TrackingId,
|
||||||
"TrackingId": accountResp.Result.TrackingId,
|
PublicKeyKey: accountResp.Result.PublicKey,
|
||||||
"PublicKey": accountResp.Result.PublicKey,
|
CustodialIdKey: accountResp.Result.CustodialId.String(),
|
||||||
"CustodialId": accountResp.Result.CustodialId.String(),
|
|
||||||
"Status": "PENDING",
|
|
||||||
"Gender": "Not provided",
|
|
||||||
"YOB": "Not provided",
|
|
||||||
"Location": "Not provided",
|
|
||||||
"Offerings": "Not provided",
|
|
||||||
"FirstName": "Not provided",
|
|
||||||
"FamilyName": "Not provided",
|
|
||||||
}
|
}
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
|
||||||
if err != nil {
|
for key, value := range data {
|
||||||
return res, err
|
err := h.db.Store(toBytes(key), toBytes(value), true)
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.FlagSet = append(res.FlagSet, flags["flag_account_created"])
|
res.FlagSet = append(res.FlagSet, flags["flag_account_created"])
|
||||||
@ -160,8 +185,6 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
|
|||||||
// SavePin persists the user's PIN choice into the filesystem
|
// SavePin persists the user's PIN choice into the filesystem
|
||||||
func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
// Preload the required flags
|
|
||||||
flagKeys := []string{"flag_incorrect_pin"}
|
flagKeys := []string{"flag_incorrect_pin"}
|
||||||
flags, err := h.PreloadFlags(flagKeys)
|
flags, err := h.PreloadFlags(flagKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -170,10 +193,10 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
|
|||||||
|
|
||||||
accountPIN := string(input)
|
accountPIN := string(input)
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return res, err
|
// return res, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Validate that the PIN is a 4-digit number
|
// Validate that the PIN is a 4-digit number
|
||||||
if !isValidPIN(accountPIN) {
|
if !isValidPIN(accountPIN) {
|
||||||
@ -182,12 +205,17 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"])
|
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"])
|
||||||
accountData["AccountPIN"] = accountPIN
|
//accountData["AccountPIN"] = accountPIN
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
key := []byte(AccountPin)
|
||||||
if err != nil {
|
value := []byte(accountPIN)
|
||||||
return res, err
|
|
||||||
}
|
h.db.Store(key, value, true)
|
||||||
|
|
||||||
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -265,18 +293,23 @@ func codeFromCtx(ctx context.Context) string {
|
|||||||
func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return res, err
|
// return res, err
|
||||||
}
|
// }
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
name := string(input)
|
name := string(input)
|
||||||
accountData["FirstName"] = name
|
//accountData["FirstName"] = name
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
key := []byte(FirstName)
|
||||||
if err != nil {
|
value := []byte(name)
|
||||||
return res, err
|
|
||||||
}
|
h.db.Store(key, value, true)
|
||||||
|
|
||||||
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -285,19 +318,12 @@ func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte)
|
|||||||
// SaveFamilyname updates the family name in a JSON data file with the provided input.
|
// SaveFamilyname updates the family name in a JSON data file with the provided input.
|
||||||
func (h *Handlers) SaveFamilyname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveFamilyname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
secondname := string(input)
|
secondname := string(input)
|
||||||
accountData["FamilyName"] = secondname
|
key := []byte(FamilyName)
|
||||||
|
value := []byte(secondname)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -307,20 +333,18 @@ func (h *Handlers) SaveFamilyname(cxt context.Context, sym string, input []byte)
|
|||||||
func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
yob := string(input)
|
yob := string(input)
|
||||||
if len(yob) == 4 {
|
if len(yob) == 4 {
|
||||||
yob := string(input)
|
yob := string(input)
|
||||||
accountData["YOB"] = yob
|
//accountData["YOB"] = yob
|
||||||
|
key := []byte(YearOfBirth)
|
||||||
|
value := []byte(yob)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
return res, err
|
// if err != nil {
|
||||||
}
|
// return res, err
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -330,19 +354,17 @@ func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resou
|
|||||||
func (h *Handlers) SaveLocation(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveLocation(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return res, err
|
// return res, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
location := string(input)
|
location := string(input)
|
||||||
accountData["Location"] = location
|
key := []byte(Location)
|
||||||
|
value := []byte(location)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -351,12 +373,6 @@ func (h *Handlers) SaveLocation(cxt context.Context, sym string, input []byte) (
|
|||||||
// SaveGender updates the gender in a JSON data file with the provided input.
|
// SaveGender updates the gender in a JSON data file with the provided input.
|
||||||
func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
gender := string(input)
|
gender := string(input)
|
||||||
|
|
||||||
@ -368,12 +384,16 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
|||||||
case "3":
|
case "3":
|
||||||
gender = "Unspecified"
|
gender = "Unspecified"
|
||||||
}
|
}
|
||||||
accountData["Gender"] = gender
|
//accountData["Gender"] = gender
|
||||||
|
key := []byte(Gender)
|
||||||
|
value := []byte(gender)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
|
||||||
return res, err
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -382,19 +402,23 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
|||||||
func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return res, err
|
// return res, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
offerings := string(input)
|
offerings := string(input)
|
||||||
accountData["Offerings"] = offerings
|
//accountData["Offerings"] = offerings
|
||||||
|
key := []byte(Offerings)
|
||||||
|
value := []byte(offerings)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
|
||||||
return res, err
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -433,12 +457,16 @@ func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input
|
|||||||
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = accountData["PublicKey"]
|
res.Content = string(publicKey)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -455,27 +483,35 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pin := string(input)
|
// pin := string(input)
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
storedpin, err := h.db.Fetch([]byte(AccountPin))
|
||||||
|
if err == nil {
|
||||||
|
if len(input) == 4 {
|
||||||
|
if bytes.Equal(input, storedpin) {
|
||||||
|
if h.fs.St.MatchFlag(flags["flag_account_authorized"], false) {
|
||||||
|
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"])
|
||||||
|
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"], flags["flag_account_authorized"])
|
||||||
|
} else {
|
||||||
|
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"])
|
||||||
|
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_pin"])
|
||||||
|
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"])
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if errors.Is(err, gdbm.ErrItemNotFound) {
|
||||||
|
//PIN not set yet
|
||||||
|
} else {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(input) == 4 {
|
|
||||||
if pin != accountData["AccountPIN"] {
|
|
||||||
res.FlagSet = append(res.FlagSet, flags["flag_incorrect_pin"])
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"])
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
if h.fs.St.MatchFlag(flags["flag_account_authorized"], false) {
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_incorrect_pin"])
|
|
||||||
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"], flags["flag_account_authorized"])
|
|
||||||
} else {
|
|
||||||
res.FlagSet = append(res.FlagSet, flags["flag_allow_update"])
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_account_authorized"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -506,19 +542,30 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
|
trackingId, err := h.db.Fetch([]byte(TrackingIdKey))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := h.accountService.CheckAccountStatus(accountData["TrackingId"])
|
status, err := h.accountService.CheckAccountStatus(string(trackingId))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error checking account status:", err)
|
fmt.Println("Error checking account status:", err)
|
||||||
return res, nil
|
return res, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
accountData["Status"] = status
|
//accountData["Status"] = status
|
||||||
|
err = h.db.Store(toBytes(TrackingIdKey), toBytes(status), true)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
if status == "SUCCESS" {
|
if status == "SUCCESS" {
|
||||||
res.FlagSet = append(res.FlagSet, flags["flag_account_success"])
|
res.FlagSet = append(res.FlagSet, flags["flag_account_success"])
|
||||||
@ -528,10 +575,10 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
res.FlagSet = append(res.FlagReset, flags["flag_account_pending"])
|
res.FlagSet = append(res.FlagReset, flags["flag_account_pending"])
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
// err = h.accountFileHandler.WriteAccountData(accountData)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return res, err
|
// return res, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -584,7 +631,7 @@ func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (res
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetIncorrectYob resets the incorrect date format after a new attempt
|
// ResetIncorrectYob resets the incorrect date format flag after a new attempt
|
||||||
func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
@ -603,13 +650,13 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by
|
|||||||
// the balance as the result content
|
// the balance as the result content
|
||||||
func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
balance, err := h.accountService.CheckBalance(accountData["PublicKey"])
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -621,17 +668,10 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
|||||||
// ValidateRecipient validates that the given input is a valid phone number.
|
// 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) {
|
func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
// Preload the required flags
|
|
||||||
flagKeys := []string{"flag_invalid_recipient"}
|
|
||||||
flags, err := h.PreloadFlags(flagKeys)
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
recipient := string(input)
|
recipient := string(input)
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
flagKeys := []string{"flag_invalid_recipient"}
|
||||||
|
flags, err := h.PreloadFlags(flagKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
@ -645,12 +685,11 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
accountData["Recipient"] = recipient
|
// accountData["Recipient"] = recipient
|
||||||
|
key := []byte(Recipient)
|
||||||
|
value := []byte(recipient)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
h.db.Store(key, value, true)
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -668,18 +707,13 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
err = h.db.Delete([]byte(Amount))
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||||
return res, err
|
panic(err)
|
||||||
}
|
}
|
||||||
|
err = h.db.Delete([]byte(Recipient))
|
||||||
// reset the transaction
|
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||||
accountData["Recipient"] = ""
|
panic(err)
|
||||||
accountData["Amount"] = ""
|
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_invalid_recipient"], flags["flag_invalid_recipient_with_invite"])
|
res.FlagReset = append(res.FlagReset, flags["flag_invalid_recipient"], flags["flag_invalid_recipient_with_invite"])
|
||||||
@ -698,17 +732,9 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
err = h.db.Delete([]byte(Amount))
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||||
return res, err
|
panic(err)
|
||||||
}
|
|
||||||
|
|
||||||
// reset the amount
|
|
||||||
accountData["Amount"] = ""
|
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.FlagReset = append(res.FlagReset, flags["flag_invalid_amount"])
|
res.FlagReset = append(res.FlagReset, flags["flag_invalid_amount"])
|
||||||
@ -720,13 +746,12 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
|
|||||||
// the result content.
|
// the result content.
|
||||||
func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
balance, err := h.accountService.CheckBalance(accountData["PublicKey"])
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -740,7 +765,6 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
|
|||||||
// it is not more than the current balance.
|
// it is not more than the current balance.
|
||||||
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
// Preload the required flag
|
// Preload the required flag
|
||||||
flagKeys := []string{"flag_invalid_amount"}
|
flagKeys := []string{"flag_invalid_amount"}
|
||||||
flags, err := h.PreloadFlags(flagKeys)
|
flags, err := h.PreloadFlags(flagKeys)
|
||||||
@ -749,13 +773,14 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
|||||||
}
|
}
|
||||||
|
|
||||||
amountStr := string(input)
|
amountStr := string(input)
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
balanceStr, err := h.accountService.CheckBalance(accountData["PublicKey"])
|
balanceStr, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
@ -794,9 +819,10 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
|
res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
|
||||||
accountData["Amount"] = res.Content
|
key := []byte(Amount)
|
||||||
|
value := []byte(res.Content)
|
||||||
|
h.db.Store(key, value, true)
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
@ -807,13 +833,12 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
|||||||
// GetRecipient returns the transaction recipient from a JSON data file.
|
// GetRecipient returns the transaction recipient from a JSON data file.
|
||||||
func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
recipient, err := h.db.Fetch([]byte(Recipient))
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = accountData["Recipient"]
|
res.Content = string(recipient)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -855,12 +880,13 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
|
|||||||
func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
//accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = accountData["PublicKey"]
|
res.Content = string(publicKey)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -869,12 +895,13 @@ func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (res
|
|||||||
func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
res := resource.Result{}
|
res := resource.Result{}
|
||||||
|
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
//accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
|
amount, err := h.db.Fetch([]byte(Amount))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Content = accountData["Amount"]
|
res.Content = string(amount)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -894,11 +921,15 @@ func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte
|
|||||||
code := codeFromCtx(ctx)
|
code := codeFromCtx(ctx)
|
||||||
l := gotext.NewLocale(translationDir, code)
|
l := gotext.NewLocale(translationDir, code)
|
||||||
l.AddDomain("default")
|
l.AddDomain("default")
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// accountData, err := h.accountFileHandler.ReadAccountData()
|
||||||
|
// if err != nil {
|
||||||
|
// return res, err
|
||||||
|
// }
|
||||||
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
balance, err := h.accountService.CheckBalance(accountData["PublicKey"])
|
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -914,25 +945,29 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
|||||||
code := codeFromCtx(ctx)
|
code := codeFromCtx(ctx)
|
||||||
l := gotext.NewLocale(translationDir, code)
|
l := gotext.NewLocale(translationDir, code)
|
||||||
l.AddDomain("default")
|
l.AddDomain("default")
|
||||||
|
// Preload the required flags
|
||||||
accountData, err := h.accountFileHandler.ReadAccountData()
|
// flagKeys := []string{"flag_invalid_recipient"}
|
||||||
if err != nil {
|
// flags, err := h.PreloadFlags(flagKeys)
|
||||||
return res, err
|
// if err != nil {
|
||||||
}
|
// return res, err
|
||||||
|
// }
|
||||||
// TODO
|
// TODO
|
||||||
// Use the amount, recipient and sender to call the API and initialize the transaction
|
// Use the amount, recipient and sender to call the API and initialize the transaction
|
||||||
|
|
||||||
res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", accountData["Recipient"], accountData["Amount"], accountData["PublicKey"])
|
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||||
|
|
||||||
// reset the transaction
|
|
||||||
accountData["Recipient"] = ""
|
|
||||||
accountData["Amount"] = ""
|
|
||||||
|
|
||||||
err = h.accountFileHandler.WriteAccountData(accountData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
amount, err := h.db.Fetch([]byte(Amount))
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
recipient, err := h.db.Fetch([]byte(Recipient))
|
||||||
|
if err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey))
|
||||||
|
|
||||||
account_authorized_flag, err := h.parser.GetFlag("flag_account_authorized")
|
account_authorized_flag, err := h.parser.GetFlag("flag_account_authorized")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user