diff --git a/delivery/http_server/agent/kind_box/get_all.go b/delivery/http_server/agent/kind_box/get_all.go new file mode 100644 index 0000000..9723ebe --- /dev/null +++ b/delivery/http_server/agent/kind_box/get_all.go @@ -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) +} diff --git a/delivery/http_server/agent/kind_box/route.go b/delivery/http_server/agent/kind_box/route.go index 295339f..baef1ef 100644 --- a/delivery/http_server/agent/kind_box/route.go +++ b/delivery/http_server/agent/kind_box/route.go @@ -12,4 +12,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.Use(middleware.Auth(h.authSvc, h.authConfig)) r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAwaitingReturnPermission)) + r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAwaitingReturnPermission)) } diff --git a/docs/docs.go b/docs/docs.go index 19ecc04..0cf5d62 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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}": { "get": { "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": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 24a04c5..2c42313 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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}": { "get": { "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": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 0874e5c..188b0f3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -438,6 +438,15 @@ definitions: refresh_token: type: string type: object + agentkindboxparam.GetAllResponse: + properties: + all_kind_boxes: + items: + $ref: '#/definitions/entity.KindBox' + type: array + pagination: + $ref: '#/definitions/param.PaginationResponse' + type: object benefactoreparam.BenefactroInfo: properties: first_name: @@ -1741,6 +1750,106 @@ paths: summary: Register an admin by super-admin tags: - 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}: get: consumes: diff --git a/param/agent/kind_box/get.go b/param/agent/kind_box/get.go index 02d25a9..916c9b6 100644 --- a/param/agent/kind_box/get.go +++ b/param/agent/kind_box/get.go @@ -1,4 +1,4 @@ -package kind_box +package agentkindboxparam import "git.gocasts.ir/ebhomengo/niki/entity" diff --git a/param/agent/kind_box/get_all.go b/param/agent/kind_box/get_all.go new file mode 100644 index 0000000..e2489f2 --- /dev/null +++ b/param/agent/kind_box/get_all.go @@ -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"` +} diff --git a/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql b/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql index d24a961..14179c8 100644 --- a/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql +++ b/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql @@ -16,6 +16,6 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindboxreq-update', 'kindboxreq-get', 'kindbox-get_awaiting_return' - ) NOT NULL; - + ) NOT NULL; + -- +migrate Down \ No newline at end of file diff --git a/service/agent/kind_box/get_all.go b/service/agent/kind_box/get_all.go new file mode 100644 index 0000000..510c509 --- /dev/null +++ b/service/agent/kind_box/get_all.go @@ -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 +} diff --git a/service/agent/kind_box/service.go b/service/agent/kind_box/service.go index d376f35..eb6b219 100644 --- a/service/agent/kind_box/service.go +++ b/service/agent/kind_box/service.go @@ -2,12 +2,13 @@ package agentkindboxservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" ) type Repository interface { 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 { diff --git a/validator/admin/kind_box/get_all.go b/validator/admin/kind_box/get_all.go index e4cb38e..60549f7 100644 --- a/validator/admin/kind_box/get_all.go +++ b/validator/admin/kind_box/get_all.go @@ -2,6 +2,7 @@ package adminkindboxvalidator import ( "errors" + "git.gocasts.ir/ebhomengo/niki/validator" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" 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", } if err := validation.ValidateStruct(&req, - validation.Field(&req.Filter, validation.By(v.areFilterFieldsValid(validFields))), - validation.Field(&req.Sort, validation.By(v.areSortFieldsValid(validFields))), + 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) diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index a5e6495..1b5650c 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -3,12 +3,8 @@ package adminkindboxvalidator import ( "context" "fmt" - "slices" - "git.gocasts.ir/ebhomengo/niki/entity" - params "git.gocasts.ir/ebhomengo/niki/param" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" - validation "github.com/go-ozzo/ozzo-validation/v4" ) type Repository interface { @@ -76,37 +72,3 @@ func (v Validator) doesAgentExist(value interface{}) error { 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 - } -} diff --git a/validator/agent/kind_box/get_all.go b/validator/agent/kind_box/get_all.go new file mode 100644 index 0000000..b3e1e10 --- /dev/null +++ b/validator/agent/kind_box/get_all.go @@ -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 +}