feat(niki): add `delete kind_box_req by benefactor`

This commit is contained in:
Iman Mirazimi 2024-05-31 18:19:04 +03:30
parent ed894c7520
commit 51a401f066
22 changed files with 377 additions and 145 deletions

View File

@ -0,0 +1,46 @@
package benefactorkindboxreqhandler
import (
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
"net/http"
)
// delete godoc
// @Summary delete kindboxreq by benefactor
// @Description This endpoint is used to delete benefactor's kindboxreq at pending status
// @Tags KindBoxReq
// @Accept json
// @Produce json
// @Param id path int true "Kind box request ID"
// @Success 200 {object} param.KindBoxReqDeleteResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /benefactor/kindboxreqs/{id} [delete]
func (h Handler) Delete(c echo.Context) error {
req := param.KindBoxReqDeleteRequest{}
if bErr := echo.PathParamsBinder(c).Uint("id", &req.KindBoxReqID).BindError(); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateDeleteRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, dErr := h.benefactorKindBoxReqSvc.Delete(c.Request().Context(), req)
if dErr != nil {
msg, code := httpmsg.Error(dErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -16,4 +16,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/", h.Add) r.POST("/", h.Add)
r.GET("/:id", h.Get) r.GET("/:id", h.Get)
r.DELETE("/:id", h.Delete)
} }

View File

@ -601,6 +601,47 @@ const docTemplate = `{
} }
} }
} }
},
"delete": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"description": "This endpoint is used to delete benefactor's kindboxreq at pending status",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "delete kindboxreq by benefactor",
"parameters": [
{
"type": "integer",
"description": "Kind box request ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqDeleteResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
} }
}, },
"/benefactor/login-register": { "/benefactor/login-register": {
@ -1171,6 +1212,9 @@ const docTemplate = `{
} }
} }
}, },
"benefactorkindboxreqparam.KindBoxReqDeleteResponse": {
"type": "object"
},
"benefactorkindboxreqparam.KindBoxReqGetResponse": { "benefactorkindboxreqparam.KindBoxReqGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -590,6 +590,47 @@
} }
} }
} }
},
"delete": {
"security": [
{
"AuthBearerBenefactor": []
}
],
"description": "This endpoint is used to delete benefactor's kindboxreq at pending status",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "delete kindboxreq by benefactor",
"parameters": [
{
"type": "integer",
"description": "Kind box request ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqDeleteResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
} }
}, },
"/benefactor/login-register": { "/benefactor/login-register": {
@ -1160,6 +1201,9 @@
} }
} }
}, },
"benefactorkindboxreqparam.KindBoxReqDeleteResponse": {
"type": "object"
},
"benefactorkindboxreqparam.KindBoxReqGetResponse": { "benefactorkindboxreqparam.KindBoxReqGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -316,6 +316,8 @@ definitions:
kind_box_req: kind_box_req:
$ref: '#/definitions/entity.KindBoxReq' $ref: '#/definitions/entity.KindBoxReq'
type: object type: object
benefactorkindboxreqparam.KindBoxReqDeleteResponse:
type: object
benefactorkindboxreqparam.KindBoxReqGetResponse: benefactorkindboxreqparam.KindBoxReqGetResponse:
properties: properties:
kind_box_req: kind_box_req:
@ -829,6 +831,33 @@ paths:
tags: tags:
- KindBoxReq - KindBoxReq
/benefactor/kindboxreqs/{id}: /benefactor/kindboxreqs/{id}:
delete:
consumes:
- application/json
description: This endpoint is used to delete benefactor's kindboxreq at pending
status
parameters:
- description: Kind box request ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/benefactorkindboxreqparam.KindBoxReqDeleteResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: delete kindboxreq by benefactor
tags:
- KindBoxReq
get: get:
consumes: consumes:
- application/json - application/json

View File

@ -35,6 +35,7 @@ func InitBenefactorKindBoxReqValidator(cfg config.Config, redisAdapter redis.Ada
return benefactorkindboxreqvalidator.New( return benefactorkindboxreqvalidator.New(
InitBenefactorService(cfg, redisAdapter, db), InitBenefactorService(cfg, redisAdapter, db),
InitBenefactorAddressService(db), InitBenefactorAddressService(db),
InitBenefactorKindBoxReqDB(db),
) )
} }

View File

@ -22,4 +22,7 @@ const (
ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered" ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered"
ErrorMsgAdminIsNotAgent = "admin is not agent" ErrorMsgAdminIsNotAgent = "admin is not agent"
ErrorMsgCountAcceptedOverflow = "count accepted is greather than count requested" ErrorMsgCountAcceptedOverflow = "count accepted is greather than count requested"
ErrorMsgReferTimeNotFound = "refer time not found"
ErrorMsgReferTimeIsNotActive = "refer time is not active"
ErrorMsgKindBoxReqDoesntBelongToBenefactor = "kind box req doesnt belong to benefactor"
) )

View File

@ -0,0 +1,20 @@
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"
)
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`,
countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}

View File

@ -0,0 +1,26 @@
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"
)
func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.AddKindBoxReq"
res, err := d.conn.Conn().ExecContext(ctx, `insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,status) values (?,?,?,?,?,?)`,
kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(), kindBoxReq.DeliverAddressID, kindBoxReq.CountRequested, kindBoxReq.DeliverReferDate, kindBoxReq.Status.String())
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
}
//nolint
// err is always nil
id, _ := res.LastInsertId()
kindBoxReq.ID = uint(id)
return kindBoxReq, nil
}

View File

@ -0,0 +1,18 @@
package mysqlkindboxreq
import (
"context"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"time"
)
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)
if dErr != nil {
return richerror.New(op).WithErr(dErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}

View File

@ -0,0 +1,38 @@
package mysqlkindboxreq
import (
"context"
"database/sql"
"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) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetByID")
row := d.conn.Conn().QueryRowContext(ctx, `select * from kind_box_reqs where id = ? and deleted_at is null`, id)
k, err := scanKindBoxReq(row)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}
func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
row := d.conn.Conn().QueryRowContext(ctx,
"select * from kind_box_reqs where id = ? and deleted_at is null", kindBoxReqID)
k, err := scanKindBoxReq(row)
if err != nil {
if err == sql.ErrNoRows {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
}
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}

View File

@ -14,7 +14,7 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.Pag
// TODO: create getCount function // TODO: create getCount function
var count uint var count uint
rows, err := d.conn.Conn().QueryContext(ctx, "SELECT COUNT(*) FROM kind_box_reqs") rows, err := d.conn.Conn().QueryContext(ctx, "SELECT COUNT(*) FROM kind_box_reqs where deleted_at is null")
if err != nil { if err != nil {
return nil, paginationparam.PaginationResponse{}, return nil, paginationparam.PaginationResponse{},
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)
@ -35,7 +35,7 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.Pag
} }
// TODO - add sort and filter // TODO - add sort and filter
rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs limit ? offset ?", pagination.GetPageSize(), pagination.GetOffset()) rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs where deleted_at is null limit ? offset ?", pagination.GetPageSize(), pagination.GetOffset())
if err != nil { if err != nil {
return nil, paginationparam.PaginationResponse{}, return nil, paginationparam.PaginationResponse{},
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)

View File

@ -9,48 +9,6 @@ import (
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
const op = "mysqlkindboxreq.AddKindBoxReq"
res, err := d.conn.Conn().ExecContext(ctx, `insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,status) values (?,?,?,?,?,?)`,
kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(), kindBoxReq.DeliverAddressID, kindBoxReq.CountRequested, kindBoxReq.DeliverReferDate, kindBoxReq.Status.String())
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
}
//nolint
// err is always nil
id, _ := res.LastInsertId()
kindBoxReq.ID = uint(id)
return kindBoxReq, nil
}
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`,
countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}
func (d DB) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetByID")
row := d.conn.Conn().QueryRowContext(ctx, `select * from kind_box_reqs where id = ?`, id)
k, err := scanKindBoxReq(row)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}
func (d DB) KindBoxRequestExist(id uint) (bool, error) { func (d DB) KindBoxRequestExist(id uint) (bool, error) {
op := richerror.Op("mysqlkindboxreq.KindBoxRequestExist") op := richerror.Op("mysqlkindboxreq.KindBoxRequestExist")
row := d.conn.Conn().QueryRow(`select * from kind_box_reqs where id = ?`, id) row := d.conn.Conn().QueryRow(`select * from kind_box_reqs where id = ?`, id)
@ -67,18 +25,6 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) {
return true, nil return true, nil
} }
func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`,
description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}
func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id) _, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id)
@ -89,21 +35,3 @@ func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
return nil return nil
} }
func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID, benefactorID uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
row := d.conn.Conn().QueryRowContext(ctx,
"select kind_box_reqs.* from kind_box_reqs where kind_box_reqs.id = ? and kind_box_reqs.benefactor_id = ?", kindBoxReqID, benefactorID,
)
k, err := scanKindBoxReq(row)
if err != nil {
if err == sql.ErrNoRows {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
}
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}

View File

@ -0,0 +1,20 @@
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"
)
func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`,
description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}

View File

@ -19,6 +19,7 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
deliveredAt sql.NullTime deliveredAt sql.NullTime
createdAt time.Time createdAt time.Time
updatedAt time.Time updatedAt time.Time
deletedAt sql.NullTime
) )
err := scanner.Scan( err := scanner.Scan(
@ -35,6 +36,7 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
&deliveredAt, &deliveredAt,
&createdAt, &createdAt,
&updatedAt, &updatedAt,
&deletedAt,
) )
if err != nil { if err != nil {
return entity.KindBoxReq{}, err return entity.KindBoxReq{}, err

View File

@ -14,6 +14,7 @@ CREATE TABLE `kind_box_reqs` (
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 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 (`benefactor_id`) REFERENCES `benefactors` (`id`),
FOREIGN KEY (`deliver_address_id`) REFERENCES `addresses` (`id`), FOREIGN KEY (`deliver_address_id`) REFERENCES `addresses` (`id`),
FOREIGN KEY (`sender_agent_id`) REFERENCES `admins` (`id`) FOREIGN KEY (`sender_agent_id`) REFERENCES `admins` (`id`)

View File

@ -0,0 +1,16 @@
package benefactorkindboxreqservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) {
const op = richerror.Op("benefactorkindboxreqservice.Delete")
dErr := s.repo.DeleteKindBoxReqByID(ctx, req.KindBoxReqID)
if dErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqDeleteResponse{}, nil
}

View File

@ -10,7 +10,7 @@ import (
func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) {
const op = "userkindboxreqservice.Get" const op = "userkindboxreqservice.Get"
kindBoxReq, err := s.repo.GetKindBoxReqByID(ctx, req.KindBoxReqID, req.BenefactorID) kindBoxReq, err := s.repo.GetKindBoxReqByID(ctx, req.KindBoxReqID)
if err != nil { if err != nil {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err) return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err)
} }

View File

@ -8,7 +8,8 @@ import (
type Repository interface { type Repository interface {
AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint, benefactorID uint) (entity.KindBoxReq, error) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error
} }
type Service struct { type Service struct {

View File

@ -16,12 +16,10 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist)), validation.By(v.doesBenefactorExist)),
validation.Field(&req.KindBoxReqID,
// validation.Field(&req.KindBoxReqID, validation.Required,
// validation.Required, validation.By(v.doesKindBoxRequestHavePendingStatus),
// validation.By(v.doesKindBoxRequestExist), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
// validation.By(v.hasPendingStatus),
// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -24,9 +24,14 @@ type AddressSvc interface {
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
} }
type Repository interface {
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
}
type Validator struct { type Validator struct {
benefactorSvc BenefactorSvc benefactorSvc BenefactorSvc
addressSvc AddressSvc addressSvc AddressSvc
repo Repository
} }
type ValidatorError struct { type ValidatorError struct {
@ -44,8 +49,8 @@ func (v ValidatorError) Error() string {
return err return err
} }
func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc) Validator { func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, repo Repository) Validator {
return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc} return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, repo: repo}
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
@ -110,43 +115,34 @@ func (v Validator) isDateValid(value interface{}) error {
return nil return nil
} }
// func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
// kindBoxReqID, ok := value.(uint) return func(value interface{}) error {
// if !ok { kindBoxReqID, ok := value.(uint)
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) if !ok {
// } return fmt.Errorf(errmsg.ErrorMsgNotFound)
// _, err := v.repo.KindBoxReqExist(kindBoxReqID) }
// if err != nil { kindBoxReq, err := v.repo.GetKindBoxReqByID(context.Background(), kindBoxReqID)
// return fmt.Errorf(errmsg.ErrorMsgNotFound) if err != nil {
// } return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
// return nil if kindBoxReq.BenefactorID != benefactorID {
//} return fmt.Errorf(errmsg.ErrorMsgKindBoxReqDoesntBelongToBenefactor)
// }
// func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { return nil
// return func(value interface{}) error { }
// kbID, ok := value.(uint) }
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgNotFound) func (v Validator) doesKindBoxRequestHavePendingStatus(value interface{}) error {
// } kindBoxReqID, ok := value.(uint)
// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) if !ok {
// if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound)
// return fmt.Errorf(errmsg.ErrorMsgNotFound) }
// } kindBoxReq, err := v.repo.GetKindBoxReqByID(context.Background(), kindBoxReqID)
// if err != nil {
// return nil return fmt.Errorf(errmsg.ErrorMsgNotFound)
// } }
//} if kindBoxReq.Status != entity.KindBoxReqPendingStatus {
// return fmt.Errorf(errmsg.ErrorMsgInvalidStatus)
// func (v Validator) hasPendingStatus(value interface{}) error { }
// kindboxID, ok := value.(uint) return nil
// if !ok { }
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.PendingStatus(kindboxID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}