Skip to content

Commit

Permalink
feat: admin endpoints for transactions retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Nov 30, 2024
1 parent dc45927 commit a46bfde
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 29 deletions.
63 changes: 59 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Admin"
"Admin | Books"
],
"summary": "List Books with Pagination",
"parameters": [
Expand Down Expand Up @@ -85,7 +85,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Admin"
"Admin | Books"
],
"summary": "List Book Contracts with Pagination",
"parameters": [
Expand Down Expand Up @@ -131,6 +131,61 @@ const docTemplate = `{
}
}
},
"/admin/payments/transactions": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Retrieves a list of current transactions with support for pagination and optional filtering based on username.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin | Payments"
],
"summary": "Latest Transactions with Pagination",
"parameters": [
{
"type": "string",
"description": "Username to filter by",
"name": "username",
"in": "query"
},
{
"type": "integer",
"default": 1,
"description": "Current page",
"name": "page",
"in": "query"
}
],
"responses": {
"200": {
"description": "Successfully retrieved list of transactions",
"schema": {
"$ref": "#/definitions/schemas.TransactionsResponseSchema"
}
},
"400": {
"description": "Invalid query parameters",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
}
}
}
},
"/admin/users": {
"get": {
"security": [
Expand All @@ -146,7 +201,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Admin"
"Admin | Users"
],
"summary": "List Users with Pagination",
"parameters": [
Expand Down Expand Up @@ -206,7 +261,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Admin"
"Admin | Users"
],
"summary": "Update User Role",
"parameters": [
Expand Down
63 changes: 59 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"application/json"
],
"tags": [
"Admin"
"Admin | Books"
],
"summary": "List Books with Pagination",
"parameters": [
Expand Down Expand Up @@ -78,7 +78,7 @@
"application/json"
],
"tags": [
"Admin"
"Admin | Books"
],
"summary": "List Book Contracts with Pagination",
"parameters": [
Expand Down Expand Up @@ -124,6 +124,61 @@
}
}
},
"/admin/payments/transactions": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Retrieves a list of current transactions with support for pagination and optional filtering based on username.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin | Payments"
],
"summary": "Latest Transactions with Pagination",
"parameters": [
{
"type": "string",
"description": "Username to filter by",
"name": "username",
"in": "query"
},
{
"type": "integer",
"default": 1,
"description": "Current page",
"name": "page",
"in": "query"
}
],
"responses": {
"200": {
"description": "Successfully retrieved list of transactions",
"schema": {
"$ref": "#/definitions/schemas.TransactionsResponseSchema"
}
},
"400": {
"description": "Invalid query parameters",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
}
}
}
},
"/admin/users": {
"get": {
"security": [
Expand All @@ -139,7 +194,7 @@
"application/json"
],
"tags": [
"Admin"
"Admin | Users"
],
"summary": "List Users with Pagination",
"parameters": [
Expand Down Expand Up @@ -199,7 +254,7 @@
"application/json"
],
"tags": [
"Admin"
"Admin | Users"
],
"summary": "Update User Role",
"parameters": [
Expand Down
44 changes: 40 additions & 4 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ paths:
- BearerAuth: []
summary: List Books with Pagination
tags:
- Admin
- Admin | Books
/admin/contracts:
get:
consumes:
Expand Down Expand Up @@ -1407,7 +1407,43 @@ paths:
- BearerAuth: []
summary: List Book Contracts with Pagination
tags:
- Admin
- Admin | Books
/admin/payments/transactions:
get:
consumes:
- application/json
description: Retrieves a list of current transactions with support for pagination
and optional filtering based on username.
parameters:
- description: Username to filter by
in: query
name: username
type: string
- default: 1
description: Current page
in: query
name: page
type: integer
produces:
- application/json
responses:
"200":
description: Successfully retrieved list of transactions
schema:
$ref: '#/definitions/schemas.TransactionsResponseSchema'
"400":
description: Invalid query parameters
schema:
$ref: '#/definitions/utils.ErrorResponse'
"500":
description: Internal server error
schema:
$ref: '#/definitions/utils.ErrorResponse'
security:
- BearerAuth: []
summary: Latest Transactions with Pagination
tags:
- Admin | Payments
/admin/users:
get:
consumes:
Expand Down Expand Up @@ -1447,7 +1483,7 @@ paths:
- BearerAuth: []
summary: List Users with Pagination
tags:
- Admin
- Admin | Users
/admin/users/user:
put:
consumes:
Expand Down Expand Up @@ -1483,7 +1519,7 @@ paths:
- BearerAuth: []
summary: Update User Role
tags:
- Admin
- Admin | Users
/auth/facebook:
post:
description: |-
Expand Down
4 changes: 2 additions & 2 deletions routes/admin_books.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// @Summary List Books with Pagination
// @Description Retrieves a list of books with support for pagination and optional filtering based on book title.
// @Tags Admin
// @Tags Admin | Books
// @Accept json
// @Produce json
// @Param page query int false "Current Page" default(1)
Expand Down Expand Up @@ -41,7 +41,7 @@ func (ep Endpoint) AdminGetBooks(c *fiber.Ctx) error {

// @Summary List Book Contracts with Pagination
// @Description Retrieves a list of book contracts with support for pagination and optional filtering based on contract status.
// @Tags Admin
// @Tags Admin | Books
// @Accept json
// @Produce json
// @Param page query int false "Current Page" default(1)
Expand Down
52 changes: 52 additions & 0 deletions routes/admin_payments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package routes

import (
"github.com/LitPad/backend/models"
"github.com/LitPad/backend/schemas"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

// @Summary Latest Transactions with Pagination
// @Description Retrieves a list of current transactions with support for pagination and optional filtering based on username.
// @Tags Admin | Payments
// @Accept json
// @Produce json
// @Param username query string false "Username to filter by"
// @Param page query int false "Current page" default(1)
// @Success 200 {object} schemas.TransactionsResponseSchema "Successfully retrieved list of transactions"
// @Failure 400 {object} utils.ErrorResponse "Invalid query parameters"
// @Failure 500 {object} utils.ErrorResponse "Internal server error"
// @Router /admin/payments/transactions [get]
// @Security BearerAuth
func (ep Endpoint) AdminGetTransactions(c *fiber.Ctx) error {
db := ep.DB
username := GetQueryValue(c, "username")
transactions := []models.Transaction{}
query := db
if username != nil {
user := models.User{Username: *username}
db.Take(&user, user)
if user.ID != uuid.Nil {
query = query.Where("user_id = ?", user.ID)
} else {
// Ensure the query empties
query = query.Where("user_id = ?", uuid.Nil)
}
}

query.Joins("Coin").Joins("SubscriptionPlan").Order("created_at DESC").Find(&transactions)
// Paginate and return transactions
paginatedData, paginatedTransactions, err := PaginateQueryset(transactions, c, 100)
if err != nil {
return c.Status(400).JSON(err)
}
transactions = paginatedTransactions.([]models.Transaction)
response := schemas.TransactionsResponseSchema{
ResponseSchema: ResponseMessage("Transactions fetched successfully"),
Data: schemas.TransactionsResponseDataSchema{
PaginatedResponseDataSchema: *paginatedData,
}.Init(transactions),
}
return c.Status(200).JSON(response)
}
5 changes: 2 additions & 3 deletions routes/admin_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var truthy = true
// @Summary List Users with Pagination
// @Description Retrieves a list of user profiles with support for pagination and optional filtering based on user account type.
// @Tags Admin
// @Tags Admin | Users
// @Accept json
// @Produce json
// @Param account_type query string false "Type of user to filter by" Enums(READER, WRITER, ADMIN)
Expand Down Expand Up @@ -47,7 +47,6 @@ func (ep Endpoint) AdminGetUsers(c *fiber.Ctx) error {
return c.Status(400).JSON(err)
}
users = paginatedUsers.([]models.User)

response := schemas.UserProfilesResponseSchema{
ResponseSchema: ResponseMessage("Profiles fetched successfully"),
Data: schemas.UserProfilesResponseDataSchema{
Expand All @@ -60,7 +59,7 @@ func (ep Endpoint) AdminGetUsers(c *fiber.Ctx) error {

// @Summary Update User Role
// @Description Updates the account type of a specified user.
// @Tags Admin
// @Tags Admin | Users
// @Accept json
// @Produce json
// @Param data body schemas.UpdateUserRoleSchema true "User role update data"
Expand Down
18 changes: 14 additions & 4 deletions routes/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,25 @@ func SetupRoutes(app *fiber.App, db *gorm.DB, ws *internetcomputer.WalletService
// Internet Computer
walletRouter.Get("balance", walletService.GetOnChainBalance)

// Admin Routes (6)
// ADMIN ROUTES (7)
adminRouter := api.Group("/admin")
// Admin Users
adminRouter.Put("/", endpoint.AdminMiddleware, endpoint.UpdateProfile)
adminRouter.Get("/users", endpoint.AdminMiddleware, endpoint.AdminGetUsers)
adminRouter.Get("/books", endpoint.AdminMiddleware, endpoint.AdminGetBooks)
adminRouter.Get("/waitlist", endpoint.AdminMiddleware, endpoint.AdminGetWaitlist)
adminRouter.Put("/users/user", endpoint.AdminMiddleware, endpoint.AdminUpdateUser)
adminRouter.Put("/", endpoint.AdminMiddleware, endpoint.UpdateProfile)

// Admin Books (2)
adminRouter.Get("/books", endpoint.AdminMiddleware, endpoint.AdminGetBooks)
adminRouter.Get("/contracts", endpoint.AdminMiddleware, endpoint.AdminGetBookContracts)

// Admin Waitlist (1)
adminRouter.Get("/waitlist", endpoint.AdminMiddleware, endpoint.AdminGetWaitlist)

// Admin Payments (1)
adminRouter.Get("/payments/transactions", endpoint.AdminMiddleware, endpoint.AdminGetTransactions)
// --------------------------------------------------------------------------------

// Waitlist Routes (1)
api.Post("/waitlist", endpoint.AddToWaitlist)

// Register Sockets (1)
Expand Down
Loading

0 comments on commit a46bfde

Please sign in to comment.