From 827debe020436319721a3b99d8f5f51ae0656cce Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 3 Jul 2025 16:55:57 +0300 Subject: [PATCH] added account_status.go for account status related functions --- handlers/application/account_status.go | 122 +++++++++++++++++++++++++ handlers/application/menuhandler.go | 112 ----------------------- 2 files changed, 122 insertions(+), 112 deletions(-) create mode 100644 handlers/application/account_status.go diff --git a/handlers/application/account_status.go b/handlers/application/account_status.go new file mode 100644 index 0000000..845107b --- /dev/null +++ b/handlers/application/account_status.go @@ -0,0 +1,122 @@ +package application + +import ( + "context" + "fmt" + "strconv" + + "git.defalsify.org/vise.git/db" + "git.defalsify.org/vise.git/resource" + storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" +) + +// CheckAccountStatus queries the API using the TrackingId and sets flags +// based on the account status. +func (h *MenuHandlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + + flag_account_success, _ := h.flagManager.GetFlag("flag_account_success") + flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending") + flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + store := h.userdataStore + publicKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) + if err != nil { + logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err) + return res, err + } + + r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey)) + if err != nil { + res.FlagSet = append(res.FlagSet, flag_api_error) + logg.ErrorCtxf(ctx, "failed on TrackAccountStatus", "error", err) + return res, nil + } + + res.FlagReset = append(res.FlagReset, flag_api_error) + + if r.Active { + res.FlagSet = append(res.FlagSet, flag_account_success) + res.FlagReset = append(res.FlagReset, flag_account_pending) + } else { + res.FlagReset = append(res.FlagReset, flag_account_success) + res.FlagSet = append(res.FlagSet, flag_account_pending) + } + + return res, nil +} + +func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + flag_language_set, _ := h.flagManager.GetFlag("flag_language_set") + flag_account_created, _ := h.flagManager.GetFlag("flag_account_created") + + store := h.userdataStore + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + _, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) + if err != nil { + if db.IsNotFound(err) { + // reset major flags + res.FlagReset = append(res.FlagReset, flag_language_set) + res.FlagReset = append(res.FlagReset, flag_account_created) + + return res, nil + } + + return res, nil + } + + res.FlagSet = append(res.FlagSet, flag_account_created) + return res, nil +} + +// CheckBlockedStatus: +// 1. Checks whether the DATA_SELF_PIN_RESET is 1 and sets the flag_account_pin_reset +// 2. resets the account blocked flag if the PIN attempts have been reset by an admin. +func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + store := h.userdataStore + + flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked") + flag_account_pin_reset, _ := h.flagManager.GetFlag("flag_account_pin_reset") + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + + res.FlagReset = append(res.FlagReset, flag_account_pin_reset) + + selfPinReset, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELF_PIN_RESET) + if err == nil { + pinResetValue, _ := strconv.ParseUint(string(selfPinReset), 0, 64) + if pinResetValue == 1 { + res.FlagSet = append(res.FlagSet, flag_account_pin_reset) + } + } + + currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS) + if err != nil { + if !db.IsNotFound(err) { + return res, nil + } + } + + pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) + if pinAttemptsValue == 0 { + res.FlagReset = append(res.FlagReset, flag_account_blocked) + return res, nil + } + + return res, nil +} diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 084facd..4c7b4ec 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "path" - "strconv" "gopkg.in/leonelquinteros/gotext.v1" @@ -155,76 +154,6 @@ func codeFromCtx(ctx context.Context) string { return code } -func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - flag_language_set, _ := h.flagManager.GetFlag("flag_language_set") - flag_account_created, _ := h.flagManager.GetFlag("flag_account_created") - - store := h.userdataStore - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - - _, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) - if err != nil { - if db.IsNotFound(err) { - // reset major flags - res.FlagReset = append(res.FlagReset, flag_language_set) - res.FlagReset = append(res.FlagReset, flag_account_created) - - return res, nil - } - - return res, nil - } - - res.FlagSet = append(res.FlagSet, flag_account_created) - return res, nil -} - -// CheckBlockedStatus: -// 1. Checks whether the DATA_SELF_PIN_RESET is 1 and sets the flag_account_pin_reset -// 2. resets the account blocked flag if the PIN attempts have been reset by an admin. -func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - store := h.userdataStore - - flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked") - flag_account_pin_reset, _ := h.flagManager.GetFlag("flag_account_pin_reset") - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - - res.FlagReset = append(res.FlagReset, flag_account_pin_reset) - - selfPinReset, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELF_PIN_RESET) - if err == nil { - pinResetValue, _ := strconv.ParseUint(string(selfPinReset), 0, 64) - if pinResetValue == 1 { - res.FlagSet = append(res.FlagSet, flag_account_pin_reset) - } - } - - currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS) - if err != nil { - if !db.IsNotFound(err) { - return res, nil - } - } - - pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) - if pinAttemptsValue == 0 { - res.FlagReset = append(res.FlagReset, flag_account_blocked) - return res, nil - } - - return res, nil -} - // ResetApiCallFailure resets the api call failure flag func (h *MenuHandlers) ResetApiCallFailure(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -284,47 +213,6 @@ func (h *MenuHandlers) SetBack(ctx context.Context, sym string, input []byte) (r return res, nil } -// CheckAccountStatus queries the API using the TrackingId and sets flags -// based on the account status. -func (h *MenuHandlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { - var res resource.Result - - flag_account_success, _ := h.flagManager.GetFlag("flag_account_success") - flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending") - flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") - - sessionId, ok := ctx.Value("SessionId").(string) - if !ok { - return res, fmt.Errorf("missing session") - } - - store := h.userdataStore - publicKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) - if err != nil { - logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err) - return res, err - } - - r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey)) - if err != nil { - res.FlagSet = append(res.FlagSet, flag_api_error) - logg.ErrorCtxf(ctx, "failed on TrackAccountStatus", "error", err) - return res, nil - } - - res.FlagReset = append(res.FlagReset, flag_api_error) - - if r.Active { - res.FlagSet = append(res.FlagSet, flag_account_success) - res.FlagReset = append(res.FlagReset, flag_account_pending) - } else { - res.FlagReset = append(res.FlagReset, flag_account_success) - res.FlagSet = append(res.FlagSet, flag_account_pending) - } - - return res, nil -} - // Quit displays the Thank you message and exits the menu. func (h *MenuHandlers) Quit(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result