From 722bb10441dc58746c3c82d9c1ce042b4dc0e1e7 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 23 Aug 2024 06:35:49 +0300 Subject: [PATCH] implement profile data persistence --- cmd/main.go | 267 ++++++++++++++++++++- services/registration/balances.vis | 1 + services/registration/edit_profile.vis | 2 + services/registration/enter_familyname | 1 + services/registration/enter_familyname.vis | 6 + services/registration/enter_familyname_swa | 0 services/registration/enter_location.vis | 2 + services/registration/enter_name.vis | 1 + services/registration/enter_offerings.vis | 7 + services/registration/enter_yob.vis | 2 + services/registration/my_account.vis | 1 + services/registration/select_gender.vis | 4 + services/registration/update_success | 1 + services/registration/update_success.vis | 3 + services/registration/update_success_swa | 1 + services/registration/view_profile | 2 + services/registration/view_profile.vis | 7 + services/registration/view_profile_swa | 1 + 18 files changed, 307 insertions(+), 2 deletions(-) create mode 100644 services/registration/enter_familyname create mode 100644 services/registration/enter_familyname.vis create mode 100644 services/registration/enter_familyname_swa create mode 100644 services/registration/update_success create mode 100644 services/registration/update_success.vis create mode 100644 services/registration/update_success_swa create mode 100644 services/registration/view_profile create mode 100644 services/registration/view_profile_swa diff --git a/cmd/main.go b/cmd/main.go index 5944e71..996b7be 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ const ( USERFLAG_INVALID_RECIPIENT USERFLAG_INVALID_RECIPIENT_WITH_INVITE USERFLAG_INCORRECTPIN + USERFLAG_UNLOCKFORUPDATE ) const ( @@ -70,6 +71,193 @@ type fsData struct { st *state.State } +func (fsd *fsData) saveFirstName(cxt context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + 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) + if err != nil { + return res, err + } + } + + return res, nil +} + +func (fsd *fsData) saveFamilyName(cxt context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + if len(input) > 0 { + //Save name + secondname := string(input) + fmt.Println("FamilyName:", secondname) + accountData["FamilyName"] = secondname + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + } + + return res, nil +} +func (fsd *fsData) saveYOB(cxt context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + if len(input) > 0 { + yob := string(input) + fmt.Println("YOB", yob) + accountData["YOB"] = yob + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + } + + return res, nil +} + +func (fsd *fsData) saveLocation(cxt context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + if len(input) > 0 { + location := string(input) + fmt.Println("Location:", location) + accountData["Location"] = location + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + + } + + return res, nil +} + +func (fsd *fsData) saveGender(ctx context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + if len(input) > 0 { + gender := string(input) + + switch gender { + case "1" : gender = "Male" + case "2" : gender = "Female" + case "3" : gender = "Other" + } + fmt.Println("gender", gender) + accountData["Gender"] = gender + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + } + return res, nil +} + +func (fsd *fsData) saveOfferings(ctx context.Context, sym string, input []byte) (resource.Result, error) { + 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 + } + if len(input) > 0 { + offerings := string(input) + fmt.Println("Offerings:", offerings) + accountData["Offerings"] = offerings + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + } + return res, nil +} + func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) { inputStr := string(input) res := resource.Result{} @@ -97,7 +285,18 @@ func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) } f.Close() - accountResp, err := createAccount() + //accountResp, err := createAccount() + accountResp := accountResponse{ + Ok: true, + Result: struct { + CustodialId json.Number `json:"custodialId"` + PublicKey string `json:"publicKey"` + TrackingId string `json:"trackingId"` + }{ + CustodialId: "636", + PublicKey: "0x8d86F9D4A4eae41Dc3B68034895EA97BcA90e8c1", + TrackingId: "45c67314-7995-4890-89d6-e5af987754ac", + }} if err != nil { fmt.Println("Failed to create account:", err) @@ -124,6 +323,21 @@ func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) return res, err } +func (fsd *fsData) resetUnlockForUpdate(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{} + res.FlagReset = append(res.FlagReset, USERFLAG_UNLOCKFORUPDATE) + return res, nil +} + +func (fsd *fsData) resetAccountUnlocked(ctx context.Context,sym string,input []byte) (resource.Result,error){ + res := resource.Result{} + st := fsd.st + isSet := st.MatchFlag(USERFLAG_ACCOUNT_UNLOCKED,true) + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + fmt.Println("ISSET:",isSet) + return res,nil +} + func (fsd *fsData) checkIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} fp := fsd.path + "_data" @@ -154,9 +368,11 @@ func (fsd *fsData) unLock(ctx context.Context, sym string, input []byte) (resour return res, nil } if fsd.st.MatchFlag(USERFLAG_ACCOUNT_UNLOCKED, false) { + //res.FlagSet = append(res.FlagSet, USERFLAG_UNLOCKFORUPDATE) res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_UNLOCKED) } else { res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + //res.FlagReset = append(res.FlagReset, USERFLAG_UNLOCKFORUPDATE) } } return res, nil @@ -173,6 +389,11 @@ func (fsd *fsData) ResetIncorrectPin(ctx context.Context, sym string, input []by return res, nil } +func (fsd *fsData) ShowUpdateSuccess(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{} + 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" @@ -197,7 +418,7 @@ func (fsd *fsData) check_account_status(ctx context.Context, sym string, input [ accountData["Status"] = status - if status == "SUCCESS" { + if status == "REVERTED" { res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_SUCCESS) res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_PENDING) } else { @@ -456,6 +677,35 @@ func (fsd *fsData) get_recipient(ctx context.Context, sym string, input []byte) return res, nil } + +func (fsd *fsData) getProfileInfo(ctx context.Context,sym string,input []byte) (resource.Result,error){ + 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 + } + name := accountData["FirstName"] + gender := accountData["Gender"] + age := accountData["YOB"] + location := accountData["Location"] + + // Format the data into a string + formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %s\nLocation: %s\n", name, gender, age, location) + + + res.Content = formattedData + + return res,nil +} + func (fsd *fsData) get_sender(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} fp := fsd.path + "_data" @@ -585,7 +835,20 @@ func main() { rfs.AddLocalFunc("get_recipient", fs.get_recipient) rfs.AddLocalFunc("get_sender", fs.get_sender) rfs.AddLocalFunc("reset_incorrect", fs.ResetIncorrectPin) + rfs.AddLocalFunc("save_firstname", fs.saveFirstName) + rfs.AddLocalFunc("save_familyname", fs.saveFamilyName) + rfs.AddLocalFunc("save_gender", fs.saveGender) + rfs.AddLocalFunc("save_location", fs.saveLocation) + rfs.AddLocalFunc("save_yob", fs.saveYOB) + rfs.AddLocalFunc("save_offerings", fs.saveOfferings) rfs.AddLocalFunc("quit_with_balance", fs.quitWithBalance) + rfs.AddLocalFunc("show_update_success", fs.ShowUpdateSuccess) + rfs.AddLocalFunc("reset_unlocked",fs.resetAccountUnlocked) + rfs.AddLocalFunc("reset_unlock_for_update", fs.resetUnlockForUpdate) + rfs.AddLocalFunc("get_profile_info",fs.getProfileInfo) + + + cont, err := en.Init(ctx) en.SetDebugger(engine.NewSimpleDebug(nil)) diff --git a/services/registration/balances.vis b/services/registration/balances.vis index 50c3943..7873dd2 100644 --- a/services/registration/balances.vis +++ b/services/registration/balances.vis @@ -1,3 +1,4 @@ +LOAD reset_unlocked 0 MOUT my_balance 1 MOUT community_balance 2 MOUT back 0 diff --git a/services/registration/edit_profile.vis b/services/registration/edit_profile.vis index 8afb933..a8b0e49 100644 --- a/services/registration/edit_profile.vis +++ b/services/registration/edit_profile.vis @@ -1,3 +1,5 @@ +LOAD reset_unlocked 0 +LOAD get_profile_info 0 MOUT edit_name 1 MOUT edit_gender 2 MOUT edit_yob 3 diff --git a/services/registration/enter_familyname b/services/registration/enter_familyname new file mode 100644 index 0000000..15ffb07 --- /dev/null +++ b/services/registration/enter_familyname @@ -0,0 +1 @@ +Enter family name: diff --git a/services/registration/enter_familyname.vis b/services/registration/enter_familyname.vis new file mode 100644 index 0000000..2085c38 --- /dev/null +++ b/services/registration/enter_familyname.vis @@ -0,0 +1,6 @@ +LOAD save_firstname 0 +MOUT back 0 +HALT +INCMP _ 0 +INCMP select_gender * + diff --git a/services/registration/enter_familyname_swa b/services/registration/enter_familyname_swa new file mode 100644 index 0000000..e69de29 diff --git a/services/registration/enter_location.vis b/services/registration/enter_location.vis index 3790a08..7ef9348 100644 --- a/services/registration/enter_location.vis +++ b/services/registration/enter_location.vis @@ -1,3 +1,5 @@ +LOAD save_yob 0 MOUT back 0 HALT INCMP _ 0 +INCMP enter_offerings * diff --git a/services/registration/enter_name.vis b/services/registration/enter_name.vis index 3790a08..4126f07 100644 --- a/services/registration/enter_name.vis +++ b/services/registration/enter_name.vis @@ -1,3 +1,4 @@ MOUT back 0 HALT INCMP _ 0 +INCMP enter_familyname * diff --git a/services/registration/enter_offerings.vis b/services/registration/enter_offerings.vis index 3790a08..eb7dad2 100644 --- a/services/registration/enter_offerings.vis +++ b/services/registration/enter_offerings.vis @@ -1,3 +1,10 @@ +LOAD save_location 0 +CATCH incorrect_pin 15 1 +CATCH update_success 16 1 MOUT back 0 HALT +LOAD save_offerings 0 INCMP _ 0 +INCMP pin_entry * + + diff --git a/services/registration/enter_yob.vis b/services/registration/enter_yob.vis index 3790a08..d169e05 100644 --- a/services/registration/enter_yob.vis +++ b/services/registration/enter_yob.vis @@ -1,3 +1,5 @@ +LOAD save_gender 0 MOUT back 0 HALT INCMP _ 0 +INCMP enter_location * diff --git a/services/registration/my_account.vis b/services/registration/my_account.vis index 5cbe722..48f12ee 100644 --- a/services/registration/my_account.vis +++ b/services/registration/my_account.vis @@ -1,3 +1,4 @@ +LOAD reset_unlock_for_update 0 MOUT profile 1 MOUT change_language 2 MOUT check_balance 3 diff --git a/services/registration/select_gender.vis b/services/registration/select_gender.vis index 8a2ef60..e9868a5 100644 --- a/services/registration/select_gender.vis +++ b/services/registration/select_gender.vis @@ -1,6 +1,10 @@ +LOAD save_familyname 0 MOUT male 1 MOUT female 2 MOUT other_gender 3 MOUT back 0 HALT INCMP _ 0 +INCMP enter_yob 1 +INCMP enter_yob 2 +INCMP enter_yob 3 diff --git a/services/registration/update_success b/services/registration/update_success new file mode 100644 index 0000000..652942a --- /dev/null +++ b/services/registration/update_success @@ -0,0 +1 @@ +Profile updated successfully diff --git a/services/registration/update_success.vis b/services/registration/update_success.vis new file mode 100644 index 0000000..07fcf94 --- /dev/null +++ b/services/registration/update_success.vis @@ -0,0 +1,3 @@ +MOUT back 0 +HALT +INCMP ^ 0 diff --git a/services/registration/update_success_swa b/services/registration/update_success_swa new file mode 100644 index 0000000..19949ad --- /dev/null +++ b/services/registration/update_success_swa @@ -0,0 +1 @@ +Akaunti imeupdatiwa diff --git a/services/registration/view_profile b/services/registration/view_profile new file mode 100644 index 0000000..7708bd5 --- /dev/null +++ b/services/registration/view_profile @@ -0,0 +1,2 @@ +My profile: +{{.get_profile_info}} diff --git a/services/registration/view_profile.vis b/services/registration/view_profile.vis index 72e55ad..8bf2b76 100644 --- a/services/registration/view_profile.vis +++ b/services/registration/view_profile.vis @@ -1 +1,8 @@ +LOAD get_profile_info 0 +MAP get_profile_info +LOAD reset_incorrect 0 +CATCH incorrect_pin 15 1 +CATCH pin_entry 12 0 +MOUT back 0 HALT +INCMP _ 0 diff --git a/services/registration/view_profile_swa b/services/registration/view_profile_swa new file mode 100644 index 0000000..8a12b7d --- /dev/null +++ b/services/registration/view_profile_swa @@ -0,0 +1 @@ +Wasifu wangu \ No newline at end of file