forked from ebhomengo/niki
1
0
Fork 0

feat(niki): agent get all return awaiting kindboxes (#82)

This commit is contained in:
Ruhollah 2024-07-14 12:28:37 +03:30
parent b119b08830
commit 0a553ffbbd
13 changed files with 590 additions and 44 deletions

View File

@ -0,0 +1,66 @@
package agentkindboxhandler
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
"github.com/labstack/echo/v4"
)
// GetAll godoc
// @Summary Get all awaiting return KindBoxes by agent
// @Description Retrieves a list of all awaiting return KindBoxes for agent with filtering, sorting, and pagination options
// @Tags KindBox
// @Accept json
// @Produce json
// @Param filter_id query int false "Filter by ID"
// @Param filter_benefactor_id query int false "Filter by benefactor ID"
// @Param filter_type query entity.KindBoxType false "Filter by KindBox type" Format(enum)
// @Param filter_serial_number query string false "Filter by serial number"
// @Param filter_return_refer_time_id query int false "Filter by return refer time ID"
// @Param filter_return_refer_date query string false "Filter by return refer date" Format(date)
// @Param filter_return_address_id query int false "Filter by return address ID"
// @Param page_number query int false "Page number"
// @Param page_size query int false "Page size"
// @Param sort_field query string false "Sort by field" Enums(id,benefactor_id,type,serial_number,return_refer_time_id,return_refer_date,return_address_id)
// @Param sort_direction query string false "Sort order" Enums(asc,desc)
// @Success 200 {object} param.GetAllResponse
// @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 /agents/kindboxes [get]
func (h Handler) GetAll(c echo.Context) error {
var req param.GetAllRequest
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
req.Filter = queryparam.GetFilterParams(c)
if fieldErrors, err := h.agentKindBoxVld.ValidateGetAll(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, httpmsg.ErrorResponse{
Message: msg,
Errors: fieldErrors,
})
}
req.Filter["receiver_agent_id"] = claim.GetClaimsFromEchoContext(c).UserID
req.Filter["status"] = entity.KindBoxAssignedReceiverAgentStatus.String()
resp, err := h.agentKindBoxSvc.GetAll(c.Request().Context(), req)
if err != nil {
msg, code := httpmsg.Error(err)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -12,4 +12,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.Use(middleware.Auth(h.authSvc, h.authConfig)) r.Use(middleware.Auth(h.authSvc, h.authConfig))
r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAwaitingReturnPermission)) r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAwaitingReturnPermission))
r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAwaitingReturnPermission))
} }

View File

@ -1391,6 +1391,152 @@ const docTemplate = `{
} }
} }
}, },
"/agents/kindboxes": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all awaiting return KindBoxes for agent with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get all awaiting return KindBoxes by agent",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
1,
2,
3
],
"type": "integer",
"format": "enum",
"description": "Filter by KindBox type",
"name": "filter_type",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"benefactor_id",
"type",
"serial_number",
"return_refer_time_id",
"return_refer_date",
"return_address_id"
],
"type": "string",
"description": "Sort by field",
"name": "sort_field",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"description": "Sort order",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/agentkindboxparam.GetAllResponse"
}
},
"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"
}
}
}
}
},
"/agents/kindboxes/{id}": { "/agents/kindboxes/{id}": {
"get": { "get": {
"security": [ "security": [
@ -2770,6 +2916,20 @@ const docTemplate = `{
} }
} }
}, },
"agentkindboxparam.GetAllResponse": {
"type": "object",
"properties": {
"all_kind_boxes": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"benefactoreparam.BenefactroInfo": { "benefactoreparam.BenefactroInfo": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -1380,6 +1380,152 @@
} }
} }
}, },
"/agents/kindboxes": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all awaiting return KindBoxes for agent with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get all awaiting return KindBoxes by agent",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
1,
2,
3
],
"type": "integer",
"format": "enum",
"description": "Filter by KindBox type",
"name": "filter_type",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"benefactor_id",
"type",
"serial_number",
"return_refer_time_id",
"return_refer_date",
"return_address_id"
],
"type": "string",
"description": "Sort by field",
"name": "sort_field",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"description": "Sort order",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/agentkindboxparam.GetAllResponse"
}
},
"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"
}
}
}
}
},
"/agents/kindboxes/{id}": { "/agents/kindboxes/{id}": {
"get": { "get": {
"security": [ "security": [
@ -2759,6 +2905,20 @@
} }
} }
}, },
"agentkindboxparam.GetAllResponse": {
"type": "object",
"properties": {
"all_kind_boxes": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"benefactoreparam.BenefactroInfo": { "benefactoreparam.BenefactroInfo": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -438,6 +438,15 @@ definitions:
refresh_token: refresh_token:
type: string type: string
type: object type: object
agentkindboxparam.GetAllResponse:
properties:
all_kind_boxes:
items:
$ref: '#/definitions/entity.KindBox'
type: array
pagination:
$ref: '#/definitions/param.PaginationResponse'
type: object
benefactoreparam.BenefactroInfo: benefactoreparam.BenefactroInfo:
properties: properties:
first_name: first_name:
@ -1741,6 +1750,106 @@ paths:
summary: Register an admin by super-admin summary: Register an admin by super-admin
tags: tags:
- Admin - Admin
/agents/kindboxes:
get:
consumes:
- application/json
description: Retrieves a list of all awaiting return KindBoxes for agent with
filtering, sorting, and pagination options
parameters:
- description: Filter by ID
in: query
name: filter_id
type: integer
- description: Filter by benefactor ID
in: query
name: filter_benefactor_id
type: integer
- description: Filter by KindBox type
enum:
- 1
- 2
- 3
format: enum
in: query
name: filter_type
type: integer
- description: Filter by serial number
in: query
name: filter_serial_number
type: string
- description: Filter by return refer time ID
in: query
name: filter_return_refer_time_id
type: integer
- description: Filter by return refer date
format: date
in: query
name: filter_return_refer_date
type: string
- description: Filter by return address ID
in: query
name: filter_return_address_id
type: integer
- description: Page number
in: query
name: page_number
type: integer
- description: Page size
in: query
name: page_size
type: integer
- description: Sort by field
enum:
- id
- benefactor_id
- type
- serial_number
- return_refer_time_id
- return_refer_date
- return_address_id
in: query
name: sort_field
type: string
- description: Sort order
enum:
- asc
- desc
in: query
name: sort_direction
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/agentkindboxparam.GetAllResponse'
"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: Get all awaiting return KindBoxes by agent
tags:
- KindBox
/agents/kindboxes/{id}: /agents/kindboxes/{id}:
get: get:
consumes: consumes:

View File

@ -1,4 +1,4 @@
package kind_box package agentkindboxparam
import "git.gocasts.ir/ebhomengo/niki/entity" import "git.gocasts.ir/ebhomengo/niki/entity"

View File

@ -0,0 +1,17 @@
package agentkindboxparam
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/param"
)
type GetAllRequest struct {
Pagination param.PaginationRequest
Sort param.SortRequest
Filter param.FilterRequest
}
type GetAllResponse struct {
AllKindBoxes []entity.KindBox `json:"all_kind_boxes"`
Pagination param.PaginationResponse `json:"pagination"`
}

View File

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

View File

@ -0,0 +1,26 @@
package agentkindboxservice
import (
"context"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.GetAllResponse, error) {
const op = "agentkindboxservice.GetAll"
allKindBoxes, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil {
return param.GetAllResponse{}, richerror.New(op).WithErr(err)
}
return param.GetAllResponse{
AllKindBoxes: allKindBoxes,
Pagination: paginationparam.PaginationResponse{
PageSize: req.Pagination.GetPageSize(),
PageNumber: req.Pagination.GetPageNumber(),
Total: total,
},
}, nil
}

View File

@ -2,12 +2,13 @@ package agentkindboxservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
) )
type Repository interface { type Repository interface {
GetAwaitingReturnByAgent(ctx context.Context, kindBoxID uint, agentID uint) (entity.KindBox, error) GetAwaitingReturnByAgent(ctx context.Context, kindBoxID uint, agentID uint) (entity.KindBox, error)
GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error)
} }
type Service struct { type Service struct {

View File

@ -2,6 +2,7 @@ package adminkindboxvalidator
import ( import (
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/validator"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
@ -19,8 +20,8 @@ func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]st
"return_address_id", "receiver_agent_id", "returned_at", "return_address_id", "receiver_agent_id", "returned_at",
} }
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.Filter, validation.By(v.areFilterFieldsValid(validFields))), validation.Field(&req.Filter, validation.By(validator.AreFilterFieldsValid(validFields))),
validation.Field(&req.Sort, validation.By(v.areSortFieldsValid(validFields))), validation.Field(&req.Sort, validation.By(validator.AreSortFieldsValid(validFields))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -3,12 +3,8 @@ package adminkindboxvalidator
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
) )
type Repository interface { type Repository interface {
@ -76,37 +72,3 @@ func (v Validator) doesAgentExist(value interface{}) error {
return nil return nil
} }
func (v Validator) areFilterFieldsValid(validFilters []string) validation.RuleFunc {
return func(value interface{}) error {
filters, ok := value.(params.FilterRequest)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
for filter := range filters {
if !slices.Contains(validFilters, filter) {
return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid)
}
}
return nil
}
}
func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleFunc {
return func(value interface{}) error {
sort, ok := value.(params.SortRequest)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if sort.Field == "" && sort.Direction != "" {
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired)
}
if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection {
return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc)
}
if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) {
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid)
}
return nil
}
}

View File

@ -0,0 +1,43 @@
package agentkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/validator"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateGetAll(req param.GetAllRequest) (map[string]string, error) {
const op = "agentkindboxvalidator.ValidateGetAll"
validFields := []string{
"id", "kind_box_req_id",
"benefactor_id", "type", "serial_number",
"return_refer_time_id", "return_refer_date", "return_address_id",
}
if err := validation.ValidateStruct(&req,
validation.Field(&req.Filter, validation.By(validator.AreFilterFieldsValid(validFields))),
validation.Field(&req.Sort, validation.By(validator.AreSortFieldsValid(validFields))),
); 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
}