Skip to content

Commit

Permalink
feat/return user (#35)
Browse files Browse the repository at this point in the history
* feat: central staff

* feat: return user

* fix: lint
  • Loading branch information
ImSoZRious authored Jan 12, 2024
1 parent 2765946 commit a9d250e
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 38 deletions.
2 changes: 1 addition & 1 deletion di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,11 @@
}
],
"responses": {
"204": {
"description": "No Content"
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/dto.AttendeeStaffCheckinResponse"
}
},
"403": {
"description": "Forbidden",
Expand All @@ -395,6 +398,38 @@
}
},
"definitions": {
"dto.AttendeeStaffCheckinResponse": {
"type": "object",
"properties": {
"already_checkin": {
"type": "boolean"
},
"user": {
"$ref": "#/definitions/dto.AttendeeStaffCheckinUser"
}
}
},
"dto.AttendeeStaffCheckinUser": {
"type": "object",
"properties": {
"allergies": {
"type": "string",
"example": "Romantic"
},
"first_name": {
"type": "string",
"example": "John"
},
"last_name": {
"type": "string",
"example": "Doe"
},
"medical_condition": {
"type": "string",
"example": "Unlovable"
}
}
},
"dto.BilingualField": {
"type": "object",
"properties": {
Expand Down
28 changes: 26 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
definitions:
dto.AttendeeStaffCheckinResponse:
properties:
already_checkin:
type: boolean
user:
$ref: '#/definitions/dto.AttendeeStaffCheckinUser'
type: object
dto.AttendeeStaffCheckinUser:
properties:
allergies:
example: Romantic
type: string
first_name:
example: John
type: string
last_name:
example: Doe
type: string
medical_condition:
example: Unlovable
type: string
type: object
dto.BilingualField:
properties:
en:
Expand Down Expand Up @@ -640,8 +662,10 @@ paths:
produces:
- application/json
responses:
"204":
description: No Content
"200":
description: OK
schema:
$ref: '#/definitions/dto.AttendeeStaffCheckinResponse'
"403":
description: Forbidden
schema:
Expand Down
5 changes: 5 additions & 0 deletions internal/auth/auth.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Repository interface {
CreateUser(user *model.User) error
GetUserByEmail(user *model.User, email string) error
GetUserById(model *model.User, id int) error
}

type repositoryImpl struct {
Expand Down Expand Up @@ -41,3 +42,7 @@ func (r *repositoryImpl) GetUserByEmail(user *model.User, email string) error {
Where("email = ?", email).
First(&user).Error
}

func (r *repositoryImpl) GetUserById(user *model.User, id int) error {
return r.db.Model(user).First(&user, "id = ?", id).Error
}
13 changes: 13 additions & 0 deletions internal/dto/staff.dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dto

type AttendeeStaffCheckinResponse struct {
User *AttendeeStaffCheckinUser `json:"user"`
AlreadyCheckin bool `json:"already_checkin"`
}

type AttendeeStaffCheckinUser struct {
FirstName string `example:"John" json:"first_name"`
LastName string `example:"Doe" json:"last_name"`
Allergies string `example:"Romantic" json:"allergies"`
MedicalCondition string `example:"Unlovable" json:"medical_condition"`
}
23 changes: 17 additions & 6 deletions internal/middleware/auth.middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,23 @@ func NewAuthMiddleware(userRepo auth.Repository, cfg *cfgldr.Config) AuthMiddlew
c.Abort()
return
}
if staffToken.Valid && staffToken.Claims.(jwt.MapClaims)["role"] == "staff" {
c.Set("role", "staff")
c.Set("faculty", staffToken.Claims.(jwt.MapClaims)["faculty"])
c.Set("department", staffToken.Claims.(jwt.MapClaims)["department"])
c.Next()
return
if staffToken.Valid {
switch staffToken.Claims.(jwt.MapClaims)["role"] {
case "faculty-staff":
c.Set("role", "faculty-staff")
c.Set("faculty", staffToken.Claims.(jwt.MapClaims)["faculty"])
c.Set("department", staffToken.Claims.(jwt.MapClaims)["department"])
c.Next()
return
case "central-staff":
c.Set("role", "central-staff")
c.Next()
return
default:
utils.ReturnError(c, apperror.InvalidToken)
c.Abort()
return
}
} else {
utils.ReturnError(c, apperror.InvalidToken)
c.Abort()
Expand Down
18 changes: 17 additions & 1 deletion internal/model/staff.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

import "time"

type AttendeeCheckin struct {
type AttendeeFacultyCheckin struct {
Id int `gorm:"primaryKey,autoIncrement"`
CreatedAt time.Time `gorm:"not null;autoCreateTime"`
UpdatedAt time.Time `gorm:"not null;autoUpdateTime:milli"`
Expand All @@ -13,3 +13,19 @@ type AttendeeCheckin struct {
DepartmentCode string `gorm:"not null"`
Department Department `gorm:"foreignKey:DepartmentCode,FacultyCode;references:Code,FacultyCode"`
}

func (AttendeeFacultyCheckin) TableName() string {
return "attendee_faculty_checkins"
}

type AttendeeCentralCheckin struct {
Id int `gorm:"primaryKey,autoIncrement"`
CreatedAt time.Time `gorm:"not null;autoCreateTime"`
UpdatedAt time.Time `gorm:"not null;autoUpdateTime:milli"`
UserId int `gorm:"not null"`
User User `gorm:"foreignKey:UserId;references:Id"`
}

func (AttendeeCentralCheckin) TableName() string {
return "attendee_central_checkins"
}
29 changes: 22 additions & 7 deletions internal/staff/staff.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/isd-sgcu/oph66-backend/apperror"
"github.com/isd-sgcu/oph66-backend/internal/dto"
"github.com/isd-sgcu/oph66-backend/utils"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -35,13 +36,12 @@ type handlerImpl struct {
// @router /staff/checkin/{userId} [post]
// @param userId path int true "User id"
// @Security Bearer
// @success 204
// @success 200 {object} dto.AttendeeStaffCheckinResponse
// @Failure 403 {object} dto.EventInvalidResponse
// @Failure 404 {object} dto.EventInvalidResponse
// @Failure 409 {object} dto.EventInvalidResponse
// @Failure 500 {object} dto.EventAllErrorResponse
func (h *handlerImpl) AttendeeStaffCheckin(c *gin.Context) {
if c.GetString("role") != "staff" {
if c.GetString("role") != "central-staff" && c.GetString("role") != "faculty-staff" {
utils.ReturnError(c, apperror.Forbidden)
return
}
Expand All @@ -53,13 +53,28 @@ func (h *handlerImpl) AttendeeStaffCheckin(c *gin.Context) {
return
}

faculty := c.GetString("faculty")
department := c.GetString("department")
apperr := h.service.AttendeeStaffCheckin(userId, department, faculty)
var (
apperr *apperror.AppError
ciu *dto.AttendeeStaffCheckinUser
alreadyCheckin bool
)

switch c.GetString("role") {
case "faculty-staff":
faculty := c.GetString("faculty")
department := c.GetString("department")
ciu, alreadyCheckin, apperr = h.service.AttendeeFacultyStaffCheckin(userId, department, faculty)
case "central-staff":
ciu, alreadyCheckin, apperr = h.service.AttendeeCentralStaffCheckin(userId)
}

if apperr != nil {
utils.ReturnError(c, apperr)
return
}

c.AbortWithStatus(http.StatusNoContent)
c.JSON(http.StatusOK, dto.AttendeeStaffCheckinResponse{
User: ciu,
AlreadyCheckin: alreadyCheckin,
})
}
9 changes: 7 additions & 2 deletions internal/staff/staff.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type Repository interface {
CreateCheckin(checkin *model.AttendeeCheckin) error
CreateFacultyCheckin(checkin *model.AttendeeFacultyCheckin) error
CreateCentralCheckin(checkin *model.AttendeeCentralCheckin) error
}

type repositoryImpl struct {
Expand All @@ -19,6 +20,10 @@ func NewRepository(db *gorm.DB) Repository {
}
}

func (r *repositoryImpl) CreateCheckin(checkin *model.AttendeeCheckin) error {
func (r *repositoryImpl) CreateFacultyCheckin(checkin *model.AttendeeFacultyCheckin) error {
return r.db.Model(checkin).Create(checkin).Error
}

func (r *repositoryImpl) CreateCentralCheckin(checkin *model.AttendeeCentralCheckin) error {
return r.db.Model(checkin).Create(checkin).Error
}
Loading

0 comments on commit a9d250e

Please sign in to comment.