Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rpkm time #39

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ IMG_MAX_FILE_SIZE_MB=1
IMG_CROP_WIDTH=500
IMG_CROP_HEIGHT=500

RPKM_REG_START=2024-07-20T20:00:00Z

SERVICE_AUTH=localhost:3002
SERVICE_BACKEND=localhost:3003
SERVICE_CHECKIN=localhost:3004
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func main() {

groupClient := groupProto.NewGroupServiceClient(backendConn)
groupSvc := group.NewService(groupClient, logger)
groupHdr := group.NewHandler(groupSvc, validate, logger)
groupHdr := group.NewHandler(groupSvc, &conf.Rpkm, validate, logger)

selectionClient := selectionProto.NewSelectionServiceClient(backendConn)
selectionSvc := selection.NewService(selectionClient, logger)
selectionHdr := selection.NewHandler(selectionSvc, groupSvc, validate, logger)
selectionHdr := selection.NewHandler(selectionSvc, groupSvc, &conf.Rpkm, validate, logger)

pinClient := pinProto.NewPinServiceClient(backendConn)
pinSvc := pin.NewService(pinClient, logger)
Expand Down
24 changes: 24 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

import (
"fmt"
"os"
"strconv"
"time"

"github.com/joho/godotenv"
)
Expand All @@ -19,6 +21,10 @@ type ImageConfig struct {
CropHeight int
}

type RpkmConfig struct {
RegStart time.Time
}

type ServiceConfig struct {
Auth string
Backend string
Expand All @@ -41,6 +47,7 @@ type TracerConfig struct {
type Config struct {
App AppConfig
Img ImageConfig
Rpkm RpkmConfig
Svc ServiceConfig
Cors CorsConfig
Db DbConfig
Expand Down Expand Up @@ -79,6 +86,22 @@ func LoadConfig() (*Config, error) {
CropHeight: int(cropHeight),
}

parsedTime, err := time.Parse(time.RFC3339, os.Getenv("RPKM_REG_START"))
if err != nil {
return nil, err
}

const gmtPlus7 = 7 * 60 * 60
gmtPlus7Location := time.FixedZone("GMT+7", gmtPlus7)
localTime := time.Date(
parsedTime.Year(), parsedTime.Month(), parsedTime.Day(),
parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(),
parsedTime.Nanosecond(), gmtPlus7Location)
fmt.Println("Local time (GMT+7):", localTime)
rpkmConfig := RpkmConfig{
RegStart: localTime,
}

serviceConfig := ServiceConfig{
Auth: os.Getenv("SERVICE_AUTH"),
Backend: os.Getenv("SERVICE_BACKEND"),
Expand All @@ -101,6 +124,7 @@ func LoadConfig() (*Config, error) {
return &Config{
App: appConfig,
Img: imageConfig,
Rpkm: rpkmConfig,
Svc: serviceConfig,
Cors: corsConfig,
Db: DbConfig,
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.qa.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ services:
APP_ENV: development
APP_MAX_FILE_SIZE_MB: 10
APP_SERVICE_NAME: rpkm67-gateway
IMG_MAX_FILE_SIZE_MB: 1
IMG_CROP_WIDTH: 500
IMG_CROP_HEIGHT: 500
RPKM_REG_START: 2024-07-20T20:00:00Z
SERVICE_AUTH: auth:3002
SERVICE_BACKEND: backend:3003
SERVICE_CHECKIN: checkin:3004
Expand Down
48 changes: 47 additions & 1 deletion internal/group/group.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package group
import (
"net/http"
"strings"
"time"

"github.com/isd-sgcu/rpkm67-gateway/config"
"github.com/isd-sgcu/rpkm67-gateway/internal/context"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
"github.com/isd-sgcu/rpkm67-gateway/internal/validator"
Expand All @@ -19,17 +21,19 @@ type Handler interface {
DeleteMember(c context.Ctx)
}

func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler {
func NewHandler(svc Service, rpkmConf *config.RpkmConfig, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
validate: validate,
rpkmConf: rpkmConf,
log: log,
}
}

type handlerImpl struct {
svc Service
validate validator.DtoValidator
rpkmConf *config.RpkmConfig
log *zap.Logger
}

Expand All @@ -48,6 +52,11 @@ 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 @@ -90,6 +99,11 @@ 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 @@ -129,6 +143,11 @@ 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")
return
}

userId := c.Param("userId")
if userId == "" {
c.BadRequestError("url parameter 'user_id' not found")
Expand Down Expand Up @@ -179,6 +198,11 @@ 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")
return
}

body := &dto.JoinGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("Join").Error("Bind: failed to bind request body", zap.Error(err))
Expand Down Expand Up @@ -224,6 +248,11 @@ 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")
return
}

body := &dto.LeaveGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("Leave").Error("Bind: failed to bind request body", zap.Error(err))
Expand Down Expand Up @@ -268,6 +297,11 @@ 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")
return
}

body := &dto.DeleteMemberGroupBody{}
if err := c.Bind(body); err != nil {
h.log.Named("DeleteMember").Error("Bind: failed to bind request body", zap.Error(err))
Expand Down Expand Up @@ -297,3 +331,15 @@ func (h *handlerImpl) DeleteMember(c context.Ctx) {
Group: res.Group,
})
}

func (h *handlerImpl) checkRegTime() bool {
nowUTC := time.Now().UTC()
gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60)
nowGMTPlus7 := nowUTC.In(gmtPlus7Location)
if nowGMTPlus7.Before(h.rpkmConf.RegStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
}

return true
}
40 changes: 39 additions & 1 deletion internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package selection
import (
"net/http"
"strings"
"time"

"github.com/isd-sgcu/rpkm67-gateway/config"
"github.com/isd-sgcu/rpkm67-gateway/internal/context"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
"github.com/isd-sgcu/rpkm67-gateway/internal/group"
Expand All @@ -23,14 +25,16 @@ type handlerImpl struct {
svc Service
groupSvc group.Service
validate validator.DtoValidator
rpkmConf *config.RpkmConfig
log *zap.Logger
}

func NewHandler(svc Service, groupSvc group.Service, validate validator.DtoValidator, log *zap.Logger) Handler {
func NewHandler(svc Service, groupSvc group.Service, rpkmConf *config.RpkmConfig, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
groupSvc: groupSvc,
validate: validate,
rpkmConf: rpkmConf,
log: log,
}
}
Expand All @@ -51,6 +55,10 @@ func NewHandler(svc Service, groupSvc group.Service, validate validator.DtoValid
// @Failure 500 {object} apperror.AppError
// @Router /selection [post]
func (h *handlerImpl) Create(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}
h.checkGroupLeader(c)

body := &dto.CreateSelectionRequest{}
Expand Down Expand Up @@ -91,6 +99,11 @@ 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 @@ -134,6 +147,10 @@ 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")
return
}
h.checkGroupLeader(c)

body := &dto.UpdateSelectionRequest{}
Expand Down Expand Up @@ -177,6 +194,10 @@ 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")
return
}
h.checkGroupLeader(c)

body := &dto.DeleteSelectionRequest{}
Expand Down Expand Up @@ -217,6 +238,11 @@ 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 @@ -249,3 +275,15 @@ func (h *handlerImpl) checkGroupLeader(c context.Ctx) {

c.Next()
}

func (h *handlerImpl) checkRegTime() bool {
nowUTC := time.Now().UTC()
gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60)
nowGMTPlus7 := nowUTC.In(gmtPlus7Location)
if nowGMTPlus7.Before(h.rpkmConf.RegStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
}

return true
}
Loading
Loading