From 9af7b775a78cddcf0ba0ea33517daf2d0fb3542a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 11 Nov 2024 16:32:17 +0300 Subject: [PATCH 1/7] log directly to the terminal --- cmd/africastalking/main.go | 26 +++++--------------------- cmd/async/main.go | 18 +----------------- cmd/http/main.go | 17 ----------------- cmd/main.go | 17 ----------------- remote/accountservice.go | 13 ++++--------- 5 files changed, 10 insertions(+), 81 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index bae83b9..5a523e7 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -31,26 +31,10 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - InfoLogger *log.Logger - ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() - - logFile := "urdt-ussd-africastalking.log" - - file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - log.Fatal(err) - } - - InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) - ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - - // Inject into remote package - remote.InfoLogger = InfoLogger - remote.ErrorLogger = ErrorLogger } type atRequestParser struct{} @@ -58,14 +42,14 @@ type atRequestParser struct{} func (arp *atRequestParser) GetSessionId(rq any) (string, error) { rqv, ok := rq.(*http.Request) if !ok { - ErrorLogger.Println("got an invalid request:", rq) + log.Println("got an invalid request:", rq) return "", handlers.ErrInvalidRequest } // Capture body (if any) for logging body, err := io.ReadAll(rqv.Body) if err != nil { - ErrorLogger.Println("failed to read request body:", err) + log.Println("failed to read request body:", err) return "", fmt.Errorf("failed to read request body: %v", err) } // Reset the body for further reading @@ -75,13 +59,13 @@ func (arp *atRequestParser) GetSessionId(rq any) (string, error) { bodyLog := map[string]string{"body": string(body)} logBytes, err := json.Marshal(bodyLog) if err != nil { - ErrorLogger.Println("failed to marshal request body:", err) + log.Println("failed to marshal request body:", err) } else { - InfoLogger.Println("Received request:", string(logBytes)) + log.Println("Received request:", string(logBytes)) } if err := rqv.ParseForm(); err != nil { - ErrorLogger.Println("failed to parse form data: %v", err) + log.Println("failed to parse form data: %v", err) return "", fmt.Errorf("failed to parse form data: %v", err) } diff --git a/cmd/async/main.go b/cmd/async/main.go index afc0bca..e4c94b0 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -4,7 +4,6 @@ import ( "context" "flag" "fmt" - "log" "os" "os/signal" "path" @@ -24,27 +23,12 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - InfoLogger *log.Logger - ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() - - logFile := "urdt-ussd-async.log" - - file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - log.Fatal(err) - } - - InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) - ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - - // Inject into remote package - remote.InfoLogger = InfoLogger - remote.ErrorLogger = ErrorLogger } + type asyncRequestParser struct { sessionId string input []byte diff --git a/cmd/http/main.go b/cmd/http/main.go index 0565c45..96e2688 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -4,7 +4,6 @@ import ( "context" "flag" "fmt" - "log" "net/http" "os" "os/signal" @@ -27,26 +26,10 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - InfoLogger *log.Logger - ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() - - logFile := "urdt-ussd-http.log" - - file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - log.Fatal(err) - } - - InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) - ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - - // Inject into remote package - remote.InfoLogger = InfoLogger - remote.ErrorLogger = ErrorLogger } func main() { diff --git a/cmd/main.go b/cmd/main.go index 857ccb9..9599eb7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,7 +4,6 @@ import ( "context" "flag" "fmt" - "log" "os" "path" @@ -21,26 +20,10 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - InfoLogger *log.Logger - ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() - - logFile := "urdt-ussd-cli.log" - - file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) - if err != nil { - log.Fatal(err) - } - - InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) - ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - - // Inject into remote package - remote.InfoLogger = InfoLogger - remote.ErrorLogger = ErrorLogger } func main() { diff --git a/remote/accountservice.go b/remote/accountservice.go index e6a5556..388fd9c 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -16,11 +16,6 @@ import ( dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) -var ( - InfoLogger *log.Logger - ErrorLogger *log.Logger -) - type AccountServiceInterface interface { CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) CreateAccount(ctx context.Context) (*models.AccountResult, error) @@ -98,7 +93,6 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes } _, err = doCustodialRequest(ctx, req, &r) if err != nil { - log.Printf("Failed to make custodial %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error()) return nil, err } @@ -179,12 +173,13 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { + log.Printf("Failed to make %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error()) errResponse.Description = err.Error() return nil, err } defer resp.Body.Close() - InfoLogger.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type")) + log.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type")) body, err := io.ReadAll(resp.Body) if err != nil { return nil, err @@ -231,7 +226,7 @@ func logRequestDetails(req *http.Request) { if req.Body != nil { bodyBytes, err := io.ReadAll(req.Body) if err != nil { - ErrorLogger.Printf("Error reading request body: %s", err) + log.Printf("Error reading request body: %s", err) return } req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) @@ -239,5 +234,5 @@ func logRequestDetails(req *http.Request) { bodyBytes = []byte("-") } - InfoLogger.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) + log.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) } From abdb17640b22e6fbdd6e860f1169557acafb60e2 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 12 Nov 2024 10:36:08 +0300 Subject: [PATCH 2/7] add terminal logs --- internal/handlers/ussd/menuhandler.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index ca4af09..6fcb865 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -239,6 +239,7 @@ func (h *Handlers) CheckPinMisMatch(ctx context.Context, sym string, input []byt } temporaryPin, err := store.ReadEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE) if err != nil { + return res, err } if bytes.Equal(temporaryPin, input) { @@ -291,6 +292,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt store := h.userdataStore err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(accountPIN)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", accountPIN, "error", err) return res, err } @@ -310,10 +312,12 @@ func (h *Handlers) SaveOthersTemporaryPin(ctx context.Context, sym string, input blockedNumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) if err != nil { + logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) return res, err } err = store.WriteEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE, []byte(temporaryPin)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryPin, "error", err) return res, err } @@ -331,6 +335,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt store := h.userdataStore temporaryPin, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) if err != nil { + logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) return res, err } if bytes.Equal(temporaryPin, input) { @@ -340,6 +345,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt } err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) return res, err } return res, nil @@ -374,6 +380,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) return res, err } @@ -406,11 +413,13 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) temporaryFirstName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_FIRST_NAME, []byte(temporaryFirstName)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(firstName)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) return res, err } } @@ -465,11 +474,13 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou temporaryYob, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_YOB, []byte(temporaryYob)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(yob)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) return res, err } } @@ -495,11 +506,13 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( temporaryLocation, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_LOCATION, []byte(temporaryLocation)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_LOCATION, "value", temporaryLocation, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(location)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) return res, err } } @@ -525,11 +538,13 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re temporaryGender, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_GENDER, []byte(temporaryGender)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_GENDER, "value", gender, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(gender)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) return res, err } } @@ -556,11 +571,13 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) temporaryOfferings, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_OFFERINGS, []byte(temporaryOfferings)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(offerings)) if err != nil { + logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } } From 09f61eb64d07c852011e54b1eeec02374e3922a5 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 13 Nov 2024 15:19:45 +0300 Subject: [PATCH 3/7] log all errors from the hander --- internal/handlers/ussd/menuhandler.go | 50 ++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 6fcb865..58df521 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "log" "path" "regexp" "strconv" @@ -161,6 +162,7 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r languageSetFlag, err := h.flagManager.GetFlag("flag_language_set") if err != nil { + log.Printf("Error: %s", err) return res, err } res.FlagSet = append(res.FlagSet, languageSetFlag) @@ -198,7 +200,6 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r } res.FlagSet = append(res.FlagSet, flag_account_created) return nil - } // CreateAccount checks if any account exists on the JSON data file, and if not @@ -218,10 +219,12 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) logg.Printf(logging.LVL_INFO, "Creating an account because it doesn't exist") err = h.createAccountNoExist(ctx, sessionId, &res) if err != nil { + log.Printf("Error: %s", err) return res, err } } } + return res, nil } @@ -235,11 +238,13 @@ func (h *Handlers) CheckPinMisMatch(ctx context.Context, sym string, input []byt store := h.userdataStore blockedNumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) if err != nil { + log.Printf("Error: %s", err) return res, err } temporaryPin, err := store.ReadEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE) if err != nil { + log.Printf("Error: %s", err) return res, err } if bytes.Equal(temporaryPin, input) { @@ -293,6 +298,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(accountPIN)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", accountPIN, "error", err) + log.Printf("Error: %s", err) return res, err } @@ -313,11 +319,13 @@ func (h *Handlers) SaveOthersTemporaryPin(ctx context.Context, sym string, input if err != nil { logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) + log.Printf("Error: %s", err) return res, err } err = store.WriteEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE, []byte(temporaryPin)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryPin, "error", err) + log.Printf("Error: %s", err) return res, err } @@ -336,6 +344,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt temporaryPin, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) if err != nil { logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) + log.Printf("Error: %s", err) return res, err } if bytes.Equal(temporaryPin, input) { @@ -346,6 +355,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) + log.Printf("Error: %s", err) return res, err } return res, nil @@ -368,6 +378,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte store := h.userdataStore temporaryPin, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) if err != nil { + log.Printf("Error: %s", err) return res, err } if bytes.Equal(input, temporaryPin) { @@ -381,6 +392,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) + log.Printf("Error: %s", err) return res, err } @@ -414,12 +426,14 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) err = store.WriteEntry(ctx, sessionId, common.DATA_FIRST_NAME, []byte(temporaryFirstName)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(firstName)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) + log.Printf("Error: %s", err) return res, err } } @@ -446,11 +460,13 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte) temporaryFamilyName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_FAMILY_NAME, []byte(temporaryFamilyName)) if err != nil { + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(familyName)) if err != nil { + log.Printf("Error: %s", err) return res, err } } @@ -475,12 +491,14 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou err = store.WriteEntry(ctx, sessionId, common.DATA_YOB, []byte(temporaryYob)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(yob)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) + log.Printf("Error: %s", err) return res, err } } @@ -507,12 +525,14 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( err = store.WriteEntry(ctx, sessionId, common.DATA_LOCATION, []byte(temporaryLocation)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_LOCATION, "value", temporaryLocation, "error", err) + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(location)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) + log.Printf("Error: %s", err) return res, err } } @@ -539,12 +559,14 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re err = store.WriteEntry(ctx, sessionId, common.DATA_GENDER, []byte(temporaryGender)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_GENDER, "value", gender, "error", err) + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(gender)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) + log.Printf("Error: %s", err) return res, err } } @@ -572,12 +594,14 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) err = store.WriteEntry(ctx, sessionId, common.DATA_OFFERINGS, []byte(temporaryOfferings)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) + log.Printf("Error: %s", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(offerings)) if err != nil { logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) + log.Printf("Error: %s", err) return res, err } } @@ -643,6 +667,7 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res store := h.userdataStore AccountPin, err := store.ReadEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN) if err != nil { + log.Printf("Error: %s", err) return res, err } if len(input) == 4 { @@ -690,16 +715,19 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b store := h.userdataStore publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { + log.Printf("Error: %s", err) return res, err } r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey)) if err != nil { res.FlagSet = append(res.FlagSet, flag_api_error) + log.Printf("Error: %s", err) return res, err } res.FlagReset = append(res.FlagReset, flag_api_error) if !ok { + log.Printf("Error: %s", err) return res, err } if r.Active { @@ -800,11 +828,13 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( return res, nil } + log.Printf("Error: %s", err) return res, err } activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -828,6 +858,7 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input store := h.userdataStore publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -860,10 +891,12 @@ func (h *Handlers) ResetOthersPin(ctx context.Context, sym string, input []byte) } blockedPhonenumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) if err != nil { + log.Printf("Error: %s", err) return res, err } temporaryPin, err := store.ReadEntry(ctx, string(blockedPhonenumber), common.DATA_TEMPORARY_VALUE) if err != nil { + log.Printf("Error: %s", err) return res, err } err = store.WriteEntry(ctx, string(blockedPhonenumber), common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) @@ -902,6 +935,7 @@ func (h *Handlers) ValidateBlockedNumber(ctx context.Context, sym string, input res.FlagSet = append(res.FlagSet, flag_unregistered_number) return res, nil } else { + log.Printf("Error: %s", err) return res, err } } @@ -1009,6 +1043,7 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1034,10 +1069,12 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) // retrieve the active balance activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { + log.Printf("Error: %s", err) return res, err } balanceValue, err = strconv.ParseFloat(string(activeBal), 64) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1060,6 +1097,7 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) formattedAmount := fmt.Sprintf("%.2f", inputAmount) err = store.WriteEntry(ctx, sessionId, common.DATA_AMOUNT, []byte(formattedAmount)) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1126,6 +1164,7 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res // retrieve the active symbol activeSym, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_SYM) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1163,6 +1202,7 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input [] account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized") if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1265,6 +1305,7 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by if db.IsNotFound(err) { publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1289,17 +1330,20 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by // set the active symbol err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(defaultSym)) if err != nil { + log.Printf("Error: %s", err) return res, err } // set the active balance err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(defaultBal)) if err != nil { + log.Printf("Error: %s", err) return res, err } return res, nil } + log.Printf("Error: %s", err) return res, err } @@ -1355,6 +1399,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) // Read vouchers from the store voucherData, err := h.prefixDb.Get(ctx, []byte("sym")) if err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1390,6 +1435,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r } if err := common.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil { + log.Printf("Error: %s", err) return res, err } @@ -1411,11 +1457,13 @@ func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (re // Get temporary data tempData, err := common.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId) if err != nil { + log.Printf("Error: %s", err) return res, err } // Set as active and clear temporary data if err := common.UpdateVoucherData(ctx, h.userdataStore, sessionId, tempData); err != nil { + log.Printf("Error: %s", err) return res, err } From 047bf0e12ee59764e21829acb3aa7fe932f79694 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 13 Nov 2024 16:15:37 +0300 Subject: [PATCH 4/7] updated the env example --- .env.example | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 29b935e..d392c9b 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,9 @@ PORT=7123 HOST=127.0.0.1 +#AfricasTalking USSD POST endpoint +AT_ENDPOINT=/ussd/africastalking + #PostgreSQL DB_HOST=localhost DB_USER=postgres @@ -12,10 +15,7 @@ DB_SSLMODE=disable DB_TIMEZONE=Africa/Nairobi #External API Calls -CREATE_ACCOUNT_URL=http://localhost:5003/api/v2/account/create -TRACK_STATUS_URL=https://custodial.sarafu.africa/api/track/ -BALANCE_URL=https://custodial.sarafu.africa/api/account/status/ -TRACK_URL=http://localhost:5003/api/v2/account/status - -#AfricasTalking USSD POST endpoint -AT_ENDPOINT=/ussd/africastalking +CUSTODIAL_URL_BASE=http://localhost:5003 +CUSTODIAL_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr +DATA_URL_BASE=http://localhost:5006 +DATA_BEARER_TOKEN=eyJeSIsIRcCI6IXVCJ.yJwdWJsaLZXkiOiIwrrrrrr From d7232a53efab7acd5d9ba3bc378ccd42defc7add Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 13 Nov 2024 16:16:32 +0300 Subject: [PATCH 5/7] use the bearer token --- config/config.go | 40 ++++++++++++++++++++-------------------- remote/accountservice.go | 4 ++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index fbf518b..c4445eb 100644 --- a/config/config.go +++ b/config/config.go @@ -7,30 +7,30 @@ import ( ) const ( - createAccountPath = "/api/v2/account/create" - trackStatusPath = "/api/track" - balancePathPrefix = "/api/account" - trackPath = "/api/v2/account/status" - voucherHoldingsPathPrefix = "/api/v1/holdings" + createAccountPath = "/api/v2/account/create" + trackStatusPath = "/api/track" + balancePathPrefix = "/api/account" + trackPath = "/api/v2/account/status" + voucherHoldingsPathPrefix = "/api/v1/holdings" voucherTransfersPathPrefix = "/api/v1/transfers/last10" - voucherDataPathPrefix = "/api/v1/token" + voucherDataPathPrefix = "/api/v1/token" ) var ( - custodialURLBase string - dataURLBase string - CustodialAPIKey string - DataAPIKey string + custodialURLBase string + dataURLBase string + CustodialBearerToken string + DataBearerToken string ) var ( - CreateAccountURL string - TrackStatusURL string - BalanceURL string - TrackURL string - VoucherHoldingsURL string - VoucherTransfersURL string - VoucherDataURL string + CreateAccountURL string + TrackStatusURL string + BalanceURL string + TrackURL string + VoucherHoldingsURL string + VoucherTransfersURL string + VoucherDataURL string ) func setBase() error { @@ -38,8 +38,8 @@ func setBase() error { custodialURLBase = initializers.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003") dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006") - CustodialAPIKey = initializers.GetEnv("CUSTODIAL_API_KEY", "xd") - DataAPIKey = initializers.GetEnv("DATA_API_KEY", "xd") + CustodialBearerToken = initializers.GetEnv("CUSTODIAL_BEARER_TOKEN", "") + DataBearerToken = initializers.GetEnv("DATA_BEARER_TOKEN", "") _, err = url.JoinPath(custodialURLBase, "/foo") if err != nil { @@ -58,7 +58,7 @@ func LoadConfig() error { if err != nil { return err } - CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath) + CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath) TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath) BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix) TrackURL, _ = url.JoinPath(custodialURLBase, trackPath) diff --git a/remote/accountservice.go b/remote/accountservice.go index 388fd9c..19914cd 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -209,13 +209,13 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons } func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { - req.Header.Set("X-GE-KEY", config.CustodialAPIKey) + req.Header.Set("Authorization", "Bearer "+config.CustodialBearerToken) logRequestDetails(req) return doRequest(ctx, req, rcpt) } func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { - req.Header.Set("X-GE-KEY", config.DataAPIKey) + req.Header.Set("Authorization", "Bearer "+config.DataBearerToken) logRequestDetails(req) return doRequest(ctx, req, rcpt) } From b6b3ef83a466ce6ad7e357697e717dc96859d4d0 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 13 Nov 2024 18:02:02 +0300 Subject: [PATCH 6/7] updated the comment to match the functionality --- common/vouchers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/vouchers.go b/common/vouchers.go index 2fed043..dd8852e 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -135,7 +135,7 @@ func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId str return data, nil } -// UpdateVoucherData sets the active voucher data and clears the temporary voucher data in the DataStore. +// UpdateVoucherData sets the active voucher data in the DataStore. func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error { logg.TraceCtxf(ctx, "dtal", "data", data) // Active voucher data entries From f9f25d898b0ec435c7fef41a1ab32277c1914478 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 13 Nov 2024 19:00:27 +0300 Subject: [PATCH 7/7] use logg --- internal/handlers/ussd/menuhandler.go | 135 +++++++++++--------------- 1 file changed, 58 insertions(+), 77 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 58df521..1836833 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "log" "path" "regexp" "strconv" @@ -162,7 +161,7 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r languageSetFlag, err := h.flagManager.GetFlag("flag_language_set") if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "Error setting the languageSetFlag", "error", err) return res, err } res.FlagSet = append(res.FlagSet, languageSetFlag) @@ -216,10 +215,10 @@ func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) _, err = store.ReadEntry(ctx, sessionId, common.DATA_ACCOUNT_CREATED) if err != nil { if db.IsNotFound(err) { - logg.Printf(logging.LVL_INFO, "Creating an account because it doesn't exist") + logg.InfoCtxf(ctx, "Creating an account because it doesn't exist") err = h.createAccountNoExist(ctx, sessionId, &res) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed on createAccountNoExist", "error", err) return res, err } } @@ -238,13 +237,12 @@ func (h *Handlers) CheckPinMisMatch(ctx context.Context, sym string, input []byt store := h.userdataStore blockedNumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read blockedNumber entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) return res, err } temporaryPin, err := store.ReadEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE) if err != nil { - - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read temporaryPin entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) return res, err } if bytes.Equal(temporaryPin, input) { @@ -297,8 +295,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt store := h.userdataStore err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(accountPIN)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", accountPIN, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryAccountPIN entry with", "key", common.DATA_TEMPORARY_VALUE, "value", accountPIN, "error", err) return res, err } @@ -316,16 +313,14 @@ func (h *Handlers) SaveOthersTemporaryPin(ctx context.Context, sym string, input } temporaryPin := string(input) blockedNumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) - if err != nil { - logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read blockedNumber entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) return res, err } + err = store.WriteEntry(ctx, string(blockedNumber), common.DATA_TEMPORARY_VALUE, []byte(temporaryPin)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryPin, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryPin entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryPin, "error", err) return res, err } @@ -343,8 +338,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt store := h.userdataStore temporaryPin, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) if err != nil { - logg.ErrorCtxf(ctx, "failed to read entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read temporaryPin entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) return res, err } if bytes.Equal(temporaryPin, input) { @@ -354,8 +348,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt } err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryPin entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) return res, err } return res, nil @@ -378,7 +371,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte store := h.userdataStore temporaryPin, err := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read temporaryPin entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) return res, err } if bytes.Equal(input, temporaryPin) { @@ -391,8 +384,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte err = store.WriteEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryPin entry with", "key", common.DATA_ACCOUNT_PIN, "value", temporaryPin, "error", err) return res, err } @@ -425,15 +417,13 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) temporaryFirstName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_FIRST_NAME, []byte(temporaryFirstName)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write firstName entry with", "key", common.DATA_FIRST_NAME, "value", temporaryFirstName, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(firstName)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryFirstName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", firstName, "error", err) return res, err } } @@ -460,16 +450,17 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte) temporaryFamilyName, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_FAMILY_NAME, []byte(temporaryFamilyName)) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write familyName entry with", "key", common.DATA_FAMILY_NAME, "value", temporaryFamilyName, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(familyName)) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryFamilyName entry with", "key", common.DATA_TEMPORARY_VALUE, "value", familyName, "error", err) return res, err } } + return res, nil } @@ -490,15 +481,13 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou temporaryYob, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_YOB, []byte(temporaryYob)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write yob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", temporaryYob, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(yob)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryYob entry with", "key", common.DATA_TEMPORARY_VALUE, "value", yob, "error", err) return res, err } } @@ -524,15 +513,13 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( temporaryLocation, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_LOCATION, []byte(temporaryLocation)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_LOCATION, "value", temporaryLocation, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write location entry with", "key", common.DATA_LOCATION, "value", temporaryLocation, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(location)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryLocation entry with", "key", common.DATA_TEMPORARY_VALUE, "value", location, "error", err) return res, err } } @@ -558,15 +545,13 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re temporaryGender, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_GENDER, []byte(temporaryGender)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_GENDER, "value", gender, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write gender entry with", "key", common.DATA_GENDER, "value", gender, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(gender)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryGender entry with", "key", common.DATA_TEMPORARY_VALUE, "value", gender, "error", err) return res, err } } @@ -593,15 +578,13 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) temporaryOfferings, _ := store.ReadEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE) err = store.WriteEntry(ctx, sessionId, common.DATA_OFFERINGS, []byte(temporaryOfferings)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write offerings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } } else { err = store.WriteEntry(ctx, sessionId, common.DATA_TEMPORARY_VALUE, []byte(offerings)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write temporaryOfferings entry with", "key", common.DATA_TEMPORARY_VALUE, "value", offerings, "error", err) return res, err } } @@ -612,9 +595,7 @@ func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) // ResetAllowUpdate resets the allowupdate flag that allows a user to update profile data. func (h *Handlers) ResetAllowUpdate(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") - res.FlagReset = append(res.FlagReset, flag_allow_update) return res, nil } @@ -631,7 +612,6 @@ func (h *Handlers) ResetValidPin(ctx context.Context, sym string, input []byte) func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized") - res.FlagReset = append(res.FlagReset, flag_account_authorized) return res, nil } @@ -667,7 +647,7 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res store := h.userdataStore AccountPin, err := store.ReadEntry(ctx, sessionId, common.DATA_ACCOUNT_PIN) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read AccountPin entry with", "key", common.DATA_ACCOUNT_PIN, "error", err) return res, err } if len(input) == 4 { @@ -715,21 +695,19 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b store := h.userdataStore publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", common.DATA_PUBLIC_KEY, "error", err) return res, err } - r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey)) + r, err := h.accountService.TrackAccountStatus(ctx, string(publicKey)) if err != nil { res.FlagSet = append(res.FlagSet, flag_api_error) - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed on TrackAccountStatus", err) return res, err } + res.FlagReset = append(res.FlagReset, flag_api_error) - if !ok { - log.Printf("Error: %s", err) - return res, err - } + if r.Active { res.FlagSet = append(res.FlagSet, flag_account_success) res.FlagReset = append(res.FlagReset, flag_account_pending) @@ -737,6 +715,7 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b res.FlagReset = append(res.FlagReset, flag_account_success) res.FlagSet = append(res.FlagSet, flag_account_pending) } + return res, nil } @@ -828,13 +807,13 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) ( return res, nil } - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", common.DATA_ACTIVE_SYM, "error", err) return res, err } activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", common.DATA_ACTIVE_BAL, "error", err) return res, err } @@ -858,7 +837,7 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input store := h.userdataStore publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", common.DATA_PUBLIC_KEY, "error", err) return res, err } @@ -891,18 +870,19 @@ func (h *Handlers) ResetOthersPin(ctx context.Context, sym string, input []byte) } blockedPhonenumber, err := store.ReadEntry(ctx, sessionId, common.DATA_BLOCKED_NUMBER) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read blockedPhonenumber entry with", "key", common.DATA_BLOCKED_NUMBER, "error", err) return res, err } temporaryPin, err := store.ReadEntry(ctx, string(blockedPhonenumber), common.DATA_TEMPORARY_VALUE) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read temporaryPin entry with", "key", common.DATA_TEMPORARY_VALUE, "error", err) return res, err } err = store.WriteEntry(ctx, string(blockedPhonenumber), common.DATA_ACCOUNT_PIN, []byte(temporaryPin)) if err != nil { return res, nil } + return res, nil } @@ -931,11 +911,11 @@ func (h *Handlers) ValidateBlockedNumber(ctx context.Context, sym string, input } if err != nil { if db.IsNotFound(err) { - logg.Printf(logging.LVL_INFO, "Invalid or unregistered number") + logg.InfoCtxf(ctx, "Invalid or unregistered number") res.FlagSet = append(res.FlagSet, flag_unregistered_number) return res, nil } else { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "Error on ValidateBlockedNumber", "error", err) return res, err } } @@ -1043,7 +1023,7 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", common.DATA_ACTIVE_BAL, "error", err) return res, err } @@ -1069,12 +1049,12 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) // retrieve the active balance activeBal, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_BAL) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", common.DATA_ACTIVE_BAL, "error", err) return res, err } balanceValue, err = strconv.ParseFloat(string(activeBal), 64) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "Failed to convert the activeBal to a float", "error", err) return res, err } @@ -1097,11 +1077,11 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte) formattedAmount := fmt.Sprintf("%.2f", inputAmount) err = store.WriteEntry(ctx, sessionId, common.DATA_AMOUNT, []byte(formattedAmount)) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", common.DATA_AMOUNT, "value", formattedAmount, "error", err) return res, err } - res.Content = fmt.Sprintf("%s", formattedAmount) + res.Content = formattedAmount return res, nil } @@ -1164,7 +1144,7 @@ func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (res // retrieve the active symbol activeSym, err := store.ReadEntry(ctx, sessionId, common.DATA_ACTIVE_SYM) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", common.DATA_ACTIVE_SYM, "error", err) return res, err } @@ -1202,7 +1182,7 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input [] account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized") if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "Failed to set the flag_account_authorized", "error", err) return res, err } @@ -1305,7 +1285,7 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by if db.IsNotFound(err) { publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", common.DATA_PUBLIC_KEY, "error", err) return res, err } @@ -1330,20 +1310,20 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by // set the active symbol err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(defaultSym)) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write defaultSym entry with", "key", common.DATA_ACTIVE_SYM, "value", defaultSym, "error", err) return res, err } // set the active balance err = store.WriteEntry(ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(defaultBal)) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to write defaultBal entry with", "key", common.DATA_ACTIVE_BAL, "value", defaultBal, "error", err) return res, err } return res, nil } - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", common.DATA_ACTIVE_SYM, "error", err) return res, err } @@ -1364,7 +1344,8 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) store := h.userdataStore publicKey, err := store.ReadEntry(ctx, sessionId, common.DATA_PUBLIC_KEY) if err != nil { - return res, nil + logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", common.DATA_PUBLIC_KEY, "error", err) + return res, err } // Fetch vouchers from the API using the public key @@ -1399,7 +1380,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) // Read vouchers from the store voucherData, err := h.prefixDb.Get(ctx, []byte("sym")) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err) return res, err } @@ -1435,7 +1416,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r } if err := common.StoreTemporaryVoucher(ctx, h.userdataStore, sessionId, metadata); err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed on StoreTemporaryVoucher", "error", err) return res, err } @@ -1457,13 +1438,13 @@ func (h *Handlers) SetVoucher(ctx context.Context, sym string, input []byte) (re // Get temporary data tempData, err := common.GetTemporaryVoucherData(ctx, h.userdataStore, sessionId) if err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed on GetTemporaryVoucherData", "error", err) return res, err } // Set as active and clear temporary data if err := common.UpdateVoucherData(ctx, h.userdataStore, sessionId, tempData); err != nil { - log.Printf("Error: %s", err) + logg.ErrorCtxf(ctx, "failed on UpdateVoucherData", "error", err) return res, err }