Skip to content

Commit

Permalink
Merge pull request #40 from isd-sgcu/checkin-reg
Browse files Browse the repository at this point in the history
Checkin reg
  • Loading branch information
bookpanda authored Jul 20, 2024
2 parents e89b226 + 43b853d commit 3f93783
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ IMG_MAX_FILE_SIZE_MB=1
IMG_CROP_WIDTH=500
IMG_CROP_HEIGHT=500

RPKM_REG_START=2024-07-20T20:00:00Z
REG_CHECKIN_START=2024-07-21T07:00:00Z
REG_RPKM_START=2024-07-20T20:00:00Z

SERVICE_AUTH=localhost:3002
SERVICE_BACKEND=localhost:3003
Expand Down
6 changes: 3 additions & 3 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, &conf.Rpkm, validate, logger)
groupHdr := group.NewHandler(groupSvc, &conf.Reg, validate, logger)

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

pinClient := pinProto.NewPinServiceClient(backendConn)
pinSvc := pin.NewService(pinClient, logger)
Expand All @@ -126,7 +126,7 @@ func main() {

checkinClient := checkinProto.NewCheckInServiceClient(checkinConn)
checkinSvc := checkin.NewService(checkinClient, logger, tracer)
checkinHdr := checkin.NewHandler(checkinSvc, userSvc, validate, logger)
checkinHdr := checkin.NewHandler(checkinSvc, userSvc, &conf.Reg, validate, logger)

requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "api_requests_total",
Expand Down
38 changes: 26 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type ImageConfig struct {
CropHeight int
}

type RpkmConfig struct {
RegStart time.Time
type RegConfig struct {
RpkmStart time.Time
CheckinStart time.Time
}

type ServiceConfig struct {
Expand All @@ -47,7 +48,7 @@ type TracerConfig struct {
type Config struct {
App AppConfig
Img ImageConfig
Rpkm RpkmConfig
Reg RegConfig
Svc ServiceConfig
Cors CorsConfig
Db DbConfig
Expand Down Expand Up @@ -86,20 +87,33 @@ func LoadConfig() (*Config, error) {
CropHeight: int(cropHeight),
}

parsedTime, err := time.Parse(time.RFC3339, os.Getenv("RPKM_REG_START"))
parsedRpkmTime, err := time.Parse(time.RFC3339, os.Getenv("REG_RPKM_START"))
if err != nil {
return nil, err
}
parsedCheckinTime, err := time.Parse(time.RFC3339, os.Getenv("REG_CHECKIN_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,

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)

regConfig := RegConfig{
RpkmStart: localRpkmTime,
CheckinStart: localCheckinTime,
}

serviceConfig := ServiceConfig{
Expand All @@ -124,7 +138,7 @@ func LoadConfig() (*Config, error) {
return &Config{
App: appConfig,
Img: imageConfig,
Rpkm: rpkmConfig,
Reg: regConfig,
Svc: serviceConfig,
Cors: corsConfig,
Db: DbConfig,
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.qa.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ services:
IMG_MAX_FILE_SIZE_MB: 1
IMG_CROP_WIDTH: 500
IMG_CROP_HEIGHT: 500
RPKM_REG_START: 2024-07-20T20:00:00Z
REG_CHECKIN_START: 2024-07-21T07:00:00Z
REG_RPKM_START: 2024-07-20T20:00:00Z
SERVICE_AUTH: auth:3002
SERVICE_BACKEND: backend:3003
SERVICE_CHECKIN: checkin:3004
Expand Down
33 changes: 32 additions & 1 deletion internal/checkin/checkin.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package checkin
import (
"net/http"
"strings"
"time"

"github.com/isd-sgcu/rpkm67-gateway/apperror"
"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/user"
Expand All @@ -21,14 +23,16 @@ type Handler interface {
type handlerImpl struct {
svc Service
userSvc user.Service
regConf *config.RegConfig
validate validator.DtoValidator
log *zap.Logger
}

func NewHandler(svc Service, userSvc user.Service, validate validator.DtoValidator, log *zap.Logger) Handler {
func NewHandler(svc Service, userSvc user.Service, regConf *config.RegConfig, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
userSvc: userSvc,
regConf: regConf,
validate: validate,
log: log,
}
Expand All @@ -46,6 +50,11 @@ func NewHandler(svc Service, userSvc user.Service, validate validator.DtoValidat
// @Failure 400 {object} apperror.AppError
// @Router /checkin [post]
func (h *handlerImpl) Create(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

if c.GetString("role") != "staff" {
c.ResponseError(apperror.ForbiddenError("only staff can access this endpoint"))
return
Expand Down Expand Up @@ -114,6 +123,11 @@ func (h *handlerImpl) Create(c context.Ctx) {
// @Failure 400 {object} apperror.AppError
// @Router /checkin/email/{email} [get]
func (h *handlerImpl) FindByEmail(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

if c.GetString("role") != "staff" {
c.ResponseError(apperror.ForbiddenError("only staff can access this endpoint"))
return
Expand Down Expand Up @@ -158,6 +172,11 @@ func (h *handlerImpl) FindByEmail(c context.Ctx) {
// @Failure 400 {object} apperror.AppError
// @Router /checkin/{userId} [get]
func (h *handlerImpl) FindByUserID(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

if c.GetString("role") != "staff" {
c.ResponseError(apperror.ForbiddenError("only staff can access this endpoint"))
return
Expand Down Expand Up @@ -189,3 +208,15 @@ func (h *handlerImpl) FindByUserID(c context.Ctx) {
CheckIns: res.CheckIns,
})
}

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.regConf.CheckinStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
}

return true
}
8 changes: 4 additions & 4 deletions internal/group/group.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ type Handler interface {
DeleteMember(c context.Ctx)
}

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

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

Expand Down Expand Up @@ -336,7 +336,7 @@ 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) {
if nowGMTPlus7.Before(h.regConf.RpkmStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
}
Expand Down
8 changes: 4 additions & 4 deletions internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ type handlerImpl struct {
svc Service
groupSvc group.Service
validate validator.DtoValidator
rpkmConf *config.RpkmConfig
regConf *config.RegConfig
log *zap.Logger
}

func NewHandler(svc Service, groupSvc group.Service, rpkmConf *config.RpkmConfig, validate validator.DtoValidator, log *zap.Logger) Handler {
func NewHandler(svc Service, groupSvc group.Service, regConf *config.RegConfig, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
groupSvc: groupSvc,
validate: validate,
rpkmConf: rpkmConf,
regConf: regConf,
log: log,
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ 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) {
if nowGMTPlus7.Before(h.regConf.RpkmStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
}
Expand Down
18 changes: 9 additions & 9 deletions internal/selection/test/selection.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type SelectionHandlerTest struct {
suite.Suite
controller *gomock.Controller
logger *zap.Logger
rpkmConf *config.RpkmConfig
regConf *config.RegConfig
userId string
Selections []*dto.Selection
Selection *dto.Selection
Expand All @@ -43,8 +43,8 @@ func (t *SelectionHandlerTest) SetupTest() {
if err != nil {
t.T().Fatal(err)
}
t.rpkmConf = &config.RpkmConfig{
RegStart: regStart,
t.regConf = &config.RegConfig{
RpkmStart: regStart,
}

t.userId = faker.UUIDHyphenated()
Expand Down Expand Up @@ -73,7 +73,7 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() {
groupSvc := groupMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := ctxMock.NewMockCtx(t.controller)
handler := selection.NewHandler(selectionSvc, groupSvc, t.rpkmConf, validator, t.logger)
handler := selection.NewHandler(selectionSvc, groupSvc, t.regConf, validator, t.logger)

expectedResp := &dto.CreateSelectionResponse{
Selection: t.Selection,
Expand All @@ -94,7 +94,7 @@ func (t *SelectionHandlerTest) TestCreateSelectionSuccess() {
func (t *SelectionHandlerTest) TestCreateSelectionBindError() {
context := ctxMock.NewMockCtx(t.controller)
groupSvc := groupMock.NewMockService(t.controller)
handler := selection.NewHandler(nil, groupSvc, t.rpkmConf, nil, t.logger)
handler := selection.NewHandler(nil, groupSvc, t.regConf, nil, t.logger)

context.EXPECT().GetString("userId").Return(t.userId)
groupSvc.EXPECT().FindByUserId(&dto.FindByUserIdGroupRequest{UserId: t.userId}).
Expand All @@ -111,7 +111,7 @@ func (t *SelectionHandlerTest) TestCreateSelectionServiceError() {
groupSvc := groupMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := ctxMock.NewMockCtx(t.controller)
handler := selection.NewHandler(selectionSvc, groupSvc, t.rpkmConf, validator, t.logger)
handler := selection.NewHandler(selectionSvc, groupSvc, t.regConf, validator, t.logger)

context.EXPECT().GetString("userId").Return(t.userId)
groupSvc.EXPECT().FindByUserId(&dto.FindByUserIdGroupRequest{UserId: t.userId}).
Expand Down Expand Up @@ -189,7 +189,7 @@ func (t *SelectionHandlerTest) TestDeleteSelectionSuccess() {
groupSvc := groupMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := ctxMock.NewMockCtx(t.controller)
handler := selection.NewHandler(selectionSvc, groupSvc, t.rpkmConf, validator, t.logger)
handler := selection.NewHandler(selectionSvc, groupSvc, t.regConf, validator, t.logger)

expectedResp := &dto.DeleteSelectionResponse{
Success: true,
Expand All @@ -210,7 +210,7 @@ func (t *SelectionHandlerTest) TestDeleteSelectionSuccess() {
func (t *SelectionHandlerTest) TestDeleteSelectionBindError() {
context := ctxMock.NewMockCtx(t.controller)
groupSvc := groupMock.NewMockService(t.controller)
handler := selection.NewHandler(nil, groupSvc, t.rpkmConf, nil, t.logger)
handler := selection.NewHandler(nil, groupSvc, t.regConf, nil, t.logger)

context.EXPECT().GetString("userId").Return(t.userId)
groupSvc.EXPECT().FindByUserId(&dto.FindByUserIdGroupRequest{UserId: t.userId}).
Expand All @@ -227,7 +227,7 @@ func (t *SelectionHandlerTest) TestDeleteSelectionServiceError() {
groupSvc := groupMock.NewMockService(t.controller)
validator := validatorMock.NewMockDtoValidator(t.controller)
context := ctxMock.NewMockCtx(t.controller)
handler := selection.NewHandler(selectionSvc, groupSvc, t.rpkmConf, validator, t.logger)
handler := selection.NewHandler(selectionSvc, groupSvc, t.regConf, validator, t.logger)

context.EXPECT().GetString("userId").Return(t.userId)
groupSvc.EXPECT().FindByUserId(&dto.FindByUserIdGroupRequest{UserId: t.userId}).
Expand Down

0 comments on commit 3f93783

Please sign in to comment.