From 15698b18f95ef04c5c61c5ceb3c64775daee358c Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Wed, 10 Jul 2024 04:51:03 +0330 Subject: [PATCH] fix(niki): update kindbox request by admin --- docs/docs.go | 89 +++++++++++++++++++++++ docs/swagger.json | 89 +++++++++++++++++++++++ docs/swagger.yaml | 58 +++++++++++++++ internal/initial/validator.go | 2 +- param/admin/kind_box_req/update.go | 18 ++--- repository/mysql/kind_box_req/update.go | 31 ++++++-- service/admin/kind_box_req/update.go | 28 +++---- validator/admin/kind_box_req/add.go | 8 +- validator/admin/kind_box_req/update.go | 32 +++++--- validator/admin/kind_box_req/validator.go | 77 +++++++++++++++++++- 10 files changed, 382 insertions(+), 50 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index f38ad43..9dbb080 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1007,6 +1007,54 @@ const docTemplate = `{ } } }, + "/admin/kindboxreqs/{id}": { + "put": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "Update kind box request by admin", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Update KindBoxReq Request Body", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqUpdateRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admins/agents": { "get": { "security": [ @@ -1986,6 +2034,47 @@ const docTemplate = `{ } } }, + "adminkindboxreqparam.KindBoxReqUpdateRequest": { + "type": "object", + "properties": { + "count_accepted": { + "type": "integer", + "example": 3 + }, + "count_requested": { + "type": "integer", + "example": 5 + }, + "deliver_address_id": { + "type": "integer", + "example": 1 + }, + "deliver_refer_date": { + "type": "string", + "example": "2025-01-02T15:04:05Z" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 + }, + "description": { + "type": "string", + "example": "description" + }, + "kind_box_type": { + "allOf": [ + { + "$ref": "#/definitions/entity.KindBoxType" + } + ], + "example": 2 + }, + "sender_agent_id": { + "type": "integer", + "example": 1 + } + } + }, "adminserviceparam.AdminInfo": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 7561b6a..32d90b1 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -996,6 +996,54 @@ } } }, + "/admin/kindboxreqs/{id}": { + "put": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "Update kind box request by admin", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Update KindBoxReq Request Body", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqUpdateRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admins/agents": { "get": { "security": [ @@ -1975,6 +2023,47 @@ } } }, + "adminkindboxreqparam.KindBoxReqUpdateRequest": { + "type": "object", + "properties": { + "count_accepted": { + "type": "integer", + "example": 3 + }, + "count_requested": { + "type": "integer", + "example": 5 + }, + "deliver_address_id": { + "type": "integer", + "example": 1 + }, + "deliver_refer_date": { + "type": "string", + "example": "2025-01-02T15:04:05Z" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 + }, + "description": { + "type": "string", + "example": "description" + }, + "kind_box_type": { + "allOf": [ + { + "$ref": "#/definitions/entity.KindBoxType" + } + ], + "example": 2 + }, + "sender_agent_id": { + "type": "integer", + "example": 1 + } + } + }, "adminserviceparam.AdminInfo": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 64081bf..b98638b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -254,6 +254,34 @@ definitions: - $ref: '#/definitions/entity.KindBoxReqStatus' example: 1 type: object + adminkindboxreqparam.KindBoxReqUpdateRequest: + properties: + count_accepted: + example: 3 + type: integer + count_requested: + example: 5 + type: integer + deliver_address_id: + example: 1 + type: integer + deliver_refer_date: + example: "2025-01-02T15:04:05Z" + type: string + deliver_refer_time_id: + example: 1 + type: integer + description: + example: description + type: string + kind_box_type: + allOf: + - $ref: '#/definitions/entity.KindBoxType' + example: 2 + sender_agent_id: + example: 1 + type: integer + type: object adminserviceparam.AdminInfo: properties: description: @@ -1122,6 +1150,36 @@ paths: summary: Add a new kind box request for a benefactor by admin tags: - KindBoxReq + /admin/kindboxreqs/{id}: + put: + consumes: + - application/json + parameters: + - description: KindBoxReq ID + in: path + name: id + required: true + type: integer + - description: Update KindBoxReq Request Body + in: body + name: Request + required: true + schema: + $ref: '#/definitions/adminkindboxreqparam.KindBoxReqUpdateRequest' + produces: + - application/json + responses: + "204": + description: No Content + "400": + description: Bad request + schema: + type: string + security: + - AuthBearerAdmin: [] + summary: Update kind box request by admin + tags: + - KindBoxReq /admin/kindboxreqs/accept-kind-box-req/{id}: patch: consumes: diff --git a/internal/initial/validator.go b/internal/initial/validator.go index 52d5da5..8455e79 100644 --- a/internal/initial/validator.go +++ b/internal/initial/validator.go @@ -24,7 +24,7 @@ type Validators struct { } func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator { - return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(db)) + return adminkindboxreqvalidator.New(InitBenefactorKindBoxReqDB(db), InitAdminService(cfg, db), InitBenefactorForAdminService(db), InitAdminReferTimeService(db), InitBenefactorAddressService(db)) } func InitAdminValidator(db *mysql.DB) adminvalidator.Validator { diff --git a/param/admin/kind_box_req/update.go b/param/admin/kind_box_req/update.go index 5eb93b2..b9e1efd 100644 --- a/param/admin/kind_box_req/update.go +++ b/param/admin/kind_box_req/update.go @@ -7,13 +7,13 @@ import ( ) type KindBoxReqUpdateRequest struct { - ID uint `json:"-" param:"id"` - KindBoxType entity.KindBoxType `json:"kind_box_type"` - CountRequested uint `json:"count_requested"` - CountAccepted uint `json:"count_accepted"` - Description string `json:"description"` - DeliverReferTimeID uint `json:"deliver_refer_time_id"` - DeliverReferDate time.Time `json:"deliver_refer_date"` - DeliverAddressID uint `json:"deliver_address_id"` - SenderAgentID uint `json:"sender_agent_id"` + ID uint `json:"-" param:"id" example:"1"` + KindBoxType entity.KindBoxType `json:"kind_box_type" example:"2"` + CountRequested uint `json:"count_requested" example:"5"` + CountAccepted uint `json:"count_accepted" example:"3"` + Description string `json:"description" example:"description"` + DeliverReferTimeID uint `json:"deliver_refer_time_id" example:"1"` + DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"` + DeliverAddressID uint `json:"deliver_address_id" example:"1"` + SenderAgentID uint `json:"sender_agent_id" example:"1"` } diff --git a/repository/mysql/kind_box_req/update.go b/repository/mysql/kind_box_req/update.go index db77ce6..dcc9e36 100644 --- a/repository/mysql/kind_box_req/update.go +++ b/repository/mysql/kind_box_req/update.go @@ -2,6 +2,8 @@ package mysqlkindboxreq import ( "context" + "database/sql" + "time" "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -11,12 +13,31 @@ import ( func (d DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error { const op = "mysqlkindboxreq.UpdateKindBoxReq" - query := `UPDATE kind_box_reqs - SET benfactor_id = ?, kind_box_type = ?, count_requested = ?, count_accepted = ?, description = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?, deliver_address_id + ?, sender_agent_id = ?, delivered_at = ? - WHERE id = ? AND deleted_at IS NULL` - _, uErr := d.conn.Conn().ExecContext(ctx, query, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType, kindBoxReq.CountRequested, kindBoxReq.CountAccepted, kindBoxReq.Description, kindBoxReq.Status, kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverAddressID, kindBoxReq.SenderAgentID, kindBoxReq.DeliveredAt) - if uErr != nil { + var ( + senderAgentID sql.NullInt64 + deliveredAt sql.NullTime + ) + if kindBoxReq.SenderAgentID != 0 { + senderAgentID = sql.NullInt64{Int64: int64(kindBoxReq.SenderAgentID), Valid: true} + } else { + senderAgentID = sql.NullInt64{Int64: 0, Valid: false} + } + if !kindBoxReq.DeliveredAt.IsZero() { + deliveredAt = sql.NullTime{Time: kindBoxReq.DeliveredAt, Valid: true} + } else { + deliveredAt = sql.NullTime{Time: time.Time{}, Valid: false} + } + query := `UPDATE kind_box_reqs + SET benefactor_id = ?, kind_box_type = ?, count_requested = ?, count_accepted = ?, + description = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?, + deliver_address_id = ?, sender_agent_id = ?, delivered_at = ? + WHERE id = ? AND deleted_at IS NULL` + _, uErr := d.conn.Conn().ExecContext(ctx, query, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType, + kindBoxReq.CountRequested, kindBoxReq.CountAccepted, kindBoxReq.Description, kindBoxReq.Status, + kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverAddressID, + senderAgentID, deliveredAt, kindBoxReq.ID) + if uErr != nil { return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord). WithKind(richerror.KindUnexpected) } diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go index 1be9fba..52f3ea4 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -3,7 +3,6 @@ package adminkindboxreqservice import ( "context" - "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" ) @@ -15,22 +14,19 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) if err != nil { return richerror.New(op).WithErr(err) } - uErr := s.repo.UpdateKindBoxReq(ctx, entity.KindBoxReq{ - ID: req.ID, - BenefactorID: kindBoxReq.BenefactorID, - KindBoxType: req.KindBoxType, - CountRequested: req.CountRequested, - CountAccepted: req.CountAccepted, - Description: req.Description, - Status: kindBoxReq.Status, - DeliverReferTimeID: req.DeliverReferTimeID, - DeliverReferDate: req.DeliverReferDate, - DeliverAddressID: req.DeliverAddressID, - SenderAgentID: req.SenderAgentID, - DeliveredAt: kindBoxReq.DeliveredAt, - }) + + kindBoxReq.KindBoxType = req.KindBoxType + kindBoxReq.CountRequested = req.CountRequested + kindBoxReq.CountAccepted = req.CountAccepted + kindBoxReq.Description = req.Description + kindBoxReq.DeliverReferTimeID = req.DeliverReferTimeID + kindBoxReq.DeliverReferDate = req.DeliverReferDate + kindBoxReq.DeliverAddressID = req.DeliverAddressID + kindBoxReq.SenderAgentID = req.SenderAgentID + + uErr := s.repo.UpdateKindBoxReq(ctx, kindBoxReq) if uErr != nil { - return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + return richerror.New(op).WithErr(err) } return nil diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index 79295e4..a000661 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -111,15 +111,11 @@ 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) } diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index cd104a4..dbaadbc 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -14,29 +14,39 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map if err := validation.ValidateStruct(&req, - validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist)), - - validation.Field(&req.CountRequested, validation.Required, validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq))), - + validation.Field(&req.ID, + validation.Required, + validation.By(v.doesKindBoxRequestExist), + ), + validation.Field(&req.CountRequested, + validation.Required, + validation.Min(uint(MinKindBoxReq)), + validation.Max(uint(MaxKindBoxReq)), + ), + validation.Field(&req.CountAccepted, + validation.Min(uint(MinKindBoxReq)), + validation.Max(uint(MaxKindBoxReq)), + validation.When(req.CountRequested > 0, validation.Max(uint(req.CountRequested))), + validation.By(v.checkCountAcceptedMustBeLessThanCountRequested(req.ID)), + ), validation.Field(&req.KindBoxType, validation.Required, - validation.By(v.doesTypeExist)), - + validation.By(v.doesTypeExist), + ), validation.Field(&req.DeliverAddressID, validation.Required, - validation.By(v.doesAddressExist(req.BenefactorID))), - + validation.By(v.doesBenefactorAddressExist(req.ID)), + ), validation.Field(&req.DeliverReferDate, validation.Required, validation.By(v.isDateValid), ), validation.Field(&req.DeliverReferTimeID, validation.Required, - validation.By(v.isDateValid), + validation.By(v.isReferTimeIDValid), ), validation.Field(&req.SenderAgentID, - validation.Required, - validation.By(v.isDateValid), + validation.When(req.SenderAgentID > 0, validation.By(v.doesAgentExist)), ), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 923c189..28d532e 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -9,6 +9,8 @@ import ( "git.gocasts.ir/ebhomengo/niki/entity" params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" benefactorsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor" validation "github.com/go-ozzo/ozzo-validation/v4" @@ -26,16 +28,27 @@ type Repository interface { type AdminSvc interface { AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error) + AgentExistByID(ctx context.Context, agentID uint) (bool, error) +} + +type ReferTimeSvc interface { + GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error) +} + +type AddressSvc interface { + AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) } type Validator struct { repo Repository adminSvc AdminSvc benefactorSvc benefactorsvc.Service + referTimeSvc ReferTimeSvc + addressSvc AddressSvc } -func New(repo Repository, adminSvc AdminSvc, benefactorSvc benefactorsvc.Service) Validator { - return Validator{repo: repo, adminSvc: adminSvc, benefactorSvc: benefactorSvc} +func New(repo Repository, adminSvc AdminSvc, benefactorSvc benefactorsvc.Service, referTimeSvc ReferTimeSvc, addressSvc AddressSvc) Validator { + return Validator{repo: repo, adminSvc: adminSvc, benefactorSvc: benefactorSvc, referTimeSvc: referTimeSvc, addressSvc: addressSvc} } func (v Validator) doesKindBoxRequestExist(value interface{}) error { @@ -187,3 +200,63 @@ func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleF 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) doesAgentExist(value interface{}) error { + agentID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + exists, err := v.adminSvc.AgentExistByID(context.Background(), agentID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if !exists { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesBenefactorAddressExist(kindBoxReqID uint) validation.RuleFunc { + return func(value interface{}) error { + addressID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + + kindBoxReq, err := v.repo.GetByID(context.Background(), kindBoxReqID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + + address, aErr := v.addressSvc.AddressExistByID(context.Background(), addressparam.GetAddressByIDRequest{ID: addressID}) + if aErr != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if address.Address == nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + if address.Address.BenefactorID != kindBoxReq.BenefactorID { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil + } +}