Skip to content

Commit

Permalink
Merge pull request #51 from isd-sgcu/rpkm-end
Browse files Browse the repository at this point in the history
Add end time for rpkm reg
  • Loading branch information
bookpanda authored Jul 22, 2024
2 parents 0c6c5a2 + e573abf commit ead6ade
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 59 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 30 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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\n", parsedCheckinTime)

parsedRpkmStartTime, err := parseLocalTime("REG_RPKM_START")
if err != nil {
return nil, err
}
fmt.Printf("Parsed RPKM start time: %v\n", 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\n", parsedRpkmEndTime)

regConfig := RegConfig{
RpkmStart: localRpkmTime,
CheckinStart: localCheckinTime,
CheckinStart: parsedCheckinTime,
RpkmStart: parsedRpkmStartTime,
RpkmEnd: parsedRpkmEndTime,
}

serviceConfig := ServiceConfig{
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions docker-compose.qa.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 18 additions & 21 deletions internal/group/group.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -332,14 +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, "Registration has ended"
}

return true
return true, ""
}
34 changes: 15 additions & 19 deletions internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -276,14 +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, "Registration has ended"
}

return true
return true, ""
}

0 comments on commit ead6ade

Please sign in to comment.