diff --git a/config/config.go b/config/config.go index e2e15bc..c1330d8 100644 --- a/config/config.go +++ b/config/config.go @@ -92,19 +92,19 @@ func LoadConfig() (*Config, error) { if err != nil { return nil, err } - fmt.Printf("Parsed Checkin start time: %v", parsedCheckinTime) + fmt.Printf("Parsed Checkin start time: %v\n", parsedCheckinTime) parsedRpkmStartTime, err := parseLocalTime("REG_RPKM_START") if err != nil { return nil, err } - fmt.Printf("Parsed RPKM start time: %v", parsedRpkmStartTime) + fmt.Printf("Parsed RPKM start time: %v\n", parsedRpkmStartTime) parsedRpkmEndTime, err := parseLocalTime("REG_RPKM_END") if err != nil { return nil, err } - fmt.Printf("Parsed RPKM end time: %v", parsedRpkmEndTime) + fmt.Printf("Parsed RPKM end time: %v\n", parsedRpkmEndTime) regConfig := RegConfig{ CheckinStart: parsedCheckinTime, diff --git a/internal/group/group.handler.go b/internal/group/group.handler.go index 855b98c..5094e0e 100644 --- a/internal/group/group.handler.go +++ b/internal/group/group.handler.go @@ -52,11 +52,6 @@ type handlerImpl struct { // @Failure 500 {object} apperror.AppError // @Router /group/{userId} [get] func (h *handlerImpl) FindByUserId(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") - return - } - userId := c.Param("userId") if userId == "" { c.BadRequestError("url parameter 'user_id' not found") @@ -99,11 +94,6 @@ func (h *handlerImpl) FindByUserId(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /group/token [get] func (h *handlerImpl) FindByToken(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") - return - } - token := c.Query("token") if token == "" { c.BadRequestError("url parameter 'token' not found") @@ -143,8 +133,9 @@ func (h *handlerImpl) FindByToken(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /group/{userId} [put] func (h *handlerImpl) UpdateConfirm(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } @@ -198,8 +189,9 @@ func (h *handlerImpl) UpdateConfirm(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /group/join [post] func (h *handlerImpl) Join(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } @@ -248,8 +240,9 @@ func (h *handlerImpl) Join(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /group/leave [post] func (h *handlerImpl) Leave(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } @@ -297,8 +290,9 @@ func (h *handlerImpl) Leave(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /group/delete-member [delete] func (h *handlerImpl) DeleteMember(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } @@ -332,17 +326,17 @@ func (h *handlerImpl) DeleteMember(c context.Ctx) { }) } -func (h *handlerImpl) checkRegTime() bool { +func (h *handlerImpl) checkRegTime() (bool, string) { nowUTC := time.Now().UTC() gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60) nowGMTPlus7 := nowUTC.In(gmtPlus7Location) if nowGMTPlus7.Before(h.regConf.RpkmStart) { h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started") - return false + return false, "Registration hasn't started" } else if nowGMTPlus7.After(h.regConf.RpkmEnd) { h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended") - return false + return false, "Registration has ended" } - return true + return true, "" } diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go index 361f949..ef890de 100644 --- a/internal/selection/selection.handler.go +++ b/internal/selection/selection.handler.go @@ -55,8 +55,9 @@ func NewHandler(svc Service, groupSvc group.Service, regConf *config.RegConfig, // @Failure 500 {object} apperror.AppError // @Router /selection [post] func (h *handlerImpl) Create(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } h.checkGroupLeader(c) @@ -99,11 +100,6 @@ func (h *handlerImpl) Create(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /selection/{groupId} [get] func (h *handlerImpl) FindByGroupId(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") - return - } - groupId := c.Param("groupId") if groupId == "" { h.log.Named("FindByGroupIdSelection").Error("Param: groupId not found") @@ -147,8 +143,9 @@ func (h *handlerImpl) FindByGroupId(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /selection [patch] func (h *handlerImpl) Update(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } h.checkGroupLeader(c) @@ -194,8 +191,9 @@ func (h *handlerImpl) Update(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /selection [delete] func (h *handlerImpl) Delete(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") + ok, msg := h.checkRegTime() + if !ok { + c.ForbiddenError(msg) return } h.checkGroupLeader(c) @@ -238,11 +236,6 @@ func (h *handlerImpl) Delete(c context.Ctx) { // @Failure 500 {object} apperror.AppError // @Router /selection/count-by-baan [get] func (h *handlerImpl) CountByBaanId(c context.Ctx) { - if !h.checkRegTime() { - c.ForbiddenError("Registration hasn't started") - return - } - res, appErr := h.svc.CountByBaanId() if appErr != nil { h.log.Named("CountByBaanId").Error("CountByBaanId: ", zap.Error(appErr)) @@ -276,17 +269,17 @@ func (h *handlerImpl) checkGroupLeader(c context.Ctx) { c.Next() } -func (h *handlerImpl) checkRegTime() bool { +func (h *handlerImpl) checkRegTime() (bool, string) { nowUTC := time.Now().UTC() gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60) nowGMTPlus7 := nowUTC.In(gmtPlus7Location) if nowGMTPlus7.Before(h.regConf.RpkmStart) { h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started") - return false + return false, "Registration hasn't started" } else if nowGMTPlus7.After(h.regConf.RpkmEnd) { h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended") - return false + return false, "Registration has ended" } - return true + return true, "" }