fix(niki): update kindbox request by admin

This commit is contained in:
Erfan Mohammadi 2024-07-10 04:51:03 +03:30
parent ecb3e9aedc
commit 15698b18f9
10 changed files with 382 additions and 50 deletions

View File

@ -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": { "/admins/agents": {
"get": { "get": {
"security": [ "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": { "adminserviceparam.AdminInfo": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -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": { "/admins/agents": {
"get": { "get": {
"security": [ "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": { "adminserviceparam.AdminInfo": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -254,6 +254,34 @@ definitions:
- $ref: '#/definitions/entity.KindBoxReqStatus' - $ref: '#/definitions/entity.KindBoxReqStatus'
example: 1 example: 1
type: object 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: adminserviceparam.AdminInfo:
properties: properties:
description: description:
@ -1122,6 +1150,36 @@ paths:
summary: Add a new kind box request for a benefactor by admin summary: Add a new kind box request for a benefactor by admin
tags: tags:
- KindBoxReq - 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}: /admin/kindboxreqs/accept-kind-box-req/{id}:
patch: patch:
consumes: consumes:

View File

@ -24,7 +24,7 @@ type Validators struct {
} }
func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator { 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 { func InitAdminValidator(db *mysql.DB) adminvalidator.Validator {

View File

@ -7,13 +7,13 @@ import (
) )
type KindBoxReqUpdateRequest struct { type KindBoxReqUpdateRequest struct {
ID uint `json:"-" param:"id"` ID uint `json:"-" param:"id" example:"1"`
KindBoxType entity.KindBoxType `json:"kind_box_type"` KindBoxType entity.KindBoxType `json:"kind_box_type" example:"2"`
CountRequested uint `json:"count_requested"` CountRequested uint `json:"count_requested" example:"5"`
CountAccepted uint `json:"count_accepted"` CountAccepted uint `json:"count_accepted" example:"3"`
Description string `json:"description"` Description string `json:"description" example:"description"`
DeliverReferTimeID uint `json:"deliver_refer_time_id"` DeliverReferTimeID uint `json:"deliver_refer_time_id" example:"1"`
DeliverReferDate time.Time `json:"deliver_refer_date"` DeliverReferDate time.Time `json:"deliver_refer_date" example:"2025-01-02T15:04:05Z"`
DeliverAddressID uint `json:"deliver_address_id"` DeliverAddressID uint `json:"deliver_address_id" example:"1"`
SenderAgentID uint `json:"sender_agent_id"` SenderAgentID uint `json:"sender_agent_id" example:"1"`
} }

View File

@ -2,6 +2,8 @@ package mysqlkindboxreq
import ( import (
"context" "context"
"database/sql"
"time"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" 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 { func (d DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error {
const op = "mysqlkindboxreq.UpdateKindBoxReq" const op = "mysqlkindboxreq.UpdateKindBoxReq"
query := `UPDATE kind_box_reqs var (
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 = ? senderAgentID sql.NullInt64
WHERE id = ? AND deleted_at IS NULL` deliveredAt sql.NullTime
_, 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 { 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). return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
WithKind(richerror.KindUnexpected) WithKind(richerror.KindUnexpected)
} }

View File

@ -3,7 +3,6 @@ package adminkindboxreqservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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 { if err != nil {
return richerror.New(op).WithErr(err) return richerror.New(op).WithErr(err)
} }
uErr := s.repo.UpdateKindBoxReq(ctx, entity.KindBoxReq{
ID: req.ID, kindBoxReq.KindBoxType = req.KindBoxType
BenefactorID: kindBoxReq.BenefactorID, kindBoxReq.CountRequested = req.CountRequested
KindBoxType: req.KindBoxType, kindBoxReq.CountAccepted = req.CountAccepted
CountRequested: req.CountRequested, kindBoxReq.Description = req.Description
CountAccepted: req.CountAccepted, kindBoxReq.DeliverReferTimeID = req.DeliverReferTimeID
Description: req.Description, kindBoxReq.DeliverReferDate = req.DeliverReferDate
Status: kindBoxReq.Status, kindBoxReq.DeliverAddressID = req.DeliverAddressID
DeliverReferTimeID: req.DeliverReferTimeID, kindBoxReq.SenderAgentID = req.SenderAgentID
DeliverReferDate: req.DeliverReferDate,
DeliverAddressID: req.DeliverAddressID, uErr := s.repo.UpdateKindBoxReq(ctx, kindBoxReq)
SenderAgentID: req.SenderAgentID,
DeliveredAt: kindBoxReq.DeliveredAt,
})
if uErr != nil { if uErr != nil {
return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return richerror.New(op).WithErr(err)
} }
return nil return nil

View File

@ -111,15 +111,11 @@ func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc {
} }
func (v Validator) isDateValid(value interface{}) error { func (v Validator) isDateValid(value interface{}) error {
date, ok := value.(string) date, ok := value.(time.Time)
if !ok { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
t, err := time.Parse(time.DateTime, date) if date.Before(time.Now()) {
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
}
if t.Before(time.Now()) {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput) return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
} }

View File

@ -14,29 +14,39 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist)), validation.Field(&req.ID,
validation.Required,
validation.Field(&req.CountRequested, validation.Required, validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq))), 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.Field(&req.KindBoxType,
validation.Required, validation.Required,
validation.By(v.doesTypeExist)), validation.By(v.doesTypeExist),
),
validation.Field(&req.DeliverAddressID, validation.Field(&req.DeliverAddressID,
validation.Required, validation.Required,
validation.By(v.doesAddressExist(req.BenefactorID))), validation.By(v.doesBenefactorAddressExist(req.ID)),
),
validation.Field(&req.DeliverReferDate, validation.Field(&req.DeliverReferDate,
validation.Required, validation.Required,
validation.By(v.isDateValid), validation.By(v.isDateValid),
), ),
validation.Field(&req.DeliverReferTimeID, validation.Field(&req.DeliverReferTimeID,
validation.Required, validation.Required,
validation.By(v.isDateValid), validation.By(v.isReferTimeIDValid),
), ),
validation.Field(&req.SenderAgentID, validation.Field(&req.SenderAgentID,
validation.Required, validation.When(req.SenderAgentID > 0, validation.By(v.doesAgentExist)),
validation.By(v.isDateValid),
), ),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -9,6 +9,8 @@ import (
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param" params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" 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" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
benefactorsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor" benefactorsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
@ -26,16 +28,27 @@ type Repository interface {
type AdminSvc interface { type AdminSvc interface {
AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error) 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 { type Validator struct {
repo Repository repo Repository
adminSvc AdminSvc adminSvc AdminSvc
benefactorSvc benefactorsvc.Service benefactorSvc benefactorsvc.Service
referTimeSvc ReferTimeSvc
addressSvc AddressSvc
} }
func New(repo Repository, adminSvc AdminSvc, benefactorSvc benefactorsvc.Service) Validator { func New(repo Repository, adminSvc AdminSvc, benefactorSvc benefactorsvc.Service, referTimeSvc ReferTimeSvc, addressSvc AddressSvc) Validator {
return Validator{repo: repo, adminSvc: adminSvc, benefactorSvc: benefactorSvc} return Validator{repo: repo, adminSvc: adminSvc, benefactorSvc: benefactorSvc, referTimeSvc: referTimeSvc, addressSvc: addressSvc}
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {
@ -187,3 +200,63 @@ func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleF
return nil 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
}
}