cic-dw/internal/admin/transactions.go
Mohamed Sohail b59c0ba3fc
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
2022-06-16 12:27:18 +03:00

97 lines
2.6 KiB
Go

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)
}