mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2025-02-08 12:57:37 +01:00
add: pin and address handlers
This commit is contained in:
parent
723c8bc71c
commit
35fcdeaa99
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/jackc/pgx/v4/pgxpool"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/nleof/goyesql"
|
"github.com/nleof/goyesql"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type api struct {
|
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("/check", isLoggedIn)
|
||||||
g.GET("/meta-proxy/:address", handleMetaProxy)
|
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 {
|
func newApi(db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) *api {
|
||||||
log.Info().Msgf("%s inj", jwtKey)
|
|
||||||
return &api{
|
return &api{
|
||||||
db: db,
|
db: db,
|
||||||
q: queries,
|
q: queries,
|
||||||
|
33
internal/admin/pins.go
Normal file
33
internal/admin/pins.go
Normal file
@ -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)
|
||||||
|
}
|
@ -2,6 +2,8 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cic-dw/pkg/address"
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -16,6 +18,40 @@ type metaRes struct {
|
|||||||
Name string `json:"name"`
|
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 {
|
func handleMetaProxy(c echo.Context) error {
|
||||||
var (
|
var (
|
||||||
api = c.Get("api").(*api)
|
api = c.Get("api").(*api)
|
||||||
|
@ -1,2 +1,18 @@
|
|||||||
-- name: get-password-hash
|
-- name: get-password-hash
|
||||||
SELECT password_hash from staff WHERE username = $1;
|
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;
|
Loading…
Reference in New Issue
Block a user