feat(niki): get all benefactors by admin

This commit is contained in:
Erfan Mohammadi 2024-10-09 01:55:49 +03:30
parent 046b292f9f
commit 300b69b449
58 changed files with 1232 additions and 525 deletions

View File

@ -1,9 +1,9 @@
package adminhandler package adminhandler
import ( import (
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
"net/http" "net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )

View File

@ -1,26 +1,55 @@
package adminbenefactorhandler package adminbenefactorhandler
import ( 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" "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 // GetAllBenefactor godoc
// @Summary Get all benefactor by admin // @Summary Get all benefactors by admin
// @Tags Admins Benefactors // @Tags Admins Benefactors
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} adminbenefactorparam.GetAllBenefactorResponse // @Param filter_id query int false "Filter by ID"
// @Failure 400 {string} "Bad request" // @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 // @Security AuthBearerAdmin
// @Router /admins/benefactors [get]. // @Router /admins/benefactors [get].
func (h Handler) GetAllBenefactor(c echo.Context) error { func (h Handler) GetAllBenefactor(c echo.Context) error {
var resp adminbenefactorparam.GetAllBenefactorResponse var req param.BenefactorGetAllRequest
resp, sErr := h.benefactorSvc.GetAllBenefactor(c.Request().Context())
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 { if sErr != nil {
msg, code := httpmsg.Error(sErr) 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) return echo.NewHTTPError(code, msg)
} }

View File

@ -7,7 +7,9 @@ import (
) )
func (h Handler) SetRoutes(e *echo.Echo) { 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))
} }

View File

@ -24,7 +24,6 @@ import (
// @Security AuthBearerAdmin // @Security AuthBearerAdmin
// @Router /admins/kindboxes/update/{id} [put]. // @Router /admins/kindboxes/update/{id} [put].
func (h Handler) Update(c echo.Context) error { func (h Handler) Update(c echo.Context) error {
var req param.KindBoxUpdateRequest var req param.KindBoxUpdateRequest
if bErr := c.Bind(&req); bErr != nil { if bErr := c.Bind(&req); bErr != nil {

View File

@ -2,10 +2,10 @@ package agentkindboxreqhandler
import ( import (
"context" "context"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"net/http" "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" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql" querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"

View File

@ -1,10 +1,10 @@
package agentkindboxreqhandler package agentkindboxreqhandler
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"net/http" "net/http"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"git.gocasts.ir/ebhomengo/niki/pkg/claim" "git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"

View File

@ -1,9 +1,9 @@
package agentkindboxreqhandler package agentkindboxreqhandler
import ( import (
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"net/http" "net/http"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"git.gocasts.ir/ebhomengo/niki/pkg/claim" "git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"

View File

@ -1,10 +1,10 @@
package benefactorhandler package benefactorhandler
import ( import (
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"net/http" "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" "github.com/labstack/echo/v4"
) )

View File

@ -6,6 +6,7 @@ import (
"git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminagenthandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/agent" 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" adminKindBoxHandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box"
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req" adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
adminrefertimehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/refer_time" adminrefertimehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/refer_time"
@ -30,6 +31,7 @@ type Server struct {
adminKindBoxReqHandler adminkindboxreqhandler.Handler adminKindBoxReqHandler adminkindboxreqhandler.Handler
adminKindBoxHandler adminKindBoxHandler.Handler adminKindBoxHandler adminKindBoxHandler.Handler
adminAgentHandler adminagenthandler.Handler adminAgentHandler adminagenthandler.Handler
adminBenefactorHandler adminbenefactorhandler.Handler
adminReferTimeHandler adminrefertimehandler.Handler adminReferTimeHandler adminrefertimehandler.Handler
agentKindBoxHandler agentkindboxhandler.Handler agentKindBoxHandler agentkindboxhandler.Handler
agentKindBoxReqHandler agentkindboxreqhandler.Handler agentKindBoxReqHandler agentkindboxreqhandler.Handler
@ -51,6 +53,7 @@ func New(
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc), 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), adminReferTimeHandler: adminrefertimehandler.New(svc.AdminAuthSvc, svc.AdminReferTimeSvc, svc.AdminAuthorizeSvc),
agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), agentKindBoxHandler: agentkindboxhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
agentKindBoxReqHandler: agentkindboxreqhandler.New(svc.AdminAuthSvc, svc.AgentKindBoxReqSvc, 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.adminKindBoxReqHandler.SetRoutes(s.Router)
s.adminKindBoxHandler.SetRoutes(s.Router) s.adminKindBoxHandler.SetRoutes(s.Router)
s.adminReferTimeHandler.SetRoutes(s.Router) s.adminReferTimeHandler.SetRoutes(s.Router)
s.adminBenefactorHandler.SetRoutes(s.Router)
s.agentKindBoxHandler.SetRoutes(s.Router) s.agentKindBoxHandler.SetRoutes(s.Router)
s.agentKindBoxReqHandler.SetRoutes(s.Router) s.agentKindBoxReqHandler.SetRoutes(s.Router)
} }

View File

@ -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": { "/admins/kindboxes": {
"get": { "get": {
"security": [ "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": { "adminkindboxparam.AssignReceiverRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -4026,6 +4214,17 @@ const docTemplate = `{
"AdminInactiveStatus" "AdminInactiveStatus"
] ]
}, },
"entity.BenefactorStatus": {
"type": "string",
"enum": [
"active",
"inactive"
],
"x-enum-varnames": [
"BenefactorActiveStatus",
"BenefactorInactiveStatus"
]
},
"entity.City": { "entity.City": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -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": { "/admins/kindboxes": {
"get": { "get": {
"security": [ "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": { "adminkindboxparam.AssignReceiverRequest": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -4015,6 +4203,17 @@
"AdminInactiveStatus" "AdminInactiveStatus"
] ]
}, },
"entity.BenefactorStatus": {
"type": "string",
"enum": [
"active",
"inactive"
],
"x-enum-varnames": [
"BenefactorActiveStatus",
"BenefactorInactiveStatus"
]
},
"entity.City": { "entity.City": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -123,6 +123,40 @@ definitions:
$ref: '#/definitions/adminagentparam.Data' $ref: '#/definitions/adminagentparam.Data'
type: array type: array
type: object 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: adminkindboxparam.AssignReceiverRequest:
properties: properties:
receiver_agent_id: receiver_agent_id:
@ -886,6 +920,14 @@ definitions:
x-enum-varnames: x-enum-varnames:
- AdminActiveStatus - AdminActiveStatus
- AdminInactiveStatus - AdminInactiveStatus
entity.BenefactorStatus:
enum:
- active
- inactive
type: string
x-enum-varnames:
- BenefactorActiveStatus
- BenefactorInactiveStatus
entity.City: entity.City:
properties: properties:
id: id:
@ -1015,6 +1057,97 @@ paths:
summary: Get all agents by admin summary: Get all agents by admin
tags: tags:
- Admins - 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: /admins/kindboxes:
get: get:
consumes: consumes:

View File

@ -1,8 +1,9 @@
package adminbenefactoreparam package adminbenefactoreparam
import ( import (
"git.gocasts.ir/ebhomengo/niki/entity"
"time" "time"
"git.gocasts.ir/ebhomengo/niki/entity"
) )
type Data struct { type Data struct {
@ -14,6 +15,5 @@ type Data struct {
Email string `json:"email"` Email string `json:"email"`
Gender entity.Gender `json:"gender"` Gender entity.Gender `json:"gender"`
BirthDate time.Time `json:"birth_date"` BirthDate time.Time `json:"birth_date"`
Roll entity.UserRole `json:"roll"`
Status entity.BenefactorStatus `json:"status"` Status entity.BenefactorStatus `json:"status"`
} }

View File

@ -4,12 +4,12 @@ import (
"git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
) )
type GetAllBenefactorRequest struct { type BenefactorGetAllRequest struct {
Pagination param.PaginationRequest Pagination param.PaginationRequest
Sort param.SortRequest Sort param.SortRequest
Filter param.FilterRequest Filter param.FilterRequest
} }
type GetAllBenefactorResponse struct { type BenefactorGetAllResponse struct {
Data []Data `json:"data"` Data []Data `json:"data"`
Pagination param.PaginationResponse `json:"pagination"` Pagination param.PaginationResponse `json:"pagination"`
FieldErrors map[string]string `json:"field_errors,omitempty"` FieldErrors map[string]string `json:"field_errors,omitempty"`

View File

@ -4,15 +4,15 @@ import (
"time" "time"
) )
//when kindbox creates, which fields are // when kindbox creates, which fields are
type KindBoxUpdateRequest struct { type KindBoxUpdateRequest struct {
KindBoxID uint `json:"-" param:"id" example:"1"` // ID is passed in the URL path KindBoxID uint `json:"-" param:"id" example:"1"` // ID is passed in the URL path
BenefactorID uint `json:"benefactor_id" example:"1"` BenefactorID uint `json:"benefactor_id" example:"1"`
ReturnReferTimeID uint `json:"return_refere_time_id" example:"3"` ReturnReferTimeID uint `json:"return_refere_time_id" example:"3"`
ReturnReferDate time.Time `json:"return_refer_date" example:"2025-01-02T15:04:05Z"` ReturnReferDate time.Time `json:"return_refer_date" example:"2025-01-02T15:04:05Z"`
ReturnAddressID uint `json:"return_address_id" example:"1"` ReturnAddressID uint `json:"return_address_id" example:"1"`
ReceiverAgentID uint `json:"receiver_agent_id" example:"23"` ReceiverAgentID uint `json:"receiver_agent_id" example:"23"`
Amount uint `json:"amount" example:"3"` Amount uint `json:"amount" example:"3"`
} }
type KindBoxUpdateResponse struct { type KindBoxUpdateResponse struct {

View File

@ -1,8 +1,9 @@
package adminkindboxreqparam package adminkindboxreqparam
import ( import (
entity "git.gocasts.ir/ebhomengo/niki/entity"
"time" "time"
entity "git.gocasts.ir/ebhomengo/niki/entity"
) )
type KindBoxReqAddRequest struct { type KindBoxReqAddRequest struct {

View File

@ -35,7 +35,7 @@ const (
ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc"
ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgSortFieldIsNotValid = "sort field is not valid"
ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" 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" ErrorMsgInvalidSerialNumberRange = "invalid serial number range"
ErrorMsgInvalidOrExpiredJwt = "invalid or expired jwt" ErrorMsgInvalidOrExpiredJwt = "invalid or expired jwt"
ErrorMsgInvalidRefreshToken = "invalid refresh token" ErrorMsgInvalidRefreshToken = "invalid refresh token"

View File

@ -3,6 +3,7 @@ package mysqlquerybuilder
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"strings"
) )
var datePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}Z)?$`) 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) 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 { for key, value := range filter {
if strVal, ok := value.(string); ok && isValidDateOrDateTime(strVal) { 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) args = append(args, strVal)
} else { } else {
filterQuery += fmt.Sprintf("AND %s = ? ", key) conditions = append(conditions, fmt.Sprintf("%s = ?", key))
args = append(args, value) 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
} }

View File

@ -7,17 +7,15 @@ import (
) )
func BuildGetAllQuery(baseQuery string, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) (query string, args []any) { 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) paginationQuery, pArgs := BuildPaginationQuery(pagination)
sortQuery := BuildSortQuery(sort) sortQuery := BuildSortQuery(sort)
args = append(args, fArgs...) args = append(args, fArgs...)
args = append(args, pArgs...) args = append(args, pArgs...)
query = baseQuery query = filterQuery
if filterQuery != "" {
query = fmt.Sprintf("%s %s", query, filterQuery)
}
if sortQuery != "" { if sortQuery != "" {
query = fmt.Sprintf("%s %s", query, sortQuery) query = fmt.Sprintf("%s %s", query, sortQuery)
} }

View File

@ -3,6 +3,7 @@ package mysqlbenefactor
import ( import (
"context" "context"
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package mysqlbenefactor
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param" params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" 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 var total uint
baseQuery = `SELECT COUNT(*) FROM benefactors` 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) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil { if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -2,9 +2,10 @@ package mysqlbenefactor
import ( import (
"database/sql" "database/sql"
"time"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/repository/mysql" "git.gocasts.ir/ebhomengo/niki/repository/mysql"
"time"
) )
func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) { func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) {

View File

@ -5,7 +5,6 @@ import (
"database/sql" "database/sql"
"time" "time"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -15,76 +14,76 @@ import (
func (d *DB) UpdateKindBox(ctx context.Context, kindBox entity.KindBox) error { func (d *DB) UpdateKindBox(ctx context.Context, kindBox entity.KindBox) error {
const op = "mysqlkindbox.UpdateKindBox" const op = "mysqlkindbox.UpdateKindBox"
// Handle nullable fields // Handle nullable fields
var ( var (
returnReferTimeID sql.NullInt64 returnReferTimeID sql.NullInt64
returnReferDate sql.NullTime returnReferDate sql.NullTime
returnAddressID sql.NullInt64 returnAddressID sql.NullInt64
receiverAgentID sql.NullInt64 receiverAgentID sql.NullInt64
returnedAt sql.NullTime 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}
}
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 = ?, 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 = ? sender_agent_id = ?, delivered_at = ?, return_refer_time_id = ?, return_refer_date = ?, return_address_id = ?, receiver_agent_id = ?, returned_at = ?
WHERE WHERE
id = ? AND deleted_at IS NULL` id = ? AND deleted_at IS NULL`
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxUpdate, query) stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxUpdate, query)
if err != nil { if err != nil {
return richerror.New(op).WithErr(err). return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
} }
_, uErr := stmt.ExecContext(ctx, _, uErr := stmt.ExecContext(ctx,
kindBox.BenefactorID, kindBox.BenefactorID,
kindBox.KindBoxType, kindBox.KindBoxType,
kindBox.Amount, kindBox.Amount,
kindBox.SerialNumber, kindBox.SerialNumber,
kindBox.Status, kindBox.Status,
kindBox.DeliverReferTimeID, kindBox.DeliverReferTimeID,
kindBox.DeliverReferDate, kindBox.DeliverReferDate,
kindBox.DeliverAddressID, kindBox.DeliverAddressID,
kindBox.SenderAgentID, kindBox.SenderAgentID,
kindBox.DeliveredAt, kindBox.DeliveredAt,
returnReferTimeID, returnReferTimeID,
returnReferDate, returnReferDate,
returnAddressID, returnAddressID,
receiverAgentID, receiverAgentID,
returnedAt, returnedAt,
kindBox.ID, kindBox.ID,
) )
if uErr != nil { if uErr != nil {
return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord). return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
WithKind(richerror.KindUnexpected) WithKind(richerror.KindUnexpected)

View File

@ -2,6 +2,7 @@ package mysqlkindboxreq
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package adminservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"

View File

@ -3,10 +3,10 @@ package adminservice
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/service/auth"
"git.gocasts.ir/ebhomengo/niki/config" "git.gocasts.ir/ebhomengo/niki/config"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/service/auth"
validator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" validator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )

View File

@ -2,23 +2,29 @@ package adminbenefactorservice
import ( import (
"context" "context"
paginationparam "git.gocasts.ir/ebhomengo/niki/param" paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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" 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 { 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 { for _, benefactor := range benefactors {
benefactorInfo = append(benefactorInfo, param.Data{ resp = append(resp, param.Data{
ID: benefactor.ID, ID: benefactor.ID,
FirstName: benefactor.FirstName, FirstName: benefactor.FirstName,
LastName: benefactor.LastName, LastName: benefactor.LastName,
@ -27,14 +33,16 @@ func (s Service) GetAllBenefactor(ctx context.Context, req param.GetAllBenefacto
Email: benefactor.Email, Email: benefactor.Email,
Gender: benefactor.Gender, Gender: benefactor.Gender,
BirthDate: benefactor.BirthDate, BirthDate: benefactor.BirthDate,
Roll: benefactor.Role,
Status: benefactor.Status, Status: benefactor.Status,
}) })
} }
return param.GetAllBenefactorResponse{Data: benefactorInfo, Pagination: paginationparam.PaginationResponse{ return param.BenefactorGetAllResponse{
PageSize: req.Pagination.PageSize, Data: resp,
PageNumber: req.Pagination.PageNumber, Pagination: paginationparam.PaginationResponse{
Total: total, PageSize: req.Pagination.PageSize,
}}, nil PageNumber: req.Pagination.PageNumber,
Total: total,
},
}, nil
} }

View File

@ -2,29 +2,28 @@ package adminbenefactorservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/param"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/param"
adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" 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 { type Repository interface {
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
GetByID(ctx context.Context, id uint) (entity.Benefactor, 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 { type AddressSvc interface {
GetAddressByID(ctx context.Context, request adminaddressparam.AddressGetRequest) (adminaddressparam.AddressGetResponse, error) GetAddressByID(ctx context.Context, request adminaddressparam.AddressGetRequest) (adminaddressparam.AddressGetResponse, error)
} }
type Service struct { type Service struct {
repo Repository repo Repository
addressSvc AddressSvc addressSvc AddressSvc
benefactorSvc BenefactorSvc vld validator.Validator
} }
func New(repo Repository, addressSvc AddressSvc, benefactorSvc BenefactorSvc) Service { func New(repo Repository, addressSvc AddressSvc, vld validator.Validator) Service {
return Service{repo: repo, addressSvc: addressSvc, benefactorSvc: benefactorSvc} return Service{repo: repo, addressSvc: addressSvc, vld: vld}
} }

View File

@ -2,6 +2,7 @@ package adminkindboxreqservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package agentkindboxservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity" entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,10 +2,10 @@ package agentkindboxreqservice
import ( import (
"context" "context"
kindboxParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"time" "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" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )

View File

@ -2,6 +2,7 @@ package agentkindboxreqservice
import ( import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )

View File

@ -2,10 +2,10 @@ package agentkindboxreqservice
import ( import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param" 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" validator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box_req"
) )

View File

@ -1,9 +1,10 @@
package auth package auth
import ( import (
"time"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"time"
) )
func (s Service) CreateAccessToken(user entity.Authenticable) (string, error) { func (s Service) CreateAccessToken(user entity.Authenticable) (string, error) {

View File

@ -2,9 +2,10 @@ package auth
import ( import (
"fmt" "fmt"
"strings"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"strings"
) )
func (s Service) ParseBearerToken(bearerToken string) (*Claims, error) { func (s Service) ParseBearerToken(bearerToken string) (*Claims, error) {

View File

@ -2,6 +2,7 @@ package benefactorservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" benefactorparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"

View File

@ -2,11 +2,11 @@ package benefactorservice
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/service/auth"
"time" "time"
smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms" smscontract "git.gocasts.ir/ebhomengo/niki/contract/sms"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/service/auth"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
) )

View File

@ -3,6 +3,7 @@ package notification
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
kbp "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" kbp "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"

View File

@ -3,6 +3,7 @@ package notification
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" bnfparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" kbparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"

View File

@ -31,6 +31,7 @@ import (
benefactorrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/refer_time" benefactorrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/refer_time"
"git.gocasts.ir/ebhomengo/niki/service/notification" "git.gocasts.ir/ebhomengo/niki/service/notification"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" 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" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box" agentkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/agent/kind_box"
@ -48,6 +49,7 @@ type Service struct {
AdminAuthorizeSvc adminauthorizationservice.Service AdminAuthorizeSvc adminauthorizationservice.Service
AdminSvc adminservice.Service AdminSvc adminservice.Service
AdminAgentSvc adminagentservice.Service AdminAgentSvc adminagentservice.Service
AdminBenefactorSvc adminbenefactorservice.Service
AgentKindBoxSvc agentkindboxservice.Service AgentKindBoxSvc agentkindboxservice.Service
AgentKindBoxReqSvc agentkindboxreqservice.Service AgentKindBoxReqSvc agentkindboxreqservice.Service
BenefactorAuthSvc auth.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) AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo)
AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo) AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo)
AdminAddressSvc = adminaddressservice.New(addressRepo) AdminAddressSvc = adminaddressservice.New(addressRepo)
AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc) AdminBenefactorVld = adminbenefactorvalidator.New()
AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld)
AdminAgentSvc = adminagentservice.New(agentRepo) AdminAgentSvc = adminagentservice.New(agentRepo)
AdminVld = adminvalidator.New(adminRepo) AdminVld = adminvalidator.New(adminRepo)
@ -116,6 +119,7 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
AdminAuthorizeSvc: AdminAuthorizeSvc, AdminAuthorizeSvc: AdminAuthorizeSvc,
AdminSvc: AdminSvc, AdminSvc: AdminSvc,
AdminAgentSvc: AdminAgentSvc, AdminAgentSvc: AdminAgentSvc,
AdminBenefactorSvc: AdminBenefactorSvc,
AgentKindBoxSvc: AgentKindBoxSvc, AgentKindBoxSvc: AgentKindBoxSvc,
AgentKindBoxReqSvc: AgentKindBoxReqSvc, AgentKindBoxReqSvc: AgentKindBoxReqSvc,
BenefactorAuthSvc: BenefactorAuthSvc, BenefactorAuthSvc: BenefactorAuthSvc,

View File

@ -3,9 +3,9 @@ package adminvalidator
import ( import (
"context" "context"
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/entity"
"testing" "testing"
"git.gocasts.ir/ebhomengo/niki/entity"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"

View File

@ -3,6 +3,7 @@ package adminvalidator
import ( import (
"context" "context"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"

View File

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

View File

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

View File

@ -19,7 +19,6 @@ func (v Validator) ValidateEnumerate(ctx context.Context, req param.EnumerateKin
validation.By(v.CheckKindBoxStatusForEnumeration(ctx))), validation.By(v.CheckKindBoxStatusForEnumeration(ctx))),
validation.Field(&req.Amount, validation.Required), validation.Field(&req.Amount, validation.Required),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -12,7 +12,7 @@ import (
func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxUpdateRequest) (map[string]string, error) { func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxUpdateRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateUpdateRequest" const op = "adminkindboxvalidator.ValidateUpdateRequest"
err := validation.ValidateStruct(&req, err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
@ -22,7 +22,6 @@ func (v Validator) ValidateUpdateRequest(ctx context.Context, req param.KindBoxU
validation.By(v.doesBenefactorExist(ctx)), validation.By(v.doesBenefactorExist(ctx)),
validation.By(v.doesBenefactorHaveKindBox(ctx, req.KindBoxID, req.BenefactorID))), validation.By(v.doesBenefactorHaveKindBox(ctx, req.KindBoxID, req.BenefactorID))),
) )
if err != nil { if err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)
v.processValidationErrors(err, fieldErrors) v.processValidationErrors(err, fieldErrors)

View File

@ -1,453 +1,428 @@
package adminkindboxvalidator package adminkindboxvalidator
import ( import (
"context" "context"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/entity" addressserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
adminupdateparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" agentserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent"
benefactorserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" benefactorserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
agentserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" adminupdateparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
addressserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" refertimeserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
refertimeserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time" "github.com/stretchr/testify/assert"
) )
func TestValidator_ValidateUpdateRequest(t *testing.T) { func TestValidator_ValidateUpdateRequest(t *testing.T) {
now := time.Now()
twoDaysLater := now.Add(48 * time.Hour)
threeDaysLater := now.Add(52 * time.Hour)
now := time.Now() mockRepository := NewMockRepository(t)
twoDaysLater := now.Add(48 * time.Hour) mockAgentSvc := NewMockAgentSvc(t)
threeDaysLater := now.Add(52 * time.Hour) mockBenefactorSvc := NewMockBenefactorSvc(t)
mockReferTimeSvc := NewMockReferTimeSvc(t)
mockAddressSvc := NewMockAddressSvc(t)
mockRepository := NewMockRepository(t) validator := New(mockRepository, mockAgentSvc, mockBenefactorSvc, mockReferTimeSvc, mockAddressSvc)
mockAgentSvc := NewMockAgentSvc(t) ctx := context.Background()
mockBenefactorSvc := NewMockBenefactorSvc(t)
mockReferTimeSvc := NewMockReferTimeSvc(t)
mockAddressSvc := NewMockAddressSvc(t)
validator := New(mockRepository, mockAgentSvc, mockBenefactorSvc, mockReferTimeSvc, mockAddressSvc) t.Run("KindBox Delivered And Benefactor Does Not Exist", func(t *testing.T) {
ctx := context.Background() 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) { mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
req := adminupdateparam.KindBoxUpdateRequest{ Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: false}, nil).Once()
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()
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
assert.Error(t, err) fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "benefactor_id")
}) assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "benefactor_id")
})
t.Run("KindBox Does Not Exist", func(t *testing.T) { t.Run("KindBox Does Not Exist", func(t *testing.T) {
req := adminupdateparam.KindBoxUpdateRequest{ req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1, KindBoxID: 1,
BenefactorID: 1, BenefactorID: 1,
} }
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(false, nil).Once()
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(false, nil).Once()
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
Return(entity.KindBox{}, nil).Once() Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "KindBoxID")
})
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) { fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
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")
})
t.Run("KindBoxID is Null", func(t *testing.T) { assert.Error(t, err)
req := adminupdateparam.KindBoxUpdateRequest{ assert.NotNil(t, fieldErrors)
KindBoxID: 0, assert.Contains(t, fieldErrors, "KindBoxID")
BenefactorID: 1, })
}
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). t.Run("KindBox Delivered And Does Not Belong to Benefactor", func(t *testing.T) {
Return(entity.KindBox{}, nil).Once() req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1,
BenefactorID: 1,
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) }
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "KindBoxID")
})
t.Run("BenefactorID is Null", func(t *testing.T) { mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once()
req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1,
BenefactorID: 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). mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 1}, nil).Once() // Note the different BenefactorID Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 2}, nil).Once()
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "benefactor_id")
})
assert.Error(t, err)
assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "benefactor_id")
})
t.Run("Valid KindBox Delivered Request", func(t *testing.T) { t.Run("KindBoxID is Null", func(t *testing.T) {
req := adminupdateparam.KindBoxUpdateRequest{ req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1, KindBoxID: 0,
BenefactorID: 1, BenefactorID: 1,
} }
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
Return(true, nil).Once() Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() Return(entity.KindBox{}, nil).Once()
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
assert.Error(t, err) assert.Error(t, err)
assert.NotNil(t, fieldErrors) 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) { t.Run("BenefactorID is Null", func(t *testing.T) {
req := adminupdateparam.KindBoxUpdateRequest{ req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1, KindBoxID: 1,
BenefactorID: 1, BenefactorID: 0,
ReturnReferDate: twoDaysLater, }
ReturnReferTimeID: 1,
ReturnAddressID: 23,
}
validReferTime := entity.ReferTime{ mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once()
ID: 1,
Duration: "30m",
Status: entity.ReferTimeActiveStatus,
}
addressExm := entity.Address{ mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
ID: 23, Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus, BenefactorID: 1}, nil).Once() // Note the different BenefactorID
PostalCode: "1231231231",
Address : "Tehran",
Name : "Home",
CityID : 1,
ProvinceID: 1,
BenefactorID: 2,
}
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
Return(true, nil).Once()
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). assert.Error(t, err)
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "benefactor_id")
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()
mockRepository.EXPECT().GetKindBox(ctx, uint(req.KindBoxID)). t.Run("Valid KindBox Delivered Request", func(t *testing.T) {
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1,
BenefactorID: 1,
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) }
assert.Error(t, err) mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).
assert.NotNil(t, fieldErrors) Return(true, nil).Once()
assert.Contains(t, fieldErrors, "return_address_id")
})
t.Run("KindBox Ready to Return Status and ReturnReferTimeID Invalid", func(t *testing.T) { mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
req := adminupdateparam.KindBoxUpdateRequest{ Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
KindBoxID: 1,
BenefactorID: 1,
ReturnReferDate: twoDaysLater,
ReturnReferTimeID: 14,
ReturnAddressID: 1,
}
validAddress := entity.Address{ mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
ID: 1, Return(entity.KindBox{Status: entity.KindBoxDeliveredStatus}, nil).Once()
PostalCode: "1234567890",
Address: "tehran",
Name: "home",
CityID: 1,
ProvinceID: 1,
BenefactorID: 1,
}
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
Return(true, nil).Once()
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). assert.Error(t, err)
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() assert.NotNil(t, fieldErrors)
})
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). t.Run("KindBox Ready to Return Status and Address Does Not Belong", func(t *testing.T) {
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() req := adminupdateparam.KindBoxUpdateRequest{
KindBoxID: 1,
mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}). BenefactorID: 1,
Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once() ReturnReferDate: twoDaysLater,
ReturnReferTimeID: 1,
mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}). ReturnAddressID: 23,
Return(refertimeserviceparam.GetReferTimeResponse{}, nil).Once() }
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). validReferTime := entity.ReferTime{
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() ID: 1,
Duration: "30m",
Status: entity.ReferTimeActiveStatus,
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) }
assert.Error(t, err) addressExm := entity.Address{
assert.NotNil(t, fieldErrors) ID: 23,
assert.Contains(t, fieldErrors, "return_refere_time_id") 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) { mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
req := adminupdateparam.KindBoxUpdateRequest{ Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once()
KindBoxID: 1,
BenefactorID: 1,
ReturnReferDate: twoDaysLater,
ReturnReferTimeID: 1,
ReturnAddressID: 1,
}
validAddress := entity.Address{ mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
ID: 1, Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
PostalCode: "1234567890",
Address: "tehran",
Name: "home",
CityID: 1,
ProvinceID: 1,
BenefactorID: 1,
}
validReferTime := entity.ReferTime{ mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}).
ID: 1, Return(addressserviceparam.AddressGetResponse{Address: addressExm}, nil).Once()
Duration: "30m",
Status: entity.ReferTimeActiveStatus,
}
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID). mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: 1}).
Return(true, nil).Once() Return(refertimeserviceparam.GetReferTimeResponse{ReferTime: validReferTime}, nil).Once()
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). mockRepository.EXPECT().GetKindBox(ctx, uint(req.KindBoxID)).
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once()
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID). fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
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)). assert.Error(t, err)
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once() assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "return_address_id")
})
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req)
assert.NoError(t, err) t.Run("KindBox Ready to Return Status and ReturnReferTimeID Invalid", func(t *testing.T) {
assert.Nil(t, fieldErrors) 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) { validAddress := entity.Address{
req := adminupdateparam.KindBoxUpdateRequest{ ID: 1,
KindBoxID: 1, PostalCode: "1234567890",
BenefactorID: 1, Address: "tehran",
ReturnReferDate: threeDaysLater, Name: "home",
} CityID: 1,
ProvinceID: 1,
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() BenefactorID: 1,
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) { mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).
req := adminupdateparam.KindBoxUpdateRequest{ Return(true, nil).Once()
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) { mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
req := adminupdateparam.KindBoxUpdateRequest{ Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once()
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}). mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, 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) { mockAddressSvc.EXPECT().GetAddressByID(ctx, addressserviceparam.AddressGetRequest{AddressID: req.ReturnAddressID}).
req := adminupdateparam.KindBoxUpdateRequest{ Return(addressserviceparam.AddressGetResponse{Address: validAddress}, nil).Once()
KindBoxID: 1,
BenefactorID: 1,
}
mockRepository.EXPECT().KindBoxExist(ctx, req.KindBoxID).Return(true, nil).Once() mockReferTimeSvc.EXPECT().GetReferTimeByID(ctx, refertimeserviceparam.GetReferTimeRequest{ReferTimeID: req.ReturnReferTimeID}).
mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}). Return(refertimeserviceparam.GetReferTimeResponse{}, nil).Once()
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) { mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
req := adminupdateparam.KindBoxUpdateRequest{ Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once()
KindBoxID: 1,
BenefactorID: 1,
Amount: 0,
}
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}). assert.Error(t, err)
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() assert.NotNil(t, fieldErrors)
assert.Contains(t, fieldErrors, "return_refere_time_id")
})
mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(entity.KindBox{
Status: entity.KindBoxEnumeratedStatus,
BenefactorID: req.BenefactorID,
}, nil).Twice()
fieldErrors, err := validator.ValidateUpdateRequest(ctx, req) t.Run("Valid KindBox Ready to Return Status Request", func(t *testing.T) {
req := adminupdateparam.KindBoxUpdateRequest{
assert.Error(t, err) KindBoxID: 1,
assert.NotNil(t, fieldErrors) BenefactorID: 1,
assert.Contains(t, fieldErrors, "amount" ) 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) { validReferTime := entity.ReferTime{
req := adminupdateparam.KindBoxUpdateRequest{ ID: 1,
KindBoxID: 1, Duration: "30m",
BenefactorID: 1, Status: entity.ReferTimeActiveStatus,
Amount: 100, }
}
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}). mockBenefactorSvc.EXPECT().BenefactorExistByID(ctx, benefactorserviceparam.BenefactorExistByIDRequest{ID: req.BenefactorID}).
Return(benefactorserviceparam.BenefactorExistByIDResponse{Existed: true}, nil).Once() 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) mockRepository.EXPECT().GetKindBox(ctx, req.KindBoxID).
Return(entity.KindBox{Status: entity.KindBoxReadyToReturnStatus, BenefactorID: req.BenefactorID}, nil).Once()
assert.NoError(t, err)
assert.Nil(t, fieldErrors) 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)
})
} }

View File

@ -4,11 +4,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
params "git.gocasts.ir/ebhomengo/niki/param"
"slices" "slices"
"time" "time"
entity "git.gocasts.ir/ebhomengo/niki/entity" entity "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address" adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent"
param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" 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) fetchedKindBox, err := v.repo.GetKindBox(ctx, kindBoxID)
if err != nil { if err != nil {
return richerror.New(op).WithErr(err).WithMessage("could not fetch KindBox").WithKind(richerror.KindUnexpected) 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 return nil
} }
} }
func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxID uint, benefactorID uint, addressID uint) validation.RuleFunc { func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxID uint, benefactorID uint, addressID uint) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
const op = "Validator.doesBenefactorAddressExist" const op = "Validator.doesBenefactorAddressExist"

View File

@ -3,9 +3,9 @@ package agentkindboxvalidator
import ( import (
"context" "context"
"fmt" "fmt"
params "git.gocasts.ir/ebhomengo/niki/param"
"slices" "slices"
params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
@ -39,6 +39,7 @@ func (v Validator) doesKindBoxExistForAgent(ctx context.Context, agentID uint) v
return nil return nil
} }
} }
func (v Validator) AreSortFieldsValid(validSortFields []string) validation.RuleFunc { func (v Validator) AreSortFieldsValid(validSortFields []string) validation.RuleFunc {
return func(value interface{}) error { return func(value interface{}) error {
sort, ok := value.(params.SortRequest) sort, ok := value.(params.SortRequest)

View File

@ -3,6 +3,7 @@ package agentkindboxreqvalidator
import ( import (
"context" "context"
"errors" "errors"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package agentkindboxreqvalidator
import ( import (
"errors" "errors"
kbrparam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" kbrparam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package agentkindboxreqvalidator
import ( import (
"errors" "errors"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -4,10 +4,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
"slices" "slices"
"git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )

View File

@ -2,12 +2,13 @@ package benefactorvalidator
import ( import (
"context" "context"
"testing"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" benefactoreparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestValidator_ValidateSendOtpRequest(t *testing.T) { func TestValidator_ValidateSendOtpRequest(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"