forked from ebhomengo/niki
1
0
Fork 0

Merge pull request 'fix(niki): fix create and update kind-box request by admin internal errors (#167 and #168)' (#172) from stage/ruhollahh01/fix-create-and-update-kindbox-req-by-admin into develop

Reviewed-on: ebhomengo/niki#172
This commit is contained in:
hossein 2024-09-16 04:48:55 +00:00
commit a6fd4e2357
8 changed files with 40 additions and 25 deletions

View File

@ -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": [

View File

@ -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": [

View File

@ -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'

View File

@ -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 {

View File

@ -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

View File

@ -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)

View File

@ -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)
}

View File

@ -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)),
),