mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2026-05-20 12:51:09 +02:00
feat: transaction history (including by voucher) (#24)
* sql: add transaction transaction record queries - get full tx history - get latest txs for a specific token * sql: (fix) add id column to select * sql: (fix) change column name to identifier * fix: pagination queries - refactor keyset pagination query to correctly fecth previous page - remove pagination from token list - add fetch latest transactions by token * tidy: remove dev logs * feat: (db) add index for desc for pagination
This commit is contained in:
@@ -35,6 +35,8 @@ func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, metaC
|
||||
g.GET("/pin-status", handlePinStatus)
|
||||
g.GET("/phone-2-address/:phone", handlePhone2Address)
|
||||
g.GET("/address-2-phone/:address", handleAddress2Phone)
|
||||
g.GET("/latest-transactions/:phone", handleLatestTransactions)
|
||||
g.GET("/latest-transactions-by-token/:phone/:token", handleLatestTransactionsByToken)
|
||||
}
|
||||
|
||||
func newApi(db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) *api {
|
||||
|
||||
96
internal/admin/transactions.go
Normal file
96
internal/admin/transactions.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"cic-dw/pkg/pagination"
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/georgysavva/scany/pgxscan"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type userTransactionRes struct {
|
||||
Id int64 `db:"id" json:"id"`
|
||||
Date time.Time `db:"date_block" json:"tx_date"`
|
||||
TxHash string `db:"tx_hash" json:"tx_hash"`
|
||||
TokenSymbol string `db:"token_symbol" json:"voucher"`
|
||||
SenderAddress string `db:"sender_address" json:"sender_address"`
|
||||
RecipeintAddress string `db:"recipient_address" json:"recipient_address"`
|
||||
TxValue int64 `db:"tx_value" json:"tx_value"`
|
||||
TxSuccess bool `db:"success" json:"tx_success"`
|
||||
SenderIdentifier string `db:"sender_identifier" json:"sender_identifier"`
|
||||
RecipientIdentifier string `db:"recipient_identifier" json:"recipient_identifier"`
|
||||
}
|
||||
|
||||
func handleLatestTransactions(c echo.Context) error {
|
||||
var (
|
||||
api = c.Get("api").(*api)
|
||||
phone = c.Param("phone")
|
||||
pg = pagination.GetPagination(c.QueryParams())
|
||||
|
||||
data []userTransactionRes
|
||||
rows pgx.Rows
|
||||
err error
|
||||
)
|
||||
|
||||
if pg.FirstPage {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions"], phone, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if pg.Next {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-next"], phone, pg.Cursor, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-previous"], phone, pg.Cursor, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := pgxscan.ScanAll(&data, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
func handleLatestTransactionsByToken(c echo.Context) error {
|
||||
var (
|
||||
api = c.Get("api").(*api)
|
||||
phone = c.Param("phone")
|
||||
token = c.Param("token")
|
||||
pg = pagination.GetPagination(c.QueryParams())
|
||||
|
||||
data []userTransactionRes
|
||||
rows pgx.Rows
|
||||
err error
|
||||
)
|
||||
|
||||
if pg.FirstPage {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-token"], phone, token, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if pg.Next {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-token-next"], phone, token, pg.Cursor, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-token-previous"], phone, token, pg.Cursor, pg.PerPage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := pgxscan.ScanAll(&data, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, data)
|
||||
}
|
||||
Reference in New Issue
Block a user