From 35fcdeaa99d9b79e8ef3e8c35d5e7ea87bbfeefe Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Thu, 2 Jun 2022 11:22:57 +0300 Subject: [PATCH] add: pin and address handlers --- internal/admin/api.go | 5 +++-- internal/admin/pins.go | 33 +++++++++++++++++++++++++++++++++ internal/admin/user.go | 36 ++++++++++++++++++++++++++++++++++++ queries/admin.sql | 18 +++++++++++++++++- 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 internal/admin/pins.go diff --git a/internal/admin/api.go b/internal/admin/api.go index 8b39054..633a309 100644 --- a/internal/admin/api.go +++ b/internal/admin/api.go @@ -8,7 +8,6 @@ import ( "github.com/jackc/pgx/v4/pgxpool" "github.com/labstack/echo/v4" "github.com/nleof/goyesql" - "github.com/rs/zerolog/log" ) type api struct { @@ -33,10 +32,12 @@ func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, metaC g.GET("/check", isLoggedIn) g.GET("/meta-proxy/:address", handleMetaProxy) + g.GET("/pin-status", handlePinStatus) + g.GET("/phone-2-address/:phone", handlePhone2Address) + g.GET("/address-2-phone/:address", handleAddress2Phone) } func newApi(db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) *api { - log.Info().Msgf("%s inj", jwtKey) return &api{ db: db, q: queries, diff --git a/internal/admin/pins.go b/internal/admin/pins.go new file mode 100644 index 0000000..28bebe4 --- /dev/null +++ b/internal/admin/pins.go @@ -0,0 +1,33 @@ +package admin + +import ( + "context" + "net/http" + + "github.com/georgysavva/scany/pgxscan" + "github.com/labstack/echo/v4" +) + +type pinStatusResponse struct { + PhoneNumber string `db:"phone_number" json:"phone_number"` + FailedPinAttempts int `db:"failed_pin_attempts" json:"failed_pin_attempts"` + AccountStatus string `db:"account_status" json:"account_status"` +} + +func handlePinStatus(c echo.Context) error { + var ( + api = c.Get("api").(*api) + res []pinStatusResponse + ) + + rows, err := api.db.Query(context.Background(), api.q["pin-status"]) + if err != nil { + return err + } + + if err := pgxscan.ScanAll(&res, rows); err != nil { + return err + } + + return c.JSON(http.StatusOK, res) +} diff --git a/internal/admin/user.go b/internal/admin/user.go index 39215af..0027cc4 100644 --- a/internal/admin/user.go +++ b/internal/admin/user.go @@ -2,6 +2,8 @@ package admin import ( "bytes" + "cic-dw/pkg/address" + "context" "encoding/base64" "net/http" "strings" @@ -16,6 +18,40 @@ type metaRes struct { Name string `json:"name"` } +func handlePhone2Address(c echo.Context) error { + var ( + api = c.Get("api").(*api) + phone = c.Param("phone") + + address string + ) + + if err := api.db.QueryRow(context.Background(), api.q["phone-2-address"], phone).Scan(&address); err != nil { + return c.String(http.StatusNotFound, "phone not found") + } + + return c.String(http.StatusOK, address) +} + +func handleAddress2Phone(c echo.Context) error { + var ( + api = c.Get("api").(*api) + + phone string + ) + + sarafuAddress, err := address.SarafuAddress(c.Param("address")) + if err != nil { + return err + } + + if err := api.db.QueryRow(context.Background(), api.q["address-2-phone"], sarafuAddress).Scan(&phone); err != nil { + return c.String(http.StatusNotFound, "address not found") + } + + return c.String(http.StatusOK, phone) +} + func handleMetaProxy(c echo.Context) error { var ( api = c.Get("api").(*api) diff --git a/queries/admin.sql b/queries/admin.sql index 489ad28..fbba55a 100644 --- a/queries/admin.sql +++ b/queries/admin.sql @@ -1,2 +1,18 @@ -- name: get-password-hash -SELECT password_hash from staff WHERE username = $1; \ No newline at end of file +SELECT password_hash FROM staff WHERE username = $1; + +-- name: pin-status +SELECT phone_number, failed_pin_attempts, +CASE STATUS + WHEN 1 THEN 'PENDING' + WHEN 2 THEN 'ACTIVE' + WHEN 3 THEN 'LOCKED' + WHEN 4 THEN 'RESET' END AS account_status +FROM cic_ussd.account WHERE +failed_pin_attempts > 0 OR STATUS = 4; + +--name: phone-2-address +SELECT blockchain_address FROM users WHERE phone_number = $1; + +--name: address-2-phone +SELECT phone_number FROM users WHERE blockchain_address = $1; \ No newline at end of file