From 07033199287f95d8bba2b0f2af72ee4d30208b61 Mon Sep 17 00:00:00 2001 From: Ruhollah Date: Sun, 15 Sep 2024 23:47:26 +0330 Subject: [PATCH] fix(niki): fix create and update kind-box request by admin internal errors (#167 and #168) --- docs/docs.go | 6 +++++- docs/swagger.json | 6 +++++- docs/swagger.yaml | 5 ++++- param/admin/kind_box_req/add.go | 16 ++++++++++------ repository/mysql/kind_box_req/add.go | 3 +-- service/admin/kind_box_req/add.go | 19 +++++++------------ validator/admin/kind_box_req/add.go | 6 ++++++ validator/admin/kind_box_req/update.go | 4 ++-- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 4ca78a4..ba10048 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -3021,7 +3021,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 }, "kind_box_type": { "allOf": [ diff --git a/docs/swagger.json b/docs/swagger.json index b1559fd..e8c878b 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -3010,7 +3010,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 }, "kind_box_type": { "allOf": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 98c9827..b735c50 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -327,8 +327,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 kind_box_type: allOf: - $ref: '#/definitions/entity.KindBoxType' diff --git a/param/admin/kind_box_req/add.go b/param/admin/kind_box_req/add.go index 42db343..3c94ebe 100644 --- a/param/admin/kind_box_req/add.go +++ b/param/admin/kind_box_req/add.go @@ -1,13 +1,17 @@ package adminkindboxreqparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + entity "git.gocasts.ir/ebhomengo/niki/entity" + "time" +) type KindBoxReqAddRequest struct { - BenefactorID uint `json:"benefactor_id" example:"1"` - KindBoxType entity.KindBoxType `json:"kind_box_type" example:"on-table"` - 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"` + KindBoxType entity.KindBoxType `json:"kind_box_type" example:"on-table"` + 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/add.go b/repository/mysql/kind_box_req/add.go index 6e61f3c..1a88ee6 100644 --- a/repository/mysql/kind_box_req/add.go +++ b/repository/mysql/kind_box_req/add.go @@ -2,7 +2,6 @@ package mysqlkindboxreq 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" @@ -26,7 +25,7 @@ func (d *DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (e kindBoxReq.DeliverReferTimeID, kindBoxReq.Status) if err != nil { return entity.KindBoxReq{}, richerror.New(op).WithErr(err). - WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) + WithKind(richerror.KindUnexpected) } //nolint diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index 1d856aa..e94edc8 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -2,8 +2,6 @@ package adminkindboxreqservice import ( "context" - "time" - "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -14,17 +12,14 @@ func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param if fieldErrors, vErr := s.vld.ValidateAddRequest(ctx, req); vErr != nil { return param.KindBoxReqAddResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } - date, 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.KindBoxType, - DeliverAddressID: req.DeliverAddressID, - DeliverReferDate: date, - CountRequested: req.CountRequested, - Status: entity.KindBoxReqPendingStatus, + BenefactorID: req.BenefactorID, + KindBoxType: req.KindBoxType, + 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/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index db7f507..d1594a9 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -38,6 +38,11 @@ func (v Validator) ValidateAddRequest(ctx context.Context, req param.KindBoxReqA validation.Required, validation.By(v.isDateValid), ), + + validation.Field(&req.DeliverReferTimeID, + validation.Required, + validation.By(v.isReferTimeIDValid(ctx)), + ), ); err != nil { fieldErrors := make(map[string]string) @@ -112,6 +117,7 @@ func (v Validator) isDateValid(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } + if date.Before(time.Now()) { return fmt.Errorf(errmsg.ErrorMsgInvalidInput) } diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index 8a12027..0b8764d 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -25,8 +25,8 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxR validation.Max(uint(MaxKindBoxReq)), ), validation.Field(&req.CountAccepted, - validation.Min(MinKindBoxReq), - validation.Max(MaxKindBoxReq), + validation.Min(uint(MinKindBoxReq)), + validation.Max(uint(MaxKindBoxReq)), validation.When(req.CountRequested > 0, validation.Max(req.CountRequested)), validation.By(v.checkCountAcceptedMustBeLessThanCountRequested(ctx, req.ID)), ),