From 768535b5403f5317d6644dfb2cf04534f39bb22b Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 28 Aug 2024 14:54:39 +0300 Subject: [PATCH 1/2] Quit with the transaction confirmation --- internal/handlers/ussd/menuhandler.go | 21 +++++++++++++++++++ services/registration/transaction_initiated | 1 - .../registration/transaction_initiated.vis | 3 --- .../registration/transaction_initiated_swa | 1 - 4 files changed, 21 insertions(+), 5 deletions(-) delete mode 100644 services/registration/transaction_initiated delete mode 100644 services/registration/transaction_initiated_swa diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 894d0f1..81ac382 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -706,9 +706,30 @@ func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} + accountData, err := h.accountFileHandler.ReadAccountData() + if err != nil { + return res, err + } + // TODO // Use the amount, recipient and sender to call the API and initialize the transaction + switch codeFromCtx(ctx) { + case "swa": + res.Content = fmt.Sprintf("Ombi lako limetumwa. %s atapokea %s kutoka kwa %s.", accountData["Recipient"], accountData["Amount"], accountData["PublicKey"]) + default: + res.Content = fmt.Sprintf("Your request has been sent. %s will receive %s from %s.", accountData["Recipient"], accountData["Amount"], accountData["PublicKey"]) + } + + // reset the transaction + accountData["Recipient"] = "" + accountData["Amount"] = "" + + err = h.accountFileHandler.WriteAccountData(accountData) + if err != nil { + return res, err + } + res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED) return res, nil } diff --git a/services/registration/transaction_initiated b/services/registration/transaction_initiated deleted file mode 100644 index 2909071..0000000 --- a/services/registration/transaction_initiated +++ /dev/null @@ -1 +0,0 @@ -Your request has been sent. {{.get_recipient}} will receive {{.get_amount}} from {{.get_sender}}. \ No newline at end of file diff --git a/services/registration/transaction_initiated.vis b/services/registration/transaction_initiated.vis index c51a441..b1042f5 100644 --- a/services/registration/transaction_initiated.vis +++ b/services/registration/transaction_initiated.vis @@ -6,7 +6,4 @@ MAP get_recipient RELOAD get_sender MAP get_sender LOAD initiate_transaction 0 -RELOAD transaction_reset -MOUT quit 9 HALT -INCMP quit 9 diff --git a/services/registration/transaction_initiated_swa b/services/registration/transaction_initiated_swa deleted file mode 100644 index fb6125a..0000000 --- a/services/registration/transaction_initiated_swa +++ /dev/null @@ -1 +0,0 @@ -Ombi lako limetumwa. {{.get_recipient}} atapokea {{.get_amount}} kutoka kwa {{.get_sender}}. \ No newline at end of file From 9f0ef131981f0aafca02124edd85a4206285e0e1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 28 Aug 2024 16:23:52 +0300 Subject: [PATCH 2/2] Added documentation lines on functions --- internal/handlers/ussd/menuhandler.go | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 81ac382..e771749 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -37,6 +37,7 @@ func NewHandlers(path string, st *state.State) *Handlers { } } +// SetLanguage sets the language across the menu func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) { inputStr := string(input) res := resource.Result{} @@ -55,6 +56,9 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r return res, nil } +// CreateAccount checks if any account exists on the JSON data file, and if not +// creates an account on the API, +// sets the default values and flags func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -146,6 +150,9 @@ func (h *Handlers) SetResetSingleEdit(ctx context.Context, sym string, input []b return res, nil } +// VerifyPin checks whether the confirmation PIN is similar to the account PIN +// If similar, it sets the USERFLAG_PIN_SET flag allowing the user +// to access the main menu func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -165,6 +172,7 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res return res, nil } +// isValidPIN checks whether the given input is a 4 digit number func isValidPIN(pin string) bool { match, _ := regexp.MatchString(`^\d{4}$`, pin) return match @@ -330,6 +338,7 @@ func (h *Handlers) ResetAccountUnlocked(ctx context.Context, sym string, input [ return res, nil } +// CheckIdentifier retrieves the PublicKey from the JSON data file. func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -378,6 +387,8 @@ func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []by return res, nil } +// CheckAccountStatus queries the API using the TrackingId and sets flags +// based on the account status func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -411,6 +422,7 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b return res, nil } +// Quit displays the Thank you message and exits the menu func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} switch codeFromCtx(ctx) { @@ -423,6 +435,7 @@ func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource return res, nil } +// VerifyYob verifies the length of the given input func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} date := string(input) @@ -448,6 +461,8 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by return res, nil } +// CheckBalance retrieves the balance from the API using the "PublicKey" and sets +// the balance as the result content func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -465,6 +480,7 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( return res, nil } +// ValidateRecipient validates that the given input is a valid phone number. func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} recipient := string(input) @@ -494,6 +510,8 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by return res, nil } +// TransactionReset resets the previous transaction data (Recipient and Amount) +// as well as the invalid flags func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} accountData, err := h.accountFileHandler.ReadAccountData() @@ -515,6 +533,7 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt return res, nil } +// ResetTransactionAmount resets the transaction amount and invalid flag func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} accountData, err := h.accountFileHandler.ReadAccountData() @@ -535,6 +554,8 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input return res, nil } +// MaxAmount gets the current balance from the API and sets it as +// the result content. func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -553,6 +574,8 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res return res, nil } +// ValidateAmount ensures that the given input is a valid amount and that +// it is not more than the current balance. func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} amountStr := string(input) @@ -610,6 +633,8 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) return res, nil } + +// GetRecipient returns the transaction recipient from a JSON data file. func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} @@ -684,7 +709,6 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res return res, nil } - // QuickWithBalance retrieves the balance for a given public key from the custodial balance API endpoint before // gracefully exiting the session. func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -703,6 +727,8 @@ func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte return res, nil } +// InitiateTransaction returns a confirmation and resets the transaction data +// on the JSON file. func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{}