diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 8d3b928..cddb473 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -21,6 +21,7 @@ import ( "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/internal/utils" + "git.grassecon.net/urdt/ussd/models" "git.grassecon.net/urdt/ussd/remote" "gopkg.in/leonelquinteros/gotext.v1" @@ -78,6 +79,7 @@ type Handlers struct { flagManager *asm.FlagParser accountService remote.AccountServiceInterface prefixDb storage.PrefixDb + profile *models.Profile } func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *utils.AdminStore, accountService remote.AccountServiceInterface) (*Handlers, error) { @@ -96,6 +98,7 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *util adminstore: adminstore, accountService: accountService, prefixDb: prefixDb, + profile: &models.Profile{Max: 6}, } return h, nil } @@ -406,7 +409,10 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) firstName := string(input) store := h.userdataStore flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_firstname_set, _ := h.flagManager.GetFlag("flag_firstname_set") + allowUpdate := h.st.MatchFlag(flag_allow_update, true) + firstNameSet := h.st.MatchFlag(flag_firstname_set, true) if allowUpdate { temporaryFirstName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_FIRST_NAME, []byte(temporaryFirstName)) @@ -414,11 +420,16 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) logg.ErrorCtxf(ctx, "failed to write firstName entry with", "key", common.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) return res, err } + res.FlagSet = append(res.FlagSet, flag_firstname_set) } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(firstName)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryFirstName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) - return res, err + if firstNameSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(firstName)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryFirstName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) + return res, err + } + } else { + h.profile.InsertOrShift(0, firstName) } } @@ -438,7 +449,9 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte) familyName := string(input) flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_familyname_set, _ := h.flagManager.GetFlag("flag_familyname_set") allowUpdate := h.st.MatchFlag(flag_allow_update, true) + familyNameSet := h.st.MatchFlag(flag_familyname_set, true) if allowUpdate { temporaryFamilyName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) @@ -447,11 +460,16 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte) logg.ErrorCtxf(ctx, "failed to write familyName entry with", "key", common.DATA_FAMILY_NAME, "value", temporaryFamilyName, "error", err) return res, err } + res.FlagSet = append(res.FlagSet, flag_familyname_set) } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(familyName)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryFamilyName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", familyName, "error", err) - return res, err + if familyNameSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(familyName)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryFamilyName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", familyName, "error", err) + return res, err + } + } else { + h.profile.InsertOrShift(1, familyName) } } @@ -469,7 +487,10 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou yob := string(input) store := h.userdataStore flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_yob_set, _ := h.flagManager.GetFlag("flag_yob_set") + allowUpdate := h.st.MatchFlag(flag_allow_update, true) + yobSet := h.st.MatchFlag(flag_yob_set, true) if allowUpdate { temporaryYob, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) @@ -478,11 +499,16 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou logg.ErrorCtxf(ctx, "failed to write yob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) return res, err } + res.FlagSet = append(res.FlagSet, flag_yob_set) } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(yob)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryYob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) - return res, err + if yobSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(yob)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryYob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) + return res, err + } + } else { + h.profile.InsertOrShift(3, yob) } } @@ -501,7 +527,9 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( store := h.userdataStore flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_location_set, _ := h.flagManager.GetFlag("flag_location_set") allowUpdate := h.st.MatchFlag(flag_allow_update, true) + locationSet := h.st.MatchFlag(flag_location_set, true) if allowUpdate { temporaryLocation, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) @@ -511,10 +539,15 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( return res, err } } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(location)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryLocation entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) - return res, err + if locationSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(location)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryLocation entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) + return res, err + } + res.FlagSet = append(res.FlagSet, flag_location_set) + } else { + h.profile.InsertOrShift(4, location) } } @@ -533,7 +566,11 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re gender := strings.Split(symbol, "_")[1] store := h.userdataStore flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_gender_set, _ := h.flagManager.GetFlag("flag_gender_set") + allowUpdate := h.st.MatchFlag(flag_allow_update, true) + genderSet := h.st.MatchFlag(flag_gender_set, true) + fmt.Println("GenderSet:", genderSet) if allowUpdate { temporaryGender, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) @@ -542,11 +579,16 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re logg.ErrorCtxf(ctx, "failed to write gender entry with", "key", common.DATA_GENDER, "value", gender, "error", err) return res, err } + res.FlagSet = append(res.FlagSet, flag_gender_set) } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(gender)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryGender entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) - return res, err + if genderSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(gender)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryGender entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) + return res, err + } + } else { + h.profile.InsertOrShift(2, gender) } } @@ -566,7 +608,10 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) store := h.userdataStore flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_offerings_set, _ := h.flagManager.GetFlag("flag_offerings_set") + allowUpdate := h.st.MatchFlag(flag_allow_update, true) + offeringsSet := h.st.MatchFlag(flag_offerings_set, true) if allowUpdate { temporaryOfferings, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) @@ -575,11 +620,16 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) logg.ErrorCtxf(ctx, "failed to write offerings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } + res.FlagSet = append(res.FlagSet, flag_offerings_set) } else { - err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(offerings)) - if err != nil { - logg.ErrorCtxf(ctx, "failed to write temporaryOfferings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) - return res, err + if offeringsSet { + err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(offerings)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write temporaryOfferings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) + return res, err + } + } else { + h.profile.InsertOrShift(5, offerings) } } @@ -1834,3 +1884,94 @@ func (h *Handlers) ViewTransactionStatement(ctx context.Context, sym string, inp return res, nil } + +func (h *Handlers) insertProfileItems(ctx context.Context, sessionId string, res *resource.Result) error { + var err error + store := h.userdataStore + profileFlagNames := []string{ + "flag_firstname_set", + "flag_familyname_set", + "flag_yob_set", + "flag_gender_set", + "flag_location_set", + "flag_offerings_set", + } + for index, profileItem := range h.profile.ProfileItems { + switch index { + case 0: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_FIRST_NAME, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile first name entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + case 1: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_FAMILY_NAME, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile family name entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + case 2: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_GENDER, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile gender entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + case 3: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_YOB, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile yob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + case 4: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_LOCATION, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile location entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + case 5: + if profileItem != "0" { + err = store.WriteEntry(ctx, sessionId, common.DATA_OFFERINGS, []byte(profileItem)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write profile offerings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", profileItem, "error", err) + return err + } + flag, _ := h.flagManager.GetFlag(profileFlagNames[index]) + res.FlagSet = append(res.FlagSet, flag) + } + } + } + return nil +} + +func (h *Handlers) UpdateAllProfileItems(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + err := h.insertProfileItems(ctx, sessionId, &res) + if err != nil { + return res, err + } + return res, nil +}