From b8e3a80d6e348568078251e37cfba638b04ca34b Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 22 Jul 2024 21:10:14 +0700 Subject: [PATCH 1/4] config --- .env.template | 1 + config/config.go | 49 +++++++++++++++++++++------------- docker-compose.qa.template.yml | 1 + 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.env.template b/.env.template index 2e16664..8132258 100644 --- a/.env.template +++ b/.env.template @@ -8,6 +8,7 @@ IMG_CROP_HEIGHT=500 REG_CHECKIN_START=2024-07-21T07:00:00Z REG_RPKM_START=2024-07-20T20:00:00Z +REG_RPKM_END=2024-07-25T00:00:00Z SERVICE_AUTH=localhost:3002 SERVICE_BACKEND=localhost:3003 diff --git a/config/config.go b/config/config.go index 99e9b78..e2e15bc 100644 --- a/config/config.go +++ b/config/config.go @@ -22,8 +22,9 @@ type ImageConfig struct { } type RegConfig struct { - RpkmStart time.Time CheckinStart time.Time + RpkmStart time.Time + RpkmEnd time.Time } type ServiceConfig struct { @@ -87,33 +88,28 @@ func LoadConfig() (*Config, error) { CropHeight: int(cropHeight), } - parsedRpkmTime, err := time.Parse(time.RFC3339, os.Getenv("REG_RPKM_START")) + parsedCheckinTime, err := parseLocalTime("REG_CHECKIN_START") if err != nil { return nil, err } - parsedCheckinTime, err := time.Parse(time.RFC3339, os.Getenv("REG_CHECKIN_START")) + fmt.Printf("Parsed Checkin start time: %v", parsedCheckinTime) + + parsedRpkmStartTime, err := parseLocalTime("REG_RPKM_START") if err != nil { return nil, err } + fmt.Printf("Parsed RPKM start time: %v", parsedRpkmStartTime) - const gmtPlus7 = 7 * 60 * 60 - gmtPlus7Location := time.FixedZone("GMT+7", gmtPlus7) - - localRpkmTime := time.Date( - parsedRpkmTime.Year(), parsedRpkmTime.Month(), parsedRpkmTime.Day(), - parsedRpkmTime.Hour(), parsedRpkmTime.Minute(), parsedRpkmTime.Second(), - parsedRpkmTime.Nanosecond(), gmtPlus7Location) - fmt.Println("Local RPKM time (GMT+7):", localRpkmTime) - - localCheckinTime := time.Date( - parsedCheckinTime.Year(), parsedCheckinTime.Month(), parsedCheckinTime.Day(), - parsedCheckinTime.Hour(), parsedCheckinTime.Minute(), parsedCheckinTime.Second(), - parsedCheckinTime.Nanosecond(), gmtPlus7Location) - fmt.Println("Local Firstdate time (GMT+7):", localCheckinTime) + parsedRpkmEndTime, err := parseLocalTime("REG_RPKM_END") + if err != nil { + return nil, err + } + fmt.Printf("Parsed RPKM end time: %v", parsedRpkmEndTime) regConfig := RegConfig{ - RpkmStart: localRpkmTime, - CheckinStart: localCheckinTime, + CheckinStart: parsedCheckinTime, + RpkmStart: parsedRpkmStartTime, + RpkmEnd: parsedRpkmEndTime, } serviceConfig := ServiceConfig{ @@ -149,3 +145,18 @@ func LoadConfig() (*Config, error) { func (ac *AppConfig) IsDevelopment() bool { return ac.Env == "development" } + +func parseLocalTime(envName string) (time.Time, error) { + parsedTime, err := time.Parse(time.RFC3339, os.Getenv(envName)) + if err != nil { + return time.Time{}, err + } + + const gmtPlus7 = 7 * 60 * 60 + gmtPlus7Location := time.FixedZone("GMT+7", gmtPlus7) + + return time.Date( + parsedTime.Year(), parsedTime.Month(), parsedTime.Day(), + parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), + parsedTime.Nanosecond(), gmtPlus7Location), nil +} diff --git a/docker-compose.qa.template.yml b/docker-compose.qa.template.yml index 14e1759..e8dc044 100644 --- a/docker-compose.qa.template.yml +++ b/docker-compose.qa.template.yml @@ -25,6 +25,7 @@ services: IMG_CROP_HEIGHT: 500 REG_CHECKIN_START: "2024-07-21T07:00:00Z" REG_RPKM_START: "2024-07-20T20:00:00Z" + REG_RPKM_END: "2024-07-25T00:00:00Z" SERVICE_AUTH: auth:3002 SERVICE_BACKEND: backend:3003 SERVICE_CHECKIN: checkin:3004 From 7630af8316531cf62b791d0c279a763fc27a158b Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 22 Jul 2024 21:11:34 +0700 Subject: [PATCH 2/4] check reg time --- internal/group/group.handler.go | 3 +++ internal/selection/selection.handler.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/internal/group/group.handler.go b/internal/group/group.handler.go index f50ce6f..855b98c 100644 --- a/internal/group/group.handler.go +++ b/internal/group/group.handler.go @@ -339,6 +339,9 @@ func (h *handlerImpl) checkRegTime() bool { if nowGMTPlus7.Before(h.regConf.RpkmStart) { h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started") return false + } else if nowGMTPlus7.After(h.regConf.RpkmEnd) { + h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended") + return false } return true diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go index a3a7f13..361f949 100644 --- a/internal/selection/selection.handler.go +++ b/internal/selection/selection.handler.go @@ -283,6 +283,9 @@ func (h *handlerImpl) checkRegTime() bool { if nowGMTPlus7.Before(h.regConf.RpkmStart) { h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started") return false + } else if nowGMTPlus7.After(h.regConf.RpkmEnd) { + h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended") + return false } return true From e573abf5f1d52a5a1dcbf4625adc05864a9f3a02 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 22 Jul 2024 21:49:56 +0700 Subject: [PATCH 3/4] st, ed --- config/config.go | 6 ++-- internal/group/group.handler.go | 38 +++++++++++-------------- internal/selection/selection.handler.go | 33 +++++++++------------ 3 files changed, 32 insertions(+), 45 deletions(-) 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, "" } From 431c740a5638518e1b4db84767ae15c82da848c6 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 22 Jul 2024 22:06:35 +0700 Subject: [PATCH 4/4] fix: test --- internal/selection/test/selection.handler_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index 3718784..29e90f5 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -43,8 +43,13 @@ func (t *SelectionHandlerTest) SetupTest() { if err != nil { t.T().Fatal(err) } + regEnd, err := time.Parse(time.RFC3339, "2030-08-01T00:00:00Z") + if err != nil { + t.T().Fatal(err) + } t.regConf = &config.RegConfig{ RpkmStart: regStart, + RpkmEnd: regEnd, } t.userId = faker.UUIDHyphenated()