From ad916d7ae7d473ba2f0a2e0d52a94a319b3f9dc7 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Tue, 11 Jun 2024 02:24:16 +0330 Subject: [PATCH] feat(niki): add get an awaiting-for-delivery kindbox request by agent --- .../kind_box_req/get_delivery_awaiting.go | 49 +++++++++++ .../http_server/admin/kind_box_req/route.go | 1 + docs/docs.go | 83 +++++++++++++++++++ docs/swagger.json | 83 +++++++++++++++++++ docs/swagger.yaml | 53 ++++++++++++ entity/admin_permission.go | 1 + .../kind_box_req/get_delivery_awaiting.go | 12 +++ .../kind_box_req/get_delivery_awaiting.go | 30 +++++++ ...s_controls_table_permission_enum_field.sql | 3 +- ...565_insert_admin_access_controls_table.sql | 7 +- .../kind_box_req/get_delivery_awaiting.go | 21 +++++ service/admin/kind_box_req/service.go | 1 + .../kind_box_req/get_delivery_awaiting.go | 37 +++++++++ 13 files changed, 378 insertions(+), 3 deletions(-) create mode 100644 delivery/http_server/admin/kind_box_req/get_delivery_awaiting.go create mode 100644 param/admin/kind_box_req/get_delivery_awaiting.go create mode 100644 repository/mysql/kind_box_req/get_delivery_awaiting.go create mode 100644 service/admin/kind_box_req/get_delivery_awaiting.go create mode 100644 validator/admin/kind_box_req/get_delivery_awaiting.go diff --git a/delivery/http_server/admin/kind_box_req/get_delivery_awaiting.go b/delivery/http_server/admin/kind_box_req/get_delivery_awaiting.go new file mode 100644 index 0000000..5c00947 --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/get_delivery_awaiting.go @@ -0,0 +1,49 @@ +package adminkindboxreqhandler + +import ( + "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + "git.gocasts.ir/ebhomengo/niki/pkg/claim" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" +) + +// GetAwaitingDelivery godoc +// @Summary Get a kind box reqs that is awaiting delivery by agent +// @Tags KindBoxReq +// @Accept json +// @Produce json +// @Param id path int true "KindBoxReq ID" +// @Success 200 {object} param.DeliveryAwaitingGetResponse +// @Failure 400 {string} "Bad request" +// @Security AuthBearerAdmin +// @Router /admin/kindboxreqs/awaiting-delivery/{id} [get] +func (h Handler) GetAwaitingDelivery(c echo.Context) error { + var req param.DeliveryAwaitingGetRequest + if bErr := c.Bind(&req); bErr != nil { + + return echo.NewHTTPError(http.StatusBadRequest) + } + + claims := claim.GetClaimsFromEchoContext(c) + req.AgentID = claims.UserID + + if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetAwaitingDeliveryRequest(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + resp, sErr := h.adminKindBoxReqSvc.GetAwaitingDelivery(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_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index 81769d8..be36b24 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -15,4 +15,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.PATCH("/deliver-kind-box-req/:id", h.Deliver, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqDeliverPermission)) r.PATCH("/assign-sender-agent/:id", h.AssignSenderAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqAssignSenderAgentPermission)) r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission)) + r.GET("/awaiting-delivery/:id", h.GetAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission)) } diff --git a/docs/docs.go b/docs/docs.go index 458ae7e..dbc927c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -416,6 +416,48 @@ const docTemplate = `{ } } }, + "/admin/kindboxreqs/awaiting-delivery/{id}": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "Get a kind box reqs that is awaiting delivery by agent", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminkindboxreqparam.DeliveryAwaitingGetResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxreqs/deliver-kind-box-req/{id}": { "patch": { "security": [ @@ -1031,6 +1073,47 @@ const docTemplate = `{ "adminkindboxreqparam.DeliverKindBoxReqResponse": { "type": "object" }, + "adminkindboxreqparam.DeliveryAwaitingGetResponse": { + "type": "object", + "properties": { + "benefactorID": { + "type": "integer" + }, + "countAccepted": { + "type": "integer" + }, + "countRequested": { + "type": "integer" + }, + "deliverAddressID": { + "type": "integer" + }, + "deliverReferDate": { + "type": "string" + }, + "deliverReferTimeID": { + "type": "integer" + }, + "deliveredAt": { + "type": "string" + }, + "description": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "kindBoxType": { + "$ref": "#/definitions/entity.KindBoxType" + }, + "senderAgentID": { + "type": "integer" + }, + "status": { + "$ref": "#/definitions/entity.KindBoxReqStatus" + } + } + }, "adminkindboxreqparam.KindBoxReqAcceptRequest": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 3895348..567dc95 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -405,6 +405,48 @@ } } }, + "/admin/kindboxreqs/awaiting-delivery/{id}": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "Get a kind box reqs that is awaiting delivery by agent", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminkindboxreqparam.DeliveryAwaitingGetResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxreqs/deliver-kind-box-req/{id}": { "patch": { "security": [ @@ -1020,6 +1062,47 @@ "adminkindboxreqparam.DeliverKindBoxReqResponse": { "type": "object" }, + "adminkindboxreqparam.DeliveryAwaitingGetResponse": { + "type": "object", + "properties": { + "benefactorID": { + "type": "integer" + }, + "countAccepted": { + "type": "integer" + }, + "countRequested": { + "type": "integer" + }, + "deliverAddressID": { + "type": "integer" + }, + "deliverReferDate": { + "type": "string" + }, + "deliverReferTimeID": { + "type": "integer" + }, + "deliveredAt": { + "type": "string" + }, + "description": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "kindBoxType": { + "$ref": "#/definitions/entity.KindBoxType" + }, + "senderAgentID": { + "type": "integer" + }, + "status": { + "$ref": "#/definitions/entity.KindBoxReqStatus" + } + } + }, "adminkindboxreqparam.KindBoxReqAcceptRequest": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index dd24963..758d76e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -81,6 +81,33 @@ definitions: type: object adminkindboxreqparam.DeliverKindBoxReqResponse: type: object + adminkindboxreqparam.DeliveryAwaitingGetResponse: + properties: + benefactorID: + type: integer + countAccepted: + type: integer + countRequested: + type: integer + deliverAddressID: + type: integer + deliverReferDate: + type: string + deliverReferTimeID: + type: integer + deliveredAt: + type: string + description: + type: string + id: + type: integer + kindBoxType: + $ref: '#/definitions/entity.KindBoxType' + senderAgentID: + type: integer + status: + $ref: '#/definitions/entity.KindBoxReqStatus' + type: object adminkindboxreqparam.KindBoxReqAcceptRequest: properties: count_accepted: @@ -799,6 +826,32 @@ paths: summary: Admin Assign Sender Agent to kindboxreq tags: - KindBoxReq + /admin/kindboxreqs/awaiting-delivery/{id}: + get: + consumes: + - application/json + parameters: + - description: KindBoxReq ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/adminkindboxreqparam.DeliveryAwaitingGetResponse' + "400": + description: Bad request + schema: + type: string + security: + - AuthBearerAdmin: [] + summary: Get a kind box reqs that is awaiting delivery by agent + tags: + - KindBoxReq /admin/kindboxreqs/deliver-kind-box-req/{id}: patch: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 822a25b..8807c31 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -10,4 +10,5 @@ const ( AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") + AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery") ) diff --git a/param/admin/kind_box_req/get_delivery_awaiting.go b/param/admin/kind_box_req/get_delivery_awaiting.go new file mode 100644 index 0000000..4ce125b --- /dev/null +++ b/param/admin/kind_box_req/get_delivery_awaiting.go @@ -0,0 +1,12 @@ +package adminkindboxreqparam + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type DeliveryAwaitingGetRequest struct { + KindBoxReqID uint `param:"id"` + AgentID uint +} + +type DeliveryAwaitingGetResponse struct { + entity.KindBoxReq +} diff --git a/repository/mysql/kind_box_req/get_delivery_awaiting.go b/repository/mysql/kind_box_req/get_delivery_awaiting.go new file mode 100644 index 0000000..cf7fb33 --- /dev/null +++ b/repository/mysql/kind_box_req/get_delivery_awaiting.go @@ -0,0 +1,30 @@ +package mysqlkindboxreq + +import ( + "context" + "database/sql" + + "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) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) { + const op = "mysqlkindboxreq.GetAwaitingDeliveryByAgent" + + query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = 'assigned-sender-agent' AND deleted_at IS NULL ` + row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID) + k, err := scanKindBoxReq(row) + if err != nil { + if err == sql.ErrNoRows { + + return entity.KindBoxReq{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound) + } + + return entity.KindBoxReq{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return k, 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 da3496a..a40a8bf 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 @@ -7,7 +7,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindboxreq-getall', 'kindboxreq-deliver', 'kindboxreq-assign_sender_agent', - 'admin-getall_agent' + 'admin-getall_agent', + 'kindboxreq-get_awaiting_delivery' ) 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 2dd20a8..f8636ef 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -12,8 +12,11 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission` (10, 2 , 'role','kindboxreq-getall'), (11, 2 , 'role','kindboxreq-deliver'), (12, 2 , 'role','kindboxreq-assign_sender_agent'), - (13, 1, 'role', 'admin-getall_agent'), - (13, 2, 'role', 'admin-getall_agent'); + (13, 1 , 'role','admin-getall_agent'), + (14, 2 , 'role','admin-getall_agent'), + (15, 1 , 'role','kindboxreq-get_awaiting_delivery'), + (16, 3 , 'role','kindboxreq-get_awaiting_delivery'); + -- +migrate Down DELETE diff --git a/service/admin/kind_box_req/get_delivery_awaiting.go b/service/admin/kind_box_req/get_delivery_awaiting.go new file mode 100644 index 0000000..8b257b4 --- /dev/null +++ b/service/admin/kind_box_req/get_delivery_awaiting.go @@ -0,0 +1,21 @@ +package adminkindboxreqservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +) + +func (s Service) GetAwaitingDelivery(ctx context.Context, req param.DeliveryAwaitingGetRequest) (param.DeliveryAwaitingGetResponse, error) { + const op = "adminkindboxreqservice.GetAwaitingDelivery" + + kindBoxReq, err := s.repo.GetAwaitingDeliveryByAgent(ctx, req.KindBoxReqID, req.AgentID) + if err != nil { + + return param.DeliveryAwaitingGetResponse{}, err + } + + return param.DeliveryAwaitingGetResponse{ + KindBoxReq: kindBoxReq, + }, nil +} diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 1820aae..1a51878 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -16,6 +16,7 @@ type Repository interface { GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID uint, senderAgentID uint) error DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error + GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) } type KindBoxSvc interface { diff --git a/validator/admin/kind_box_req/get_delivery_awaiting.go b/validator/admin/kind_box_req/get_delivery_awaiting.go new file mode 100644 index 0000000..b0dbc08 --- /dev/null +++ b/validator/admin/kind_box_req/get_delivery_awaiting.go @@ -0,0 +1,37 @@ +package adminkindboxreqvalidator + +import ( + "errors" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + 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) ValidateGetAwaitingDeliveryRequest(req param.DeliveryAwaitingGetRequest) (map[string]string, error) { + const op = "adminkindboxreqvalidator.ValidateGetAwaitingDeliveryRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.KindBoxReqID, validation.Required), + ); 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 +}