forked from urdt/ussd
Compare commits
11 Commits
dev/issue-
...
restart-st
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02d43a8c09
|
||
| 3caee98cdb | |||
|
|
db7c9bf56d
|
||
|
|
0a332ec501
|
||
| 90367fe53e | |||
|
|
58a60f2c81
|
||
|
|
0820e1b9f2 | ||
|
|
46edf2b819
|
||
|
|
11eb61ba35
|
||
| 813b92af78 | |||
|
|
5579991d66
|
76
devtools/restart_state/main.go
Normal file
76
devtools/restart_state/main.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/initializers"
|
||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
)
|
||||
|
||||
func init() {
|
||||
initializers.LoadEnvVariables()
|
||||
}
|
||||
|
||||
func main() {
|
||||
config.LoadConfig()
|
||||
|
||||
var dbDir string
|
||||
var sessionId string
|
||||
var database string
|
||||
|
||||
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
||||
flag.StringVar(&database, "db", "gdbm", "database to be used")
|
||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||
flag.Parse()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
ctx = context.WithValue(ctx, "Database", database)
|
||||
|
||||
resourceDir := scriptDir
|
||||
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
||||
|
||||
err := menuStorageService.EnsureDbDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pe, err := menuStorageService.GetPersister(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
st := pe.GetState()
|
||||
|
||||
if st == nil {
|
||||
logg.ErrorCtxf(ctx, "state fail in devtool", "state", st)
|
||||
fmt.Println("cannot get state")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("The state:", st)
|
||||
|
||||
// set empty Code to allow the menu to run from the top
|
||||
st.Code = []byte{}
|
||||
|
||||
err = pe.Save(sessionId)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to persist the state and cache", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -1367,6 +1367,7 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
||||
func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
var profileInfo []byte
|
||||
var defaultValue string
|
||||
var err error
|
||||
|
||||
flag_firstname_set, _ := h.flagManager.GetFlag("flag_firstname_set")
|
||||
@@ -1383,7 +1384,18 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
// Extract the field name from the state machine position.
|
||||
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"
|
||||
}
|
||||
|
||||
|
||||
sm, _ := h.st.Where()
|
||||
parts := strings.SplitN(sm, "_", 2)
|
||||
filename := parts[1]
|
||||
@@ -1400,7 +1412,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_FIRST_NAME)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read first name entry with", "key", "error", common.DATA_FIRST_NAME, err)
|
||||
@@ -1412,7 +1424,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_FAMILY_NAME)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read family name entry with", "key", "error", common.DATA_FAMILY_NAME, err)
|
||||
@@ -1425,7 +1437,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_GENDER)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read gender entry with", "key", "error", common.DATA_GENDER, err)
|
||||
@@ -1437,7 +1449,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_YOB)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read year of birth(yob) entry with", "key", "error", common.DATA_YOB, err)
|
||||
@@ -1449,7 +1461,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_LOCATION)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read location entry with", "key", "error", common.DATA_LOCATION, err)
|
||||
@@ -1461,7 +1473,7 @@ func (h *Handlers) GetCurrentProfileInfo(ctx context.Context, sym string, input
|
||||
profileInfo, err = store.ReadEntry(ctx, sessionId, common.DATA_OFFERINGS)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
res.Content = "Not provided"
|
||||
res.Content = defaultValue
|
||||
break
|
||||
}
|
||||
logg.ErrorCtxf(ctx, "Failed to read offerings entry with", "key", "error", common.DATA_OFFERINGS, err)
|
||||
@@ -2015,15 +2027,16 @@ func (h *Handlers) insertProfileItems(ctx context.Context, sessionId string, res
|
||||
for index, profileItem := range h.profile.ProfileItems {
|
||||
// Ensure the profileItem is not "0"(is set)
|
||||
if profileItem != "0" {
|
||||
err = store.WriteEntry(ctx, sessionId, profileDataKeys[index], []byte(profileItem))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write profile entry with", "key", profileDataKeys[index], "value", profileItem, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the flag for the current index
|
||||
flag, _ := h.flagManager.GetFlag(profileFlagNames[index])
|
||||
res.FlagSet = append(res.FlagSet, flag)
|
||||
isProfileItemSet := h.st.MatchFlag(flag, true)
|
||||
if !isProfileItemSet {
|
||||
err = store.WriteEntry(ctx, sessionId, profileDataKeys[index], []byte(profileItem))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write profile entry with", "key", profileDataKeys[index], "value", profileItem, "error", err)
|
||||
return err
|
||||
}
|
||||
res.FlagSet = append(res.FlagSet, flag)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -430,7 +430,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1234",
|
||||
"expectedContent": "My profile:\nName: foo bar\nGender: male\nAge: 84\nLocation: Kilifi\nYou provide: Bananas\n\n0:Back"
|
||||
"expectedContent": "My profile:\nName: foo bar\nGender: male\nAge: 79\nLocation: Kilifi\nYou provide: Bananas\n\n0:Back"
|
||||
},
|
||||
{
|
||||
"input": "0",
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
Jina la kwanza la sasa {{.get_current_profile_info}}
|
||||
Jina la kwanza la sasa: {{.get_current_profile_info}}
|
||||
Weka majina yako ya kwanza:
|
||||
@@ -1,2 +1,2 @@
|
||||
Eneo la sasa {{.get_current_profile_info}}
|
||||
Eneo la sasa: {{.get_current_profile_info}}
|
||||
Weka eneo:
|
||||
@@ -10,5 +10,4 @@ CATCH _ flag_back_set 1
|
||||
RELOAD save_offerings
|
||||
INCMP _ 0
|
||||
CATCH pin_entry flag_offerings_set 1
|
||||
CATCH pin_entry flag_offerings_set 0
|
||||
INCMP update_profile_items *
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
Mwaka wa sasa wa kuzaliwa {{.get_current_profile_info}}
|
||||
Mwaka wa sasa wa kuzaliwa: {{.get_current_profile_info}}
|
||||
Weka mwaka wa kuzaliwa
|
||||
@@ -11,3 +11,4 @@ INCMP _ 0
|
||||
INCMP set_male 1
|
||||
INCMP set_female 2
|
||||
INCMP set_unspecified 3
|
||||
INCMP . *
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
Jinsia ya sasa {{.get_current_profile_info}}
|
||||
Jinsia ya sasa: {{.get_current_profile_info}}
|
||||
Chagua jinsia
|
||||
Reference in New Issue
Block a user