feat(niki): admin assign kindbox to a agent

This commit is contained in:
Erfan Mohammadi 2024-07-02 23:47:40 +03:30
parent 8a66e43789
commit 47bc77baee
20 changed files with 283 additions and 941 deletions

View File

@ -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)
}

View File

@ -12,4 +12,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.Use(middleware.Auth(h.authSvc, h.authConfig)) r.Use(middleware.Auth(h.authSvc, h.authConfig))
r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission)) 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))
} }

View File

@ -267,14 +267,13 @@ const docTemplate = `{
} }
} }
}, },
"/admin/kindboxes/{id}": { "/admin/kindboxes/assign-receiver-agent/{id}": {
"get": { "patch": {
"security": [ "security": [
{ {
"AuthBearerAdmin": [] "AuthBearerAdmin": []
} }
], ],
"description": "This endpoint retrieves a specific kind box by admin",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -284,22 +283,28 @@ const docTemplate = `{
"tags": [ "tags": [
"KindBox" "KindBox"
], ],
"summary": "Get a specific kind box by admin", "summary": "Admin assign receiver agent to kindbox",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "integer",
"description": "Kind box ID", "description": "KindBox ID",
"name": "id", "name": "id",
"in": "path", "in": "path",
"required": true "required": true
},
{
"description": "Assign Receiver Agent Request Body",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/adminkindboxparam.AssignReceiverRequest"
}
} }
], ],
"responses": { "responses": {
"200": { "204": {
"description": "OK", "description": "No Content"
"schema": {
"$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse"
}
}, },
"400": { "400": {
"description": "Bad request", "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}": { "/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}": { "/admin/kindboxreqs/awaiting-delivery/{id}": {
"get": { "get": {
"security": [ "security": [
@ -881,7 +715,7 @@ const docTemplate = `{
} }
} }
}, },
"/benefactor/kindboxes": { "/benefactor/kindboxes/": {
"get": { "get": {
"security": [ "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/": { "/benefactor/kindboxreqs/": {
"post": { "post": {
"security": [ "security": [
@ -1324,56 +1107,11 @@ const docTemplate = `{
} }
} }
}, },
"adminkindboxparam.KindBoxGetResponse": { "adminkindboxparam.AssignReceiverRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
"amount": { "receiver_agent_id": {
"type": "integer" "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": { "adminkindboxreqparam.DeliverKindBoxReqResponse": {
"type": "object" "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": { "adminkindboxreqparam.DeliveryAwaitingGetResponse": {
"type": "object", "type": "object",
"properties": { "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": { "adminkindboxreqparam.KindBoxReqGetAllResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
"all_awaiting_kind_box_req": { "all_kind_box_req": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/entity.KindBoxReq" "$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": { "benefactorkindboxreqparam.KindBoxReqAddRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -256,14 +256,13 @@
} }
} }
}, },
"/admin/kindboxes/{id}": { "/admin/kindboxes/assign-receiver-agent/{id}": {
"get": { "patch": {
"security": [ "security": [
{ {
"AuthBearerAdmin": [] "AuthBearerAdmin": []
} }
], ],
"description": "This endpoint retrieves a specific kind box by admin",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -273,22 +272,28 @@
"tags": [ "tags": [
"KindBox" "KindBox"
], ],
"summary": "Get a specific kind box by admin", "summary": "Admin assign receiver agent to kindbox",
"parameters": [ "parameters": [
{ {
"type": "integer", "type": "integer",
"description": "Kind box ID", "description": "KindBox ID",
"name": "id", "name": "id",
"in": "path", "in": "path",
"required": true "required": true
},
{
"description": "Assign Receiver Agent Request Body",
"name": "Request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/adminkindboxparam.AssignReceiverRequest"
}
} }
], ],
"responses": { "responses": {
"200": { "204": {
"description": "OK", "description": "No Content"
"schema": {
"$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse"
}
}, },
"400": { "400": {
"description": "Bad request", "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}": { "/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}": { "/admin/kindboxreqs/awaiting-delivery/{id}": {
"get": { "get": {
"security": [ "security": [
@ -870,7 +704,7 @@
} }
} }
}, },
"/benefactor/kindboxes": { "/benefactor/kindboxes/": {
"get": { "get": {
"security": [ "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/": { "/benefactor/kindboxreqs/": {
"post": { "post": {
"security": [ "security": [
@ -1313,56 +1096,11 @@
} }
} }
}, },
"adminkindboxparam.KindBoxGetResponse": { "adminkindboxparam.AssignReceiverRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
"amount": { "receiver_agent_id": {
"type": "integer" "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": { "adminkindboxreqparam.DeliverKindBoxReqResponse": {
"type": "object" "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": { "adminkindboxreqparam.DeliveryAwaitingGetResponse": {
"type": "object", "type": "object",
"properties": { "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": { "adminkindboxreqparam.KindBoxReqGetAllResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
"all_awaiting_kind_box_req": { "all_kind_box_req": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/entity.KindBoxReq" "$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": { "benefactorkindboxreqparam.KindBoxReqAddRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -72,40 +72,10 @@ definitions:
example: "1234567890" example: "1234567890"
type: string type: string
type: object type: object
adminkindboxparam.KindBoxGetResponse: adminkindboxparam.AssignReceiverRequest:
properties: properties:
amount: receiver_agent_id:
type: integer 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 type: object
adminkindboxreqparam.AssignSenderRequest: adminkindboxreqparam.AssignSenderRequest:
properties: properties:
@ -116,15 +86,6 @@ definitions:
type: object type: object
adminkindboxreqparam.DeliverKindBoxReqResponse: adminkindboxreqparam.DeliverKindBoxReqResponse:
type: object 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: adminkindboxreqparam.DeliveryAwaitingGetResponse:
properties: properties:
benefactorID: benefactorID:
@ -172,33 +133,9 @@ definitions:
kind_box_req_status: kind_box_req_status:
$ref: '#/definitions/entity.KindBoxReqStatus' $ref: '#/definitions/entity.KindBoxReqStatus'
type: object 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: adminkindboxreqparam.KindBoxReqGetAllResponse:
properties: properties:
all_awaiting_kind_box_req: all_kind_box_req:
items: items:
$ref: '#/definitions/entity.KindBoxReq' $ref: '#/definitions/entity.KindBoxReq'
type: array type: array
@ -444,18 +381,6 @@ definitions:
type: type:
$ref: '#/definitions/entity.KindBoxType' $ref: '#/definitions/entity.KindBoxType'
type: object 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: benefactorkindboxreqparam.KindBoxReqAddRequest:
properties: properties:
benefactor_id: benefactor_id:
@ -815,31 +740,34 @@ paths:
summary: Get all provinces summary: Get all provinces
tags: tags:
- Address - Address
/admin/kindboxes/{id}: /admin/kindboxes/assign-receiver-agent/{id}:
get: patch:
consumes: consumes:
- application/json - application/json
description: This endpoint retrieves a specific kind box by admin
parameters: parameters:
- description: Kind box ID - description: KindBox ID
in: path in: path
name: id name: id
required: true required: true
type: integer type: integer
- description: Assign Receiver Agent Request Body
in: body
name: Request
required: true
schema:
$ref: '#/definitions/adminkindboxparam.AssignReceiverRequest'
produces: produces:
- application/json - application/json
responses: responses:
"200": "204":
description: OK description: No Content
schema:
$ref: '#/definitions/adminkindboxparam.KindBoxGetResponse'
"400": "400":
description: Bad request description: Bad request
schema: schema:
type: string type: string
security: security:
- AuthBearerAdmin: [] - AuthBearerAdmin: []
summary: Get a specific kind box by admin summary: Admin assign receiver agent to kindbox
tags: tags:
- KindBox - KindBox
/admin/kindboxreqs: /admin/kindboxreqs:
@ -871,32 +799,6 @@ paths:
summary: Admin get all kindboxreq summary: Admin get all kindboxreq
tags: tags:
- KindBoxReq - 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}: /admin/kindboxreqs/accept-kind-box-req/{id}:
patch: patch:
consumes: consumes:
@ -961,95 +863,6 @@ paths:
summary: Admin Assign Sender Agent to kindboxreq summary: Admin Assign Sender Agent to kindboxreq
tags: tags:
- KindBoxReq - 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}: /admin/kindboxreqs/awaiting-delivery/{id}:
get: get:
consumes: consumes:
@ -1206,7 +1019,7 @@ paths:
summary: Register an admin by super-admin summary: Register an admin by super-admin
tags: tags:
- Admin - Admin
/benefactor/kindboxes: /benefactor/kindboxes/:
get: get:
consumes: consumes:
- application/json - application/json
@ -1254,38 +1067,6 @@ paths:
summary: Get a specific kind box for a benefactor summary: Get a specific kind box for a benefactor
tags: tags:
- KindBox - 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/: /benefactor/kindboxreqs/:
post: post:
consumes: consumes:

View File

@ -13,4 +13,5 @@ const (
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery") AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
AdminKindBoxGetPermission = AdminPermission("kindbox-get") AdminKindBoxGetPermission = AdminPermission("kindbox-get")
AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent")
) )

View File

@ -51,8 +51,8 @@ func InitBenefactorAddressValidator(cfg config.Config, redisAdapter redis.Adapte
) )
} }
func InitAdminKindBoxValidator(db *mysql.DB) adminkindboxvalidator.Validator { func InitAdminKindBoxValidator(db *mysql.DB, cfg config.Config) adminkindboxvalidator.Validator {
return adminkindboxvalidator.New(InitKindBoxRepo(db)) return adminkindboxvalidator.New(InitKindBoxRepo(db), InitAdminService(cfg, db))
} }
func InitBenefactorKindBoxValidator(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorkindboxvalidator.Validator { func InitBenefactorKindBoxValidator(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorkindboxvalidator.Validator {

View File

@ -65,7 +65,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D
BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db), BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db),
AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg), AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg),
AdminVld: initial.InitAdminValidator(db), AdminVld: initial.InitAdminValidator(db),
AdminKindBoxVld: initial.InitAdminKindBoxValidator(db), AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg),
}, },
initial.Services{ initial.Services{
BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db), BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db),

View File

@ -0,0 +1,6 @@
package adminkindboxparam
type AssignReceiverRequest struct {
KindBoxID uint `json:"-" param:"id"`
ReceiverAgentID uint `json:"receiver_agent_id"`
}

View File

@ -33,4 +33,5 @@ const (
ErrorMsgSortFieldIsRequired = "sort field is required" ErrorMsgSortFieldIsRequired = "sort field is required"
ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc"
ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgSortFieldIsNotValid = "sort field is not valid"
ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent"
) )

View File

@ -75,3 +75,17 @@ func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error
return admin, nil 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
}

View File

@ -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
}

View File

@ -9,7 +9,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
'kindboxreq-assign_sender_agent', 'kindboxreq-assign_sender_agent',
'admin-getall_agent', 'admin-getall_agent',
'kindboxreq-get_awaiting_delivery', 'kindboxreq-get_awaiting_delivery',
'kindbox-get' 'kindbox-get',
'kindbox-assign_receiver_agent'
) NOT NULL; ) NOT NULL;
-- +migrate Down -- +migrate Down

View File

@ -17,7 +17,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`
(15, 1 , 'role','kindboxreq-get_awaiting_delivery'), (15, 1 , 'role','kindboxreq-get_awaiting_delivery'),
(16, 3 , 'role','kindboxreq-get_awaiting_delivery'), (16, 3 , 'role','kindboxreq-get_awaiting_delivery'),
(17, 1 , 'role','kindbox-get'), (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 -- +migrate Down

View File

@ -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
}

View File

@ -19,6 +19,7 @@ type Repository interface {
GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error)
GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error)
GetAllAgent(ctx context.Context) ([]entity.Admin, error) GetAllAgent(ctx context.Context) ([]entity.Admin, error)
AgentExistByID(ctx context.Context, agentID uint) (bool, error)
} }
type Service struct { type Service struct {

View File

@ -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
}

View File

@ -10,6 +10,7 @@ type Repository interface {
AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error
AddKindBox(ctx context.Context, kindBox entity.KindBox) error AddKindBox(ctx context.Context, kindBox entity.KindBox) error
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error
} }
type Service struct { type Service struct {

View File

@ -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
}

View File

@ -4,19 +4,26 @@ import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
) )
type Repository interface { type Repository interface {
KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) 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 { type Validator struct {
repo Repository repo Repository
adminSvc AdminSvc
} }
func New(repo Repository) Validator { func New(repo Repository, adminSvc AdminSvc) Validator {
return Validator{repo: repo} return Validator{repo: repo, adminSvc: adminSvc}
} }
func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxExist(value interface{}) error {
@ -34,3 +41,35 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
return nil 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
}