diff --git a/internal/checkin/checkin.handler.go b/internal/checkin/checkin.handler.go index 44f6efb..a48a141 100644 --- a/internal/checkin/checkin.handler.go +++ b/internal/checkin/checkin.handler.go @@ -83,6 +83,18 @@ func (h *handlerImpl) Create(c context.Ctx) { return } + if body.StudentID != "" { + req := &dto.FindByEmailUserRequest{Email: body.StudentID + "@student.chula.ac.th"} + res, appErr := h.userSvc.FindByEmail(req) + if appErr != nil { + h.log.Named("Create").Error("FindOne: ", zap.Error(appErr)) + c.ResponseError(appErr) + return + } + + body.UserID = res.User.Id + } + req := &dto.CreateCheckInRequest{ Email: body.Email, UserID: body.UserID, diff --git a/internal/dto/checkin.dto.go b/internal/dto/checkin.dto.go index e74fbe4..212296f 100644 --- a/internal/dto/checkin.dto.go +++ b/internal/dto/checkin.dto.go @@ -10,9 +10,10 @@ type CheckIn struct { } type CreateCheckInRequest struct { - UserID string `json:"user_id"` - Email string `json:"email"` - Event string `json:"event"` + UserID string `json:"user_id"` + StudentID string `json:"student_id"` + Email string `json:"email"` + Event string `json:"event"` } type CreateCheckInResponse struct { diff --git a/internal/dto/user.dto.go b/internal/dto/user.dto.go index 4dfac8b..f822a69 100644 --- a/internal/dto/user.dto.go +++ b/internal/dto/user.dto.go @@ -36,6 +36,14 @@ type FindOneUserResponse struct { User *User `json:"user"` } +type FindByEmailUserRequest struct { + Email string `json:"email" validate:"required,email"` +} + +type FindByEmailUserResponse struct { + User *User `json:"user"` +} + type UpdateUserProfileBody struct { Nickname string `json:"nickname"` Title string `json:"title"` diff --git a/internal/user/user.service.go b/internal/user/user.service.go index 8b7a42f..8bd3b12 100644 --- a/internal/user/user.service.go +++ b/internal/user/user.service.go @@ -13,6 +13,7 @@ import ( type Service interface { FindOne(req *dto.FindOneUserRequest) (*dto.FindOneUserResponse, *apperror.AppError) + FindByEmail(req *dto.FindByEmailUserRequest) (*dto.FindByEmailUserResponse, *apperror.AppError) UpdateProfile(req *dto.UpdateUserProfileRequest) (*dto.UpdateUserProfileResponse, *apperror.AppError) UpdatePicture(req *dto.UpdateUserPictureRequest) (*dto.UpdateUserPictureResponse, *apperror.AppError) } @@ -48,6 +49,23 @@ func (s *serviceImpl) FindOne(req *dto.FindOneUserRequest) (*dto.FindOneUserResp }, nil } +func (s *serviceImpl) FindByEmail(req *dto.FindByEmailUserRequest) (*dto.FindByEmailUserResponse, *apperror.AppError) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + res, err := s.client.FindByEmail(ctx, &userProto.FindByEmailRequest{ + Email: req.Email, + }) + if err != nil { + s.log.Named("FindByEmail").Error("FindByEmail: ", zap.Error(err)) + return nil, apperror.HandleServiceError(err) + } + + return &dto.FindByEmailUserResponse{ + User: ProtoToDto(res.User), + }, nil +} + func (s *serviceImpl) UpdateProfile(req *dto.UpdateUserProfileRequest) (*dto.UpdateUserProfileResponse, *apperror.AppError) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel()