Files
cms-server/service/event/checkin.go
2026-01-21 13:48:37 +08:00

194 lines
5.5 KiB
Go

package event
import (
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/utils"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func Checkin(c *gin.Context) {
data := new(data.Attendance)
userIdOrig, ok := c.Get("user_id")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusUser).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorMissingUserId).
Build()
utils.HttpResponse(c, 403, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
}
// Get event id from query
eventIdOrig, ok := c.GetQuery("event_id")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
// Parse event id to uuid
eventId, err := uuid.Parse(eventIdOrig)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
data.UserId = userId
code, err := data.GenCheckinCode(eventId)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.EventCheckinGenCodeFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
checkinCodeResp := struct {
CheckinCode *string `json:"checkin_code"`
}{code}
utils.HttpResponse(c, 200, "", "success", checkinCodeResp)
}
func CheckinSubmit(c *gin.Context) {
var req struct {
ChekinCode string `json:"checkin_code"`
}
c.ShouldBindJSON(&req)
attendanceData := new(data.Attendance)
err := attendanceData.VerifyCheckinCode(req.ChekinCode)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinSubmitEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
utils.HttpResponse(c, 200, "", "success")
}
func CheckinQuery(c *gin.Context) {
userIdOrig, ok := c.Get("user_id")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusUser).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorMissingUserId).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
return
}
eventIdOrig, ok := c.GetQuery("event_id")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
eventId, err := uuid.Parse(eventIdOrig)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
attendanceData := new(data.Attendance)
attendance, err := attendanceData.GetAttendance(userId, eventId)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorDatabase).
Build()
utils.HttpResponse(c, 500, errorCode)
return
} else if attendance == nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.EventService).
SetEndpoint(exception.EventCheckinQueryEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.EventCheckinQueryRecordNotFound).
Build()
utils.HttpResponse(c, 404, errorCode)
return
} else if attendance.CheckinAt.IsZero() {
utils.HttpResponse(c, 200, "", "success", gin.H{"checkin_at": nil})
return
}
checkInAtResp := struct {
CheckinAt time.Time `json:"checkin_at"`
}{attendance.CheckinAt}
utils.HttpResponse(c, 200, "", "success", checkInAtResp)
}