diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 97b84ea..7178188 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -199,10 +199,16 @@ func (h *MenuHandlers) SetLanguage(ctx context.Context, sym string, input []byte // handles the account creation when no existing account is present for the session and stores associated data in the user data store. func (h *MenuHandlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error { flag_account_created, _ := h.flagManager.GetFlag("flag_account_created") + flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed") + r, err := h.accountService.CreateAccount(ctx) if err != nil { - return err + res.FlagSet = append(res.FlagSet, flag_account_creation_failed) + logg.ErrorCtxf(ctx, "failed to create an account", "error", err) + return nil } + res.FlagReset = append(res.FlagReset, flag_account_creation_failed) + trackingId := r.TrackingId publicKey := r.PublicKey @@ -430,6 +436,14 @@ func (h *MenuHandlers) ResetInvalidPIN(ctx context.Context, sym string, input [] 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 + flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") + res.FlagReset = append(res.FlagReset, flag_api_error) + return res, nil +} + // ConfirmPinChange validates user's new PIN. If input matches the temporary PIN, saves it as the new account PIN. func (h *MenuHandlers) ConfirmPinChange(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -1440,7 +1454,7 @@ func (h *MenuHandlers) CheckAccountStatus(ctx context.Context, sym string, input if err != nil { res.FlagSet = append(res.FlagSet, flag_api_error) logg.ErrorCtxf(ctx, "failed on TrackAccountStatus", "error", err) - return res, err + return res, nil } res.FlagReset = append(res.FlagReset, flag_api_error) @@ -1606,6 +1620,7 @@ func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input } flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient") flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite") + flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") recipient := string(input) @@ -1679,10 +1694,13 @@ func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input logg.InfoCtxf(ctx, "Resolving with fqdn alias", "alias", fqdn) AliasAddress, err = h.accountService.CheckAliasAddress(ctx, fqdn) if err == nil { + res.FlagReset = append(res.FlagReset, flag_api_error) AliasAddressResult = AliasAddress.Address continue } else { + res.FlagSet = append(res.FlagSet, flag_api_error) logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", recipient, "error_alias_check", err) + return res, nil } } } @@ -2004,6 +2022,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b } flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher") + flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY) if err != nil { @@ -2014,9 +2033,10 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b // Fetch vouchers from API vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey)) if err != nil { - res.FlagSet = append(res.FlagSet, flag_no_active_voucher) + res.FlagSet = append(res.FlagSet, flag_api_error) return res, nil } + res.FlagReset = append(res.FlagReset, flag_api_error) if len(vouchersResp) == 0 { res.FlagSet = append(res.FlagSet, flag_no_active_voucher) @@ -2231,6 +2251,7 @@ func (h *MenuHandlers) GetVoucherDetails(ctx context.Context, sym string, input res.FlagSet = append(res.FlagSet, flag_api_error) return res, nil } + res.FlagReset = append(res.FlagReset, flag_api_error) res.Content = fmt.Sprintf( "Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", voucherData.TokenName, voucherData.TokenSymbol, voucherData.TokenCommodity, voucherData.TokenLocation, @@ -2265,6 +2286,7 @@ func (h *MenuHandlers) CheckTransactions(ctx context.Context, sym string, input logg.ErrorCtxf(ctx, "failed on FetchTransactions", "error", err) return res, err } + res.FlagReset = append(res.FlagReset, flag_api_error) // Return if there are no transactions if len(transactionsResp) == 0 { @@ -2506,6 +2528,8 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input return res, nil } + flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error") + store := h.userdataStore aliasHint, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE) if err != nil { @@ -2529,9 +2553,12 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input sanitizedInput := sanitizeAliasHint(string(input)) aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput) if err != nil { + res.FlagSet = append(res.FlagSet, flag_api_error) logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", string(aliasHint), "error_alias_request", err) - return res, fmt.Errorf("Failed to retrieve alias: %s", err.Error()) + return res, nil } + res.FlagReset = append(res.FlagReset, flag_api_error) + alias := aliasResult.Alias logg.InfoCtxf(ctx, "Suggested alias ", "alias", alias) diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index bd0211c..7afe790 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -226,6 +226,8 @@ func TestCreateAccount(t *testing.T) { } flag_account_created, err := fm.GetFlag("flag_account_created") + flag_account_creation_failed, _ := fm.GetFlag("flag_account_creation_failed") + if err != nil { t.Logf(err.Error()) } @@ -242,7 +244,8 @@ func TestCreateAccount(t *testing.T) { PublicKey: "0xD3adB33f", }, expectedResult: resource.Result{ - FlagSet: []uint32{flag_account_created}, + FlagSet: []uint32{flag_account_created}, + FlagReset: []uint32{flag_account_creation_failed}, }, }, } @@ -620,9 +623,15 @@ func TestSaveGender(t *testing.T) { func TestSaveTemporaryPin(t *testing.T) { sessionId := "session123" - ctx, store := InitializeTestStore(t) + + ctx, userdatastore := InitializeTestStore(t) ctx = context.WithValue(ctx, "SessionId", sessionId) + _, logdb := InitializeTestLogdbStore(t) + logDb := store.LogDb{ + Db: logdb, + } + fm, err := NewFlagManager(flagsPath) if err != nil { log.Fatal(err) @@ -633,7 +642,8 @@ func TestSaveTemporaryPin(t *testing.T) { // Create the MenuHandlers instance with the mock flag manager h := &MenuHandlers{ flagManager: fm, - userdataStore: store, + userdataStore: userdatastore, + logDb: logDb, } // Define test cases @@ -674,9 +684,15 @@ func TestSaveTemporaryPin(t *testing.T) { func TestCheckIdentifier(t *testing.T) { sessionId := "session123" - ctx, store := InitializeTestStore(t) + ctx, userdatastore := InitializeTestStore(t) ctx = context.WithValue(ctx, "SessionId", sessionId) + _, logdb := InitializeTestLogdbStore(t) + + logDb := store.LogDb{ + Db: logdb, + } + // Define test cases tests := []struct { name string @@ -696,14 +712,15 @@ func TestCheckIdentifier(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(tt.publicKey)) + err := userdatastore.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(tt.publicKey)) if err != nil { t.Fatal(err) } // Create the MenuHandlers instance with the mock store h := &MenuHandlers{ - userdataStore: store, + userdataStore: userdatastore, + logDb: logDb, } // Call the method @@ -2025,6 +2042,11 @@ func TestManageVouchers(t *testing.T) { t.Fatal(err) } flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher") + if err != nil { + t.Fatal(err) + } + flag_api_error, err := fm.GetFlag("flag_api_call_error") + if err != nil { t.Fatal(err) } @@ -2048,7 +2070,8 @@ func TestManageVouchers(t *testing.T) { expectedVoucherSymbols: []byte(""), expectedUpdatedAddress: []byte(""), expectedResult: resource.Result{ - FlagSet: []uint32{flag_no_active_voucher}, + FlagSet: []uint32{flag_no_active_voucher}, + FlagReset: []uint32{flag_api_error}, }, }, { @@ -2064,7 +2087,7 @@ func TestManageVouchers(t *testing.T) { expectedVoucherSymbols: []byte("1:TOKEN1"), expectedUpdatedAddress: []byte("0x123"), expectedResult: resource.Result{ - FlagReset: []uint32{flag_no_active_voucher}, + FlagReset: []uint32{flag_api_error, flag_no_active_voucher}, }, }, { @@ -2077,7 +2100,7 @@ func TestManageVouchers(t *testing.T) { expectedVoucherSymbols: []byte("1:SRF\n2:MILO"), expectedUpdatedAddress: []byte("0xd4c288865Ce"), expectedResult: resource.Result{ - FlagReset: []uint32{flag_no_active_voucher}, + FlagReset: []uint32{flag_api_error, flag_no_active_voucher}, }, }, } @@ -2229,6 +2252,8 @@ func TestGetVoucherDetails(t *testing.T) { if err != nil { t.Logf(err.Error()) } + + flag_api_error, _ := fm.GetFlag("flag_api_call_error") mockAccountService := new(mocks.MockAccountService) sessionId := "session123" @@ -2256,8 +2281,8 @@ func TestGetVoucherDetails(t *testing.T) { "Name: %s\nSymbol: %s\nCommodity: %s\nLocation: %s", tokenDetails.TokenName, tokenDetails.TokenSymbol, tokenDetails.TokenCommodity, tokenDetails.TokenLocation, ) mockAccountService.On("VoucherData", string(tokA_AAddress)).Return(tokenDetails, nil) - res, err := h.GetVoucherDetails(ctx, "SessionId", []byte("")) + expectedResult.FlagReset = append(expectedResult.FlagReset, flag_api_error) assert.NoError(t, err) assert.Equal(t, expectedResult, res) } diff --git a/handlers/local.go b/handlers/local.go index 9f80e7e..69f5883 100644 --- a/handlers/local.go +++ b/handlers/local.go @@ -129,6 +129,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService) ls.DbRs.AddLocalFunc("get_suggested_alias", appHandlers.GetSuggestedAlias) ls.DbRs.AddLocalFunc("confirm_new_alias", appHandlers.ConfirmNewAlias) ls.DbRs.AddLocalFunc("check_account_created", appHandlers.CheckAccountCreated) + ls.DbRs.AddLocalFunc("reset_api_call_failure", appHandlers.ResetApiCallFailure) ls.first = appHandlers.Init diff --git a/services/registration/api_failure b/services/registration/api_failure index 06d2d9e..915b7fc 100644 --- a/services/registration/api_failure +++ b/services/registration/api_failure @@ -1 +1 @@ -Failed to connect to the custodial service.Please try again. \ No newline at end of file +Failed to connect to the custodial service .Please try again. \ No newline at end of file diff --git a/services/registration/api_failure.vis b/services/registration/api_failure.vis index 37b3deb..469b5db 100644 --- a/services/registration/api_failure.vis +++ b/services/registration/api_failure.vis @@ -1,5 +1,7 @@ +LOAD reset_api_call_failure 6 +RELOAD reset_api_call_failure MOUT retry 1 MOUT quit 9 HALT -INCMP _ 1 +INCMP ^ 1 INCMP quit 9 diff --git a/services/registration/api_failure_swa b/services/registration/api_failure_swa new file mode 100644 index 0000000..fb24f2c --- /dev/null +++ b/services/registration/api_failure_swa @@ -0,0 +1 @@ +Imeshindwa kuunganisha kwenye huduma ya uangalizi. Tafadhali jaribu tena. \ No newline at end of file diff --git a/services/registration/check_statement.vis b/services/registration/check_statement.vis index d79b5ca..c393a0a 100644 --- a/services/registration/check_statement.vis +++ b/services/registration/check_statement.vis @@ -1,5 +1,6 @@ LOAD check_transactions 0 RELOAD check_transactions +CATCH api_failure flag_api_call_error 1 CATCH no_transfers flag_no_transfers 1 LOAD authorize_account 6 MOUT back 0 diff --git a/services/registration/main.vis b/services/registration/main.vis index cda6844..efa8b2a 100644 --- a/services/registration/main.vis +++ b/services/registration/main.vis @@ -2,6 +2,7 @@ LOAD clear_temporary_value 2 RELOAD clear_temporary_value LOAD manage_vouchers 160 RELOAD manage_vouchers +CATCH api_failure flag_api_call_error 1 LOAD check_balance 128 RELOAD check_balance CATCH api_failure flag_api_call_error 1 diff --git a/services/registration/my_account_alias.vis b/services/registration/my_account_alias.vis index b2c48f8..c44cce1 100644 --- a/services/registration/my_account_alias.vis +++ b/services/registration/my_account_alias.vis @@ -5,4 +5,5 @@ HALT INCMP _ 0 LOAD request_custom_alias 0 RELOAD request_custom_alias +CATCH api_failure flag_api_call_error 1 INCMP confirm_new_alias * diff --git a/services/registration/send.vis b/services/registration/send.vis index 369bb67..dfdcd70 100644 --- a/services/registration/send.vis +++ b/services/registration/send.vis @@ -5,6 +5,7 @@ MOUT back 0 HALT LOAD validate_recipient 50 RELOAD validate_recipient +CATCH api_failure flag_api_call_error 1 CATCH invalid_recipient flag_invalid_recipient 1 CATCH invite_recipient flag_invalid_recipient_with_invite 1 INCMP _ 0 diff --git a/services/registration/voucher_details.vis b/services/registration/voucher_details.vis index 1b009f1..628e034 100644 --- a/services/registration/voucher_details.vis +++ b/services/registration/voucher_details.vis @@ -1,5 +1,7 @@ CATCH no_voucher flag_no_active_voucher 1 LOAD get_voucher_details 0 +RELOAD get_voucher_details +CATCH api_failure flag_api_call_error 1 MAP get_voucher_details MOUT back 0 HALT