From bf9f79a5249892c893f4e8704e5aa9ae826ad00b Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Tue, 9 Jul 2024 20:47:04 +0330 Subject: [PATCH] feat(niki): get all kindboxes by admin --- .../http_server/admin/kind_box/get_all.go | 67 +++++ delivery/http_server/admin/kind_box/route.go | 1 + docs/docs.go | 261 ++++++++++++++++++ docs/swagger.json | 261 ++++++++++++++++++ docs/swagger.yaml | 181 ++++++++++++ entity/admin_permission.go | 17 +- param/admin/kind_box/get_all.go | 14 +- repository/mysql/kind_box/get.go | 5 - repository/mysql/kind_box/get_all.go | 47 ++++ repository/mysql/kind_box_req/get_all.go | 2 - ...s_controls_table_permission_enum_field.sql | 4 +- ...565_insert_admin_access_controls_table.sql | 6 +- service/admin/kind_box/get_all.go | 27 ++ service/admin/kind_box/service.go | 4 +- service/benefactor/kind_box/get_all.go | 3 +- service/benefactor/kind_box/service.go | 3 +- validator/admin/kind_box/get_all.go | 45 +++ validator/admin/kind_box/validator.go | 37 +++ 18 files changed, 962 insertions(+), 23 deletions(-) create mode 100644 delivery/http_server/admin/kind_box/get_all.go create mode 100644 repository/mysql/kind_box/get_all.go create mode 100644 service/admin/kind_box/get_all.go create mode 100644 validator/admin/kind_box/get_all.go diff --git a/delivery/http_server/admin/kind_box/get_all.go b/delivery/http_server/admin/kind_box/get_all.go new file mode 100644 index 0000000..4f908aa --- /dev/null +++ b/delivery/http_server/admin/kind_box/get_all.go @@ -0,0 +1,67 @@ +package adminkindboxhandler + +import ( + "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/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 KindBoxes by admin +// @Description Retrieves a list of all KindBoxes with filtering, sorting, and pagination options +// @Tags KindBox +// @Accept json +// @Produce json +// @Param filter_id query int false "Filter by ID" +// @Param filter_kind_box_req_id query int false "Filter by KindBox request ID" +// @Param filter_benefactor_id query int false "Filter by benefactor ID" +// @Param filter_kind_box_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up) +// @Param filter_amount query int false "Filter by amount" +// @Param filter_serial_number query string false "Filter by serial number" +// @Param filter_status query string false "Filter by status" Enums(delivered,ready-to-return,assigned-receiver-agent,returned,enumerated) +// @Param filter_delivered_at query string false "Filter by delivered at" Format(date-time) +// @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 filter_receiver_agent_id query int false "Filter by receiver agent ID" +// @Param filter_returned_at query string false "Filter by returned at" Format(date-time) +// @Param filter_sender_agent_id query int false "Filter by sender agent ID" +// @Param filter_deliver_refer_date query string false "Filter by deliver refer date" Format(date) +// @Param filter_deliver_address_id query int false "Filter by deliver 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,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id) +// @Param sort_direction query string false "Sort order" Enums(asc,desc) +// @Success 200 {object} param.KindBoxGetAllResponse +// @Failure 400 {string} "Bad request" +// @Security AuthBearerAdmin +// @Router /admin/kindboxes [get]. +func (h Handler) GetAll(c echo.Context) error { + var req param.KindBoxGetAllRequest + + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + req.Filter = queryparam.GetFilterParams(c) + if fieldErrors, err := h.adminKindBoxVld.ValidateGetAll(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req) + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusOK, resp) +} diff --git a/delivery/http_server/admin/kind_box/route.go b/delivery/http_server/admin/kind_box/route.go index 99d5b38..ec829d2 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/kind_box/route.go @@ -13,4 +13,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)) } diff --git a/docs/docs.go b/docs/docs.go index 903f736..f38ad43 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -267,6 +267,200 @@ const docTemplate = `{ } } }, + "/admin/kindboxes": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBox" + ], + "summary": "Get all KindBoxes by admin", + "parameters": [ + { + "type": "integer", + "description": "Filter by ID", + "name": "filter_id", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by KindBox request ID", + "name": "filter_kind_box_req_id", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by benefactor ID", + "name": "filter_benefactor_id", + "in": "query" + }, + { + "enum": [ + "on-table", + "cylindrical", + "stand-up" + ], + "type": "string", + "description": "Filter by KindBox type", + "name": "filter_kind_box_type", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by amount", + "name": "filter_amount", + "in": "query" + }, + { + "type": "string", + "description": "Filter by serial number", + "name": "filter_serial_number", + "in": "query" + }, + { + "enum": [ + "delivered", + "ready-to-return", + "assigned-receiver-agent", + "returned", + "enumerated" + ], + "type": "string", + "description": "Filter by status", + "name": "filter_status", + "in": "query" + }, + { + "type": "string", + "format": "date-time", + "description": "Filter by delivered at", + "name": "filter_delivered_at", + "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": "Filter by receiver agent ID", + "name": "filter_receiver_agent_id", + "in": "query" + }, + { + "type": "string", + "format": "date-time", + "description": "Filter by returned at", + "name": "filter_returned_at", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by sender agent ID", + "name": "filter_sender_agent_id", + "in": "query" + }, + { + "type": "string", + "format": "date", + "description": "Filter by deliver refer date", + "name": "filter_deliver_refer_date", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by deliver address ID", + "name": "filter_deliver_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", + "kind_box_req_id", + "benefactor_id", + "kind_box_type", + "amount", + "serial_number", + "status", + "delivered_at", + "return_refer_time_id", + "return_refer_date", + "return_address_id", + "receiver_agent_id", + "returned_at", + "sender_agent_id", + "deliver_refer_date", + "deliver_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/adminkindboxparam.KindBoxGetAllResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxes/assign-receiver-agent/{id}": { "patch": { "security": [ @@ -1520,6 +1714,20 @@ const docTemplate = `{ } } }, + "adminkindboxparam.KindBoxGetAllResponse": { + "type": "object", + "properties": { + "all_kind_box": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.KindBox" + } + }, + "pagination": { + "$ref": "#/definitions/param.PaginationResponse" + } + } + }, "adminkindboxparam.KindBoxGetResponse": { "type": "object", "properties": { @@ -2289,6 +2497,59 @@ const docTemplate = `{ "FemaleGender" ] }, + "entity.KindBox": { + "type": "object", + "properties": { + "amount": { + "type": "integer" + }, + "benefactorID": { + "type": "integer" + }, + "deliverAddressID": { + "type": "integer" + }, + "deliverReferDate": { + "type": "string" + }, + "deliveredAt": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "kindBoxReqID": { + "type": "integer" + }, + "kindBoxType": { + "$ref": "#/definitions/entity.KindBoxType" + }, + "receiverAgentID": { + "type": "integer" + }, + "returnAddressID": { + "type": "integer" + }, + "returnReferDate": { + "type": "string" + }, + "returnReferTimeID": { + "type": "integer" + }, + "returnedAt": { + "type": "string" + }, + "senderAgentID": { + "type": "integer" + }, + "serialNumber": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/entity.KindBoxStatus" + } + } + }, "entity.KindBoxReq": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 80f20ed..7561b6a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -256,6 +256,200 @@ } } }, + "/admin/kindboxes": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBox" + ], + "summary": "Get all KindBoxes by admin", + "parameters": [ + { + "type": "integer", + "description": "Filter by ID", + "name": "filter_id", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by KindBox request ID", + "name": "filter_kind_box_req_id", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by benefactor ID", + "name": "filter_benefactor_id", + "in": "query" + }, + { + "enum": [ + "on-table", + "cylindrical", + "stand-up" + ], + "type": "string", + "description": "Filter by KindBox type", + "name": "filter_kind_box_type", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by amount", + "name": "filter_amount", + "in": "query" + }, + { + "type": "string", + "description": "Filter by serial number", + "name": "filter_serial_number", + "in": "query" + }, + { + "enum": [ + "delivered", + "ready-to-return", + "assigned-receiver-agent", + "returned", + "enumerated" + ], + "type": "string", + "description": "Filter by status", + "name": "filter_status", + "in": "query" + }, + { + "type": "string", + "format": "date-time", + "description": "Filter by delivered at", + "name": "filter_delivered_at", + "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": "Filter by receiver agent ID", + "name": "filter_receiver_agent_id", + "in": "query" + }, + { + "type": "string", + "format": "date-time", + "description": "Filter by returned at", + "name": "filter_returned_at", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by sender agent ID", + "name": "filter_sender_agent_id", + "in": "query" + }, + { + "type": "string", + "format": "date", + "description": "Filter by deliver refer date", + "name": "filter_deliver_refer_date", + "in": "query" + }, + { + "type": "integer", + "description": "Filter by deliver address ID", + "name": "filter_deliver_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", + "kind_box_req_id", + "benefactor_id", + "kind_box_type", + "amount", + "serial_number", + "status", + "delivered_at", + "return_refer_time_id", + "return_refer_date", + "return_address_id", + "receiver_agent_id", + "returned_at", + "sender_agent_id", + "deliver_refer_date", + "deliver_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/adminkindboxparam.KindBoxGetAllResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxes/assign-receiver-agent/{id}": { "patch": { "security": [ @@ -1509,6 +1703,20 @@ } } }, + "adminkindboxparam.KindBoxGetAllResponse": { + "type": "object", + "properties": { + "all_kind_box": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.KindBox" + } + }, + "pagination": { + "$ref": "#/definitions/param.PaginationResponse" + } + } + }, "adminkindboxparam.KindBoxGetResponse": { "type": "object", "properties": { @@ -2278,6 +2486,59 @@ "FemaleGender" ] }, + "entity.KindBox": { + "type": "object", + "properties": { + "amount": { + "type": "integer" + }, + "benefactorID": { + "type": "integer" + }, + "deliverAddressID": { + "type": "integer" + }, + "deliverReferDate": { + "type": "string" + }, + "deliveredAt": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "kindBoxReqID": { + "type": "integer" + }, + "kindBoxType": { + "$ref": "#/definitions/entity.KindBoxType" + }, + "receiverAgentID": { + "type": "integer" + }, + "returnAddressID": { + "type": "integer" + }, + "returnReferDate": { + "type": "string" + }, + "returnReferTimeID": { + "type": "integer" + }, + "returnedAt": { + "type": "string" + }, + "senderAgentID": { + "type": "integer" + }, + "serialNumber": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/entity.KindBoxStatus" + } + } + }, "entity.KindBoxReq": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a2d9d20..64081bf 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -77,6 +77,15 @@ definitions: receiver_agent_id: type: integer type: object + adminkindboxparam.KindBoxGetAllResponse: + properties: + all_kind_box: + items: + $ref: '#/definitions/entity.KindBox' + type: array + pagination: + $ref: '#/definitions/param.PaginationResponse' + type: object adminkindboxparam.KindBoxGetResponse: properties: amount: @@ -583,6 +592,41 @@ definitions: x-enum-varnames: - MaleGender - FemaleGender + entity.KindBox: + properties: + amount: + type: integer + benefactorID: + type: integer + deliverAddressID: + type: integer + deliverReferDate: + type: string + deliveredAt: + type: string + id: + type: integer + kindBoxReqID: + type: integer + kindBoxType: + $ref: '#/definitions/entity.KindBoxType' + receiverAgentID: + type: integer + returnAddressID: + type: integer + returnReferDate: + type: string + returnReferTimeID: + type: integer + returnedAt: + type: string + senderAgentID: + type: integer + serialNumber: + type: string + status: + $ref: '#/definitions/entity.KindBoxStatus' + type: object entity.KindBoxReq: properties: benefactorID: @@ -829,6 +873,143 @@ paths: summary: Get all provinces tags: - Address + /admin/kindboxes: + get: + consumes: + - application/json + description: Retrieves a list of all KindBoxes with filtering, sorting, and + pagination options + parameters: + - description: Filter by ID + in: query + name: filter_id + type: integer + - description: Filter by KindBox request ID + in: query + name: filter_kind_box_req_id + type: integer + - description: Filter by benefactor ID + in: query + name: filter_benefactor_id + type: integer + - description: Filter by KindBox type + enum: + - on-table + - cylindrical + - stand-up + in: query + name: filter_kind_box_type + type: string + - description: Filter by amount + in: query + name: filter_amount + type: integer + - description: Filter by serial number + in: query + name: filter_serial_number + type: string + - description: Filter by status + enum: + - delivered + - ready-to-return + - assigned-receiver-agent + - returned + - enumerated + in: query + name: filter_status + type: string + - description: Filter by delivered at + format: date-time + in: query + name: filter_delivered_at + 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: Filter by receiver agent ID + in: query + name: filter_receiver_agent_id + type: integer + - description: Filter by returned at + format: date-time + in: query + name: filter_returned_at + type: string + - description: Filter by sender agent ID + in: query + name: filter_sender_agent_id + type: integer + - description: Filter by deliver refer date + format: date + in: query + name: filter_deliver_refer_date + type: string + - description: Filter by deliver address ID + in: query + name: filter_deliver_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 + - kind_box_req_id + - benefactor_id + - kind_box_type + - amount + - serial_number + - status + - delivered_at + - return_refer_time_id + - return_refer_date + - return_address_id + - receiver_agent_id + - returned_at + - sender_agent_id + - deliver_refer_date + - deliver_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/adminkindboxparam.KindBoxGetAllResponse' + "400": + description: Bad request + schema: + type: string + security: + - AuthBearerAdmin: [] + summary: Get all KindBoxes by admin + tags: + - KindBox /admin/kindboxes/{id}: get: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 60ce2a4..5cd9ae6 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -3,15 +3,16 @@ package entity type AdminPermission string const ( - AdminAdminRegisterPermission = AdminPermission("admin-register") - AdminKindBoxReqAcceptPermission = AdminPermission("kindboxreq-accept") - AdminKindBoxReqAddPermission = AdminPermission("kindboxreq-add") - AdminKindBoxReqRejectPermission = AdminPermission("kindboxreq-reject") - AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall") - AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") - AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") - AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") + AdminAdminRegisterPermission = AdminPermission("admin-register") + AdminKindBoxReqAcceptPermission = AdminPermission("kindboxreq-accept") + AdminKindBoxReqAddPermission = AdminPermission("kindboxreq-add") + AdminKindBoxReqRejectPermission = AdminPermission("kindboxreq-reject") + AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall") + AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") + AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") + AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery") AdminKindBoxGetPermission = AdminPermission("kindbox-get") AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent") + AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall") ) diff --git a/param/admin/kind_box/get_all.go b/param/admin/kind_box/get_all.go index bd4f255..999a074 100644 --- a/param/admin/kind_box/get_all.go +++ b/param/admin/kind_box/get_all.go @@ -1,9 +1,17 @@ package adminkindboxparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/param" +) -type KindBoxGetAllRequest struct{} +type KindBoxGetAllRequest struct { + Pagination param.PaginationRequest + Sort param.SortRequest + Filter param.FilterRequest +} type KindBoxGetAllResponse struct { - AllKindBox []entity.KindBox + AllKindBox []entity.KindBox `json:"all_kind_box"` + Pagination param.PaginationResponse `json:"pagination"` } diff --git a/repository/mysql/kind_box/get.go b/repository/mysql/kind_box/get.go index e4a37e6..ac2ce58 100644 --- a/repository/mysql/kind_box/get.go +++ b/repository/mysql/kind_box/get.go @@ -27,8 +27,3 @@ func (d DB) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, err return k, nil } - -func (d DB) GetAllKindBox(ctx context.Context, benefactorID uint) ([]entity.KindBox, error) { - // TODO implement me - panic("implement me") -} diff --git a/repository/mysql/kind_box/get_all.go b/repository/mysql/kind_box/get_all.go new file mode 100644 index 0000000..80878bc --- /dev/null +++ b/repository/mysql/kind_box/get_all.go @@ -0,0 +1,47 @@ +package mysqlkindbox + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) { + const op = "mysqlkindbox.GetAllKindBox" + + baseQuery := `SELECT * FROM kind_boxes WHERE deleted_at IS NULL` + query, args := builder.BuildGetAllQuery(baseQuery, filter, pagination, sort) + rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...) + if qErr != nil { + return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + defer rows.Close() + kindBoxes := make([]entity.KindBox, 0) + for rows.Next() { + kindBox, sErr := scanKindBox(rows) + if sErr != nil { + return nil, 0, richerror.New(op).WithErr(sErr). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + kindBoxes = append(kindBoxes, kindBox) + } + + if rErr := rows.Err(); rErr != nil { + return nil, 0, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + var total uint + baseQuery = `SELECT COUNT(*) FROM kind_boxes WHERE deleted_at IS NULL` + query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort) + qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) + if qErr != nil { + return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return kindBoxes, total, nil +} diff --git a/repository/mysql/kind_box_req/get_all.go b/repository/mysql/kind_box_req/get_all.go index 4e8a40b..c496a59 100644 --- a/repository/mysql/kind_box_req/get_all.go +++ b/repository/mysql/kind_box_req/get_all.go @@ -2,7 +2,6 @@ package mysqlkindboxreq import ( "context" - "fmt" "git.gocasts.ir/ebhomengo/niki/param" builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql" @@ -25,7 +24,6 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pa for rows.Next() { kindBoxReq, sErr := scanKindBoxReq(rows) if sErr != nil { - fmt.Println(sErr) return nil, 0, richerror.New(op).WithErr(sErr). WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) } 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 81e8b59..62ef269 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 @@ -10,7 +10,9 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'admin-getall_agent', 'kindboxreq-get_awaiting_delivery', 'kindbox-get', - 'kindboxreq-add' + 'kindboxreq-add', + 'kindbox-assign_receiver_agent', + 'kindbox-getall' ) NOT NULL; -- +migrate Down \ No newline at end of file diff --git a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql index 20c44ed..c7051b1 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -19,7 +19,11 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission` (17, 1 , 'role','kindbox-get'), (18, 2 , 'role','kindbox-get'), (19, 1 , 'role','kindbox-assign_receiver_agent'), - (20, 2 , 'role','kindbox-assign_receiver_agent'); + (20, 2 , 'role','kindbox-assign_receiver_agent'), + (21, 1 , 'role','kindboxreq-add'), + (22, 2 , 'role','kindboxreq-add'), + (23, 1 , 'role','kindbox-getall'), + (24, 2 , 'role','kindbox-getall'); -- +migrate Down diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go new file mode 100644 index 0000000..cab96f1 --- /dev/null +++ b/service/admin/kind_box/get_all.go @@ -0,0 +1,27 @@ +package adminkindboxservice + +import ( + "context" + + paginationparam "git.gocasts.ir/ebhomengo/niki/param" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { + const op = "adminkindboxservice.GetAll" + + allKindBox, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort) + if err != nil { + return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err) + } + + return param.KindBoxGetAllResponse{ + AllKindBox: allKindBox, + Pagination: paginationparam.PaginationResponse{ + PageSize: req.Pagination.GetPageSize(), + PageNumber: req.Pagination.GetPageNumber(), + Total: total, + }, + }, nil +} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index ef7d37b..dca638d 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -3,7 +3,8 @@ package adminkindboxservice import ( "context" - entity "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" ) type Repository interface { @@ -11,6 +12,7 @@ type Repository interface { AddKindBox(ctx context.Context, kindBox entity.KindBox) error 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) } type Service struct { diff --git a/service/benefactor/kind_box/get_all.go b/service/benefactor/kind_box/get_all.go index 9d93fa5..f15af66 100644 --- a/service/benefactor/kind_box/get_all.go +++ b/service/benefactor/kind_box/get_all.go @@ -3,6 +3,7 @@ package benefactorkindboxservice import ( "context" + params "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -11,7 +12,7 @@ import ( func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { const op = "userkindboxservice.GetAll" - allKindBox, err := s.repo.GetAllKindBox(ctx, req.BenefactorID) + allKindBox, _, err := s.repo.GetAllKindBox(ctx, params.FilterRequest{}, params.PaginationRequest{}, params.SortRequest{}) if err != nil { return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } diff --git a/service/benefactor/kind_box/service.go b/service/benefactor/kind_box/service.go index fe7a0d0..ab66837 100644 --- a/service/benefactor/kind_box/service.go +++ b/service/benefactor/kind_box/service.go @@ -4,10 +4,11 @@ import ( "context" entity "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" ) type Repository interface { - GetAllKindBox(ctx context.Context, benefactorID uint) ([]entity.KindBox, error) + GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error } diff --git a/validator/admin/kind_box/get_all.go b/validator/admin/kind_box/get_all.go new file mode 100644 index 0000000..93e7405 --- /dev/null +++ b/validator/admin/kind_box/get_all.go @@ -0,0 +1,45 @@ +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) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]string, error) { + const op = "adminkindboxvalidator.ValidateGetAll" + + validFields := []string{ + "id", "kind_box_req_id", "benefactor_id", "kind_box_type", "amount", "serial_number", + "status", "deliver_refer_date", "deliver_address_id", "sender_agent_id", + "delivered_at", "return_refer_time_id", "return_refer_date", + "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))), + ); 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 +} diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 8bbfc49..a5e6495 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -3,9 +3,12 @@ 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 { @@ -73,3 +76,37 @@ 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 + } +}