Skip to content

Commit

Permalink
endpoints: toggle user activation
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Nov 30, 2024
1 parent a46bfde commit 930309c
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 24 deletions.
56 changes: 56 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,56 @@ const docTemplate = `{
}
}
},
"/admin/users/user/{username}/toggle-activation": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Allows the admin to deactivate/reactivate a user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin | Users"
],
"summary": "Reactivate/Deactivate User",
"parameters": [
{
"type": "string",
"default": "username",
"description": "Username",
"name": "username",
"in": "path",
"required": true
}
],
"responses": {
"400": {
"description": "Invalid request data",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"404": {
"description": "User not found",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
}
}
}
},
"/auth/facebook": {
"post": {
"description": "` + "`" + `This endpoint generates new access and refresh tokens for authentication via facebook` + "`" + `\n` + "`" + `Pass in token gotten from facebook client authentication here in payload to retrieve tokens for authorization` + "`" + `",
Expand Down Expand Up @@ -4334,6 +4384,9 @@ const docTemplate = `{
"type": "integer",
"example": 30
},
"created_at": {
"type": "string"
},
"payment_purpose": {
"allOf": [
{
Expand All @@ -4359,6 +4412,9 @@ const docTemplate = `{
},
"reference": {
"type": "string"
},
"updated_at": {
"type": "string"
}
}
},
Expand Down
56 changes: 56 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,56 @@
}
}
},
"/admin/users/user/{username}/toggle-activation": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Allows the admin to deactivate/reactivate a user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin | Users"
],
"summary": "Reactivate/Deactivate User",
"parameters": [
{
"type": "string",
"default": "username",
"description": "Username",
"name": "username",
"in": "path",
"required": true
}
],
"responses": {
"400": {
"description": "Invalid request data",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"404": {
"description": "User not found",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/utils.ErrorResponse"
}
}
}
}
},
"/auth/facebook": {
"post": {
"description": "`This endpoint generates new access and refresh tokens for authentication via facebook`\n`Pass in token gotten from facebook client authentication here in payload to retrieve tokens for authorization`",
Expand Down Expand Up @@ -4327,6 +4377,9 @@
"type": "integer",
"example": 30
},
"created_at": {
"type": "string"
},
"payment_purpose": {
"allOf": [
{
Expand All @@ -4352,6 +4405,9 @@
},
"reference": {
"type": "string"
},
"updated_at": {
"type": "string"
}
}
},
Expand Down
36 changes: 36 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ definitions:
coins_total:
example: 30
type: integer
created_at:
type: string
payment_purpose:
allOf:
- $ref: '#/definitions/choices.PaymentPurpose'
Expand All @@ -1144,6 +1146,8 @@ definitions:
type: integer
reference:
type: string
updated_at:
type: string
type: object
schemas.TransactionsResponseDataSchema:
properties:
Expand Down Expand Up @@ -1520,6 +1524,38 @@ paths:
summary: Update User Role
tags:
- Admin | Users
/admin/users/user/{username}/toggle-activation:
get:
consumes:
- application/json
description: Allows the admin to deactivate/reactivate a user.
parameters:
- default: username
description: Username
in: path
name: username
required: true
type: string
produces:
- application/json
responses:
"400":
description: Invalid request data
schema:
$ref: '#/definitions/utils.ErrorResponse'
"404":
description: User not found
schema:
$ref: '#/definitions/utils.ErrorResponse'
"500":
description: Internal server error
schema:
$ref: '#/definitions/utils.ErrorResponse'
security:
- BearerAuth: []
summary: Reactivate/Deactivate User
tags:
- Admin | Users
/auth/facebook:
post:
description: |-
Expand Down
45 changes: 23 additions & 22 deletions models/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import (

type User struct {
BaseModel
FirstName string `json:"first_name" gorm:"type: varchar(255);not null" validate:"required,max=255" example:"John"`
LastName string `json:"last_name" gorm:"type: varchar(255);not null" validate:"required,max=255" example:"Doe"`
Username string `json:"username" gorm:"type: varchar(1000);not null;unique;" validate:"required,max=255" example:"john-doe"`
Email string `json:"email" gorm:"not null;unique;" validate:"required,min=5,email" example:"[email protected]"`
Password string `json:"password" gorm:"not null" validate:"required,min=8,max=50" example:"strongpassword"`
IsEmailVerified bool `json:"is_email_verified" gorm:"default:false" swaggerignore:"true"`
IsSuperuser bool `json:"is_superuser" gorm:"default:false" swaggerignore:"true"`
IsStaff bool `json:"is_staff" gorm:"default:false" swaggerignore:"true"`
TermsAgreement bool `json:"terms_agreement" gorm:"default:false" validate:"eq=true"`
Avatar string `gorm:"type:varchar(1000);null;" json:"avatar"`
Access *string `gorm:"type:varchar(1000);null;" json:"access"`
Refresh *string `gorm:"type:varchar(1000);null;" json:"refresh"`
FirstName string `gorm:"type: varchar(255);not null"`
LastName string `gorm:"type: varchar(255);not null"`
Username string `gorm:"type: varchar(1000);not null;unique;"`
Email string `gorm:"not null;unique;"`
Password string `gorm:"not null"`
IsEmailVerified bool `gorm:"default:false"`
IsSuperuser bool `gorm:"default:false"`
IsStaff bool `gorm:"default:false"`
IsActive bool `gorm:"default:true"`
TermsAgreement bool `gorm:"default:false"`
Avatar string `gorm:"type:varchar(1000);null;"`
Access *string `gorm:"type:varchar(1000);null;"`
Refresh *string `gorm:"type:varchar(1000);null;"`
SocialLogin bool `gorm:"default:false"`
Bio *string `gorm:"type:varchar(1000);null;" json:"bio"`
AccountType choices.AccType `gorm:"type:varchar(100); default:READER" json:"account_type"`
Bio *string `gorm:"type:varchar(1000);null;"`
AccountType choices.AccType `gorm:"type:varchar(100); default:READER"`
Followings []User `gorm:"many2many:user_followers;foreignKey:ID;joinForeignKey:Follower;References:ID;joinReferences:Following"`
Followers []User `gorm:"many2many:user_followers;foreignKey:ID;joinForeignKey:Following;References:ID;joinReferences:Follower"`
Coins int `json:"coins" gorm:"default:0"`
Lanterns int `json:"lanterns" gorm:"default:0"`
Coins int `gorm:"default:0"`
Lanterns int `gorm:"default:0"`
LikeNotification bool `gorm:"default:false"`
ReplyNotification bool `gorm:"default:false"`
SubscriptionExpiry *time.Time `gorm:"null"`
Expand All @@ -41,10 +42,10 @@ type User struct {
}

func (user User) SubscriptionExpired() bool {
if user.SubscriptionExpiry == nil {
return true
}
return time.Now().After(*user.SubscriptionExpiry)
if user.SubscriptionExpiry == nil {
return true
}
return time.Now().After(*user.SubscriptionExpiry)
}

func (u User) AvatarUrl() *string {
Expand Down Expand Up @@ -80,9 +81,9 @@ func (user *User) BeforeCreate(tx *gorm.DB) (err error) {

type Token struct {
BaseModel
UserId uuid.UUID `json:"user_id" gorm:"unique"`
UserId uuid.UUID `gorm:"unique"`
User User `gorm:"foreignKey:UserId;constraint:OnDelete:CASCADE"`
TokenString string `json:"token_string"`
TokenString string
}

func (token *Token) BeforeSave(tx *gorm.DB) (err error) {
Expand Down
37 changes: 35 additions & 2 deletions routes/admin_users.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package routes

import (
"fmt"

"github.com/LitPad/backend/models"
"github.com/LitPad/backend/models/choices"
"github.com/LitPad/backend/schemas"
"github.com/LitPad/backend/utils"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)

var truthy = true
Expand Down Expand Up @@ -79,7 +82,7 @@ func (ep Endpoint) AdminUpdateUser(c *fiber.Ctx) error {
return c.Status(*errCode).JSON(errData)
}

account_type := choices.AccType(data.AccountType)
accountType := choices.AccType(data.AccountType)

var user models.User

Expand All @@ -90,7 +93,7 @@ func (ep Endpoint) AdminUpdateUser(c *fiber.Ctx) error {
return c.Status(404).JSON(utils.RequestErr(utils.ERR_NOT_FOUND, "User Not Found"))
}

user.AccountType = account_type
user.AccountType = accountType
}

db.Save(&user)
Expand All @@ -102,6 +105,36 @@ func (ep Endpoint) AdminUpdateUser(c *fiber.Ctx) error {
return c.Status(200).JSON(response)
}

// @Summary Reactivate/Deactivate User
// @Description Allows the admin to deactivate/reactivate a user.
// @Tags Admin | Users
// @Param username path string true "Username" default(username)
// @Accept json
// @Produce json
// @Failure 400 {object} utils.ErrorResponse "Invalid request data"
// @Failure 404 {object} utils.ErrorResponse "User not found"
// @Failure 500 {object} utils.ErrorResponse "Internal server error"
// @Router /admin/users/user/{username}/toggle-activation [get]
// @Security BearerAuth
func (ep Endpoint) ToggleUserActivation(c *fiber.Ctx) error {
db := ep.DB
username := c.Params("username")
user := models.User{Username: username}
db.Take(&user, user)
if user.ID == uuid.Nil {
return c.Status(404).JSON(utils.NotFoundErr("User with that username not found"))
}
responseMessageSubstring := "deactivated"
if user.IsActive {
user.IsActive = false
} else {
responseMessageSubstring = "reactivated"
user.IsActive = true
}
db.Save(&user)
return c.Status(200).JSON(ResponseMessage(fmt.Sprintf("User %s successfully", responseMessageSubstring)))
}

func (ep Endpoint) AdminGetWaitlist(c *fiber.Ctx)error{
db := ep.DB

Expand Down
1 change: 1 addition & 0 deletions routes/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func SetupRoutes(app *fiber.App, db *gorm.DB, ws *internetcomputer.WalletService
adminRouter.Put("/", endpoint.AdminMiddleware, endpoint.UpdateProfile)
adminRouter.Get("/users", endpoint.AdminMiddleware, endpoint.AdminGetUsers)
adminRouter.Put("/users/user", endpoint.AdminMiddleware, endpoint.AdminUpdateUser)
adminRouter.Get("/users/user/:username/toggle-activation", endpoint.AdminMiddleware, endpoint.ToggleUserActivation)

// Admin Books (2)
adminRouter.Get("/books", endpoint.AdminMiddleware, endpoint.AdminGetBooks)
Expand Down
4 changes: 4 additions & 0 deletions utils/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ func RequestErr(code string, message string, opts ...map[string]string) ErrorRes
resp := ErrorResponse{Status: "failure", Code: code, Message: message, Data: data}
return resp
}

func NotFoundErr(message string) ErrorResponse {
return RequestErr(ERR_NON_EXISTENT, message)
}

0 comments on commit 930309c

Please sign in to comment.