Files
cms-server/service/service_event/join_event.go
Asai Neko 67e2cbbd04
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Add attendance id resp for event join api, set root api to /app/api/v1
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-02-06 10:30:28 +08:00

247 lines
6.1 KiB
Go

package service_event
import (
"context"
"errors"
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/service/shared"
"github.com/google/uuid"
"gorm.io/gorm"
)
type EventJoinData struct {
EventId string `json:"event_id"`
KycId string `json:"kyc_id"`
UserId string `json:"user_id" swaggerignore:"true"`
Role string `json:"role" swaggerignore:"true"`
State string `json:"state" swaggerignore:"true"`
}
type EventJoinPayload struct {
Context context.Context
Data *EventJoinData
}
type EventJoinResponse struct {
AttendanceId string `json:"attendance_id"`
}
type EventJoinResult struct {
Common shared.CommonResult
Data *EventJoinResponse
}
func (self *EventServiceImpl) JoinEvent(payload *EventJoinPayload) (result *EventJoinResult) {
var err error
attendenceData := new(data.Attendance)
eventId, err := uuid.Parse(payload.Data.EventId)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
SetError(err).
Throw(payload.Context)
result = &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
eventData, err := new(data.Event).GetEventById(payload.Context, eventId)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context)
result = &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
if eventData.EnableKYC == true && payload.Data.KycId == "" {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(nil).
Throw(payload.Context)
result = &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
userId, err := uuid.Parse(payload.Data.UserId)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUuidParseFailed).
SetError(err).
Throw(payload.Context)
result = &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
return
}
attendenceSearch, err := new(data.Attendance).GetAttendance(payload.Context, eventId, userId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context),
},
Data: nil,
}
}
if err == nil && attendenceSearch != nil && attendenceSearch.AttendanceId != uuid.Nil {
return &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 403,
Exception: new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeSpecific).
SetOriginal(exception.EventJoinEventInvalid).
SetError(errors.New("user already joined this event")).
Throw(payload.Context),
},
Data: nil,
}
}
attendenceData.SetUserId(userId)
attendenceData.SetEventId(eventId)
attendenceCount, err := attendenceData.CountUsersByEventID(payload.Context, eventId)
if err != nil {
return &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context),
},
Data: nil,
}
}
if attendenceCount >= eventData.Quota {
if attendenceCount >= eventData.Limit {
return &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 403,
Exception: new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeSpecific).
SetOriginal(exception.EventJoinLimitExceeded).
SetError(errors.New("event limit exceeded")).
Throw(payload.Context),
},
Data: nil,
}
}
attendenceData.SetState("out_of_limit")
} else {
attendenceData.SetState("success")
}
attendenceData.SetRole("normal")
attendanceId, err := attendenceData.Create(payload.Context)
if err != nil {
return &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorDatabase).
SetError(err).
Throw(payload.Context),
},
Data: nil,
}
}
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceJoin).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &EventJoinResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: exception,
},
Data: &EventJoinResponse{
AttendanceId: attendanceId.String(),
},
}
return
}