forked from ebhomengo/niki
1
0
Fork 0

feat(niki): agent get return awaiting kindbox (#81)

This commit is contained in:
Ruhollah 2024-07-13 16:03:07 +03:30
parent cd18bd2e71
commit b119b08830
21 changed files with 663 additions and 34 deletions

View File

@ -0,0 +1,52 @@
package agentkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// Get godoc
// @Summary Get a kind box that is awaiting return by agent
// @Tags KindBox
// @Accept json
// @Produce json
// @Param id path int true "KindBox ID"
// @Success 200 {object} param.GetKindBoxResponse
// @Failure 400 {string} "Bad Request"
// @Failure 401 {string} "invalid or expired jwt"
// @Failure 403 {string} "user not allowed"
// @Failure 422 {object} httpmsg.ErrorResponse
// @Failure 500 {string} "something went wrong"
// @Security AuthBearerAdmin
// @Router /agents/kindboxes/{id} [get]
func (h Handler) Get(c echo.Context) error {
var req param.GetKindBoxRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
claims := claim.GetClaimsFromEchoContext(c)
req.AgentID = claims.UserID
if fieldErrors, err := h.agentKindBoxVld.ValidateGetRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, httpmsg.ErrorResponse{
Message: msg,
Errors: fieldErrors,
})
}
resp, sErr := h.agentKindBoxSvc.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

@ -0,0 +1,29 @@
package agentkindboxhandler
import (
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
agentkindboxservice "git.gocasts.ir/ebhomengo/niki/service/agent/kind_box"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box"
)
type Handler struct {
authConfig authservice.Config
authSvc authservice.Service
agentKindBoxSvc agentkindboxservice.Service
agentKindBoxVld agentkindboxvalidator.Validator
adminAuthorizeSvc adminauthorizationservice.Service
}
func New(authConfig authservice.Config, authSvc authservice.Service,
agentKindBoxSvc agentkindboxservice.Service, agentKindBoxVld agentkindboxvalidator.Validator,
adminAuthorizeSvc adminauthorizationservice.Service,
) Handler {
return Handler{
authConfig: authConfig,
authSvc: authSvc,
agentKindBoxSvc: agentKindBoxSvc,
agentKindBoxVld: agentKindBoxVld,
adminAuthorizeSvc: adminAuthorizeSvc,
}
}

View File

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

View File

@ -2,11 +2,14 @@ package httpserver
import ( import (
"fmt" "fmt"
agentkindboxservice "git.gocasts.ir/ebhomengo/niki/service/agent/kind_box"
agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box"
config "git.gocasts.ir/ebhomengo/niki/config" config "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminKindBoxHandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box" 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" adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
agentkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box"
benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address" benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box" benefactorkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box"
@ -43,6 +46,7 @@ type Server struct {
adminHandler adminhandler.Handler adminHandler adminhandler.Handler
adminKindBoxReqHandler adminkindboxreqhandler.Handler adminKindBoxReqHandler adminkindboxreqhandler.Handler
adminKindBoxHandler adminKindBoxHandler.Handler adminKindBoxHandler adminKindBoxHandler.Handler
agentKindBoxHandler agentkindboxhandler.Handler
} }
func New( func New(
@ -64,6 +68,8 @@ func New(
adminAuthorizeSvc adminauthorizationservice.Service, adminAuthorizeSvc adminauthorizationservice.Service,
adminKindBoxSvc adminkindboxservice.Service, adminKindBoxSvc adminkindboxservice.Service,
adminKindBoxVld adminkindboxvalidator.Validator, adminKindBoxVld adminkindboxvalidator.Validator,
agentKindBoxSvc agentkindboxservice.Service,
agentKindBoxVld agentkindboxvalidator.Validator,
) Server { ) Server {
return Server{ return Server{
Router: echo.New(), Router: echo.New(),
@ -75,6 +81,7 @@ func New(
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld, adminAuthorizeSvc), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld, adminAuthorizeSvc),
adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld, adminAuthorizeSvc), adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld, adminAuthorizeSvc),
adminKindBoxHandler: adminKindBoxHandler.New(cfg.Auth, adminAuthSvc, adminKindBoxSvc, adminKindBoxVld, adminAuthorizeSvc), adminKindBoxHandler: adminKindBoxHandler.New(cfg.Auth, adminAuthSvc, adminKindBoxSvc, adminKindBoxVld, adminAuthorizeSvc),
agentKindBoxHandler: agentkindboxhandler.New(cfg.AdminAuth, adminAuthSvc, agentKindBoxSvc, agentKindBoxVld, adminAuthorizeSvc),
} }
} }
@ -92,6 +99,8 @@ func (s Server) Serve() {
s.adminHandler.SetRoutes(s.Router) s.adminHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router) s.adminKindBoxReqHandler.SetRoutes(s.Router)
s.adminKindBoxHandler.SetRoutes(s.Router) s.adminKindBoxHandler.SetRoutes(s.Router)
s.agentKindBoxHandler.SetRoutes(s.Router)
// Start server // Start server
address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) address := fmt.Sprintf(":%d", s.config.HTTPServer.Port)
fmt.Printf("start echo server on %s\n", address) fmt.Printf("start echo server on %s\n", address)

View File

@ -1391,6 +1391,72 @@ const docTemplate = `{
} }
} }
}, },
"/agents/kindboxes/{id}": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get a kind box that is awaiting return by agent",
"parameters": [
{
"type": "integer",
"description": "KindBox ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/kind_box.GetKindBoxResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"422": {
"description": "Unprocessable Entity",
"schema": {
"$ref": "#/definitions/httpmsg.ErrorResponse"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactor/kindboxes": { "/benefactor/kindboxes": {
"get": { "get": {
"security": [ "security": [
@ -3220,6 +3286,62 @@ const docTemplate = `{
} }
} }
}, },
"kind_box.GetKindBoxResponse": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
},
"benefactorID": {
"type": "integer"
},
"deliverAddressID": {
"type": "integer"
},
"deliverReferDate": {
"type": "string"
},
"deliverReferTimeID": {
"type": "integer"
},
"deliveredAt": {
"type": "string"
},
"id": {
"type": "integer"
},
"kindBoxReqID": {
"type": "integer"
},
"kindBoxType": {
"$ref": "#/definitions/entity.KindBoxType"
},
"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"
}
}
},
"param.PaginationResponse": { "param.PaginationResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -1380,6 +1380,72 @@
} }
} }
}, },
"/agents/kindboxes/{id}": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get a kind box that is awaiting return by agent",
"parameters": [
{
"type": "integer",
"description": "KindBox ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/kind_box.GetKindBoxResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"422": {
"description": "Unprocessable Entity",
"schema": {
"$ref": "#/definitions/httpmsg.ErrorResponse"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/benefactor/kindboxes": { "/benefactor/kindboxes": {
"get": { "get": {
"security": [ "security": [
@ -3209,6 +3275,62 @@
} }
} }
}, },
"kind_box.GetKindBoxResponse": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
},
"benefactorID": {
"type": "integer"
},
"deliverAddressID": {
"type": "integer"
},
"deliverReferDate": {
"type": "string"
},
"deliverReferTimeID": {
"type": "integer"
},
"deliveredAt": {
"type": "string"
},
"id": {
"type": "integer"
},
"kindBoxReqID": {
"type": "integer"
},
"kindBoxType": {
"$ref": "#/definitions/entity.KindBoxType"
},
"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"
}
}
},
"param.PaginationResponse": { "param.PaginationResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -789,6 +789,43 @@ definitions:
message: message:
type: string type: string
type: object type: object
kind_box.GetKindBoxResponse:
properties:
amount:
type: integer
benefactorID:
type: integer
deliverAddressID:
type: integer
deliverReferDate:
type: string
deliverReferTimeID:
type: integer
deliveredAt:
type: string
id:
type: integer
kindBoxReqID:
type: integer
kindBoxType:
$ref: '#/definitions/entity.KindBoxType'
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: object
param.PaginationResponse: param.PaginationResponse:
properties: properties:
page_number: page_number:
@ -1704,6 +1741,48 @@ paths:
summary: Register an admin by super-admin summary: Register an admin by super-admin
tags: tags:
- Admin - Admin
/agents/kindboxes/{id}:
get:
consumes:
- application/json
parameters:
- description: KindBox ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/kind_box.GetKindBoxResponse'
"400":
description: Bad Request
schema:
type: string
"401":
description: invalid or expired jwt
schema:
type: string
"403":
description: user not allowed
schema:
type: string
"422":
description: Unprocessable Entity
schema:
$ref: '#/definitions/httpmsg.ErrorResponse'
"500":
description: something went wrong
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Get a kind box that is awaiting return by agent
tags:
- KindBox
/benefactor/kindboxes: /benefactor/kindboxes:
get: get:
consumes: consumes:

View File

@ -17,4 +17,5 @@ const (
AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall") AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall")
AdminKindBoxReqUpdatePermission = AdminPermission("kindboxreq-update") AdminKindBoxReqUpdatePermission = AdminPermission("kindboxreq-update")
AdminKindBoxReqGetPermission = AdminPermission("kindboxreq-get") AdminKindBoxReqGetPermission = AdminPermission("kindboxreq-get")
AdminKindBoxGetAwaitingReturnPermission = AdminPermission("kindbox-get_awaiting_return")
) )

View File

@ -16,6 +16,7 @@ import (
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time" adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time"
agentkindboxservice "git.gocasts.ir/ebhomengo/niki/service/agent/kind_box"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box"
@ -28,6 +29,7 @@ type Services struct {
BenefactorAddressSvc benefactoraddressservice.Service BenefactorAddressSvc benefactoraddressservice.Service
BenefactorKindBoxSvc benefactorkindboxservice.Service BenefactorKindBoxSvc benefactorkindboxservice.Service
AdminKindBoxSvc adminkindboxservice.Service AdminKindBoxSvc adminkindboxservice.Service
AgentKindBoxSvc agentkindboxservice.Service
AdminSvc adminservice.Service AdminSvc adminservice.Service
AdminKindBoxReqSvc adminkindboxreqservice.Service AdminKindBoxReqSvc adminkindboxreqservice.Service
AdminReferTimeSvc adminrefertimeservice.Service AdminReferTimeSvc adminrefertimeservice.Service
@ -66,6 +68,10 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service {
return adminkindboxservice.New(InitKindBoxRepo(db)) return adminkindboxservice.New(InitKindBoxRepo(db))
} }
func InitAgentKindBoxService(db *mysql.DB) agentkindboxservice.Service {
return agentkindboxservice.New(InitKindBoxRepo(db))
}
func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service { func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service {
return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db)) return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db))
} }

View File

@ -7,6 +7,7 @@ import (
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address" benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box" benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box"
@ -21,6 +22,7 @@ type Validators struct {
AdminKindBoxReqVld adminkindboxreqvalidator.Validator AdminKindBoxReqVld adminkindboxreqvalidator.Validator
AdminVld adminvalidator.Validator AdminVld adminvalidator.Validator
AdminKindBoxVld adminkindboxvalidator.Validator AdminKindBoxVld adminkindboxvalidator.Validator
AgentKindBoxVld agentkindboxvalidator.Validator
} }
func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator { func InitAdminKindBoxReqValidator(db *mysql.DB, cfg config.Config) adminkindboxreqvalidator.Validator {
@ -55,6 +57,10 @@ func InitAdminKindBoxValidator(db *mysql.DB, cfg config.Config) adminkindboxvali
return adminkindboxvalidator.New(InitKindBoxRepo(db), InitAdminService(cfg, db)) return adminkindboxvalidator.New(InitKindBoxRepo(db), InitAdminService(cfg, db))
} }
func InitAgentKindBoxValidator(db *mysql.DB) agentkindboxvalidator.Validator {
return agentkindboxvalidator.New(InitKindBoxRepo(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 {
return benefactorkindboxvalidator.New( return benefactorkindboxvalidator.New(
InitKindBoxRepo(db), InitKindBoxRepo(db),

View File

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"git.gocasts.ir/ebhomengo/niki/adapter/redis" "git.gocasts.ir/ebhomengo/niki/adapter/redis"
"git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/config"
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server" httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
@ -67,6 +66,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D
AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg), AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg),
AdminVld: initial.InitAdminValidator(db), AdminVld: initial.InitAdminValidator(db),
AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg), AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg),
AgentKindBoxVld: initial.InitAgentKindBoxValidator(db),
}, },
initial.Services{ initial.Services{
BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db), BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db),
@ -74,6 +74,7 @@ func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.D
BenefactorAddressSvc: initial.InitBenefactorAddressService(db), BenefactorAddressSvc: initial.InitBenefactorAddressService(db),
BenefactorKindBoxSvc: initial.InitBenefactorKindBoxService(db), BenefactorKindBoxSvc: initial.InitBenefactorKindBoxService(db),
AdminKindBoxSvc: initial.InitAdminKindBoxService(db), AdminKindBoxSvc: initial.InitAdminKindBoxService(db),
AgentKindBoxSvc: initial.InitAgentKindBoxService(db),
AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db),
AdminSvc: initial.InitAdminService(cfg, db), AdminSvc: initial.InitAdminService(cfg, db),
AdminReferTimeSvc: initial.InitAdminReferTimeService(db), AdminReferTimeSvc: initial.InitAdminReferTimeService(db),
@ -92,7 +93,8 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) {
dependencies.BenefactorKindBoxSvc, dependencies.BenefactorKindBoxVld, dependencies.BenefactorKindBoxSvc, dependencies.BenefactorKindBoxVld,
dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc, dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc,
dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc, dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc,
dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld) dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld,
dependencies.AgentKindBoxSvc, dependencies.AgentKindBoxVld)
server.Serve() server.Serve()
} }

View File

@ -0,0 +1,12 @@
package kind_box
import "git.gocasts.ir/ebhomengo/niki/entity"
type GetKindBoxRequest struct {
KindBoxID uint `param:"id"`
AgentID uint
}
type GetKindBoxResponse struct {
entity.KindBox
}

View File

@ -1,6 +1,8 @@
package mysqlkindbox package mysqlkindbox
import "git.gocasts.ir/ebhomengo/niki/repository/mysql" import (
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
type DB struct { type DB struct {
conn *mysql.DB conn *mysql.DB

View File

@ -2,6 +2,7 @@ package mysqlkindbox
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -20,6 +21,25 @@ func (d DB) BenefactorKindBoxExist(ctx context.Context, benefactorID, kindBoxID
return count > 0, nil return count > 0, nil
} }
func (d DB) KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExistForAgent"
query := `SELECT COUNT(*) FROM kind_boxes WHERE id = ? AND receiver_agent_id = ? AND status = ? AND deleted_at IS NULL`
stmt, err := d.conn.PrepareStatement(op, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
var count int
if err = stmt.QueryRowContext(ctx, kindBoxID, agentID, entity.KindBoxAssignedReceiverAgentStatus.String()).Scan(&count); err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return count > 0, nil
}
func (d DB) KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) { func (d DB) KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) {
const op = "mysqlkindbox.KindBoxExist" const op = "mysqlkindbox.KindBoxExist"

View File

@ -0,0 +1,35 @@
package mysqlkindbox
import (
"context"
"database/sql"
"errors"
"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) GetAwaitingReturnByAgent(ctx context.Context, kindBoxID uint, agentID uint) (entity.KindBox, error) {
const op = "mysqlkindbox.GetAwaitingReturnByAgent"
query := `SELECT * FROM kind_boxes WHERE id = ? AND receiver_agent_id = ? AND status = ? AND deleted_at IS NULL`
stmt, err := d.conn.PrepareStatement(op, query)
if err != nil {
return entity.KindBox{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, kindBoxID, agentID, entity.KindBoxAssignedReceiverAgentStatus.String())
k, err := scanKindBox(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return entity.KindBox{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
}
return entity.KindBox{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}

View File

@ -14,7 +14,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
'kindbox-assign_receiver_agent', 'kindbox-assign_receiver_agent',
'kindbox-getall', 'kindbox-getall',
'kindboxreq-update', 'kindboxreq-update',
'kindboxreq-get' 'kindboxreq-get',
'kindbox-get_awaiting_return'
) NOT NULL; ) NOT NULL;
-- +migrate Down -- +migrate Down

View File

@ -31,7 +31,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`
(DEFAULT, 1 , 'role','kindbox-get'), (DEFAULT, 1 , 'role','kindbox-get'),
(DEFAULT, 2 , 'role','kindbox-get'), (DEFAULT, 2 , 'role','kindbox-get'),
(DEFAULT, 1 , 'role','kindbox-getall'), (DEFAULT, 1 , 'role','kindbox-getall'),
(DEFAULT, 2 , 'role','kindbox-getall'); (DEFAULT, 2 , 'role','kindbox-getall'),
(DEFAULT, 1 , 'role','kindbox-get_awaiting_return'),
(DEFAULT, 3 , 'role','kindbox-get_awaiting_return');

View File

@ -0,0 +1,20 @@
package agentkindboxservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Get(ctx context.Context, req param.GetKindBoxRequest) (param.GetKindBoxResponse, error) {
const op = "agentkindboxservice.Get"
kindBox, err := s.repo.GetAwaitingReturnByAgent(ctx, req.KindBoxID, req.AgentID)
if err != nil {
return param.GetKindBoxResponse{}, richerror.New(op).WithErr(err)
}
return param.GetKindBoxResponse{
KindBox: kindBox,
}, nil
}

View File

@ -0,0 +1,21 @@
package agentkindboxservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type Repository interface {
GetAwaitingReturnByAgent(ctx context.Context, kindBoxID uint, agentID uint) (entity.KindBox, error)
}
type Service struct {
repo Repository
}
func New(repository Repository) Service {
return Service{
repo: repository,
}
}

View File

@ -0,0 +1,36 @@
package agentkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/agent/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.GetKindBoxRequest) (map[string]string, error) {
const op = "agentkindboxvalidator.ValidateGetRequest"
if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxID, validation.Required, validation.By(v.doesKindBoxExistForAgent(req.AgentID))),
); 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

@ -0,0 +1,37 @@
package agentkindboxvalidator
import (
"context"
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
type Repository interface {
KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error)
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesKindBoxExistForAgent(agentID uint) func(value interface{}) error {
return func(value interface{}) error {
kindBoxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
exists, err := v.repo.KindBoxExistForAgent(context.Background(), kindBoxID, agentID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if !exists {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
}