forked from ebhomengo/niki
				
			"feat(niki): add update kind box request by admin"
This commit is contained in:
		
							parent
							
								
									9de27badb9
								
							
						
					
					
						commit
						ecb3e9aedc
					
				| 
						 | 
				
			
			@ -18,4 +18,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
 | 
			
		|||
	r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission))
 | 
			
		||||
	r.GET("/awaiting-delivery/:id", h.GetAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission))
 | 
			
		||||
	r.GET("/awaiting-delivery", h.GetAllAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission))
 | 
			
		||||
	r.PUT("/:id", h.Update, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqUpdatePermission))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
package adminkindboxreqhandler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
 | 
			
		||||
	httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
 | 
			
		||||
	"github.com/labstack/echo/v4"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Update godoc
 | 
			
		||||
// @Summary      Update kind box request by admin
 | 
			
		||||
// @Tags         KindBoxReq
 | 
			
		||||
// @Accept       json
 | 
			
		||||
// @Produce      json
 | 
			
		||||
// @Param 		 id path int true "KindBoxReq ID"
 | 
			
		||||
// @Param 		 Request body param.KindBoxReqUpdateRequest true "Update KindBoxReq Request Body"
 | 
			
		||||
// @Success      204
 | 
			
		||||
// @Failure      400  {string}  "Bad request"
 | 
			
		||||
// @Security 	 AuthBearerAdmin
 | 
			
		||||
// @Router       /admin/kindboxreqs/{id} [put].
 | 
			
		||||
func (h Handler) Update(c echo.Context) error {
 | 
			
		||||
	var req param.KindBoxReqUpdateRequest
 | 
			
		||||
	if bErr := c.Bind(&req); bErr != nil {
 | 
			
		||||
		return echo.NewHTTPError(http.StatusBadRequest)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if fieldErrors, err := h.adminKindBoxReqVld.ValidateUpdateRequest(req); err != nil {
 | 
			
		||||
		msg, code := httpmsg.Error(err)
 | 
			
		||||
 | 
			
		||||
		return c.JSON(code, echo.Map{
 | 
			
		||||
			"message": msg,
 | 
			
		||||
			"errors":  fieldErrors,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sErr := h.adminKindBoxReqSvc.Update(c.Request().Context(), req)
 | 
			
		||||
	if sErr != nil {
 | 
			
		||||
		msg, code := httpmsg.Error(sErr)
 | 
			
		||||
 | 
			
		||||
		return echo.NewHTTPError(code, msg)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return c.JSON(http.StatusNoContent, nil)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -15,4 +15,5 @@ const (
 | 
			
		|||
	AdminKindBoxGetPermission                    = AdminPermission("kindbox-get")
 | 
			
		||||
	AdminKindBoxAssignReceiverAgentPermission    = AdminPermission("kindbox-assign_receiver_agent")
 | 
			
		||||
	AdminKindBoxGetAllPermission                 = AdminPermission("kindbox-getall")
 | 
			
		||||
	AdminKindBoxReqUpdatePermission              = AdminPermission("kindboxreq-update")
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,19 @@
 | 
			
		|||
package adminkindboxreqparam
 | 
			
		||||
 | 
			
		||||
import entity "git.gocasts.ir/ebhomengo/niki/entity"
 | 
			
		||||
import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.gocasts.ir/ebhomengo/niki/entity"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type KindBoxReqUpdateRequest struct {
 | 
			
		||||
	BenefactorID uint
 | 
			
		||||
	KindBoxReqID uint
 | 
			
		||||
	uint
 | 
			
		||||
	CountRequested uint
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KindBoxReqUpdateResponse struct {
 | 
			
		||||
	KindBoxReq entity.KindBoxReq
 | 
			
		||||
	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"`
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
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) 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 {
 | 
			
		||||
 | 
			
		||||
		return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
 | 
			
		||||
			WithKind(richerror.KindUnexpected)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -12,7 +12,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
 | 
			
		|||
        'kindbox-get',
 | 
			
		||||
        'kindboxreq-add',
 | 
			
		||||
        'kindbox-assign_receiver_agent',
 | 
			
		||||
        'kindbox-getall'
 | 
			
		||||
        'kindbox-getall',
 | 
			
		||||
        'kindboxreq-update'
 | 
			
		||||
    ) NOT NULL;
 | 
			
		||||
    
 | 
			
		||||
-- +migrate Down
 | 
			
		||||
| 
						 | 
				
			
			@ -23,7 +23,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`
 | 
			
		|||
        (21, 1 , 'role','kindboxreq-add'),
 | 
			
		||||
        (22, 2 , 'role','kindboxreq-add'),
 | 
			
		||||
        (23, 1 , 'role','kindbox-getall'),
 | 
			
		||||
        (24, 2 , 'role','kindbox-getall');
 | 
			
		||||
        (24, 2 , 'role','kindbox-getall'),
 | 
			
		||||
        (25, 1 , 'role','kindboxreq-update'),
 | 
			
		||||
        (26, 2 , 'role','kindboxreq-update');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
-- +migrate Down
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ type Repository interface {
 | 
			
		|||
	GetAllKindBoxReq(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBoxReq, uint, error)
 | 
			
		||||
	GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error)
 | 
			
		||||
	AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
 | 
			
		||||
	UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KindBoxSvc interface {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
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"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) error {
 | 
			
		||||
	const op = "adminkindboxreqservice.Update"
 | 
			
		||||
 | 
			
		||||
	kindBoxReq, err := s.repo.GetByID(ctx, req.ID)
 | 
			
		||||
	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,
 | 
			
		||||
	})
 | 
			
		||||
	if uErr != nil {
 | 
			
		||||
		return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,61 @@
 | 
			
		|||
package adminkindboxreqvalidator
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "adminkindboxreqvalidator.ValidateUpdateRequest"
 | 
			
		||||
 | 
			
		||||
	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.KindBoxType,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesTypeExist)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.DeliverAddressID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesAddressExist(req.BenefactorID))),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.DeliverReferDate,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.isDateValid),
 | 
			
		||||
		),
 | 
			
		||||
		validation.Field(&req.DeliverReferTimeID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.isDateValid),
 | 
			
		||||
		),
 | 
			
		||||
		validation.Field(&req.SenderAgentID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.isDateValid),
 | 
			
		||||
		),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		var errV validation.Errors
 | 
			
		||||
		if errors.As(err, &errV) {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return map[string]string{}, nil
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue