From b7d5eb522bd900dcc7c93d683870218bacc13ceb Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Fri, 31 May 2024 16:06:48 +0330 Subject: [PATCH] fix(niki): add refer time duration when creating kind box req by benefactor --- .../benefactor/kind_box_req/add.go | 10 ++--- docs/docs.go | 9 +++- docs/swagger.json | 9 +++- docs/swagger.yaml | 7 +++- entity/kind_box_req.go | 23 +++++----- entity/refer_time.go | 7 ++++ entity/refer_time_status.go | 42 +++++++++++++++++++ internal/initial/database.go | 5 +++ internal/initial/service.go | 7 ++++ internal/initial/validator.go | 1 + main.go | 1 + param/admin/refer_time/get.go | 11 +++++ param/benefactor/kind_box_req/add.go | 12 +++--- repository/mysql/kind_box_req/delete.go | 2 +- .../1705399371_create_refer_times_table.sql | 13 ++++++ .../1705400111_create_kind_box_reqs_table.sql | 8 ++-- .../1708712575_insert_refer_times_table.sql | 16 +++++++ repository/mysql/refer_time/db.go | 13 ++++++ repository/mysql/refer_time/get.go | 20 +++++++++ repository/mysql/refer_time/scan.go | 31 ++++++++++++++ service/admin/refer_time/get.go | 16 +++++++ service/admin/refer_time/service.go | 17 ++++++++ service/benefactor/kind_box_req/add.go | 20 ++++----- validator/benefactor/kind_box_req/add.go | 21 +++++----- .../benefactor/kind_box_req/validator.go | 35 ++++++++++++---- 25 files changed, 297 insertions(+), 59 deletions(-) create mode 100644 entity/refer_time.go create mode 100644 entity/refer_time_status.go create mode 100644 param/admin/refer_time/get.go create mode 100644 repository/mysql/migration/1705399371_create_refer_times_table.sql create mode 100644 repository/mysql/migration/1708712575_insert_refer_times_table.sql create mode 100644 repository/mysql/refer_time/db.go create mode 100644 repository/mysql/refer_time/get.go create mode 100644 repository/mysql/refer_time/scan.go create mode 100644 service/admin/refer_time/get.go create mode 100644 service/admin/refer_time/service.go diff --git a/delivery/http_server/benefactor/kind_box_req/add.go b/delivery/http_server/benefactor/kind_box_req/add.go index a89add6..03f84ff 100644 --- a/delivery/http_server/benefactor/kind_box_req/add.go +++ b/delivery/http_server/benefactor/kind_box_req/add.go @@ -1,13 +1,12 @@ package benefactorkindboxreqhandler import ( - "net/http" - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" "git.gocasts.ir/ebhomengo/niki/pkg/claim" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" echo "github.com/labstack/echo/v4" + "net/http" ) // Add godoc @@ -32,13 +31,12 @@ func (h Handler) Add(c echo.Context) error { claims := claim.GetClaimsFromEchoContext(c) req.BenefactorID = claims.UserID - result := h.benefactorKindBoxReqVld.ValidateAddRequest(req) - if result != nil { - msg, code := httpmsg.Error(result.Err) + if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateAddRequest(req); err != nil { + msg, code := httpmsg.Error(err) return c.JSON(code, echo.Map{ "message": msg, - "errors": result.Fields, + "errors": fieldErrors, }) } resp, sErr := h.benefactorKindBoxReqSvc.Add(c.Request().Context(), req) diff --git a/docs/docs.go b/docs/docs.go index 4b91f73..edb1537 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1284,7 +1284,11 @@ const docTemplate = `{ }, "deliver_refer_date": { "type": "string", - "example": "2025-01-02 15:04:05" + "example": "2025-01-02T15:04:05Z" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 }, "type_id": { "allOf": [ @@ -1449,6 +1453,9 @@ const docTemplate = `{ "deliverReferDate": { "type": "string" }, + "deliverReferTimeID": { + "type": "integer" + }, "deliveredAt": { "type": "string" }, diff --git a/docs/swagger.json b/docs/swagger.json index 388d915..892ef8c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1273,7 +1273,11 @@ }, "deliver_refer_date": { "type": "string", - "example": "2025-01-02 15:04:05" + "example": "2025-01-02T15:04:05Z" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 }, "type_id": { "allOf": [ @@ -1438,6 +1442,9 @@ "deliverReferDate": { "type": "string" }, + "deliverReferTimeID": { + "type": "integer" + }, "deliveredAt": { "type": "string" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index c8d42c5..5949748 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -316,8 +316,11 @@ definitions: example: 1 type: integer deliver_refer_date: - example: "2025-01-02 15:04:05" + example: "2025-01-02T15:04:05Z" type: string + deliver_refer_time_id: + example: 1 + type: integer type_id: allOf: - $ref: '#/definitions/entity.KindBoxType' @@ -426,6 +429,8 @@ definitions: type: integer deliverReferDate: type: string + deliverReferTimeID: + type: integer deliveredAt: type: string description: diff --git a/entity/kind_box_req.go b/entity/kind_box_req.go index 0e233f8..cf46fcc 100644 --- a/entity/kind_box_req.go +++ b/entity/kind_box_req.go @@ -3,15 +3,16 @@ package entity import "time" type KindBoxReq struct { - ID uint - BenefactorID uint - KindBoxType KindBoxType - CountRequested uint - CountAccepted uint - Description string - Status KindBoxReqStatus - DeliverReferDate time.Time - DeliverAddressID uint - SenderAgentID uint - DeliveredAt time.Time + ID uint + BenefactorID uint + KindBoxType KindBoxType + CountRequested uint + CountAccepted uint + Description string + Status KindBoxReqStatus + DeliverReferTimeID uint + DeliverReferDate time.Time + DeliverAddressID uint + SenderAgentID uint + DeliveredAt time.Time } diff --git a/entity/refer_time.go b/entity/refer_time.go new file mode 100644 index 0000000..4368863 --- /dev/null +++ b/entity/refer_time.go @@ -0,0 +1,7 @@ +package entity + +type ReferTime struct { + ID uint + Duration string + Status ReferTimeStatus +} diff --git a/entity/refer_time_status.go b/entity/refer_time_status.go new file mode 100644 index 0000000..809d34b --- /dev/null +++ b/entity/refer_time_status.go @@ -0,0 +1,42 @@ +package entity + +type ReferTimeStatus uint + +const ( + ReferTimeActiveStatus ReferTimeStatus = iota + 1 + ReferTimeInactiveStatus +) + +var ReferTimeStatusStrings = map[ReferTimeStatus]string{ + ReferTimeActiveStatus: "active", + ReferTimeInactiveStatus: "inactive", +} + +func (s ReferTimeStatus) String() string { + return ReferTimeStatusStrings[s] +} + +func (s ReferTimeStatus) IsValid() bool { + return s > 0 && int(s) <= len(ReferTimeStatusStrings) +} + +// AllReferTimeStatus returns a slice containing all string values of ReferTimeStatus. +func AllReferTimeStatus() []string { + statusStrings := make([]string, len(ReferTimeStatusStrings)) + for status, str := range ReferTimeStatusStrings { + statusStrings[int(status)-1] = str + } + + return statusStrings +} + +// MapToReferTimeStatus converts a string to the corresponding ReferTimeStatus value. +func MapToReferTimeStatus(statusStr string) ReferTimeStatus { + for status, str := range ReferTimeStatusStrings { + if str == statusStr { + return status + } + } + + return ReferTimeStatus(0) +} diff --git a/internal/initial/database.go b/internal/initial/database.go index 0a7be3f..2f96eaa 100644 --- a/internal/initial/database.go +++ b/internal/initial/database.go @@ -7,6 +7,7 @@ import ( mysqladmin "git.gocasts.ir/ebhomengo/niki/repository/mysql/admin" mysqlkindbox "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box" mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req" + mysqlrefertime "git.gocasts.ir/ebhomengo/niki/repository/mysql/refer_time" ) type Databases struct { @@ -35,3 +36,7 @@ func InitKindBoxRepo(db *mysql.DB) *mysqlkindbox.DB { func InitAdminMysql(db *mysql.DB) *mysqladmin.DB { return mysqladmin.New(db) } + +func InitAdminReferTimeDB(db *mysql.DB) *mysqlrefertime.DB { + return mysqlrefertime.New(db) +} diff --git a/internal/initial/service.go b/internal/initial/service.go index 596bd33..3a7d6a4 100644 --- a/internal/initial/service.go +++ b/internal/initial/service.go @@ -13,6 +13,7 @@ import ( adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" + adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" @@ -25,6 +26,7 @@ type Services struct { AdminKindBoxSvc adminkindboxservice.Service AdminSvc adminservice.Service AdminKindBoxReqSvc adminkindboxreqservice.Service + AdminReferTimeSvc adminrefertimeservice.Service } func initSms(cfg config.Config) *kavenegarotp.Adapter { @@ -60,3 +62,8 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service { func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service { return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db)) } +func InitAdminReferTimeService(db *mysql.DB) adminrefertimeservice.Service { + return adminrefertimeservice.New( + InitAdminReferTimeDB(db), + ) +} diff --git a/internal/initial/validator.go b/internal/initial/validator.go index b43525f..bc898e4 100644 --- a/internal/initial/validator.go +++ b/internal/initial/validator.go @@ -35,6 +35,7 @@ func InitBenefactorKindBoxReqValidator(cfg config.Config, redisAdapter redis.Ada return benefactorkindboxreqvalidator.New( InitBenefactorService(cfg, redisAdapter, db), InitBenefactorAddressService(db), + InitAdminReferTimeService(db), InitBenefactorKindBoxReqDB(db), ) } diff --git a/main.go b/main.go index 1170cb8..be5e53a 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D AdminKindBoxSvc: initial.InitAdminKindBoxService(db), AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminSvc: initial.InitAdminService(cfg, db), + AdminReferTimeSvc: initial.InitAdminReferTimeService(db), }, initial.AdminAuthorization{ AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db), diff --git a/param/admin/refer_time/get.go b/param/admin/refer_time/get.go new file mode 100644 index 0000000..dda62f5 --- /dev/null +++ b/param/admin/refer_time/get.go @@ -0,0 +1,11 @@ +package param + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type GetReferTimeRequest struct { + ReferTimeID uint +} + +type GetReferTimeResponse struct { + ReferTime entity.ReferTime +} diff --git a/param/benefactor/kind_box_req/add.go b/param/benefactor/kind_box_req/add.go index c45f8b0..9a7f994 100644 --- a/param/benefactor/kind_box_req/add.go +++ b/param/benefactor/kind_box_req/add.go @@ -2,14 +2,16 @@ package benefactorkindboxreqparam import ( entity "git.gocasts.ir/ebhomengo/niki/entity" + "time" ) type KindBoxReqAddRequest struct { - BenefactorID uint `json:"benefactor_id" example:"1"` - TypeID entity.KindBoxType `json:"type_id" example:"1"` - DeliverAddressID uint `json:"deliver_address_id" example:"1"` - DeliverReferDate string `json:"deliver_refer_date" example:"2025-01-02 15:04:05"` - CountRequested uint `json:"count_requested" example:"2"` + BenefactorID uint `json:"benefactor_id" example:"1"` + TypeID entity.KindBoxType `json:"type_id" example:"1"` + DeliverAddressID uint `json:"deliver_address_id" example:"1"` + DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"` + DeliverReferTimeID uint `json:"deliver_refer_time_id" example:"1"` + CountRequested uint `json:"count_requested" example:"2"` } type KindBoxReqAddResponse struct { diff --git a/repository/mysql/kind_box_req/delete.go b/repository/mysql/kind_box_req/delete.go index 6e36534..3d62192 100644 --- a/repository/mysql/kind_box_req/delete.go +++ b/repository/mysql/kind_box_req/delete.go @@ -9,7 +9,7 @@ import ( func (d DB) DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error { const op = richerror.Op("mysqlkindboxreq.DeleteKindBoxReqByID") - _, dErr := d.conn.Conn().ExecContext(ctx, "update kind_box_reqs set deleted_at = ? where id = ? ", time.Now(), kindBoxReqID) + _, dErr := d.conn.Conn().ExecContext(ctx, "update kind_box_reqs set deleted_at = ? where id = ? and deleted_at is null", time.Now(), kindBoxReqID) if dErr != nil { return richerror.New(op).WithErr(dErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/migration/1705399371_create_refer_times_table.sql b/repository/mysql/migration/1705399371_create_refer_times_table.sql new file mode 100644 index 0000000..b6e571b --- /dev/null +++ b/repository/mysql/migration/1705399371_create_refer_times_table.sql @@ -0,0 +1,13 @@ +-- +migrate Up +CREATE TABLE `refer_times` ( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `duration` VARCHAR(191) NOT NULL, + `status` ENUM('active','inactive') NOT NULL DEFAULT 'active', + + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted_at` TIMESTAMP +); + +-- +migrate Down +DROP TABLE `refer_times`; \ No newline at end of file diff --git a/repository/mysql/migration/1705400111_create_kind_box_reqs_table.sql b/repository/mysql/migration/1705400111_create_kind_box_reqs_table.sql index c1e6970..a9edee0 100644 --- a/repository/mysql/migration/1705400111_create_kind_box_reqs_table.sql +++ b/repository/mysql/migration/1705400111_create_kind_box_reqs_table.sql @@ -7,6 +7,7 @@ CREATE TABLE `kind_box_reqs` ( `count_accepted` INT UNSIGNED, `description` TEXT, `status` ENUM('pending','accepted','assigned-sender-agent','rejected','delivered') NOT NULL DEFAULT 'pending', + `deliver_refer_time_id` INT NOT NULL, `deliver_refer_date` DATETIME NOT NULL, `deliver_address_id` INT NOT NULL, `sender_agent_id` INT, @@ -15,9 +16,10 @@ CREATE TABLE `kind_box_reqs` ( `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `deleted_at` TIMESTAMP, - FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`), - FOREIGN KEY (`deliver_address_id`) REFERENCES `addresses` (`id`), - FOREIGN KEY (`sender_agent_id`) REFERENCES `admins` (`id`) + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`), + FOREIGN KEY (`deliver_refer_time_id`) REFERENCES `refer_times` (`id`), + FOREIGN KEY (`deliver_address_id`) REFERENCES `addresses` (`id`), + FOREIGN KEY (`sender_agent_id`) REFERENCES `admins` (`id`) ); -- +migrate Down diff --git a/repository/mysql/migration/1708712575_insert_refer_times_table.sql b/repository/mysql/migration/1708712575_insert_refer_times_table.sql new file mode 100644 index 0000000..8b6abfe --- /dev/null +++ b/repository/mysql/migration/1708712575_insert_refer_times_table.sql @@ -0,0 +1,16 @@ +-- +migrate Up +INSERT INTO `refer_times` (`id`,`duration`,`status`) + VALUES + (DEFAULT,'8-10 am',DEFAULT), + (DEFAULT,'10-12 am',DEFAULT), + (DEFAULT,'12-2 pm',DEFAULT), + (DEFAULT,'2-4 pm',DEFAULT), + (DEFAULT,'4-6 pm',DEFAULT), + (DEFAULT,'6-8 pm',DEFAULT), + (DEFAULT,'8-10 pm',DEFAULT), + (DEFAULT,'10-12 pm',DEFAULT); + +-- +migrate Down +DELETE + FROM `refer_times` + WHERE id BETWEEN 1 AND 8; \ No newline at end of file diff --git a/repository/mysql/refer_time/db.go b/repository/mysql/refer_time/db.go new file mode 100644 index 0000000..17b34db --- /dev/null +++ b/repository/mysql/refer_time/db.go @@ -0,0 +1,13 @@ +package mysqlrefertime + +import "git.gocasts.ir/ebhomengo/niki/repository/mysql" + +type DB struct { + conn *mysql.DB +} + +func New(conn *mysql.DB) *DB { + return &DB{ + conn: conn, + } +} diff --git a/repository/mysql/refer_time/get.go b/repository/mysql/refer_time/get.go new file mode 100644 index 0000000..a4d7668 --- /dev/null +++ b/repository/mysql/refer_time/get.go @@ -0,0 +1,20 @@ +package mysqlrefertime + +import ( + "context" + "git.gocasts.ir/ebhomengo/niki/entity" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) { + const op = richerror.Op("mysqlrefertime.Get") + + row := d.conn.Conn().QueryRowContext(ctx, `select * from refer_times where id = ?`, referTimeID) + r, err := scanReferTime(row) + if err != nil { + return entity.ReferTime{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + return r, nil +} diff --git a/repository/mysql/refer_time/scan.go b/repository/mysql/refer_time/scan.go new file mode 100644 index 0000000..480b3bc --- /dev/null +++ b/repository/mysql/refer_time/scan.go @@ -0,0 +1,31 @@ +package mysqlrefertime + +import ( + "database/sql" + "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/repository/mysql" + "time" +) + +func scanReferTime(scanner mysql.Scanner) (entity.ReferTime, error) { + var referTime entity.ReferTime + var ( + status string + createdAt time.Time + updatedAt time.Time + deletedAt sql.NullTime + ) + err := scanner.Scan( + &referTime.ID, + &referTime.Duration, + &status, + &createdAt, + &updatedAt, + &deletedAt, + ) + if err != nil { + return entity.ReferTime{}, err + } + referTime.Status = entity.MapToReferTimeStatus(status) + return referTime, nil +} diff --git a/service/admin/refer_time/get.go b/service/admin/refer_time/get.go new file mode 100644 index 0000000..1ba10ce --- /dev/null +++ b/service/admin/refer_time/get.go @@ -0,0 +1,16 @@ +package adminrefertimeservice + +import ( + "context" + param "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) GetReferTimeByID(ctx context.Context, req param.GetReferTimeRequest) (param.GetReferTimeResponse, error) { + const op = richerror.Op("refertimeservice.GetReferTimeByID") + referTime, gErr := s.repo.Get(ctx, req.ReferTimeID) + if gErr != nil { + return param.GetReferTimeResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) + } + return param.GetReferTimeResponse{ReferTime: referTime}, nil +} diff --git a/service/admin/refer_time/service.go b/service/admin/refer_time/service.go new file mode 100644 index 0000000..740f882 --- /dev/null +++ b/service/admin/refer_time/service.go @@ -0,0 +1,17 @@ +package adminrefertimeservice + +import ( + "context" + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type Service struct { + repo Repository +} +type Repository interface { + Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error) +} + +func New(repo Repository) Service { + return Service{repo: repo} +} diff --git a/service/benefactor/kind_box_req/add.go b/service/benefactor/kind_box_req/add.go index 38f4a15..2da2af6 100644 --- a/service/benefactor/kind_box_req/add.go +++ b/service/benefactor/kind_box_req/add.go @@ -2,8 +2,6 @@ package benefactorkindboxreqservice import ( "context" - "time" - "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -11,17 +9,15 @@ import ( func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { const op = "userkindboxreqservice.Add" - t, tErr := time.Parse(time.DateTime, req.DeliverReferDate) - if tErr != nil { - return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(tErr).WithKind(richerror.KindInvalid) - } + kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ - BenefactorID: req.BenefactorID, - KindBoxType: req.TypeID, - DeliverAddressID: req.DeliverAddressID, - DeliverReferDate: t, - CountRequested: req.CountRequested, - Status: entity.KindBoxReqPendingStatus, + BenefactorID: req.BenefactorID, + KindBoxType: req.TypeID, + DeliverAddressID: req.DeliverAddressID, + DeliverReferDate: req.DeliverReferDate, + DeliverReferTimeID: req.DeliverReferTimeID, + CountRequested: req.CountRequested, + Status: entity.KindBoxReqPendingStatus, }) if err != nil { return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index aa94a51..f37c402 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -8,7 +8,7 @@ import ( validation "github.com/go-ozzo/ozzo-validation/v4" ) -func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) *ValidatorError { +func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { const op = "userkindboxreqvalidator.ValidateAddRequest" if err := validation.ValidateStruct(&req, @@ -31,6 +31,10 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) *Validator validation.Required, validation.By(v.isDateValid), ), + validation.Field(&req.DeliverReferTimeID, + validation.Required, + validation.By(v.isReferTimeIDValid), + ), ); err != nil { fieldErrors := make(map[string]string) @@ -44,16 +48,13 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) *Validator } } - return &ValidatorError{ - Fields: fieldErrors, - Err: richerror.New(op). - WithMessage(errmsg.ErrorMsgInvalidInput). - WithKind(richerror.KindInvalid). - WithMeta(map[string]interface{}{"req": req}). - WithErr(err), - } + return fieldErrors, richerror.New(op). + WithMessage(errmsg.ErrorMsgInvalidInput). + WithKind(richerror.KindInvalid). + WithMeta(map[string]interface{}{"req": req}). + WithErr(err) } - return nil + return map[string]string{}, nil } diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index 8a1548e..feb1609 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "git.gocasts.ir/ebhomengo/niki/entity" + refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -24,6 +25,10 @@ type AddressSvc interface { AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) } +type ReferTimeSvc interface { + GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) +} + type Repository interface { GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) } @@ -31,6 +36,7 @@ type Repository interface { type Validator struct { benefactorSvc BenefactorSvc addressSvc AddressSvc + referTimeSvc ReferTimeSvc repo Repository } @@ -49,8 +55,8 @@ func (v ValidatorError) Error() string { return err } -func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, repo Repository) Validator { - return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, repo: repo} +func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, referTimeSvc ReferTimeSvc,repo Repository) Validator { + return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, referTimeSvc: referTimeSvc,repo:repo} } func (v Validator) doesBenefactorExist(value interface{}) error { @@ -100,21 +106,34 @@ func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc { } func (v Validator) isDateValid(value interface{}) error { - date, ok := value.(string) + date, ok := value.(time.Time) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - t, err := time.Parse(time.DateTime, date) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgInvalidInput) - } - if t.Before(time.Now()) { + if date.Before(time.Now()) { return fmt.Errorf(errmsg.ErrorMsgInvalidInput) } return nil } +func (v Validator) isReferTimeIDValid(value interface{}) error { + referTimeID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + resp, gErr := v.referTimeSvc.GetReferTimeByID(context.Background(), refertimeparam.GetReferTimeRequest{ + ReferTimeID: referTimeID, + }) + if gErr != nil { + return fmt.Errorf(errmsg.ErrorMsgReferTimeNotFound) + } + if resp.ReferTime.Status != entity.ReferTimeActiveStatus { + return fmt.Errorf(errmsg.ErrorMsgReferTimeIsNotActive) + } + return nil +} + func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { kindBoxReqID, ok := value.(uint)