249 lines
5.7 KiB
Go
249 lines
5.7 KiB
Go
package service_event
|
|
|
|
import (
|
|
"context"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
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 EventJoinResult struct {
|
|
Common shared.CommonResult
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if eventData.EnableKYC == true && payload.Data.KycId != "" {
|
|
kycId, err := uuid.Parse(payload.Data.KycId)
|
|
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,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
kycData, err := new(data.Kyc).GetByKycId(payload.Context, &kycId)
|
|
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,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if kycData.UserId != userId {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceJoin).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventJoinResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
attendenceData.SetKycId(kycData.KycId)
|
|
}
|
|
|
|
if !eventData.EndTime.Before(time.Now()) {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceJoin).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.EventJoinEventInvalid).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventJoinResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 403,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
attendenceData.SetUserId(userId)
|
|
attendenceData.SetEventId(eventId)
|
|
attendenceData.SetRole("notmal")
|
|
attendenceData.SetState("success")
|
|
|
|
err = attendenceData.Create(payload.Context)
|
|
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,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
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,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|