wip-account-creation #4

Merged
lash merged 143 commits from wip-account-creation into master 2024-08-30 14:37:58 +02:00
2 changed files with 96 additions and 238 deletions
Showing only changes of commit 9f1eed9ab3 - Show all commits

View File

@ -3,9 +3,7 @@ package ussd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"regexp"
"time"
@ -25,6 +23,7 @@ type FSData struct {
type Handlers struct {
fs *FSData
accountFileHandler *utils.AccountFileHandler
}
func NewHandlers(path string, st *state.State) *Handlers {
@ -33,6 +32,7 @@ func NewHandlers(path string, st *state.State) *Handlers {
Path: path,
St: st,
},
accountFileHandler: utils.NewAccountFileHandler(path + "_data"),
}
}
@ -56,12 +56,12 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
f, err := os.OpenFile(fp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
err := h.accountFileHandler.EnsureFileExists()
if err != nil {
return res, err
}
f.Close()
accountResp, err := server.CreateAccount()
if err != nil {
fmt.Println("Failed to create account:", err)
@ -75,15 +75,11 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
"Status": "PENDING",
}
jsonData, err := json.Marshal(accountData)
err = h.accountFileHandler.WriteAccountData(accountData)
Alfred-mk marked this conversation as resolved Outdated
Outdated
Review

should not be necessary to set the flag when it already has been set and persisted. That will reduce one file access call each run.

should not be necessary to set the flag when it already has been set and persisted. That will reduce one file access call each run.
if err != nil {
return res, err
}
err = os.WriteFile(fp, jsonData, 0644)
if err != nil {
return res, err
}
res.FlagSet = append(res.FlagSet, models.USERFLAG_ACCOUNT_CREATED)
return res, err
}
@ -91,27 +87,15 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte)
func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
accountPIN := string(input)
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
accountData["AccountPIN"] = accountPIN
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -121,15 +105,8 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -156,27 +133,16 @@ func codeFromCtx(ctx context.Context) string {
func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
if len(input) > 0 {
name := string(input)
accountData["FirstName"] = name
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -187,28 +153,16 @@ func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte)
func (h *Handlers) SaveFamilyname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
if len(input) > 0 {
//Save name
secondname := string(input)
accountData["FamilyName"] = secondname
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -219,26 +173,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) {
res := resource.Result{}
fp := h.fs.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
}
yob := string(input)
if len(yob) > 4 {
yob := string(input)
accountData["YOB"] = yob
updatedJsonData, err := json.Marshal(accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
yob := string(input)
if len(yob) > 4 {
Alfred-mk marked this conversation as resolved Outdated
Outdated
Review

I prefer putting regexes like this on top of the file as a const.

I prefer putting regexes like this on top of the file as a const.
yob := string(input)
accountData["YOB"] = yob
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -249,29 +195,20 @@ 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) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
if len(input) > 0 {
location := string(input)
accountData["Location"] = location
updatedJsonData, err := json.Marshal(accountData)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
if err != nil {
return res, err
}
}
return res, nil
@ -279,16 +216,12 @@ func (h *Handlers) SaveLocation(cxt context.Context, sym string, input []byte) (
func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
if len(input) > 0 {
gender := string(input)
@ -301,12 +234,8 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
gender = "Other"
}
accountData["Gender"] = gender
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -316,24 +245,17 @@ 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) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
if len(input) > 0 {
offerings := string(input)
accountData["Offerings"] = offerings
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -355,15 +277,8 @@ func (h *Handlers) ResetAccountUnlocked(ctx context.Context, sym string, input [
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -376,15 +291,8 @@ func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte
func (h *Handlers) Unlock(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
pin := string(input)
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -414,18 +322,12 @@ func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []by
func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
if err != nil {
return res, err
}
status, err := server.CheckAccountStatus(accountData["TrackingId"])
if err != nil {
@ -443,12 +345,7 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
res.FlagSet = append(res.FlagReset, models.USERFLAG_ACCOUNT_PENDING)
}
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -492,16 +389,11 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by
func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
balance, err := server.CheckBalance(accountData["PublicKey"])
if err != nil {
return res, nil
@ -515,15 +407,7 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
res := resource.Result{}
recipient := string(input)
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -539,12 +423,7 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
accountData["Recipient"] = recipient
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -555,15 +434,7 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -571,12 +442,7 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
// reset the recipient
accountData["Recipient"] = ""
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -588,15 +454,7 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -604,12 +462,7 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
// reset the amount
accountData["Amount"] = ""
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -632,15 +485,7 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
res := resource.Result{}
amount := string(input)
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -658,12 +503,7 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
accountData["Amount"] = amount
updatedJsonData, err := json.Marshal(accountData)
if err != nil {
return res, err
}
err = os.WriteFile(fp, updatedJsonData, 0644)
err = h.accountFileHandler.WriteAccountData(accountData)
if err != nil {
return res, err
}
@ -676,15 +516,8 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -696,15 +529,8 @@ func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -737,15 +563,8 @@ 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) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}
@ -757,15 +576,8 @@ func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (res
func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{}
fp := h.fs.Path + "_data"
jsonData, err := os.ReadFile(fp)
if err != nil {
return res, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
accountData, err := h.accountFileHandler.ReadAccountData()
if err != nil {
return res, err
}

View File

@ -0,0 +1,46 @@
package utils
import (
"encoding/json"
"os"
)
type AccountFileHandler struct {
FilePath string
}
func NewAccountFileHandler(path string) *AccountFileHandler {
return &AccountFileHandler{FilePath: path}
}
func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
jsonData, err := os.ReadFile(afh.FilePath)
if err != nil {
return nil, err
}
var accountData map[string]string
err = json.Unmarshal(jsonData, &accountData)
if err != nil {
return nil, err
}
return accountData, nil
}
func (afh *AccountFileHandler) WriteAccountData(accountData map[string]string) error {
jsonData, err := json.Marshal(accountData)
if err != nil {
return err
}
return os.WriteFile(afh.FilePath, jsonData, 0644)
}
func (afh *AccountFileHandler) EnsureFileExists() error {
f, err := os.OpenFile(afh.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
return f.Close()
}