From 300b69b44925b9d35dd15ce0763f09f8f9af4f08 Mon Sep 17 00:00:00 2001 From: "Mohammadi, Erfan" Date: Wed, 9 Oct 2024 01:55:49 +0330 Subject: [PATCH] feat(niki): get all benefactors by admin --- .../http_server/admin/admin/refresh_access.go | 2 +- .../http_server/admin/benefactor/get_all.go | 45 +- .../http_server/admin/benefactor/route.go | 6 +- delivery/http_server/admin/kind_box/update.go | 1 - .../http_server/agent/kind_box_req/deliver.go | 4 +- .../kind_box_req/get_all_delivery_awaiting.go | 2 +- .../kind_box_req/get_delivery_awaiting.go | 2 +- .../benefactor/benefactor/refresh_access.go | 4 +- delivery/http_server/server.go | 4 + docs/docs.go | 199 +++++ docs/swagger.json | 199 +++++ docs/swagger.yaml | 133 ++++ param/admin/benefactor/data.go | 4 +- param/admin/benefactor/get_all.go | 4 +- param/admin/kind_box/update.go | 16 +- param/admin/kind_box_req/add.go | 3 +- pkg/err_msg/message.go | 2 +- pkg/query_builder/mysql/filter.go | 23 +- pkg/query_builder/mysql/get_all.go | 8 +- repository/mysql/benefactor/exist.go | 1 + repository/mysql/benefactor/get.go | 1 + repository/mysql/benefactor/get_all.go | 3 +- repository/mysql/benefactor/scan.go | 3 +- repository/mysql/kind_box/update.go | 119 ++- repository/mysql/kind_box_req/add.go | 1 + ...s_controls_table_permission_enum_field.sql | 25 + ...159_insert_admin_access_controls_table.sql | 8 + service/admin/admin/refresh_access.go | 1 + service/admin/admin/service.go | 2 +- service/admin/benefactor/get_all.go | 30 +- service/admin/benefactor/service.go | 17 +- service/admin/kind_box_req/add.go | 1 + service/agent/kind_box/add.go | 1 + service/agent/kind_box_req/deliver.go | 4 +- .../kind_box_req/get_delivery_awaiting.go | 1 + service/agent/kind_box_req/service.go | 2 +- service/auth/create.go | 3 +- service/auth/parse.go | 3 +- .../benefactor/benefactor/refresh_access.go | 1 + service/benefactor/benefactor/service.go | 2 +- service/notification/kindbox_returned.go | 1 + service/notification/kindboxreq_delivered.go | 1 + service/service.go | 6 +- validator/admin/admin/login_test.go | 2 +- validator/admin/admin/validator.go | 1 + validator/admin/benefactor/get_all.go | 42 +- validator/admin/benefactor/validator.go | 53 +- validator/admin/kind_box/enumerate.go | 1 - validator/admin/kind_box/update.go | 3 +- validator/admin/kind_box/update_test.go | 739 +++++++++--------- validator/admin/kind_box/validator.go | 4 +- validator/agent/kind_box/validator.go | 3 +- validator/agent/kind_box_req/deliver.go | 1 + .../kind_box_req/get_all_delivery_awaiting.go | 1 + .../kind_box_req/get_delivery_awaiting.go | 1 + validator/agent/kind_box_req/validator.go | 4 +- .../benefactor/benefactor/send_otp_test.go | 3 +- validator/benefactor/benefactor/validator.go | 1 + 58 files changed, 1232 insertions(+), 525 deletions(-) create mode 100644 repository/mysql/migration/1726083158_alter_admin_access_controls_table_permission_enum_field.sql create mode 100644 repository/mysql/migration/1726083159_insert_admin_access_controls_table.sql diff --git a/delivery/http_server/admin/admin/refresh_access.go b/delivery/http_server/admin/admin/refresh_access.go index 4c960118..bbd7701a 100644 --- a/delivery/http_server/admin/admin/refresh_access.go +++ b/delivery/http_server/admin/admin/refresh_access.go @@ -1,9 +1,9 @@ package adminhandler import ( - adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" "net/http" + adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" ) diff --git a/delivery/http_server/admin/benefactor/get_all.go b/delivery/http_server/admin/benefactor/get_all.go index 9d6731b8..8a804dcf 100644 --- a/delivery/http_server/admin/benefactor/get_all.go +++ b/delivery/http_server/admin/benefactor/get_all.go @@ -1,26 +1,55 @@ package adminbenefactorhandler import ( - adminbenefactorparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" - "github.com/labstack/echo/v4" "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" + "github.com/labstack/echo/v4" ) // GetAllBenefactor godoc -// @Summary Get all benefactor by admin +// @Summary Get all benefactors by admin // @Tags Admins Benefactors // @Accept json // @Produce json -// @Success 200 {object} adminbenefactorparam.GetAllBenefactorResponse -// @Failure 400 {string} "Bad request" +// @Param filter_id query int false "Filter by ID" +// @Param filter_first_name query string false "Filter by first_name" +// @Param filter_last_name query string false "Filter by last_name" +// @Param filter_phone_number query string false "Filter by phone_number" +// @Param filter_email query string false "Filter by email" +// @Param filter_status query string false "Filter by status" Enums(active,inactive) +// @Param page_number query int false "Page number" +// @Param page_size query int false "Page size" +// @Param sort_field query string false "Sort by field" Enums(id,first_name,last_name,phone_number,email,status,created_at) +// @Param sort_direction query string false "Sort order" Enums(asc,desc) +// @Success 200 {object} param.BenefactorGetAllResponse +// @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 /admins/benefactors [get]. func (h Handler) GetAllBenefactor(c echo.Context) error { - var resp adminbenefactorparam.GetAllBenefactorResponse - resp, sErr := h.benefactorSvc.GetAllBenefactor(c.Request().Context()) + var req param.BenefactorGetAllRequest + + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + req.Filter = queryparam.GetFilterParams(c) + + resp, sErr := h.benefactorSvc.GetAllBenefactor(c.Request().Context(), req) if sErr != nil { msg, code := httpmsg.Error(sErr) + if resp.FieldErrors != nil { + return c.JSON(code, echo.Map{ + "message": msg, + "errors": resp.FieldErrors, + }) + } return echo.NewHTTPError(code, msg) } diff --git a/delivery/http_server/admin/benefactor/route.go b/delivery/http_server/admin/benefactor/route.go index 26ff08d9..28c8cabd 100644 --- a/delivery/http_server/admin/benefactor/route.go +++ b/delivery/http_server/admin/benefactor/route.go @@ -7,7 +7,9 @@ import ( ) func (h Handler) SetRoutes(e *echo.Echo) { - r := e.Group("/admins") + r := e.Group("/admins/benefactors") - r.GET("/benefactors", h.GetAllBenefactor, middleware.Auth(h.authSvc), middleware.AdminAuthorization(h.authorizeSvc, entity.AdminBenefactorGetAllPermission)) + r.Use(middleware.Auth(h.authSvc)) + + r.GET("", h.GetAllBenefactor, middleware.AdminAuthorization(h.authorizeSvc, entity.AdminBenefactorGetAllPermission)) } diff --git a/delivery/http_server/admin/kind_box/update.go b/delivery/http_server/admin/kind_box/update.go index ecf4cbb7..dd068491 100644 --- a/delivery/http_server/admin/kind_box/update.go +++ b/delivery/http_server/admin/kind_box/update.go @@ -24,7 +24,6 @@ import ( // @Security AuthBearerAdmin // @Router /admins/kindboxes/update/{id} [put]. func (h Handler) Update(c echo.Context) error { - var req param.KindBoxUpdateRequest if bErr := c.Bind(&req); bErr != nil { diff --git a/delivery/http_server/agent/kind_box_req/deliver.go b/delivery/http_server/agent/kind_box_req/deliver.go index 2b1fc166..ad5ea451 100644 --- a/delivery/http_server/agent/kind_box_req/deliver.go +++ b/delivery/http_server/agent/kind_box_req/deliver.go @@ -2,10 +2,10 @@ package agentkindboxreqhandler import ( "context" - params "git.gocasts.ir/ebhomengo/niki/param" - param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "net/http" + params "git.gocasts.ir/ebhomengo/niki/param" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql" diff --git a/delivery/http_server/agent/kind_box_req/get_all_delivery_awaiting.go b/delivery/http_server/agent/kind_box_req/get_all_delivery_awaiting.go index 4665f580..b48f1ad5 100644 --- a/delivery/http_server/agent/kind_box_req/get_all_delivery_awaiting.go +++ b/delivery/http_server/agent/kind_box_req/get_all_delivery_awaiting.go @@ -1,10 +1,10 @@ package agentkindboxreqhandler import ( - param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "net/http" "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "git.gocasts.ir/ebhomengo/niki/pkg/claim" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" diff --git a/delivery/http_server/agent/kind_box_req/get_delivery_awaiting.go b/delivery/http_server/agent/kind_box_req/get_delivery_awaiting.go index 1b8bf0be..dfe4f9ce 100644 --- a/delivery/http_server/agent/kind_box_req/get_delivery_awaiting.go +++ b/delivery/http_server/agent/kind_box_req/get_delivery_awaiting.go @@ -1,9 +1,9 @@ package agentkindboxreqhandler import ( - param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "net/http" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "git.gocasts.ir/ebhomengo/niki/pkg/claim" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" diff --git a/delivery/http_server/benefactor/benefactor/refresh_access.go b/delivery/http_server/benefactor/benefactor/refresh_access.go index ac565075..e074ccf0 100644 --- a/delivery/http_server/benefactor/benefactor/refresh_access.go +++ b/delivery/http_server/benefactor/benefactor/refresh_access.go @@ -1,10 +1,10 @@ package benefactorhandler import ( - benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "net/http" + benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" ) diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index b90c88b9..29b61e80 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -6,6 +6,7 @@ import ( "git.gocasts.ir/ebhomengo/niki/config" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" adminagenthandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/agent" + adminbenefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/benefactor" 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" adminrefertimehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/refer_time" @@ -30,6 +31,7 @@ type Server struct { adminKindBoxReqHandler adminkindboxreqhandler.Handler adminKindBoxHandler adminKindBoxHandler.Handler adminAgentHandler adminagenthandler.Handler + adminBenefactorHandler adminbenefactorhandler.Handler adminReferTimeHandler adminrefertimehandler.Handler agentKindBoxHandler agentkindboxhandler.Handler agentKindBoxReqHandler agentkindboxreqhandler.Handler @@ -51,6 +53,7 @@ func New( adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc), + adminBenefactorHandler: adminbenefactorhandler.New(svc.AdminAuthSvc, svc.AdminBenefactorSvc, svc.AdminAuthorizeSvc), adminReferTimeHandler: adminrefertimehandler.New(svc.AdminAuthSvc, svc.AdminReferTimeSvc, svc.AdminAuthorizeSvc), agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), @@ -93,6 +96,7 @@ func (s Server) RegisterRoutes() { s.adminKindBoxReqHandler.SetRoutes(s.Router) s.adminKindBoxHandler.SetRoutes(s.Router) s.adminReferTimeHandler.SetRoutes(s.Router) + s.adminBenefactorHandler.SetRoutes(s.Router) s.agentKindBoxHandler.SetRoutes(s.Router) s.agentKindBoxReqHandler.SetRoutes(s.Router) } diff --git a/docs/docs.go b/docs/docs.go index e7f56545..9928af1e 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -48,6 +48,142 @@ const docTemplate = `{ } } }, + "/admins/benefactors": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admins Benefactors" + ], + "summary": "Get all benefactors by admin", + "parameters": [ + { + "type": "integer", + "description": "Filter by ID", + "name": "filter_id", + "in": "query" + }, + { + "type": "string", + "description": "Filter by first_name", + "name": "filter_first_name", + "in": "query" + }, + { + "type": "string", + "description": "Filter by last_name", + "name": "filter_last_name", + "in": "query" + }, + { + "type": "string", + "description": "Filter by phone_number", + "name": "filter_phone_number", + "in": "query" + }, + { + "type": "string", + "description": "Filter by email", + "name": "filter_email", + "in": "query" + }, + { + "enum": [ + "active", + "inactive" + ], + "type": "string", + "description": "Filter by status", + "name": "filter_status", + "in": "query" + }, + { + "type": "integer", + "description": "Page number", + "name": "page_number", + "in": "query" + }, + { + "type": "integer", + "description": "Page size", + "name": "page_size", + "in": "query" + }, + { + "enum": [ + "id", + "first_name", + "last_name", + "phone_number", + "email", + "status", + "created_at" + ], + "type": "string", + "description": "Sort by field", + "name": "sort_field", + "in": "query" + }, + { + "enum": [ + "asc", + "desc" + ], + "type": "string", + "description": "Sort order", + "name": "sort_direction", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminbenefactoreparam.BenefactorGetAllResponse" + } + }, + "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" + } + } + } + } + }, "/admins/kindboxes": { "get": { "security": [ @@ -2862,6 +2998,58 @@ const docTemplate = `{ } } }, + "adminbenefactoreparam.BenefactorGetAllResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/adminbenefactoreparam.Data" + } + }, + "field_errors": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "pagination": { + "$ref": "#/definitions/param.PaginationResponse" + } + } + }, + "adminbenefactoreparam.Data": { + "type": "object", + "properties": { + "birth_date": { + "type": "string" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "first_name": { + "type": "string" + }, + "gender": { + "$ref": "#/definitions/entity.Gender" + }, + "id": { + "type": "integer" + }, + "last_name": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/entity.BenefactorStatus" + } + } + }, "adminkindboxparam.AssignReceiverRequest": { "type": "object", "properties": { @@ -4026,6 +4214,17 @@ const docTemplate = `{ "AdminInactiveStatus" ] }, + "entity.BenefactorStatus": { + "type": "string", + "enum": [ + "active", + "inactive" + ], + "x-enum-varnames": [ + "BenefactorActiveStatus", + "BenefactorInactiveStatus" + ] + }, "entity.City": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 1bd24198..e77cbde2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -37,6 +37,142 @@ } } }, + "/admins/benefactors": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admins Benefactors" + ], + "summary": "Get all benefactors by admin", + "parameters": [ + { + "type": "integer", + "description": "Filter by ID", + "name": "filter_id", + "in": "query" + }, + { + "type": "string", + "description": "Filter by first_name", + "name": "filter_first_name", + "in": "query" + }, + { + "type": "string", + "description": "Filter by last_name", + "name": "filter_last_name", + "in": "query" + }, + { + "type": "string", + "description": "Filter by phone_number", + "name": "filter_phone_number", + "in": "query" + }, + { + "type": "string", + "description": "Filter by email", + "name": "filter_email", + "in": "query" + }, + { + "enum": [ + "active", + "inactive" + ], + "type": "string", + "description": "Filter by status", + "name": "filter_status", + "in": "query" + }, + { + "type": "integer", + "description": "Page number", + "name": "page_number", + "in": "query" + }, + { + "type": "integer", + "description": "Page size", + "name": "page_size", + "in": "query" + }, + { + "enum": [ + "id", + "first_name", + "last_name", + "phone_number", + "email", + "status", + "created_at" + ], + "type": "string", + "description": "Sort by field", + "name": "sort_field", + "in": "query" + }, + { + "enum": [ + "asc", + "desc" + ], + "type": "string", + "description": "Sort order", + "name": "sort_direction", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminbenefactoreparam.BenefactorGetAllResponse" + } + }, + "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" + } + } + } + } + }, "/admins/kindboxes": { "get": { "security": [ @@ -2851,6 +2987,58 @@ } } }, + "adminbenefactoreparam.BenefactorGetAllResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/adminbenefactoreparam.Data" + } + }, + "field_errors": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "pagination": { + "$ref": "#/definitions/param.PaginationResponse" + } + } + }, + "adminbenefactoreparam.Data": { + "type": "object", + "properties": { + "birth_date": { + "type": "string" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "first_name": { + "type": "string" + }, + "gender": { + "$ref": "#/definitions/entity.Gender" + }, + "id": { + "type": "integer" + }, + "last_name": { + "type": "string" + }, + "phone_number": { + "type": "string" + }, + "status": { + "$ref": "#/definitions/entity.BenefactorStatus" + } + } + }, "adminkindboxparam.AssignReceiverRequest": { "type": "object", "properties": { @@ -4015,6 +4203,17 @@ "AdminInactiveStatus" ] }, + "entity.BenefactorStatus": { + "type": "string", + "enum": [ + "active", + "inactive" + ], + "x-enum-varnames": [ + "BenefactorActiveStatus", + "BenefactorInactiveStatus" + ] + }, "entity.City": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f78b1f39..367c1946 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -123,6 +123,40 @@ definitions: $ref: '#/definitions/adminagentparam.Data' type: array type: object + adminbenefactoreparam.BenefactorGetAllResponse: + properties: + data: + items: + $ref: '#/definitions/adminbenefactoreparam.Data' + type: array + field_errors: + additionalProperties: + type: string + type: object + pagination: + $ref: '#/definitions/param.PaginationResponse' + type: object + adminbenefactoreparam.Data: + properties: + birth_date: + type: string + description: + type: string + email: + type: string + first_name: + type: string + gender: + $ref: '#/definitions/entity.Gender' + id: + type: integer + last_name: + type: string + phone_number: + type: string + status: + $ref: '#/definitions/entity.BenefactorStatus' + type: object adminkindboxparam.AssignReceiverRequest: properties: receiver_agent_id: @@ -886,6 +920,14 @@ definitions: x-enum-varnames: - AdminActiveStatus - AdminInactiveStatus + entity.BenefactorStatus: + enum: + - active + - inactive + type: string + x-enum-varnames: + - BenefactorActiveStatus + - BenefactorInactiveStatus entity.City: properties: id: @@ -1015,6 +1057,97 @@ paths: summary: Get all agents by admin tags: - Admins + /admins/benefactors: + get: + consumes: + - application/json + parameters: + - description: Filter by ID + in: query + name: filter_id + type: integer + - description: Filter by first_name + in: query + name: filter_first_name + type: string + - description: Filter by last_name + in: query + name: filter_last_name + type: string + - description: Filter by phone_number + in: query + name: filter_phone_number + type: string + - description: Filter by email + in: query + name: filter_email + type: string + - description: Filter by status + enum: + - active + - inactive + in: query + name: filter_status + type: string + - description: Page number + in: query + name: page_number + type: integer + - description: Page size + in: query + name: page_size + type: integer + - description: Sort by field + enum: + - id + - first_name + - last_name + - phone_number + - email + - status + - created_at + in: query + name: sort_field + type: string + - description: Sort order + enum: + - asc + - desc + in: query + name: sort_direction + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/adminbenefactoreparam.BenefactorGetAllResponse' + "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 all benefactors by admin + tags: + - Admins Benefactors /admins/kindboxes: get: consumes: diff --git a/param/admin/benefactor/data.go b/param/admin/benefactor/data.go index dfd9cbc3..e846e075 100644 --- a/param/admin/benefactor/data.go +++ b/param/admin/benefactor/data.go @@ -1,8 +1,9 @@ package adminbenefactoreparam import ( - "git.gocasts.ir/ebhomengo/niki/entity" "time" + + "git.gocasts.ir/ebhomengo/niki/entity" ) type Data struct { @@ -14,6 +15,5 @@ type Data struct { Email string `json:"email"` Gender entity.Gender `json:"gender"` BirthDate time.Time `json:"birth_date"` - Roll entity.UserRole `json:"roll"` Status entity.BenefactorStatus `json:"status"` } diff --git a/param/admin/benefactor/get_all.go b/param/admin/benefactor/get_all.go index 9e91ece9..105bbdd7 100644 --- a/param/admin/benefactor/get_all.go +++ b/param/admin/benefactor/get_all.go @@ -4,12 +4,12 @@ import ( "git.gocasts.ir/ebhomengo/niki/param" ) -type GetAllBenefactorRequest struct { +type BenefactorGetAllRequest struct { Pagination param.PaginationRequest Sort param.SortRequest Filter param.FilterRequest } -type GetAllBenefactorResponse struct { +type BenefactorGetAllResponse struct { Data []Data `json:"data"` Pagination param.PaginationResponse `json:"pagination"` FieldErrors map[string]string `json:"field_errors,omitempty"` diff --git a/param/admin/kind_box/update.go b/param/admin/kind_box/update.go index 0bfae24e..c318237d 100644 --- a/param/admin/kind_box/update.go +++ b/param/admin/kind_box/update.go @@ -4,15 +4,15 @@ import ( "time" ) -//when kindbox creates, which fields are +// when kindbox creates, which fields are type KindBoxUpdateRequest struct { - KindBoxID uint `json:"-" param:"id" example:"1"` // ID is passed in the URL path - BenefactorID uint `json:"benefactor_id" example:"1"` - ReturnReferTimeID uint `json:"return_refere_time_id" example:"3"` - ReturnReferDate time.Time `json:"return_refer_date" example:"2025-01-02T15:04:05Z"` - ReturnAddressID uint `json:"return_address_id" example:"1"` - ReceiverAgentID uint `json:"receiver_agent_id" example:"23"` - Amount uint `json:"amount" example:"3"` + KindBoxID uint `json:"-" param:"id" example:"1"` // ID is passed in the URL path + BenefactorID uint `json:"benefactor_id" example:"1"` + ReturnReferTimeID uint `json:"return_refere_time_id" example:"3"` + ReturnReferDate time.Time `json:"return_refer_date" example:"2025-01-02T15:04:05Z"` + ReturnAddressID uint `json:"return_address_id" example:"1"` + ReceiverAgentID uint `json:"receiver_agent_id" example:"23"` + Amount uint `json:"amount" example:"3"` } type KindBoxUpdateResponse struct { diff --git a/param/admin/kind_box_req/add.go b/param/admin/kind_box_req/add.go index 3c94ebe7..f28d9679 100644 --- a/param/admin/kind_box_req/add.go +++ b/param/admin/kind_box_req/add.go @@ -1,8 +1,9 @@ package adminkindboxreqparam import ( - entity "git.gocasts.ir/ebhomengo/niki/entity" "time" + + entity "git.gocasts.ir/ebhomengo/niki/entity" ) type KindBoxReqAddRequest struct { diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 17474a46..17a4773e 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -35,7 +35,7 @@ const ( ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" - ErrorMsgReturnKindBoxStatus = "only returned kindboxes can be enumerated" + ErrorMsgReturnKindBoxStatus = "only returned kindboxes can be enumerated" ErrorMsgInvalidSerialNumberRange = "invalid serial number range" ErrorMsgInvalidOrExpiredJwt = "invalid or expired jwt" ErrorMsgInvalidRefreshToken = "invalid refresh token" diff --git a/pkg/query_builder/mysql/filter.go b/pkg/query_builder/mysql/filter.go index 4e2ff088..ee34f30d 100644 --- a/pkg/query_builder/mysql/filter.go +++ b/pkg/query_builder/mysql/filter.go @@ -3,6 +3,7 @@ package mysqlquerybuilder import ( "fmt" "regexp" + "strings" ) var datePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}Z)?$`) @@ -11,16 +12,30 @@ func isValidDateOrDateTime(value string) bool { return datePattern.MatchString(value) } -func BuildFilterQuery(filter map[string]interface{}) (filterQuery string, args []any) { +func BuildFilterQuery(baseQuery string, filter map[string]interface{}) (string, []any) { + var conditions []string + var args []any + for key, value := range filter { if strVal, ok := value.(string); ok && isValidDateOrDateTime(strVal) { - filterQuery += fmt.Sprintf("AND DATE(%s) = ? ", key) + conditions = append(conditions, fmt.Sprintf("DATE(%s) = ?", key)) args = append(args, strVal) } else { - filterQuery += fmt.Sprintf("AND %s = ? ", key) + conditions = append(conditions, fmt.Sprintf("%s = ?", key)) args = append(args, value) } } - return filterQuery, args + query := baseQuery + + if len(conditions) > 0 { + if strings.Contains(strings.ToUpper(baseQuery), "WHERE") { + query += " AND " + } else { + query += " WHERE " + } + query += strings.Join(conditions, " AND ") + } + + return query, args } diff --git a/pkg/query_builder/mysql/get_all.go b/pkg/query_builder/mysql/get_all.go index 0acc2227..7140d131 100644 --- a/pkg/query_builder/mysql/get_all.go +++ b/pkg/query_builder/mysql/get_all.go @@ -7,17 +7,15 @@ import ( ) func BuildGetAllQuery(baseQuery string, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) (query string, args []any) { - filterQuery, fArgs := BuildFilterQuery(filter) + filterQuery, fArgs := BuildFilterQuery(baseQuery, filter) paginationQuery, pArgs := BuildPaginationQuery(pagination) sortQuery := BuildSortQuery(sort) args = append(args, fArgs...) args = append(args, pArgs...) - query = baseQuery - if filterQuery != "" { - query = fmt.Sprintf("%s %s", query, filterQuery) - } + query = filterQuery + if sortQuery != "" { query = fmt.Sprintf("%s %s", query, sortQuery) } diff --git a/repository/mysql/benefactor/exist.go b/repository/mysql/benefactor/exist.go index aa1a65af..1bfe27bf 100644 --- a/repository/mysql/benefactor/exist.go +++ b/repository/mysql/benefactor/exist.go @@ -3,6 +3,7 @@ package mysqlbenefactor import ( "context" "errors" + "git.gocasts.ir/ebhomengo/niki/entity" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) diff --git a/repository/mysql/benefactor/get.go b/repository/mysql/benefactor/get.go index 789f4b33..15efa7a0 100644 --- a/repository/mysql/benefactor/get.go +++ b/repository/mysql/benefactor/get.go @@ -4,6 +4,7 @@ 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" diff --git a/repository/mysql/benefactor/get_all.go b/repository/mysql/benefactor/get_all.go index 2538b3ad..65517694 100644 --- a/repository/mysql/benefactor/get_all.go +++ b/repository/mysql/benefactor/get_all.go @@ -2,6 +2,7 @@ package mysqlbenefactor import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" params "git.gocasts.ir/ebhomengo/niki/param" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -37,7 +38,7 @@ func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest, } var total uint baseQuery = `SELECT COUNT(*) FROM benefactors` - query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort) + query, args = builder.BuildGetAllQuery(baseQuery, filter, params.PaginationRequest{}, params.SortRequest{}) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) if qErr != nil { return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/benefactor/scan.go b/repository/mysql/benefactor/scan.go index a6ad1256..882f4fcb 100644 --- a/repository/mysql/benefactor/scan.go +++ b/repository/mysql/benefactor/scan.go @@ -2,9 +2,10 @@ package mysqlbenefactor import ( "database/sql" + "time" + "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/repository/mysql" - "time" ) func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) { diff --git a/repository/mysql/kind_box/update.go b/repository/mysql/kind_box/update.go index fb051fb7..3e474f29 100644 --- a/repository/mysql/kind_box/update.go +++ b/repository/mysql/kind_box/update.go @@ -5,7 +5,6 @@ import ( "database/sql" "time" - "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -15,76 +14,76 @@ import ( func (d *DB) UpdateKindBox(ctx context.Context, kindBox entity.KindBox) error { const op = "mysqlkindbox.UpdateKindBox" - // Handle nullable fields - var ( - returnReferTimeID sql.NullInt64 - returnReferDate sql.NullTime - returnAddressID sql.NullInt64 - receiverAgentID sql.NullInt64 - returnedAt sql.NullTime - ) - - if kindBox.ReturnReferTimeID != 0 { - returnReferTimeID = sql.NullInt64{Int64: int64(kindBox.ReturnReferTimeID), Valid: true} - } else { - returnReferTimeID = sql.NullInt64{Int64: 0, Valid: false} - } - - if !kindBox.ReturnReferDate.IsZero() { - returnReferDate = sql.NullTime{Time: kindBox.ReturnReferDate, Valid: true} - } else { - returnReferDate = sql.NullTime{Time: time.Time{}, Valid: false} - } - - if kindBox.ReturnAddressID != 0 { - returnAddressID = sql.NullInt64{Int64: int64(kindBox.ReturnAddressID), Valid: true} - } else { - returnAddressID = sql.NullInt64{Int64: 0, Valid: false} - } - - if kindBox.ReceiverAgentID != 0 { - receiverAgentID = sql.NullInt64{Int64: int64(kindBox.ReceiverAgentID), Valid: true} - } else { - receiverAgentID = sql.NullInt64{Int64: 0, Valid: false} - } - - if !kindBox.ReturnedAt.IsZero() { - returnedAt = sql.NullTime{Time: kindBox.ReturnedAt, Valid: true} - } else { - returnedAt = sql.NullTime{Time: time.Time{}, Valid: false} - } + // Handle nullable fields + var ( + returnReferTimeID sql.NullInt64 + returnReferDate sql.NullTime + returnAddressID sql.NullInt64 + receiverAgentID sql.NullInt64 + returnedAt sql.NullTime + ) - query:= `UPDATE kind_boxes SET + if kindBox.ReturnReferTimeID != 0 { + returnReferTimeID = sql.NullInt64{Int64: int64(kindBox.ReturnReferTimeID), Valid: true} + } else { + returnReferTimeID = sql.NullInt64{Int64: 0, Valid: false} + } + + if !kindBox.ReturnReferDate.IsZero() { + returnReferDate = sql.NullTime{Time: kindBox.ReturnReferDate, Valid: true} + } else { + returnReferDate = sql.NullTime{Time: time.Time{}, Valid: false} + } + + if kindBox.ReturnAddressID != 0 { + returnAddressID = sql.NullInt64{Int64: int64(kindBox.ReturnAddressID), Valid: true} + } else { + returnAddressID = sql.NullInt64{Int64: 0, Valid: false} + } + + if kindBox.ReceiverAgentID != 0 { + receiverAgentID = sql.NullInt64{Int64: int64(kindBox.ReceiverAgentID), Valid: true} + } else { + receiverAgentID = sql.NullInt64{Int64: 0, Valid: false} + } + + if !kindBox.ReturnedAt.IsZero() { + returnedAt = sql.NullTime{Time: kindBox.ReturnedAt, Valid: true} + } else { + returnedAt = sql.NullTime{Time: time.Time{}, Valid: false} + } + + query := `UPDATE kind_boxes SET benefactor_id = ?, type = ?, amount = ?, serial_number = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?, deliver_address_id = ?, sender_agent_id = ?, delivered_at = ?, return_refer_time_id = ?, return_refer_date = ?, return_address_id = ?, receiver_agent_id = ?, returned_at = ? WHERE id = ? AND deleted_at IS NULL` - stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxUpdate, query) - if err != nil { + stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxUpdate, query) + if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) } _, uErr := stmt.ExecContext(ctx, - kindBox.BenefactorID, - kindBox.KindBoxType, - kindBox.Amount, - kindBox.SerialNumber, - kindBox.Status, - kindBox.DeliverReferTimeID, - kindBox.DeliverReferDate, - kindBox.DeliverAddressID, - kindBox.SenderAgentID, - kindBox.DeliveredAt, - returnReferTimeID, - returnReferDate, - returnAddressID, - receiverAgentID, - returnedAt, - kindBox.ID, - ) - + kindBox.BenefactorID, + kindBox.KindBoxType, + kindBox.Amount, + kindBox.SerialNumber, + kindBox.Status, + kindBox.DeliverReferTimeID, + kindBox.DeliverReferDate, + kindBox.DeliverAddressID, + kindBox.SenderAgentID, + kindBox.DeliveredAt, + returnReferTimeID, + returnReferDate, + returnAddressID, + receiverAgentID, + returnedAt, + kindBox.ID, + ) + if uErr != nil { return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord). WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/kind_box_req/add.go b/repository/mysql/kind_box_req/add.go index 1a88ee67..4717cfd7 100644 --- a/repository/mysql/kind_box_req/add.go +++ b/repository/mysql/kind_box_req/add.go @@ -2,6 +2,7 @@ package mysqlkindboxreq 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" diff --git a/repository/mysql/migration/1726083158_alter_admin_access_controls_table_permission_enum_field.sql b/repository/mysql/migration/1726083158_alter_admin_access_controls_table_permission_enum_field.sql new file mode 100644 index 00000000..73ecb8be --- /dev/null +++ b/repository/mysql/migration/1726083158_alter_admin_access_controls_table_permission_enum_field.sql @@ -0,0 +1,25 @@ +-- +migrate Up +ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` + enum ( + 'admin-register', + 'kindboxreq-accept', + 'kindboxreq-reject', + 'kindboxreq-getall', + 'kindboxreq-deliver', + 'kindboxreq-assign_sender_agent', + 'admin-getall_agent', + 'kindboxreq-get_awaiting_delivery', + 'kindbox-get', + 'kindboxreq-add', + 'kindbox-assign_receiver_agent', + 'kindbox-getall', + 'kindboxreq-update', + 'kindboxreq-get', + 'kindbox-get_awaiting_return', + 'kindbox-return', + 'kindbox-enumerate', + 'kindbox-update', + 'benefactor-getall' + ) NOT NULL; + +-- +migrate Down \ No newline at end of file diff --git a/repository/mysql/migration/1726083159_insert_admin_access_controls_table.sql b/repository/mysql/migration/1726083159_insert_admin_access_controls_table.sql new file mode 100644 index 00000000..99d66f7d --- /dev/null +++ b/repository/mysql/migration/1726083159_insert_admin_access_controls_table.sql @@ -0,0 +1,8 @@ +-- +migrate Up +INSERT INTO `admin_access_controls` (`actor_id`, `actor_type`,`permission`) +VALUES + (1 , 'role', 'benefactor-getall'), + (2 , 'role', 'benefactor-getall'); + +-- +migrate Down +DELETE FROM `admin_access_controls`; \ No newline at end of file diff --git a/service/admin/admin/refresh_access.go b/service/admin/admin/refresh_access.go index 49034a4a..84e14d1e 100644 --- a/service/admin/admin/refresh_access.go +++ b/service/admin/admin/refresh_access.go @@ -2,6 +2,7 @@ package adminservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index 4e3d18f8..507c66ae 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -3,10 +3,10 @@ package adminservice import ( "context" "fmt" - "git.gocasts.ir/ebhomengo/niki/service/auth" "git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/service/auth" validator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" "golang.org/x/crypto/bcrypt" ) diff --git a/service/admin/benefactor/get_all.go b/service/admin/benefactor/get_all.go index 9f6d9a2b..3b1e1dd8 100644 --- a/service/admin/benefactor/get_all.go +++ b/service/admin/benefactor/get_all.go @@ -2,23 +2,29 @@ package adminbenefactorservice import ( "context" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (s Service) GetAllBenefactor(ctx context.Context, req param.GetAllBenefactorRequest) (param.GetAllBenefactorResponse, error) { +func (s Service) GetAllBenefactor(ctx context.Context, req param.BenefactorGetAllRequest) (param.BenefactorGetAllResponse, error) { const op = "adminbenefactorservice.GetAllBenefactor" - benefactorInfo := make([]param.Data, 0) + if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil { + return param.BenefactorGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) + } + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() - benefactors, total, err := s.benefactorSvc.GetAllBenefactors(ctx, req.Filter, req.Pagination, req.Sort) + benefactors, total, err := s.repo.GetAllBenefactor(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { - return param.GetAllBenefactorResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + return param.BenefactorGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } + resp := make([]param.Data, 0) for _, benefactor := range benefactors { - benefactorInfo = append(benefactorInfo, param.Data{ + resp = append(resp, param.Data{ ID: benefactor.ID, FirstName: benefactor.FirstName, LastName: benefactor.LastName, @@ -27,14 +33,16 @@ func (s Service) GetAllBenefactor(ctx context.Context, req param.GetAllBenefacto Email: benefactor.Email, Gender: benefactor.Gender, BirthDate: benefactor.BirthDate, - Roll: benefactor.Role, Status: benefactor.Status, }) } - return param.GetAllBenefactorResponse{Data: benefactorInfo, Pagination: paginationparam.PaginationResponse{ - PageSize: req.Pagination.PageSize, - PageNumber: req.Pagination.PageNumber, - Total: total, - }}, nil + return param.BenefactorGetAllResponse{ + Data: resp, + Pagination: paginationparam.PaginationResponse{ + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, + Total: total, + }, + }, nil } diff --git a/service/admin/benefactor/service.go b/service/admin/benefactor/service.go index 19a03121..966de2c4 100644 --- a/service/admin/benefactor/service.go +++ b/service/admin/benefactor/service.go @@ -2,29 +2,28 @@ package adminbenefactorservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/param" adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" + validator "git.gocasts.ir/ebhomengo/niki/validator/admin/benefactor" ) -type BenefactorSvc interface { - GetAllBenefactors(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.Benefactor, uint, error) -} type Repository interface { IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) GetByID(ctx context.Context, id uint) (entity.Benefactor, error) + GetAllBenefactor(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.Benefactor, uint, error) } type AddressSvc interface { GetAddressByID(ctx context.Context, request adminaddressparam.AddressGetRequest) (adminaddressparam.AddressGetResponse, error) } type Service struct { - repo Repository - addressSvc AddressSvc - benefactorSvc BenefactorSvc + repo Repository + addressSvc AddressSvc + vld validator.Validator } -func New(repo Repository, addressSvc AddressSvc, benefactorSvc BenefactorSvc) Service { - return Service{repo: repo, addressSvc: addressSvc, benefactorSvc: benefactorSvc} +func New(repo Repository, addressSvc AddressSvc, vld validator.Validator) Service { + return Service{repo: repo, addressSvc: addressSvc, vld: vld} } diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index e94edc80..808ca7f3 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/service/agent/kind_box/add.go b/service/agent/kind_box/add.go index ede429e4..afde3a6d 100644 --- a/service/agent/kind_box/add.go +++ b/service/agent/kind_box/add.go @@ -2,6 +2,7 @@ package agentkindboxservice import ( "context" + entity "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/service/agent/kind_box_req/deliver.go b/service/agent/kind_box_req/deliver.go index b77d2c97..102f1bfb 100644 --- a/service/agent/kind_box_req/deliver.go +++ b/service/agent/kind_box_req/deliver.go @@ -2,10 +2,10 @@ package agentkindboxreqservice import ( "context" - kindboxParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" - param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" "time" + kindboxParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) diff --git a/service/agent/kind_box_req/get_delivery_awaiting.go b/service/agent/kind_box_req/get_delivery_awaiting.go index cc5a93f4..4f71653b 100644 --- a/service/agent/kind_box_req/get_delivery_awaiting.go +++ b/service/agent/kind_box_req/get_delivery_awaiting.go @@ -2,6 +2,7 @@ package agentkindboxreqservice import ( "context" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) diff --git a/service/agent/kind_box_req/service.go b/service/agent/kind_box_req/service.go index e88c6d60..ad1eeed4 100644 --- a/service/agent/kind_box_req/service.go +++ b/service/agent/kind_box_req/service.go @@ -2,10 +2,10 @@ package agentkindboxreqservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" "git.gocasts.ir/ebhomengo/niki/entity" params "git.gocasts.ir/ebhomengo/niki/param" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" validator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box_req" ) diff --git a/service/auth/create.go b/service/auth/create.go index 957ed402..a91e444a 100644 --- a/service/auth/create.go +++ b/service/auth/create.go @@ -1,9 +1,10 @@ package auth import ( + "time" + "git.gocasts.ir/ebhomengo/niki/entity" "github.com/golang-jwt/jwt/v4" - "time" ) func (s Service) CreateAccessToken(user entity.Authenticable) (string, error) { diff --git a/service/auth/parse.go b/service/auth/parse.go index c4722805..23a2284d 100644 --- a/service/auth/parse.go +++ b/service/auth/parse.go @@ -2,9 +2,10 @@ package auth import ( "fmt" + "strings" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "github.com/golang-jwt/jwt/v4" - "strings" ) func (s Service) ParseBearerToken(bearerToken string) (*Claims, error) { diff --git a/service/benefactor/benefactor/refresh_access.go b/service/benefactor/benefactor/refresh_access.go index ded6ef31..8cbb3bbd 100644 --- a/service/benefactor/benefactor/refresh_access.go +++ b/service/benefactor/benefactor/refresh_access.go @@ -2,6 +2,7 @@ package benefactorservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go index c92dd887..728777f7 100644 --- a/service/benefactor/benefactor/service.go +++ b/service/benefactor/benefactor/service.go @@ -2,11 +2,11 @@ package benefactorservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/service/auth" "time" smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms" "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" ) diff --git a/service/notification/kindbox_returned.go b/service/notification/kindbox_returned.go index b17c8da0..747fa4d6 100644 --- a/service/notification/kindbox_returned.go +++ b/service/notification/kindbox_returned.go @@ -3,6 +3,7 @@ package notification import ( "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/param" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" kbp "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" diff --git a/service/notification/kindboxreq_delivered.go b/service/notification/kindboxreq_delivered.go index 84ade455..18c336e1 100644 --- a/service/notification/kindboxreq_delivered.go +++ b/service/notification/kindboxreq_delivered.go @@ -3,6 +3,7 @@ package notification import ( "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/param" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" diff --git a/service/service.go b/service/service.go index 03c18706..5f53a888 100644 --- a/service/service.go +++ b/service/service.go @@ -31,6 +31,7 @@ import ( benefactorrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/refer_time" "git.gocasts.ir/ebhomengo/niki/service/notification" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" + adminbenefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/benefactor" 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" @@ -48,6 +49,7 @@ type Service struct { AdminAuthorizeSvc adminauthorizationservice.Service AdminSvc adminservice.Service AdminAgentSvc adminagentservice.Service + AdminBenefactorSvc adminbenefactorservice.Service AgentKindBoxSvc agentkindboxservice.Service AgentKindBoxReqSvc agentkindboxreqservice.Service BenefactorAuthSvc auth.Service @@ -76,7 +78,8 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo) AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo) AdminAddressSvc = adminaddressservice.New(addressRepo) - AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc) + AdminBenefactorVld = adminbenefactorvalidator.New() + AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld) AdminAgentSvc = adminagentservice.New(agentRepo) AdminVld = adminvalidator.New(adminRepo) @@ -116,6 +119,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont AdminAuthorizeSvc: AdminAuthorizeSvc, AdminSvc: AdminSvc, AdminAgentSvc: AdminAgentSvc, + AdminBenefactorSvc: AdminBenefactorSvc, AgentKindBoxSvc: AgentKindBoxSvc, AgentKindBoxReqSvc: AgentKindBoxReqSvc, BenefactorAuthSvc: BenefactorAuthSvc, diff --git a/validator/admin/admin/login_test.go b/validator/admin/admin/login_test.go index 5d3077cf..cab772cd 100644 --- a/validator/admin/admin/login_test.go +++ b/validator/admin/admin/login_test.go @@ -3,9 +3,9 @@ package adminvalidator import ( "context" "errors" - "git.gocasts.ir/ebhomengo/niki/entity" "testing" + "git.gocasts.ir/ebhomengo/niki/entity" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "github.com/stretchr/testify/assert" diff --git a/validator/admin/admin/validator.go b/validator/admin/admin/validator.go index 47312f8e..2fb1522a 100644 --- a/validator/admin/admin/validator.go +++ b/validator/admin/admin/validator.go @@ -3,6 +3,7 @@ package adminvalidator import ( "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" diff --git a/validator/admin/benefactor/get_all.go b/validator/admin/benefactor/get_all.go index 19c20519..ef2f7700 100644 --- a/validator/admin/benefactor/get_all.go +++ b/validator/admin/benefactor/get_all.go @@ -1 +1,41 @@ -package benefactor +package adminbenefactorvalidator + +import ( + "errors" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + 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) ValidateGetAll(req param.BenefactorGetAllRequest) (map[string]string, error) { + const op = "adminbenefactorvalidator.ValidateGetAll" + + validFields := []string{"id", "first_name", "last_name", "phone_number", "email", "status", "created_at"} + + if err := validation.ValidateStruct(&req, + validation.Field(&req.Filter, validation.By(v.AreFilterFieldsValid(validFields))), + validation.Field(&req.Sort, validation.By(v.AreSortFieldsValid(validFields))), + ); 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/admin/benefactor/validator.go b/validator/admin/benefactor/validator.go index 19c20519..3395a6a0 100644 --- a/validator/admin/benefactor/validator.go +++ b/validator/admin/benefactor/validator.go @@ -1 +1,52 @@ -package benefactor +package adminbenefactorvalidator + +import ( + "fmt" + "slices" + + params "git.gocasts.ir/ebhomengo/niki/param" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + validation "github.com/go-ozzo/ozzo-validation/v4" +) + +type Validator struct{} + +func New() Validator { + return Validator{} +} + +func (v Validator) AreSortFieldsValid(validSortFields []string) validation.RuleFunc { + return func(value interface{}) error { + sort, ok := value.(params.SortRequest) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if sort.Field == "" && sort.Direction != "" { + return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired) + } + if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection { + return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc) + } + if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) { + return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid) + } + + return nil + } +} + +func (v Validator) AreFilterFieldsValid(validFilters []string) validation.RuleFunc { + return func(value interface{}) error { + filters, ok := value.(params.FilterRequest) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + for filter := range filters { + if !slices.Contains(validFilters, filter) { + return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid) + } + } + + return nil + } +} diff --git a/validator/admin/kind_box/enumerate.go b/validator/admin/kind_box/enumerate.go index 4a738a24..c4b24e99 100644 --- a/validator/admin/kind_box/enumerate.go +++ b/validator/admin/kind_box/enumerate.go @@ -19,7 +19,6 @@ func (v Validator) ValidateEnumerate(ctx context.Context, req param.EnumerateKin validation.By(v.CheckKindBoxStatusForEnumeration(ctx))), validation.Field(&req.Amount, validation.Required), - ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/update.go b/validator/admin/kind_box/update.go index c0bbd42d..ca64210b 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -12,7 +12,7 @@ import ( func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxUpdateRequest) (map[string]string, error) { const op = "adminkindboxvalidator.ValidateUpdateRequest" - + err := validation.ValidateStruct(&req, validation.Field(&req.KindBoxID, validation.Required, @@ -22,7 +22,6 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxU validation.By(v.doesBenefactorExist(ctx)), validation.By(v.doesBenefactorHaveKindBox(ctx, req.KindBoxID, req.BenefactorID))), ) - if err != nil { fieldErrors := make(map[string]string) v.processValidationErrors(err, fieldErrors) diff --git a/validator/admin/kind_box/update_test.go b/validator/admin/kind_box/update_test.go index 8ddc1878..ef4145d9 100644 --- a/validator/admin/kind_box/update_test.go +++ b/validator/admin/kind_box/update_test.go @@ -1,453 +1,428 @@ package adminkindboxvalidator import ( - "context" - "testing" - "time" + "context" + "testing" + "time" - "github.com/stretchr/testify/assert" - "git.gocasts.ir/ebhomengo/niki/entity" - adminupdateparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - benefactorserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" - agentserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" - addressserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" - refertimeserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" + "git.gocasts.ir/ebhomengo/niki/entity" + addressserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" + agentserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" + benefactorserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + adminupdateparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + refertimeserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" + "github.com/stretchr/testify/assert" ) - - func TestValidator_ValidateUpdateRequest(t *testing.T) { + now := time.Now() + twoDaysLater := now.Add(48 * time.Hour) + threeDaysLater := now.Add(52 * time.Hour) - now := time.Now() - twoDaysLater := now.Add(48 * time.Hour) - threeDaysLater := now.Add(52 * time.Hour) + mockRepository := NewMockRepository(t) + mockAgentSvc := NewMockAgentSvc(t) + mockBenefactorSvc := NewMockBenefactorSvc(t) + mockReferTimeSvc := NewMockReferTimeSvc(t) + mockAddressSvc := NewMockAddressSvc(t) - mockRepository := NewMockRepository(t) - mockAgentSvc := NewMockAgentSvc(t) - mockBenefactorSvc := NewMockBenefactorSvc(t) - mockReferTimeSvc := NewMockReferTimeSvc(t) - mockAddressSvc := NewMockAddressSvc(t) + validator := New(mockRepository, mockAgentSvc, mockBenefactorSvc, mockReferTimeSvc, mockAddressSvc) + ctx := context.Background() - validator := New(mockRepository, mockAgentSvc, mockBenefactorSvc, mockReferTimeSvc, mockAddressSvc) - ctx := context.Background() + t.Run("KindBox Delivered And Benefactor Does Not Exist", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + } + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - t.Run("KindBox Delivered And Benefactor Does Not Exist", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - } - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() - + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: false}, nil).Once() - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "benefactor_id") + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - }) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "benefactor_id") + }) - t.Run("KindBox Does Not Exist", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - } - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(false, nil).Once() + t.Run("KindBox Does Not Exist", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + } - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(false, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{}, nil).Once() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "KindBoxID") - }) + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{}, nil).Once() - t.Run("KindBox Delivered And Does Not Belong to Benefactor", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - } - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 2}, nil).Once() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "benefactor_id") - }) + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - t.Run("KindBoxID is Null", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 0, - BenefactorID: 1, - } - - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "KindBoxID") + }) - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{}, nil).Once() - - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "KindBoxID") - - - }) + t.Run("KindBox Delivered And Does Not Belong to Benefactor", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + } - t.Run("BenefactorID is Null", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 0, - } + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 1}, nil).Once() // Note the different BenefactorID + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 2}, nil).Once() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "benefactor_id") - }) + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "benefactor_id") + }) - t.Run("Valid KindBox Delivered Request", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - } + t.Run("KindBoxID is Null", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 0, + BenefactorID: 1, + } - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). - Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{}, nil).Once() - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - }) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "KindBoxID") + }) - t.Run("KindBox Ready to Return Status and Address Does Not Belong", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReturnReferDate: twoDaysLater, - ReturnReferTimeID: 1, - ReturnAddressID: 23, - } + t.Run("BenefactorID is Null", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 0, + } - validReferTime := entity.ReferTime{ - ID: 1, - Duration: "30m", - Status: entity.ReferTimeActiveStatus, - } + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - addressExm := entity.Address{ - ID: 23, - PostalCode: "1231231231", - Address : "Tehran", - Name : "Home", - CityID : 1, - ProvinceID: 1, - BenefactorID: 2, - } - + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 1}, nil).Once() // Note the different BenefactorID - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). - Return(true, nil).Once() + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). - Return(addressserviceparam.AddressGetResponse{Address: addressExm}, nil).Once() - - mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: 1}). - Return(refertimeserviceparam.GetReferTimeResponse{ReferTime: validReferTime}, nil).Once() + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "benefactor_id") + }) - mockRepository.EXPECT().GetKindBox(ctx, uint(req.KindBoxID)). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + t.Run("Valid KindBox Delivered Request", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + } - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "return_address_id") - }) + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). + Return(true, nil).Once() - t.Run("KindBox Ready to Return Status and ReturnReferTimeID Invalid", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReturnReferDate: twoDaysLater, - ReturnReferTimeID: 14, - ReturnAddressID: 1, - } + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - validAddress := entity.Address{ - ID: 1, - PostalCode: "1234567890", - Address: "tehran", - Name: "home", - CityID: 1, - ProvinceID: 1, - BenefactorID: 1, - } + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once() - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). - Return(true, nil).Once() + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + }) - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). - Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once() - - mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}). - Return(refertimeserviceparam.GetReferTimeResponse{}, nil).Once() + t.Run("KindBox Ready to Return Status and Address Does Not Belong", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnReferDate: twoDaysLater, + ReturnReferTimeID: 1, + ReturnAddressID: 23, + } - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + validReferTime := entity.ReferTime{ + ID: 1, + Duration: "30m", + Status: entity.ReferTimeActiveStatus, + } - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "return_refere_time_id") - }) + addressExm := entity.Address{ + ID: 23, + PostalCode: "1231231231", + Address: "Tehran", + Name: "Home", + CityID: 1, + ProvinceID: 1, + BenefactorID: 2, + } + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). + Return(true, nil).Once() - t.Run("Valid KindBox Ready to Return Status Request", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReturnReferDate: twoDaysLater, - ReturnReferTimeID: 1, - ReturnAddressID: 1, - } + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - validAddress := entity.Address{ - ID: 1, - PostalCode: "1234567890", - Address: "tehran", - Name: "home", - CityID: 1, - ProvinceID: 1, - BenefactorID: 1, - } + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - validReferTime := entity.ReferTime{ - ID: 1, - Duration: "30m", - Status: entity.ReferTimeActiveStatus, - } + mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). + Return(addressserviceparam.AddressGetResponse{Address: addressExm}, nil).Once() - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). - Return(true, nil).Once() + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: 1}). + Return(refertimeserviceparam.GetReferTimeResponse{ReferTime: validReferTime}, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, uint(req.KindBoxID)). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - - mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). - Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once() - - mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}). - Return(refertimeserviceparam.GetReferTimeResponse{ReferTime: validReferTime}, nil).Once() + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - mockRepository.EXPECT().GetKindBox(ctx, uint(1)). - Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_address_id") + }) - assert.NoError(t, err) - assert.Nil(t, fieldErrors) - }) + t.Run("KindBox Ready to Return Status and ReturnReferTimeID Invalid", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnReferDate: twoDaysLater, + ReturnReferTimeID: 14, + ReturnAddressID: 1, + } - t.Run("Assigned Receiver Agent Status And ReturnReferDate Not Null", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReturnReferDate: threeDaysLater, - } - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus, BenefactorID: req.BenefactorID}, nil).Twice() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "Return Refer Date" ) - }) + validAddress := entity.Address{ + ID: 1, + PostalCode: "1234567890", + Address: "tehran", + Name: "home", + CityID: 1, + ProvinceID: 1, + BenefactorID: 1, + } - t.Run("Assigned Receiver Agent Status And ReturnAddressID Not Null", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReturnAddressID: 1, - } - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus, BenefactorID: req.BenefactorID}, nil).Twice() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "Return Address ID" ) - }) + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). + Return(true, nil).Once() - t.Run("Valid KindBox Assigned Receiver Agent Status Request", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - ReceiverAgentID: 1, - } - - - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{ - Status: entity.KindBoxAssignedReceiverAgentStatus, - BenefactorID: 1, - }, nil).Once() - - mockAgentSvc.EXPECT().AgentExistByID(ctx, agentserviceparam.AdminAgentExistByIDRequest{AgentID: 1}).Return(agentserviceparam.AdminAgentExistByIDResponse{Exist: true}, nil).Once() - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{ - Status: entity.KindBoxAssignedReceiverAgentStatus, - BenefactorID: req.BenefactorID, - }, nil).Once() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.NoError(t, err) - assert.Nil(t, fieldErrors) - }) + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - t.Run("Valid Kindbox Returned Status Request", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - } + mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). + Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once() - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxReturnedStatus}, nil).Once() - - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - - }) + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}). + Return(refertimeserviceparam.GetReferTimeResponse{}, nil).Once() - t.Run("Invalid KindBox Enumerate Status Amount", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - Amount: 0, - } - + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{ - Status: entity.KindBoxEnumeratedStatus, - BenefactorID: req.BenefactorID, - }, nil).Twice() + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "return_refere_time_id") + }) - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.Error(t, err) - assert.NotNil(t, fieldErrors) - assert.Contains(t, fieldErrors, "amount" ) - }) + t.Run("Valid KindBox Ready to Return Status Request", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnReferDate: twoDaysLater, + ReturnReferTimeID: 1, + ReturnAddressID: 1, + } + validAddress := entity.Address{ + ID: 1, + PostalCode: "1234567890", + Address: "tehran", + Name: "home", + CityID: 1, + ProvinceID: 1, + BenefactorID: 1, + } - t.Run("Valid KindBox Enumerated Status Request", func(t *testing.T) { - req := adminupdateparam.KindBoxUpdateRequest{ - KindBoxID: 1, - BenefactorID: 1, - Amount: 100, - } - + validReferTime := entity.ReferTime{ + ID: 1, + Duration: "30m", + Status: entity.ReferTimeActiveStatus, + } - mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). + Return(true, nil).Once() - mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). - Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - - - mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). - Return(entity.KindBox{ - Status: entity.KindBoxEnumeratedStatus, - BenefactorID: 1, - }, nil).Twice() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() - fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) - - assert.NoError(t, err) - assert.Nil(t, fieldErrors) - }) + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() + + mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). + Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once() + + mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}). + Return(refertimeserviceparam.GetReferTimeResponse{ReferTime: validReferTime}, nil).Once() + + mockRepository.EXPECT().GetKindBox(ctx, uint(1)). + Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Assigned Receiver Agent Status And ReturnReferDate Not Null", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnReferDate: threeDaysLater, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus, BenefactorID: req.BenefactorID}, nil).Twice() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Return Refer Date") + }) + + t.Run("Assigned Receiver Agent Status And ReturnAddressID Not Null", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReturnAddressID: 1, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxAssignedReceiverAgentStatus, BenefactorID: req.BenefactorID}, nil).Twice() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "Return Address ID") + }) + + t.Run("Valid KindBox Assigned Receiver Agent Status Request", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + ReceiverAgentID: 1, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{ + Status: entity.KindBoxAssignedReceiverAgentStatus, + BenefactorID: 1, + }, nil).Once() + + mockAgentSvc.EXPECT().AgentExistByID(ctx, agentserviceparam.AdminAgentExistByIDRequest{AgentID: 1}).Return(agentserviceparam.AdminAgentExistByIDResponse{Exist: true}, nil).Once() + + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{ + Status: entity.KindBoxAssignedReceiverAgentStatus, + BenefactorID: req.BenefactorID, + }, nil).Once() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) + + t.Run("Valid Kindbox Returned Status Request", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).Return(entity.KindBox{Status: entity.KindBoxReturnedStatus}, nil).Once() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + }) + + t.Run("Invalid KindBox Enumerate Status Amount", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + Amount: 0, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{ + Status: entity.KindBoxEnumeratedStatus, + BenefactorID: req.BenefactorID, + }, nil).Twice() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.Error(t, err) + assert.NotNil(t, fieldErrors) + assert.Contains(t, fieldErrors, "amount") + }) + + t.Run("Valid KindBox Enumerated Status Request", func(t *testing.T) { + req := adminupdateparam.KindBoxUpdateRequest{ + KindBoxID: 1, + BenefactorID: 1, + Amount: 100, + } + + mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() + + mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). + Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() + + mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). + Return(entity.KindBox{ + Status: entity.KindBoxEnumeratedStatus, + BenefactorID: 1, + }, nil).Twice() + + fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) + + assert.NoError(t, err) + assert.Nil(t, fieldErrors) + }) } diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 4e32b032..37c7ce25 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -4,11 +4,11 @@ import ( "context" "errors" "fmt" - params "git.gocasts.ir/ebhomengo/niki/param" "slices" "time" entity "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" @@ -106,7 +106,6 @@ func (v Validator) doesBenefactorHaveKindBox(ctx context.Context, kindBoxID uint fetchedKindBox, err := v.repo.GetKindBox(ctx, kindBoxID) if err != nil { - return richerror.New(op).WithErr(err).WithMessage("could not fetch KindBox").WithKind(richerror.KindUnexpected) } @@ -191,6 +190,7 @@ func (v Validator) CheckKindBoxStatusForEnumeration(ctx context.Context) validat return nil } } + func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxID uint, benefactorID uint, addressID uint) validation.RuleFunc { return func(value interface{}) error { const op = "Validator.doesBenefactorAddressExist" diff --git a/validator/agent/kind_box/validator.go b/validator/agent/kind_box/validator.go index 43390546..bf45fa8e 100644 --- a/validator/agent/kind_box/validator.go +++ b/validator/agent/kind_box/validator.go @@ -3,9 +3,9 @@ package agentkindboxvalidator import ( "context" "fmt" - params "git.gocasts.ir/ebhomengo/niki/param" "slices" + params "git.gocasts.ir/ebhomengo/niki/param" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" ) @@ -39,6 +39,7 @@ func (v Validator) doesKindBoxExistForAgent(ctx context.Context, agentID uint) v return nil } } + func (v Validator) AreSortFieldsValid(validSortFields []string) validation.RuleFunc { return func(value interface{}) error { sort, ok := value.(params.SortRequest) diff --git a/validator/agent/kind_box_req/deliver.go b/validator/agent/kind_box_req/deliver.go index 068bf75c..04e4b239 100644 --- a/validator/agent/kind_box_req/deliver.go +++ b/validator/agent/kind_box_req/deliver.go @@ -3,6 +3,7 @@ package agentkindboxreqvalidator import ( "context" "errors" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/validator/agent/kind_box_req/get_all_delivery_awaiting.go b/validator/agent/kind_box_req/get_all_delivery_awaiting.go index 552c33e6..9d5d370f 100644 --- a/validator/agent/kind_box_req/get_all_delivery_awaiting.go +++ b/validator/agent/kind_box_req/get_all_delivery_awaiting.go @@ -2,6 +2,7 @@ package agentkindboxreqvalidator import ( "errors" + kbrparam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/validator/agent/kind_box_req/get_delivery_awaiting.go b/validator/agent/kind_box_req/get_delivery_awaiting.go index 00499fe1..1b348a0f 100644 --- a/validator/agent/kind_box_req/get_delivery_awaiting.go +++ b/validator/agent/kind_box_req/get_delivery_awaiting.go @@ -2,6 +2,7 @@ package agentkindboxreqvalidator import ( "errors" + param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" diff --git a/validator/agent/kind_box_req/validator.go b/validator/agent/kind_box_req/validator.go index 393c9842..8355f790 100644 --- a/validator/agent/kind_box_req/validator.go +++ b/validator/agent/kind_box_req/validator.go @@ -4,10 +4,10 @@ import ( "context" "errors" "fmt" - "git.gocasts.ir/ebhomengo/niki/entity" - params "git.gocasts.ir/ebhomengo/niki/param" "slices" + "git.gocasts.ir/ebhomengo/niki/entity" + params "git.gocasts.ir/ebhomengo/niki/param" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" ) diff --git a/validator/benefactor/benefactor/send_otp_test.go b/validator/benefactor/benefactor/send_otp_test.go index 02ce180d..57b5dcf2 100644 --- a/validator/benefactor/benefactor/send_otp_test.go +++ b/validator/benefactor/benefactor/send_otp_test.go @@ -2,12 +2,13 @@ package benefactorvalidator import ( "context" + "testing" + "git.gocasts.ir/ebhomengo/niki/entity" benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" "github.com/stretchr/testify/assert" - "testing" ) func TestValidator_ValidateSendOtpRequest(t *testing.T) { diff --git a/validator/benefactor/benefactor/validator.go b/validator/benefactor/benefactor/validator.go index 07b88376..c3474548 100644 --- a/validator/benefactor/benefactor/validator.go +++ b/validator/benefactor/benefactor/validator.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"