feat(niki): add get an awaiting-for-delivery kindbox request by agent

This commit is contained in:
Erfan Mohammadi 2024-06-11 02:24:16 +03:30 committed by lmehdirezl
parent d60e4d605e
commit ad916d7ae7
13 changed files with 378 additions and 3 deletions

View File

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

View File

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

View File

@ -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": {

View File

@ -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": {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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