diff --git a/delivery/http_server/agent/kind_box/get.go b/delivery/http_server/agent/kind_box/get.go new file mode 100644 index 0000000..6d21ef9 --- /dev/null +++ b/delivery/http_server/agent/kind_box/get.go @@ -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) +} diff --git a/delivery/http_server/agent/kind_box/handler.go b/delivery/http_server/agent/kind_box/handler.go new file mode 100644 index 0000000..12a2c37 --- /dev/null +++ b/delivery/http_server/agent/kind_box/handler.go @@ -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, + } +} diff --git a/delivery/http_server/agent/kind_box/route.go b/delivery/http_server/agent/kind_box/route.go new file mode 100644 index 0000000..295339f --- /dev/null +++ b/delivery/http_server/agent/kind_box/route.go @@ -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)) +} diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 677d70d..77291fc 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -2,11 +2,14 @@ package httpserver import ( "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" 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" + agentkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/agent/kind_box" benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorkindboxhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box" @@ -43,6 +46,7 @@ type Server struct { adminHandler adminhandler.Handler adminKindBoxReqHandler adminkindboxreqhandler.Handler adminKindBoxHandler adminKindBoxHandler.Handler + agentKindBoxHandler agentkindboxhandler.Handler } func New( @@ -64,6 +68,8 @@ func New( adminAuthorizeSvc adminauthorizationservice.Service, adminKindBoxSvc adminkindboxservice.Service, adminKindBoxVld adminkindboxvalidator.Validator, + agentKindBoxSvc agentkindboxservice.Service, + agentKindBoxVld agentkindboxvalidator.Validator, ) Server { return Server{ Router: echo.New(), @@ -75,6 +81,7 @@ func New( 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), + agentKindBoxHandler: agentkindboxhandler.New(cfg.AdminAuth, adminAuthSvc, agentKindBoxSvc, agentKindBoxVld, adminAuthorizeSvc), } } @@ -92,6 +99,8 @@ func (s Server) Serve() { s.adminHandler.SetRoutes(s.Router) s.adminKindBoxReqHandler.SetRoutes(s.Router) s.adminKindBoxHandler.SetRoutes(s.Router) + s.agentKindBoxHandler.SetRoutes(s.Router) + // Start server address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) fmt.Printf("start echo server on %s\n", address) diff --git a/docs/docs.go b/docs/docs.go index 2a476df..19ecc04 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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": { "get": { "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": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index a8da8bb..24a04c5 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { "get": { "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": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d86692f..0874e5c 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -789,6 +789,43 @@ definitions: message: type: string 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: properties: page_number: @@ -1704,6 +1741,48 @@ paths: summary: Register an admin by super-admin tags: - 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: get: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 0f22856..a3fae89 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -17,4 +17,5 @@ const ( AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall") AdminKindBoxReqUpdatePermission = AdminPermission("kindboxreq-update") AdminKindBoxReqGetPermission = AdminPermission("kindboxreq-get") + AdminKindBoxGetAwaitingReturnPermission = AdminPermission("kindbox-get_awaiting_return") ) diff --git a/internal/initial/service.go b/internal/initial/service.go index 37ce427..5c8504b 100644 --- a/internal/initial/service.go +++ b/internal/initial/service.go @@ -16,6 +16,7 @@ import ( adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" 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" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" @@ -28,6 +29,7 @@ type Services struct { BenefactorAddressSvc benefactoraddressservice.Service BenefactorKindBoxSvc benefactorkindboxservice.Service AdminKindBoxSvc adminkindboxservice.Service + AgentKindBoxSvc agentkindboxservice.Service AdminSvc adminservice.Service AdminKindBoxReqSvc adminkindboxreqservice.Service AdminReferTimeSvc adminrefertimeservice.Service @@ -66,6 +68,10 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service { return adminkindboxservice.New(InitKindBoxRepo(db)) } +func InitAgentKindBoxService(db *mysql.DB) agentkindboxservice.Service { + return agentkindboxservice.New(InitKindBoxRepo(db)) +} + func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service { return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db)) } diff --git a/internal/initial/validator.go b/internal/initial/validator.go index 8455e79..c598d5e 100644 --- a/internal/initial/validator.go +++ b/internal/initial/validator.go @@ -7,6 +7,7 @@ import ( 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" + agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box" benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box" @@ -21,6 +22,7 @@ type Validators struct { AdminKindBoxReqVld adminkindboxreqvalidator.Validator AdminVld adminvalidator.Validator AdminKindBoxVld adminkindboxvalidator.Validator + AgentKindBoxVld agentkindboxvalidator.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)) } +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 { return benefactorkindboxvalidator.New( InitKindBoxRepo(db), diff --git a/main.go b/main.go index 2747403..3b69adb 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "git.gocasts.ir/ebhomengo/niki/adapter/redis" "git.gocasts.ir/ebhomengo/niki/config" 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), AdminVld: initial.InitAdminValidator(db), AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg), + AgentKindBoxVld: initial.InitAgentKindBoxValidator(db), }, initial.Services{ 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), BenefactorKindBoxSvc: initial.InitBenefactorKindBoxService(db), AdminKindBoxSvc: initial.InitAdminKindBoxService(db), + AgentKindBoxSvc: initial.InitAgentKindBoxService(db), AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db), AdminSvc: initial.InitAdminService(cfg, db), AdminReferTimeSvc: initial.InitAdminReferTimeService(db), @@ -92,7 +93,8 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) { dependencies.BenefactorKindBoxSvc, dependencies.BenefactorKindBoxVld, dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc, dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc, - dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld) + dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld, + dependencies.AgentKindBoxSvc, dependencies.AgentKindBoxVld) server.Serve() } diff --git a/param/agent/kind_box/get.go b/param/agent/kind_box/get.go new file mode 100644 index 0000000..02d25a9 --- /dev/null +++ b/param/agent/kind_box/get.go @@ -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 +} diff --git a/repository/mysql/kind_box/db.go b/repository/mysql/kind_box/db.go index 8178944..796a5c8 100644 --- a/repository/mysql/kind_box/db.go +++ b/repository/mysql/kind_box/db.go @@ -1,6 +1,8 @@ package mysqlkindbox -import "git.gocasts.ir/ebhomengo/niki/repository/mysql" +import ( + "git.gocasts.ir/ebhomengo/niki/repository/mysql" +) type DB struct { conn *mysql.DB diff --git a/repository/mysql/kind_box/exist_kindbox.go b/repository/mysql/kind_box/exist_kindbox.go index 95b417c..8524869 100644 --- a/repository/mysql/kind_box/exist_kindbox.go +++ b/repository/mysql/kind_box/exist_kindbox.go @@ -2,6 +2,7 @@ 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" @@ -20,6 +21,25 @@ func (d DB) BenefactorKindBoxExist(ctx context.Context, benefactorID, kindBoxID 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) { const op = "mysqlkindbox.KindBoxExist" diff --git a/repository/mysql/kind_box/get_awaiting_return.go b/repository/mysql/kind_box/get_awaiting_return.go new file mode 100644 index 0000000..bb030e9 --- /dev/null +++ b/repository/mysql/kind_box/get_awaiting_return.go @@ -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 +} 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 835a1c5..d24a961 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 @@ -14,7 +14,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindbox-assign_receiver_agent', 'kindbox-getall', 'kindboxreq-update', - 'kindboxreq-get' + 'kindboxreq-get', + 'kindbox-get_awaiting_return' ) 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 5145b61..a158238 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -1,37 +1,39 @@ -- +migrate Up INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`) - VALUES - (DEFAULT, 1 , 'role','admin-register'), - (DEFAULT, 1 , 'role','admin-getall_agent'), - (DEFAULT, 2 , 'role','admin-getall_agent'), +VALUES + (DEFAULT, 1 , 'role','admin-register'), + (DEFAULT, 1 , 'role','admin-getall_agent'), + (DEFAULT, 2 , 'role','admin-getall_agent'), - (DEFAULT, 1 , 'role','kindboxreq-add'), - (DEFAULT, 2 , 'role','kindboxreq-add'), - (DEFAULT, 1 , 'role','kindboxreq-accept'), - (DEFAULT, 2 , 'role','kindboxreq-accept'), - (DEFAULT, 1 , 'role','kindboxreq-reject'), - (DEFAULT, 2 , 'role','kindboxreq-reject'), - (DEFAULT, 1 , 'role','kindboxreq-assign_sender_agent'), - (DEFAULT, 2 , 'role','kindboxreq-assign_sender_agent'), - (DEFAULT, 1 , 'role','kindboxreq-deliver'), - (DEFAULT, 2 , 'role','kindboxreq-deliver'), - (DEFAULT, 3 , 'role','kindboxreq-deliver'), - (DEFAULT, 1 , 'role','kindboxreq-get'), - (DEFAULT, 2 , 'role','kindboxreq-get'), - (DEFAULT, 1 , 'role','kindboxreq-getall'), - (DEFAULT, 2 , 'role','kindboxreq-getall'), - (DEFAULT, 1 , 'role','kindboxreq-update'), - (DEFAULT, 2 , 'role','kindboxreq-update'), - (DEFAULT, 1 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 2 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 3 , 'role','kindboxreq-get_awaiting_delivery'), + (DEFAULT, 1 , 'role','kindboxreq-add'), + (DEFAULT, 2 , 'role','kindboxreq-add'), + (DEFAULT, 1 , 'role','kindboxreq-accept'), + (DEFAULT, 2 , 'role','kindboxreq-accept'), + (DEFAULT, 1 , 'role','kindboxreq-reject'), + (DEFAULT, 2 , 'role','kindboxreq-reject'), + (DEFAULT, 1 , 'role','kindboxreq-assign_sender_agent'), + (DEFAULT, 2 , 'role','kindboxreq-assign_sender_agent'), + (DEFAULT, 1 , 'role','kindboxreq-deliver'), + (DEFAULT, 2 , 'role','kindboxreq-deliver'), + (DEFAULT, 3 , 'role','kindboxreq-deliver'), + (DEFAULT, 1 , 'role','kindboxreq-get'), + (DEFAULT, 2 , 'role','kindboxreq-get'), + (DEFAULT, 1 , 'role','kindboxreq-getall'), + (DEFAULT, 2 , 'role','kindboxreq-getall'), + (DEFAULT, 1 , 'role','kindboxreq-update'), + (DEFAULT, 2 , 'role','kindboxreq-update'), + (DEFAULT, 1 , 'role','kindboxreq-get_awaiting_delivery'), + (DEFAULT, 2 , 'role','kindboxreq-get_awaiting_delivery'), + (DEFAULT, 3 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 1 , 'role','kindbox-assign_receiver_agent'), - (DEFAULT, 2 , 'role','kindbox-assign_receiver_agent'), - (DEFAULT, 1 , 'role','kindbox-get'), - (DEFAULT, 2 , 'role','kindbox-get'), - (DEFAULT, 1 , 'role','kindbox-getall'), - (DEFAULT, 2 , 'role','kindbox-getall'); + (DEFAULT, 1 , 'role','kindbox-assign_receiver_agent'), + (DEFAULT, 2 , 'role','kindbox-assign_receiver_agent'), + (DEFAULT, 1 , 'role','kindbox-get'), + (DEFAULT, 2 , 'role','kindbox-get'), + (DEFAULT, 1 , 'role','kindbox-getall'), + (DEFAULT, 2 , 'role','kindbox-getall'), + (DEFAULT, 1 , 'role','kindbox-get_awaiting_return'), + (DEFAULT, 3 , 'role','kindbox-get_awaiting_return'); diff --git a/service/agent/kind_box/get.go b/service/agent/kind_box/get.go new file mode 100644 index 0000000..dba9376 --- /dev/null +++ b/service/agent/kind_box/get.go @@ -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 +} diff --git a/service/agent/kind_box/service.go b/service/agent/kind_box/service.go new file mode 100644 index 0000000..d376f35 --- /dev/null +++ b/service/agent/kind_box/service.go @@ -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, + } +} diff --git a/validator/agent/kind_box/get.go b/validator/agent/kind_box/get.go new file mode 100644 index 0000000..95f070e --- /dev/null +++ b/validator/agent/kind_box/get.go @@ -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 +} diff --git a/validator/agent/kind_box/validator.go b/validator/agent/kind_box/validator.go new file mode 100644 index 0000000..e1428f9 --- /dev/null +++ b/validator/agent/kind_box/validator.go @@ -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 + } +}