From 47bc77baeeda836a160afeb7f1fc3701c1461b3c Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Tue, 2 Jul 2024 23:47:40 +0330 Subject: [PATCH] feat(niki): admin assign kindbox to a agent --- .../admin/kind_box/assign_receiver_agent.go | 47 +++ delivery/http_server/admin/kind_box/route.go | 1 + docs/docs.go | 368 +----------------- docs/swagger.json | 368 +----------------- docs/swagger.yaml | 251 +----------- entity/admin_permission.go | 1 + internal/initial/validator.go | 4 +- main.go | 2 +- param/admin/kind_box/assign_receiver_agent.go | 6 + pkg/err_msg/message.go | 1 + repository/mysql/admin/exist_admin.go | 14 + .../mysql/kind_box/assign_receiver_agent.go | 23 ++ ...s_controls_table_permission_enum_field.sql | 3 +- ...565_insert_admin_access_controls_table.sql | 4 +- service/admin/admin/exist.go | 18 + service/admin/admin/service.go | 1 + .../admin/kind_box/assign_receiver_agent.go | 23 ++ service/admin/kind_box/service.go | 1 + .../admin/kind_box/assign_receiver_agent.go | 43 ++ validator/admin/kind_box/validator.go | 45 ++- 20 files changed, 283 insertions(+), 941 deletions(-) create mode 100644 delivery/http_server/admin/kind_box/assign_receiver_agent.go create mode 100644 param/admin/kind_box/assign_receiver_agent.go create mode 100644 repository/mysql/kind_box/assign_receiver_agent.go create mode 100644 service/admin/admin/exist.go create mode 100644 service/admin/kind_box/assign_receiver_agent.go create mode 100644 validator/admin/kind_box/assign_receiver_agent.go diff --git a/delivery/http_server/admin/kind_box/assign_receiver_agent.go b/delivery/http_server/admin/kind_box/assign_receiver_agent.go new file mode 100644 index 0000000..db80dfe --- /dev/null +++ b/delivery/http_server/admin/kind_box/assign_receiver_agent.go @@ -0,0 +1,47 @@ +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" +) + +// AssignReceiverAgent godoc +// @Summary Admin assign receiver agent to kindbox +// @Tags KindBox +// @Accept json +// @Produce json +// @Param id path int true "KindBox ID" +// @Param Request body param.AssignReceiverRequest true "Assign Receiver Agent Request Body" +// @Success 204 +// @Failure 400 {string} "Bad request" +// @Security AuthBearerAdmin +// @Router /admin/kindboxes/assign-receiver-agent/{id} [patch]. +func (h Handler) AssignReceiverAgent(c echo.Context) error { + var req param.AssignReceiverRequest + + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + if fieldErrors, err := h.adminKindBoxVld.ValidateAssignReceiverAgent(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + sErr := h.adminKindBoxSvc.AssignReceiverAgent(c.Request().Context(), req) + + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusNoContent, nil) +} diff --git a/delivery/http_server/admin/kind_box/route.go b/delivery/http_server/admin/kind_box/route.go index 6504856..99d5b38 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/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.AdminKindBoxGetPermission)) + r.PATCH("/assign-receiver-agent/:id", h.AssignReceiverAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxAssignReceiverAgentPermission)) } diff --git a/docs/docs.go b/docs/docs.go index dc65d07..0238a13 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -267,14 +267,13 @@ const docTemplate = `{ } } }, - "/admin/kindboxes/{id}": { - "get": { + "/admin/kindboxes/assign-receiver-agent/{id}": { + "patch": { "security": [ { "AuthBearerAdmin": [] } ], - "description": "This endpoint retrieves a specific kind box by admin", "consumes": [ "application/json" ], @@ -284,22 +283,28 @@ const docTemplate = `{ "tags": [ "KindBox" ], - "summary": "Get a specific kind box by admin", + "summary": "Admin assign receiver agent to kindbox", "parameters": [ { "type": "integer", - "description": "Kind box ID", + "description": "KindBox ID", "name": "id", "in": "path", "required": true + }, + { + "description": "Assign Receiver Agent Request Body", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxparam.AssignReceiverRequest" + } } ], "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse" - } + "204": { + "description": "No Content" }, "400": { "description": "Bad request", @@ -355,48 +360,6 @@ const docTemplate = `{ } } } - }, - "post": { - "security": [ - { - "AuthBearerAdmin": [] - } - ], - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "KindBoxReq" - ], - "summary": "Add a new kind box request for a benefactor by admin", - "parameters": [ - { - "description": "New kind box request details", - "name": "Request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqAddRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqAddResponse" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } } }, "/admin/kindboxreqs/accept-kind-box-req/{id}": { @@ -501,135 +464,6 @@ const docTemplate = `{ } } }, - "/admin/kindboxreqs/awaiting-delivery": { - "get": { - "security": [ - { - "AuthBearerAdmin": [] - } - ], - "description": "Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "KindBoxReq" - ], - "summary": "Get all awaiting delivery KindBox requests", - "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_kind_box_type", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by count requested", - "name": "filter_count_requested", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by count accepted", - "name": "filter_count_accepted", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by deliver refer time ID", - "name": "filter_deliver_refer_time_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", - "benefactor_id", - "kind_box_type", - "count_requested", - "count_accepted", - "deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } - } - }, "/admin/kindboxreqs/awaiting-delivery/{id}": { "get": { "security": [ @@ -881,7 +715,7 @@ const docTemplate = `{ } } }, - "/benefactor/kindboxes": { + "/benefactor/kindboxes/": { "get": { "security": [ { @@ -957,57 +791,6 @@ const docTemplate = `{ } } }, - "/benefactor/kindboxes/{id}/emptying-requests": { - "patch": { - "security": [ - { - "AuthBearerBenefactor": [] - } - ], - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Benefactor" - ], - "summary": "Register a new emptying request for a kind box by benefactor", - "parameters": [ - { - "type": "integer", - "description": "KindBox ID", - "name": "id", - "in": "path", - "required": true - }, - { - "description": "Request body", - "name": "Request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/benefactorkindboxparam.KindBoxRegisterEmptyingRequest" - } - } - ], - "responses": { - "204": { - "description": "No content", - "schema": { - "type": "string" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } - } - }, "/benefactor/kindboxreqs/": { "post": { "security": [ @@ -1324,56 +1107,11 @@ const docTemplate = `{ } } }, - "adminkindboxparam.KindBoxGetResponse": { + "adminkindboxparam.AssignReceiverRequest": { "type": "object", "properties": { - "amount": { + "receiver_agent_id": { "type": "integer" - }, - "benefactorID": { - "type": "integer" - }, - "deliverAddressID": { - "type": "integer" - }, - "deliverReferDate": { - "type": "string" - }, - "deliveredAt": { - "type": "string" - }, - "id": { - "type": "integer" - }, - "kindBoxReqID": { - "type": "integer" - }, - "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": { - "$ref": "#/definitions/entity.KindBoxType" } } }, @@ -1391,20 +1129,6 @@ const docTemplate = `{ "adminkindboxreqparam.DeliverKindBoxReqResponse": { "type": "object" }, - "adminkindboxreqparam.DeliveryAwaitingGetAllResponse": { - "type": "object", - "properties": { - "all_awaiting_kind_box_req": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.KindBoxReq" - } - }, - "pagination": { - "$ref": "#/definitions/param.PaginationResponse" - } - } - }, "adminkindboxreqparam.DeliveryAwaitingGetResponse": { "type": "object", "properties": { @@ -1477,47 +1201,10 @@ const docTemplate = `{ } } }, - "adminkindboxreqparam.KindBoxReqAddRequest": { - "type": "object", - "properties": { - "benefactor_id": { - "type": "integer", - "example": 1 - }, - "count_requested": { - "type": "integer", - "example": 2 - }, - "deliver_address_id": { - "type": "integer", - "example": 1 - }, - "deliver_refer_date": { - "type": "string", - "example": "2025-01-02 15:04:05" - }, - "type_id": { - "allOf": [ - { - "$ref": "#/definitions/entity.KindBoxType" - } - ], - "example": 1 - } - } - }, - "adminkindboxreqparam.KindBoxReqAddResponse": { - "type": "object", - "properties": { - "kindBoxReq": { - "$ref": "#/definitions/entity.KindBoxReq" - } - } - }, "adminkindboxreqparam.KindBoxReqGetAllResponse": { "type": "object", "properties": { - "all_awaiting_kind_box_req": { + "all_kind_box_req": { "type": "array", "items": { "$ref": "#/definitions/entity.KindBoxReq" @@ -1894,23 +1581,6 @@ const docTemplate = `{ } } }, - "benefactorkindboxparam.KindBoxRegisterEmptyingRequest": { - "type": "object", - "properties": { - "return_address_id": { - "type": "integer", - "example": 1 - }, - "return_refer_date": { - "type": "string", - "example": "2025-01-02T15:04:05Z" - }, - "return_refer_time_id": { - "type": "integer", - "example": 1 - } - } - }, "benefactorkindboxreqparam.KindBoxReqAddRequest": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 87ebdf6..4a0d369 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -256,14 +256,13 @@ } } }, - "/admin/kindboxes/{id}": { - "get": { + "/admin/kindboxes/assign-receiver-agent/{id}": { + "patch": { "security": [ { "AuthBearerAdmin": [] } ], - "description": "This endpoint retrieves a specific kind box by admin", "consumes": [ "application/json" ], @@ -273,22 +272,28 @@ "tags": [ "KindBox" ], - "summary": "Get a specific kind box by admin", + "summary": "Admin assign receiver agent to kindbox", "parameters": [ { "type": "integer", - "description": "Kind box ID", + "description": "KindBox ID", "name": "id", "in": "path", "required": true + }, + { + "description": "Assign Receiver Agent Request Body", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxparam.AssignReceiverRequest" + } } ], "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse" - } + "204": { + "description": "No Content" }, "400": { "description": "Bad request", @@ -344,48 +349,6 @@ } } } - }, - "post": { - "security": [ - { - "AuthBearerAdmin": [] - } - ], - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "KindBoxReq" - ], - "summary": "Add a new kind box request for a benefactor by admin", - "parameters": [ - { - "description": "New kind box request details", - "name": "Request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqAddRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/adminkindboxreqparam.KindBoxReqAddResponse" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } } }, "/admin/kindboxreqs/accept-kind-box-req/{id}": { @@ -490,135 +453,6 @@ } } }, - "/admin/kindboxreqs/awaiting-delivery": { - "get": { - "security": [ - { - "AuthBearerAdmin": [] - } - ], - "description": "Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "KindBoxReq" - ], - "summary": "Get all awaiting delivery KindBox requests", - "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_kind_box_type", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by count requested", - "name": "filter_count_requested", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by count accepted", - "name": "filter_count_accepted", - "in": "query" - }, - { - "type": "integer", - "description": "Filter by deliver refer time ID", - "name": "filter_deliver_refer_time_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", - "benefactor_id", - "kind_box_type", - "count_requested", - "count_accepted", - "deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } - } - }, "/admin/kindboxreqs/awaiting-delivery/{id}": { "get": { "security": [ @@ -870,7 +704,7 @@ } } }, - "/benefactor/kindboxes": { + "/benefactor/kindboxes/": { "get": { "security": [ { @@ -946,57 +780,6 @@ } } }, - "/benefactor/kindboxes/{id}/emptying-requests": { - "patch": { - "security": [ - { - "AuthBearerBenefactor": [] - } - ], - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Benefactor" - ], - "summary": "Register a new emptying request for a kind box by benefactor", - "parameters": [ - { - "type": "integer", - "description": "KindBox ID", - "name": "id", - "in": "path", - "required": true - }, - { - "description": "Request body", - "name": "Request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/benefactorkindboxparam.KindBoxRegisterEmptyingRequest" - } - } - ], - "responses": { - "204": { - "description": "No content", - "schema": { - "type": "string" - } - }, - "400": { - "description": "Bad request", - "schema": { - "type": "string" - } - } - } - } - }, "/benefactor/kindboxreqs/": { "post": { "security": [ @@ -1313,56 +1096,11 @@ } } }, - "adminkindboxparam.KindBoxGetResponse": { + "adminkindboxparam.AssignReceiverRequest": { "type": "object", "properties": { - "amount": { + "receiver_agent_id": { "type": "integer" - }, - "benefactorID": { - "type": "integer" - }, - "deliverAddressID": { - "type": "integer" - }, - "deliverReferDate": { - "type": "string" - }, - "deliveredAt": { - "type": "string" - }, - "id": { - "type": "integer" - }, - "kindBoxReqID": { - "type": "integer" - }, - "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": { - "$ref": "#/definitions/entity.KindBoxType" } } }, @@ -1380,20 +1118,6 @@ "adminkindboxreqparam.DeliverKindBoxReqResponse": { "type": "object" }, - "adminkindboxreqparam.DeliveryAwaitingGetAllResponse": { - "type": "object", - "properties": { - "all_awaiting_kind_box_req": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.KindBoxReq" - } - }, - "pagination": { - "$ref": "#/definitions/param.PaginationResponse" - } - } - }, "adminkindboxreqparam.DeliveryAwaitingGetResponse": { "type": "object", "properties": { @@ -1466,47 +1190,10 @@ } } }, - "adminkindboxreqparam.KindBoxReqAddRequest": { - "type": "object", - "properties": { - "benefactor_id": { - "type": "integer", - "example": 1 - }, - "count_requested": { - "type": "integer", - "example": 2 - }, - "deliver_address_id": { - "type": "integer", - "example": 1 - }, - "deliver_refer_date": { - "type": "string", - "example": "2025-01-02 15:04:05" - }, - "type_id": { - "allOf": [ - { - "$ref": "#/definitions/entity.KindBoxType" - } - ], - "example": 1 - } - } - }, - "adminkindboxreqparam.KindBoxReqAddResponse": { - "type": "object", - "properties": { - "kindBoxReq": { - "$ref": "#/definitions/entity.KindBoxReq" - } - } - }, "adminkindboxreqparam.KindBoxReqGetAllResponse": { "type": "object", "properties": { - "all_awaiting_kind_box_req": { + "all_kind_box_req": { "type": "array", "items": { "$ref": "#/definitions/entity.KindBoxReq" @@ -1883,23 +1570,6 @@ } } }, - "benefactorkindboxparam.KindBoxRegisterEmptyingRequest": { - "type": "object", - "properties": { - "return_address_id": { - "type": "integer", - "example": 1 - }, - "return_refer_date": { - "type": "string", - "example": "2025-01-02T15:04:05Z" - }, - "return_refer_time_id": { - "type": "integer", - "example": 1 - } - } - }, "benefactorkindboxreqparam.KindBoxReqAddRequest": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b3981e5..b6e5f0a 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -72,40 +72,10 @@ definitions: example: "1234567890" type: string type: object - adminkindboxparam.KindBoxGetResponse: + adminkindboxparam.AssignReceiverRequest: properties: - amount: + receiver_agent_id: type: integer - benefactorID: - type: integer - deliverAddressID: - type: integer - deliverReferDate: - type: string - deliveredAt: - type: string - id: - type: integer - kindBoxReqID: - type: integer - 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: - $ref: '#/definitions/entity.KindBoxType' type: object adminkindboxreqparam.AssignSenderRequest: properties: @@ -116,15 +86,6 @@ definitions: type: object adminkindboxreqparam.DeliverKindBoxReqResponse: type: object - adminkindboxreqparam.DeliveryAwaitingGetAllResponse: - properties: - all_awaiting_kind_box_req: - items: - $ref: '#/definitions/entity.KindBoxReq' - type: array - pagination: - $ref: '#/definitions/param.PaginationResponse' - type: object adminkindboxreqparam.DeliveryAwaitingGetResponse: properties: benefactorID: @@ -172,33 +133,9 @@ definitions: kind_box_req_status: $ref: '#/definitions/entity.KindBoxReqStatus' type: object - adminkindboxreqparam.KindBoxReqAddRequest: - properties: - benefactor_id: - example: 1 - type: integer - count_requested: - example: 2 - type: integer - deliver_address_id: - example: 1 - type: integer - deliver_refer_date: - example: "2025-01-02 15:04:05" - type: string - type_id: - allOf: - - $ref: '#/definitions/entity.KindBoxType' - example: 1 - type: object - adminkindboxreqparam.KindBoxReqAddResponse: - properties: - kindBoxReq: - $ref: '#/definitions/entity.KindBoxReq' - type: object adminkindboxreqparam.KindBoxReqGetAllResponse: properties: - all_awaiting_kind_box_req: + all_kind_box_req: items: $ref: '#/definitions/entity.KindBoxReq' type: array @@ -444,18 +381,6 @@ definitions: type: $ref: '#/definitions/entity.KindBoxType' type: object - benefactorkindboxparam.KindBoxRegisterEmptyingRequest: - properties: - return_address_id: - example: 1 - type: integer - return_refer_date: - example: "2025-01-02T15:04:05Z" - type: string - return_refer_time_id: - example: 1 - type: integer - type: object benefactorkindboxreqparam.KindBoxReqAddRequest: properties: benefactor_id: @@ -815,31 +740,34 @@ paths: summary: Get all provinces tags: - Address - /admin/kindboxes/{id}: - get: + /admin/kindboxes/assign-receiver-agent/{id}: + patch: consumes: - application/json - description: This endpoint retrieves a specific kind box by admin parameters: - - description: Kind box ID + - description: KindBox ID in: path name: id required: true type: integer + - description: Assign Receiver Agent Request Body + in: body + name: Request + required: true + schema: + $ref: '#/definitions/adminkindboxparam.AssignReceiverRequest' produces: - application/json responses: - "200": - description: OK - schema: - $ref: '#/definitions/adminkindboxparam.KindBoxGetResponse' + "204": + description: No Content "400": description: Bad request schema: type: string security: - AuthBearerAdmin: [] - summary: Get a specific kind box by admin + summary: Admin assign receiver agent to kindbox tags: - KindBox /admin/kindboxreqs: @@ -871,32 +799,6 @@ paths: summary: Admin get all kindboxreq tags: - KindBoxReq - post: - consumes: - - application/json - parameters: - - description: New kind box request details - in: body - name: Request - required: true - schema: - $ref: '#/definitions/adminkindboxreqparam.KindBoxReqAddRequest' - produces: - - application/json - responses: - "200": - description: OK - schema: - $ref: '#/definitions/adminkindboxreqparam.KindBoxReqAddResponse' - "400": - description: Bad request - schema: - type: string - security: - - AuthBearerAdmin: [] - summary: Add a new kind box request for a benefactor by admin - tags: - - KindBoxReq /admin/kindboxreqs/accept-kind-box-req/{id}: patch: consumes: @@ -961,95 +863,6 @@ paths: summary: Admin Assign Sender Agent to kindboxreq tags: - KindBoxReq - /admin/kindboxreqs/awaiting-delivery: - get: - consumes: - - application/json - description: Retrieves a list of all awaiting KindBox requests 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_kind_box_type - type: integer - - description: Filter by count requested - in: query - name: filter_count_requested - type: integer - - description: Filter by count accepted - in: query - name: filter_count_accepted - type: integer - - description: Filter by deliver refer time ID - in: query - name: filter_deliver_refer_time_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 - - benefactor_id - - kind_box_type - - count_requested - - count_accepted - - deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse' - "400": - description: Bad request - schema: - type: string - security: - - AuthBearerAdmin: [] - summary: Get all awaiting delivery KindBox requests - tags: - - KindBoxReq /admin/kindboxreqs/awaiting-delivery/{id}: get: consumes: @@ -1206,7 +1019,7 @@ paths: summary: Register an admin by super-admin tags: - Admin - /benefactor/kindboxes: + /benefactor/kindboxes/: get: consumes: - application/json @@ -1254,38 +1067,6 @@ paths: summary: Get a specific kind box for a benefactor tags: - KindBox - /benefactor/kindboxes/{id}/emptying-requests: - patch: - consumes: - - application/json - parameters: - - description: KindBox ID - in: path - name: id - required: true - type: integer - - description: Request body - in: body - name: Request - required: true - schema: - $ref: '#/definitions/benefactorkindboxparam.KindBoxRegisterEmptyingRequest' - produces: - - application/json - responses: - "204": - description: No content - schema: - type: string - "400": - description: Bad request - schema: - type: string - security: - - AuthBearerBenefactor: [] - summary: Register a new emptying request for a kind box by benefactor - tags: - - Benefactor /benefactor/kindboxreqs/: post: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 45f1b5c..60ce2a4 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -13,4 +13,5 @@ const ( AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery") AdminKindBoxGetPermission = AdminPermission("kindbox-get") + AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent") ) diff --git a/internal/initial/validator.go b/internal/initial/validator.go index bdcad23..52d5da5 100644 --- a/internal/initial/validator.go +++ b/internal/initial/validator.go @@ -51,8 +51,8 @@ func InitBenefactorAddressValidator(cfg config.Config, redisAdapter redis.Adapte ) } -func InitAdminKindBoxValidator(db *mysql.DB) adminkindboxvalidator.Validator { - return adminkindboxvalidator.New(InitKindBoxRepo(db)) +func InitAdminKindBoxValidator(db *mysql.DB, cfg config.Config) adminkindboxvalidator.Validator { + return adminkindboxvalidator.New(InitKindBoxRepo(db), InitAdminService(cfg, db)) } func InitBenefactorKindBoxValidator(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorkindboxvalidator.Validator { diff --git a/main.go b/main.go index df662ae..6435755 100644 --- a/main.go +++ b/main.go @@ -65,7 +65,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db), AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg), AdminVld: initial.InitAdminValidator(db), - AdminKindBoxVld: initial.InitAdminKindBoxValidator(db), + AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg), }, initial.Services{ BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db), diff --git a/param/admin/kind_box/assign_receiver_agent.go b/param/admin/kind_box/assign_receiver_agent.go new file mode 100644 index 0000000..f60ce52 --- /dev/null +++ b/param/admin/kind_box/assign_receiver_agent.go @@ -0,0 +1,6 @@ +package adminkindboxparam + +type AssignReceiverRequest struct { + KindBoxID uint `json:"-" param:"id"` + ReceiverAgentID uint `json:"receiver_agent_id"` +} diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 1f77364..4c5ef34 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -33,4 +33,5 @@ const ( ErrorMsgSortFieldIsRequired = "sort field is required" ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortFieldIsNotValid = "sort field is not valid" + ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" ) diff --git a/repository/mysql/admin/exist_admin.go b/repository/mysql/admin/exist_admin.go index fe8e244..c2904d7 100644 --- a/repository/mysql/admin/exist_admin.go +++ b/repository/mysql/admin/exist_admin.go @@ -75,3 +75,17 @@ func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error return admin, nil } + +func (d DB) AgentExistByID(ctx context.Context, agentID uint) (bool, error) { + const op = "mysqladmin.AgentExistByID" + + query := `select count(*) from admins where role = ? and id = ?` + var count int + err := d.conn.Conn().QueryRowContext(ctx, query, entity.AdminAgentRole.String(), agentID).Scan(&count) + if err != nil { + return false, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return count > 0, nil +} diff --git a/repository/mysql/kind_box/assign_receiver_agent.go b/repository/mysql/kind_box/assign_receiver_agent.go new file mode 100644 index 0000000..25eb83c --- /dev/null +++ b/repository/mysql/kind_box/assign_receiver_agent.go @@ -0,0 +1,23 @@ +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) AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error { + const op = "mysqlkindbox.AssignReceiverAgent" + + query := `UPDATE kind_boxes SET receiver_agent_id = ?, status = ? WHERE status = ? AND id = ?` + _, err := d.conn.Conn().ExecContext(ctx, query, + kindBox.ReceiverAgentID, entity.KindBoxAssignedReceiverAgentStatus.String(), entity.KindBoxReadyToReturnStatus.String(), kindBox.ID) + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return nil +} 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 9ebd6a0..c7b021d 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 @@ -9,7 +9,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindboxreq-assign_sender_agent', 'admin-getall_agent', 'kindboxreq-get_awaiting_delivery', - 'kindbox-get' + 'kindbox-get', + 'kindbox-assign_receiver_agent' ) 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 63822ac..20c44ed 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -17,7 +17,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission` (15, 1 , 'role','kindboxreq-get_awaiting_delivery'), (16, 3 , 'role','kindboxreq-get_awaiting_delivery'), (17, 1 , 'role','kindbox-get'), - (18, 2 , 'role','kindbox-get'); + (18, 2 , 'role','kindbox-get'), + (19, 1 , 'role','kindbox-assign_receiver_agent'), + (20, 2 , 'role','kindbox-assign_receiver_agent'); -- +migrate Down diff --git a/service/admin/admin/exist.go b/service/admin/admin/exist.go new file mode 100644 index 0000000..d4b3c3e --- /dev/null +++ b/service/admin/admin/exist.go @@ -0,0 +1,18 @@ +package adminservice + +import ( + "context" + + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) AgentExistByID(ctx context.Context, agentID uint) (bool, error) { + const op = "adminservice.AgentExistByID" + + exists, err := s.repo.AgentExistByID(ctx, agentID) + if err != nil { + return false, richerror.New(op).WithErr(err) + } + + return exists, nil +} diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index 5921ba4..0272427 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -19,6 +19,7 @@ type Repository interface { GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) GetAllAgent(ctx context.Context) ([]entity.Admin, error) + AgentExistByID(ctx context.Context, agentID uint) (bool, error) } type Service struct { diff --git a/service/admin/kind_box/assign_receiver_agent.go b/service/admin/kind_box/assign_receiver_agent.go new file mode 100644 index 0000000..3ace2f2 --- /dev/null +++ b/service/admin/kind_box/assign_receiver_agent.go @@ -0,0 +1,23 @@ +package adminkindboxservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) AssignReceiverAgent(ctx context.Context, req param.AssignReceiverRequest) error { + const op = "AdminKindBoxService.AssignReceiverAgent" + + err := s.repo.AssignReceiverAgent(ctx, entity.KindBox{ + ID: req.KindBoxID, + ReceiverAgentID: req.ReceiverAgentID, + }) + if err != nil { + return richerror.New(op).WithErr(err) + } + + return nil +} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index f8cdf7a..ef7d37b 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -10,6 +10,7 @@ type Repository interface { AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error 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 } type Service struct { diff --git a/validator/admin/kind_box/assign_receiver_agent.go b/validator/admin/kind_box/assign_receiver_agent.go new file mode 100644 index 0000000..134a017 --- /dev/null +++ b/validator/admin/kind_box/assign_receiver_agent.go @@ -0,0 +1,43 @@ +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) ValidateAssignReceiverAgent(req param.AssignReceiverRequest) (map[string]string, error) { + const op = "adminkindboxvalidator.ValidateAssignReceiverAgent" + + if err := validation.ValidateStruct(&req, + + validation.Field(&req.KindBoxID, validation.Required, + validation.By(v.doesKindBoxExist), + validation.By(v.checkKindBoxStatusForAssigningReceiverAgent)), + + validation.Field(&req.ReceiverAgentID, validation.Required, + validation.By(v.doesAgentExist)), + ); 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 ba391af..8bbfc49 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -4,19 +4,26 @@ import ( "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) type Repository interface { KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) + GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) +} + +type AdminSvc interface { + AgentExistByID(ctx context.Context, agentID uint) (bool, error) } type Validator struct { - repo Repository + repo Repository + adminSvc AdminSvc } -func New(repo Repository) Validator { - return Validator{repo: repo} +func New(repo Repository, adminSvc AdminSvc) Validator { + return Validator{repo: repo, adminSvc: adminSvc} } func (v Validator) doesKindBoxExist(value interface{}) error { @@ -34,3 +41,35 @@ func (v Validator) doesKindBoxExist(value interface{}) error { return nil } + +func (v Validator) checkKindBoxStatusForAssigningReceiverAgent(value interface{}) error { + kindboxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + kindBox, err := v.repo.GetKindBox(context.Background(), kindboxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if kindBox.Status != entity.KindBoxReadyToReturnStatus { + return fmt.Errorf(errmsg.ErrorMsgAssignReceiverAgentKindBoxStatus) + } + + return nil +} + +func (v Validator) doesAgentExist(value interface{}) error { + agentID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + exists, err := v.adminSvc.AgentExistByID(context.Background(), agentID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if !exists { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +}