forked from ebhomengo/niki
1
0
Fork 0

Merge pull request 'feat(niki): admin enumerate(count) kindbox amount (#80)' (#99) from stage/erfan/admin-enumerate-kindbox into develop

Reviewed-on: ebhomengo/niki#99
This commit is contained in:
hossein 2024-07-24 06:22:00 +00:00
commit 331d8ab648
13 changed files with 391 additions and 35 deletions

View File

@ -0,0 +1,51 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// Enumerate godoc
// @Summary Admin enumerate kindbox
// @Tags KindBox
// @Accept json
// @Produce json
// @Param id path int true "KindBox ID"
// @Param Request body param.EnumerateKindBoxRequest true "Request"
// @Success 204
// @Failure 400 {string} "Bad Request"
// @Failure 401 {string} "invalid or expired jwt"
// @Failure 403 {string} "user not allowed"
// @Failure 422 {object} httpmsg.ErrorResponse
// @Failure 500 {string} "something went wrong"
// @Security AuthBearerAdmin
// @Router /admin/kindboxes/{id}/enumerate [patch].
func (h Handler) Enumerate(c echo.Context) error {
var req param.EnumerateKindBoxRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminKindBoxVld.ValidateEnumerate(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
sErr := h.adminKindBoxSvc.Enumerate(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusNoContent, nil)
}

View File

@ -14,4 +14,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission))
r.PATCH("/assign-receiver-agent/:id", h.AssignReceiverAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxAssignReceiverAgentPermission))
r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAllPermission))
r.PATCH("/:id/enumerate", h.Enumerate, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxEnumeratePermission))
}

View File

@ -559,6 +559,78 @@ const docTemplate = `{
}
}
},
"/admin/kindboxes/{id}/enumerate": {
"patch": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Admin enumerate kindbox",
"parameters": [
{
"type": "integer",
"description": "KindBox ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/adminkindboxparam.EnumerateKindBoxRequest"
}
}
],
"responses": {
"204": {
"description": "No Content"
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"422": {
"description": "Unprocessable Entity",
"schema": {
"$ref": "#/definitions/httpmsg.ErrorResponse"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {
"post": {
"security": [
@ -2434,6 +2506,14 @@ const docTemplate = `{
}
}
},
"adminkindboxparam.EnumerateKindBoxRequest": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
}
}
},
"adminkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {

View File

@ -548,6 +548,78 @@
}
}
},
"/admin/kindboxes/{id}/enumerate": {
"patch": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Admin enumerate kindbox",
"parameters": [
{
"type": "integer",
"description": "KindBox ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/adminkindboxparam.EnumerateKindBoxRequest"
}
}
],
"responses": {
"204": {
"description": "No Content"
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"422": {
"description": "Unprocessable Entity",
"schema": {
"$ref": "#/definitions/httpmsg.ErrorResponse"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {
"post": {
"security": [
@ -2423,6 +2495,14 @@
}
}
},
"adminkindboxparam.EnumerateKindBoxRequest": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
}
}
},
"adminkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {

View File

@ -77,6 +77,11 @@ definitions:
receiver_agent_id:
type: integer
type: object
adminkindboxparam.EnumerateKindBoxRequest:
properties:
amount:
type: integer
type: object
adminkindboxparam.KindBoxGetAllResponse:
properties:
all_kind_box:
@ -1183,6 +1188,52 @@ paths:
summary: Get a specific kind box by admin
tags:
- KindBox
/admin/kindboxes/{id}/enumerate:
patch:
consumes:
- application/json
parameters:
- description: KindBox ID
in: path
name: id
required: true
type: integer
- description: Request
in: body
name: Request
required: true
schema:
$ref: '#/definitions/adminkindboxparam.EnumerateKindBoxRequest'
produces:
- application/json
responses:
"204":
description: No Content
"400":
description: Bad Request
schema:
type: string
"401":
description: invalid or expired jwt
schema:
type: string
"403":
description: user not allowed
schema:
type: string
"422":
description: Unprocessable Entity
schema:
$ref: '#/definitions/httpmsg.ErrorResponse'
"500":
description: something went wrong
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Admin enumerate kindbox
tags:
- KindBox
/admin/kindboxes/assign-receiver-agent/{id}:
patch:
consumes:

View File

@ -19,4 +19,5 @@ const (
AdminKindBoxReqGetPermission = AdminPermission("kindboxreq-get")
AdminKindBoxGetAwaitingReturnPermission = AdminPermission("kindbox-get_awaiting_return")
AdminKindBoxReturnPermission = AdminPermission("kindbox-return")
AdminKindBoxEnumeratePermission = AdminPermission("kindbox-enumerate")
)

View File

@ -0,0 +1,6 @@
package adminkindboxparam
type EnumerateKindBoxRequest struct {
KindBoxID uint `json:"-" param:"id"`
Amount uint `json:"amount"`
}

View File

@ -0,0 +1,22 @@
package mysqlkindbox
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) EnumerateKindBox(ctx context.Context, kindBoxID uint, amount uint) error {
const op = "mysqlkindbox.EnumerateKindBox"
query := `UPDATE kind_boxes SET amount = ?, status = ? WHERE id = ?`
_, err := d.conn.Conn().ExecContext(ctx, query, amount, entity.KindBoxEnumeratedStatus.String(), kindBoxID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}

View File

@ -16,7 +16,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
'kindboxreq-update',
'kindboxreq-get',
'kindbox-get_awaiting_return',
'kindbox-return'
'kindbox-return',
'kindbox-enumerate'
) NOT NULL;
-- +migrate Down

View File

@ -1,41 +1,43 @@
-- +migrate Up
INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`)
INSERT INTO `admin_access_controls` (`actor_id`, `actor_type`,`permission`)
VALUES
(DEFAULT, 1 , 'role','admin-register'),
(DEFAULT, 1 , 'role','admin-getall_agent'),
(DEFAULT, 2 , 'role','admin-getall_agent'),
(1 , 'role','admin-register'),
(1 , 'role','admin-getall_agent'),
(2 , 'role','admin-getall_agent'),
(DEFAULT, 1 , 'role','kindboxreq-add'),
(DEFAULT, 2 , 'role','kindboxreq-add'),
(DEFAULT, 1 , 'role','kindboxreq-accept'),
(DEFAULT, 2 , 'role','kindboxreq-accept'),
(DEFAULT, 1 , 'role','kindboxreq-reject'),
(DEFAULT, 2 , 'role','kindboxreq-reject'),
(DEFAULT, 1 , 'role','kindboxreq-assign_sender_agent'),
(DEFAULT, 2 , 'role','kindboxreq-assign_sender_agent'),
(DEFAULT, 1 , 'role','kindboxreq-deliver'),
(DEFAULT, 2 , 'role','kindboxreq-deliver'),
(DEFAULT, 3 , 'role','kindboxreq-deliver'),
(DEFAULT, 1 , 'role','kindboxreq-get'),
(DEFAULT, 2 , 'role','kindboxreq-get'),
(DEFAULT, 1 , 'role','kindboxreq-getall'),
(DEFAULT, 2 , 'role','kindboxreq-getall'),
(DEFAULT, 1 , 'role','kindboxreq-update'),
(DEFAULT, 2 , 'role','kindboxreq-update'),
(DEFAULT, 1 , 'role','kindboxreq-get_awaiting_delivery'),
(DEFAULT, 2 , 'role','kindboxreq-get_awaiting_delivery'),
(DEFAULT, 3 , 'role','kindboxreq-get_awaiting_delivery'),
(1 , 'role','kindboxreq-add'),
(2 , 'role','kindboxreq-add'),
(1 , 'role','kindboxreq-accept'),
(2 , 'role','kindboxreq-accept'),
(1 , 'role','kindboxreq-reject'),
(2 , 'role','kindboxreq-reject'),
(1 , 'role','kindboxreq-assign_sender_agent'),
(2 , 'role','kindboxreq-assign_sender_agent'),
(1 , 'role','kindboxreq-deliver'),
(2 , 'role','kindboxreq-deliver'),
(3 , 'role','kindboxreq-deliver'),
(1 , 'role','kindboxreq-get'),
(2 , 'role','kindboxreq-get'),
(1 , 'role','kindboxreq-getall'),
(2 , 'role','kindboxreq-getall'),
(1 , 'role','kindboxreq-update'),
(2 , 'role','kindboxreq-update'),
(1 , 'role','kindboxreq-get_awaiting_delivery'),
(2 , 'role','kindboxreq-get_awaiting_delivery'),
(3 , 'role','kindboxreq-get_awaiting_delivery'),
(DEFAULT, 1 , 'role','kindbox-assign_receiver_agent'),
(DEFAULT, 2 , 'role','kindbox-assign_receiver_agent'),
(DEFAULT, 1 , 'role','kindbox-get'),
(DEFAULT, 2 , 'role','kindbox-get'),
(DEFAULT, 1 , 'role','kindbox-getall'),
(DEFAULT, 2 , 'role','kindbox-getall'),
(DEFAULT, 1 , 'role','kindbox-get_awaiting_return'),
(DEFAULT, 3 , 'role','kindbox-get_awaiting_return'),
(DEFAULT, 1 , 'role','kindbox-return'),
(DEFAULT, 3 , 'role','kindbox-return');
(1 , 'role','kindbox-assign_receiver_agent'),
(2 , 'role','kindbox-assign_receiver_agent'),
(1 , 'role','kindbox-get'),
(2 , 'role','kindbox-get'),
(1 , 'role','kindbox-getall'),
(2 , 'role','kindbox-getall'),
(1 , 'role','kindbox-get_awaiting_return'),
(3 , 'role','kindbox-get_awaiting_return'),
(1 , 'role','kindbox-return'),
(3 , 'role','kindbox-return'),
(1 , 'role','kindbox-enumerate'),
(2 , 'role','kindbox-enumerate');

View File

@ -0,0 +1,19 @@
package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Enumerate(ctx context.Context, req param.EnumerateKindBoxRequest) error {
const op = "AdminKindBoxService.Enumerate"
err := s.repo.EnumerateKindBox(ctx, req.KindBoxID, req.Amount)
if err != nil {
return richerror.New(op).WithErr(err)
}
return nil
}

View File

@ -13,6 +13,7 @@ type Repository interface {
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error
GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error)
EnumerateKindBox(ctx context.Context, kindBoxID uint, amount uint) error
}
type Service struct {

View File

@ -0,0 +1,41 @@
package adminkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
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) ValidateEnumerate(req param.EnumerateKindBoxRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateEnumerate"
if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxID, validation.Required,
validation.By(v.doesKindBoxExist)),
validation.Field(&req.Amount, validation.Required),
); 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
}