mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2026-05-14 02:38:41 +02:00
add: pin and address handlers
This commit is contained in:
@@ -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,
|
||||
|
||||
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 (
|
||||
"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)
|
||||
|
||||
Reference in New Issue
Block a user