feat(niki): admin get kindbox

This commit is contained in:
Erfan Mohammadi 2024-07-01 20:43:28 +03:30
parent 36d34b2b6b
commit 8a66e43789
17 changed files with 426 additions and 14 deletions

View File

@ -0,0 +1,43 @@
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"
)
// Get godoc
// @Summary Get a specific kind box by admin
// @Description This endpoint retrieves a specific kind box by admin
// @Tags KindBox
// @Accept json
// @Produce json
// @Param id path int true "Kind box ID"
// @Success 200 {object} param.KindBoxGetResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admin/kindboxes/{id} [get].
func (h Handler) Get(c echo.Context) error {
var req param.KindBoxGetRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminKindBoxSvc.Get(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

@ -1,8 +1,15 @@
package adminkindboxhandler
import (
echo "github.com/labstack/echo/v4"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/labstack/echo/v4"
)
func (h Handler) SetRoutes(_ *echo.Echo) {
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/admin/kindboxes")
r.Use(middleware.Auth(h.authSvc, h.authConfig))
r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission))
}

View File

@ -5,6 +5,7 @@ import (
config "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminKindBoxHandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box"
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
@ -13,6 +14,7 @@ import (
"git.gocasts.ir/ebhomengo/niki/docs"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
@ -20,6 +22,7 @@ import (
benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
@ -39,6 +42,7 @@ type Server struct {
benefactorKindBoxHandler benefactorkindboxhandler.Handler
adminHandler adminhandler.Handler
adminKindBoxReqHandler adminkindboxreqhandler.Handler
adminKindBoxHandler adminKindBoxHandler.Handler
}
func New(
@ -58,6 +62,8 @@ func New(
adminKinBoxReqSvc adminkindboxreqservice.Service,
adminKinBoxReqVld adminkindboxreqvalidator.Validator,
adminAuthorizeSvc adminauthorizationservice.Service,
adminKindBoxSvc adminkindboxservice.Service,
adminKindBoxVld adminkindboxvalidator.Validator,
) Server {
return Server{
Router: echo.New(),
@ -68,6 +74,7 @@ func New(
benefactorKindBoxHandler: benefactorkindboxhandler.New(cfg.Auth, benefactorAuthSvc, benefactorKindBoxSvc, benefactorKindBoxVld),
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld, adminAuthorizeSvc),
adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld, adminAuthorizeSvc),
adminKindBoxHandler: adminKindBoxHandler.New(cfg.Auth, adminAuthSvc, adminKindBoxSvc, adminKindBoxVld, adminAuthorizeSvc),
}
}
@ -84,6 +91,7 @@ func (s Server) Serve() {
s.benefactorKindBoxHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router)
s.adminKindBoxHandler.SetRoutes(s.Router)
// Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)
fmt.Printf("start echo server on %s\n", address)

View File

@ -267,6 +267,49 @@ const docTemplate = `{
}
}
},
"/admin/kindboxes/{id}": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "This endpoint retrieves a specific kind box by admin",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get a specific kind box by admin",
"parameters": [
{
"type": "integer",
"description": "Kind box ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {
"get": {
"security": [
@ -1281,6 +1324,59 @@ const docTemplate = `{
}
}
},
"adminkindboxparam.KindBoxGetResponse": {
"type": "object",
"properties": {
"amount": {
"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"
}
}
},
"adminkindboxreqparam.AssignSenderRequest": {
"type": "object",
"properties": {

View File

@ -256,6 +256,49 @@
}
}
},
"/admin/kindboxes/{id}": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "This endpoint retrieves a specific kind box by admin",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get a specific kind box by admin",
"parameters": [
{
"type": "integer",
"description": "Kind box ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminkindboxparam.KindBoxGetResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs": {
"get": {
"security": [
@ -1270,6 +1313,59 @@
}
}
},
"adminkindboxparam.KindBoxGetResponse": {
"type": "object",
"properties": {
"amount": {
"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"
}
}
},
"adminkindboxreqparam.AssignSenderRequest": {
"type": "object",
"properties": {

View File

@ -72,6 +72,41 @@ definitions:
example: "1234567890"
type: string
type: object
adminkindboxparam.KindBoxGetResponse:
properties:
amount:
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:
sender_agent_id:
@ -780,6 +815,33 @@ paths:
summary: Get all provinces
tags:
- Address
/admin/kindboxes/{id}:
get:
consumes:
- application/json
description: This endpoint retrieves a specific kind box by admin
parameters:
- description: Kind box ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/adminkindboxparam.KindBoxGetResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Get a specific kind box by admin
tags:
- KindBox
/admin/kindboxreqs:
get:
consumes:

View File

@ -12,4 +12,5 @@ const (
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
AdminKindBoxGetPermission = AdminPermission("kindbox-get")
)

View File

@ -5,6 +5,7 @@ import (
"git.gocasts.ir/ebhomengo/niki/config"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
@ -19,6 +20,7 @@ type Validators struct {
BenefactorKindBoxVld benefactorkindboxvalidator.Validator
AdminKindBoxReqVld adminkindboxreqvalidator.Validator
AdminVld adminvalidator.Validator
AdminKindBoxVld adminkindboxvalidator.Validator
}
func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator {
@ -49,6 +51,10 @@ func InitBenefactorAddressValidator(cfg config.Config, redisAdapter redis.Adapte
)
}
func InitAdminKindBoxValidator(db *mysql.DB) adminkindboxvalidator.Validator {
return adminkindboxvalidator.New(InitKindBoxRepo(db))
}
func InitBenefactorKindBoxValidator(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) benefactorkindboxvalidator.Validator {
return benefactorkindboxvalidator.New(
InitKindBoxRepo(db),

View File

@ -65,6 +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),
},
initial.Services{
BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db),
@ -89,7 +90,8 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) {
dependencies.BenefactorAddressSvc, dependencies.BenefactorAddressVld,
dependencies.BenefactorKindBoxSvc, dependencies.BenefactorKindBoxVld,
dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc,
dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc)
dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc,
dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld)
server.Serve()
}

View File

@ -3,8 +3,7 @@ package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type KindBoxGetRequest struct {
BenefactorID uint
KindBoxID uint
KindBoxID uint `param:"id"`
}
type KindBoxGetResponse struct {

View File

@ -19,3 +19,16 @@ func (d DB) BenefactorKindBoxExist(ctx context.Context, benefactorID, kindBoxID
return count > 0, nil
}
func (d DB) KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExist"
var count int
if err := d.conn.Conn().QueryRowContext(ctx, `SELECT COUNT(*) FROM kind_boxes WHERE id = ?`, kindBoxID).Scan(&count); err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return count > 0, nil
}

View File

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

View File

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

View File

@ -0,0 +1,19 @@
package adminkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Get(ctx context.Context, request param.KindBoxGetRequest) (param.KindBoxGetResponse, error) {
const op = "adminkindboxservice.Get"
kindBox, err := s.repo.GetKindBox(ctx, request.KindBoxID)
if err != nil {
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil
}

View File

@ -9,6 +9,7 @@ import (
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)
}
type Service struct {

View File

@ -0,0 +1,39 @@
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) ValidateGetRequest(req param.KindBoxGetRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxID,
validation.Required,
validation.By(v.doesKindBoxExist)),
); 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

@ -1,13 +1,14 @@
package adminkindboxvalidator
import (
"context"
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
EmployeeExist(id uint) (bool, error)
BenefactorExist(id uint) (bool, error)
KindBoxExist(id uint) (bool, error)
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
PendingStatus(id uint) (bool, error)
CheckStatus(status string) (bool, error)
KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error)
}
type Validator struct {
@ -17,3 +18,19 @@ type Validator struct {
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesKindBoxExist(value interface{}) error {
kindBoxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
exists, err := v.repo.KindBoxExist(context.Background(), kindBoxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if !exists {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}