From d1ebfbb525c0585c104de93456690c68ce6283c8 Mon Sep 17 00:00:00 2001 From: javadifa Date: Sat, 24 Aug 2024 21:43:29 +0330 Subject: [PATCH 1/3] fix(validation): Add validation for KindBox status in enumeration by admin --- pkg/err_msg/message.go | 1 + validator/admin/kind_box/enumerate.go | 5 +++-- validator/admin/kind_box/validator.go | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 0968013..8ec7e87 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -35,5 +35,6 @@ const ( ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" + ErrorMsgReturnKindBoxStatus = "only returned kindboxes can be enumerated" ErrorMsgInvalidSerialNumberRange = "invalid serial number range" ) diff --git a/validator/admin/kind_box/enumerate.go b/validator/admin/kind_box/enumerate.go index 8906d95..4a738a2 100644 --- a/validator/admin/kind_box/enumerate.go +++ b/validator/admin/kind_box/enumerate.go @@ -14,11 +14,12 @@ func (v Validator) ValidateEnumerate(ctx context.Context, req param.EnumerateKin const op = "adminkindboxvalidator.ValidateEnumerate" if err := validation.ValidateStruct(&req, - validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(ctx))), + validation.By(v.doesKindBoxExist(ctx)), + validation.By(v.CheckKindBoxStatusForEnumeration(ctx))), validation.Field(&req.Amount, validation.Required), + ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 3f360ed..25405cd 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -81,3 +81,21 @@ func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc { return nil } } + +func (v Validator) CheckKindBoxStatusForEnumeration(ctx context.Context) validation.RuleFunc { + return func(value interface{}) error { + kindBoxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + kindBox, err := v.repo.GetKindBox(ctx, kindBoxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if kindBox.Status != entity.KindBoxReturnedStatus { + return fmt.Errorf(errmsg.ErrorMsgReturnKindBoxStatus) + } + + return nil + } +} \ No newline at end of file From 2c3124ee2c458c444f7b3e22a20f1d05b66d3949 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Sun, 8 Sep 2024 13:34:06 +0330 Subject: [PATCH 2/3] feat: add CORS middleware configuration with custom allowed origins(#154) --- config/config.go | 7 ++++++- delivery/http_server/server.go | 3 +++ deploy/stage/app/config.yml | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 22aec59..f000f1e 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,12 @@ import ( ) type HTTPServer struct { - Port int `koanf:"port"` + Port int `koanf:"port"` + Cors Cors `koanf:"cors"` +} + +type Cors struct { + AllowOrigins []string `koanf:"allow_origins"` } type Config struct { diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 0d1ceca..780dca8 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -67,6 +67,9 @@ func (s Server) Serve() { func (s Server) RegisterRoutes() { s.Router.Use(middleware.RequestID()) s.Router.Use(middleware.Recover()) + s.Router.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowOrigins: s.config.HTTPServer.Cors.AllowOrigins, + })) registerSwagger(s.Router) // Routes diff --git a/deploy/stage/app/config.yml b/deploy/stage/app/config.yml index dd6c42a..dcd27e6 100644 --- a/deploy/stage/app/config.yml +++ b/deploy/stage/app/config.yml @@ -3,6 +3,9 @@ type: yml http_server: port: 1313 + cors: + allow_origins: + - "*" benefactor_service: length_of_otp_code: 5 From 82e71763e292459ecd80d036a5cc11b1d57afd42 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Mon, 9 Sep 2024 12:39:57 +0330 Subject: [PATCH 3/3] fix(makefile): skip formatting vendor and auto generated files --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8771526..79ddcd4 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ format: @which gofumpt || (go install mvdan.cc/gofumpt@latest) @gofumpt -l -w $(ROOT) @which gci || (go install github.com/daixiang0/gci@latest) - @gci write $(ROOT) + @gci write $(ROOT) --skip-generated --skip-vendor @which golangci-lint || (go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0) @golangci-lint run --fix