feat(niki): get all kindboxes by admin

This commit is contained in:
Erfan Mohammadi 2024-07-09 20:47:04 +03:30
parent ec769748fe
commit bf9f79a524
18 changed files with 962 additions and 23 deletions

View File

@ -0,0 +1,67 @@
package adminkindboxhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
"github.com/labstack/echo/v4"
)
// GetAll godoc
// @Summary Get all KindBoxes by admin
// @Description Retrieves a list of all KindBoxes with filtering, sorting, and pagination options
// @Tags KindBox
// @Accept json
// @Produce json
// @Param filter_id query int false "Filter by ID"
// @Param filter_kind_box_req_id query int false "Filter by KindBox request ID"
// @Param filter_benefactor_id query int false "Filter by benefactor ID"
// @Param filter_kind_box_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up)
// @Param filter_amount query int false "Filter by amount"
// @Param filter_serial_number query string false "Filter by serial number"
// @Param filter_status query string false "Filter by status" Enums(delivered,ready-to-return,assigned-receiver-agent,returned,enumerated)
// @Param filter_delivered_at query string false "Filter by delivered at" Format(date-time)
// @Param filter_return_refer_time_id query int false "Filter by return refer time ID"
// @Param filter_return_refer_date query string false "Filter by return refer date" Format(date)
// @Param filter_return_address_id query int false "Filter by return address ID"
// @Param filter_receiver_agent_id query int false "Filter by receiver agent ID"
// @Param filter_returned_at query string false "Filter by returned at" Format(date-time)
// @Param filter_sender_agent_id query int false "Filter by sender agent ID"
// @Param filter_deliver_refer_date query string false "Filter by deliver refer date" Format(date)
// @Param filter_deliver_address_id query int false "Filter by deliver address ID"
// @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,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id)
// @Param sort_direction query string false "Sort order" Enums(asc,desc)
// @Success 200 {object} param.KindBoxGetAllResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admin/kindboxes [get].
func (h Handler) GetAll(c echo.Context) error {
var req param.KindBoxGetAllRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
req.Filter = queryparam.GetFilterParams(c)
if fieldErrors, err := h.adminKindBoxVld.ValidateGetAll(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -13,4 +13,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission)) r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission))
r.PATCH("/assign-receiver-agent/:id", h.AssignReceiverAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxAssignReceiverAgentPermission)) r.PATCH("/assign-receiver-agent/:id", h.AssignReceiverAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxAssignReceiverAgentPermission))
r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAllPermission))
} }

View File

@ -267,6 +267,200 @@ const docTemplate = `{
} }
} }
}, },
"/admin/kindboxes": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get all KindBoxes by admin",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by KindBox request ID",
"name": "filter_kind_box_req_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
"on-table",
"cylindrical",
"stand-up"
],
"type": "string",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by amount",
"name": "filter_amount",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"enum": [
"delivered",
"ready-to-return",
"assigned-receiver-agent",
"returned",
"enumerated"
],
"type": "string",
"description": "Filter by status",
"name": "filter_status",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by delivered at",
"name": "filter_delivered_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by receiver agent ID",
"name": "filter_receiver_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by returned at",
"name": "filter_returned_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by sender agent ID",
"name": "filter_sender_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by deliver refer date",
"name": "filter_deliver_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver address ID",
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"kind_box_req_id",
"benefactor_id",
"kind_box_type",
"amount",
"serial_number",
"status",
"delivered_at",
"return_refer_time_id",
"return_refer_date",
"return_address_id",
"receiver_agent_id",
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id"
],
"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/adminkindboxparam.KindBoxGetAllResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxes/assign-receiver-agent/{id}": { "/admin/kindboxes/assign-receiver-agent/{id}": {
"patch": { "patch": {
"security": [ "security": [
@ -1520,6 +1714,20 @@ const docTemplate = `{
} }
} }
}, },
"adminkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {
"all_kind_box": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"adminkindboxparam.KindBoxGetResponse": { "adminkindboxparam.KindBoxGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -2289,6 +2497,59 @@ const docTemplate = `{
"FemaleGender" "FemaleGender"
] ]
}, },
"entity.KindBox": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
},
"benefactorID": {
"type": "integer"
},
"deliverAddressID": {
"type": "integer"
},
"deliverReferDate": {
"type": "string"
},
"deliveredAt": {
"type": "string"
},
"id": {
"type": "integer"
},
"kindBoxReqID": {
"type": "integer"
},
"kindBoxType": {
"$ref": "#/definitions/entity.KindBoxType"
},
"receiverAgentID": {
"type": "integer"
},
"returnAddressID": {
"type": "integer"
},
"returnReferDate": {
"type": "string"
},
"returnReferTimeID": {
"type": "integer"
},
"returnedAt": {
"type": "string"
},
"senderAgentID": {
"type": "integer"
},
"serialNumber": {
"type": "string"
},
"status": {
"$ref": "#/definitions/entity.KindBoxStatus"
}
}
},
"entity.KindBoxReq": { "entity.KindBoxReq": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -256,6 +256,200 @@
} }
} }
}, },
"/admin/kindboxes": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBox"
],
"summary": "Get all KindBoxes by admin",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by KindBox request ID",
"name": "filter_kind_box_req_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
"on-table",
"cylindrical",
"stand-up"
],
"type": "string",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by amount",
"name": "filter_amount",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"enum": [
"delivered",
"ready-to-return",
"assigned-receiver-agent",
"returned",
"enumerated"
],
"type": "string",
"description": "Filter by status",
"name": "filter_status",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by delivered at",
"name": "filter_delivered_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by receiver agent ID",
"name": "filter_receiver_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by returned at",
"name": "filter_returned_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by sender agent ID",
"name": "filter_sender_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by deliver refer date",
"name": "filter_deliver_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver address ID",
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"kind_box_req_id",
"benefactor_id",
"kind_box_type",
"amount",
"serial_number",
"status",
"delivered_at",
"return_refer_time_id",
"return_refer_date",
"return_address_id",
"receiver_agent_id",
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id"
],
"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/adminkindboxparam.KindBoxGetAllResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxes/assign-receiver-agent/{id}": { "/admin/kindboxes/assign-receiver-agent/{id}": {
"patch": { "patch": {
"security": [ "security": [
@ -1509,6 +1703,20 @@
} }
} }
}, },
"adminkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {
"all_kind_box": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"adminkindboxparam.KindBoxGetResponse": { "adminkindboxparam.KindBoxGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -2278,6 +2486,59 @@
"FemaleGender" "FemaleGender"
] ]
}, },
"entity.KindBox": {
"type": "object",
"properties": {
"amount": {
"type": "integer"
},
"benefactorID": {
"type": "integer"
},
"deliverAddressID": {
"type": "integer"
},
"deliverReferDate": {
"type": "string"
},
"deliveredAt": {
"type": "string"
},
"id": {
"type": "integer"
},
"kindBoxReqID": {
"type": "integer"
},
"kindBoxType": {
"$ref": "#/definitions/entity.KindBoxType"
},
"receiverAgentID": {
"type": "integer"
},
"returnAddressID": {
"type": "integer"
},
"returnReferDate": {
"type": "string"
},
"returnReferTimeID": {
"type": "integer"
},
"returnedAt": {
"type": "string"
},
"senderAgentID": {
"type": "integer"
},
"serialNumber": {
"type": "string"
},
"status": {
"$ref": "#/definitions/entity.KindBoxStatus"
}
}
},
"entity.KindBoxReq": { "entity.KindBoxReq": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -77,6 +77,15 @@ definitions:
receiver_agent_id: receiver_agent_id:
type: integer type: integer
type: object type: object
adminkindboxparam.KindBoxGetAllResponse:
properties:
all_kind_box:
items:
$ref: '#/definitions/entity.KindBox'
type: array
pagination:
$ref: '#/definitions/param.PaginationResponse'
type: object
adminkindboxparam.KindBoxGetResponse: adminkindboxparam.KindBoxGetResponse:
properties: properties:
amount: amount:
@ -583,6 +592,41 @@ definitions:
x-enum-varnames: x-enum-varnames:
- MaleGender - MaleGender
- FemaleGender - FemaleGender
entity.KindBox:
properties:
amount:
type: integer
benefactorID:
type: integer
deliverAddressID:
type: integer
deliverReferDate:
type: string
deliveredAt:
type: string
id:
type: integer
kindBoxReqID:
type: integer
kindBoxType:
$ref: '#/definitions/entity.KindBoxType'
receiverAgentID:
type: integer
returnAddressID:
type: integer
returnReferDate:
type: string
returnReferTimeID:
type: integer
returnedAt:
type: string
senderAgentID:
type: integer
serialNumber:
type: string
status:
$ref: '#/definitions/entity.KindBoxStatus'
type: object
entity.KindBoxReq: entity.KindBoxReq:
properties: properties:
benefactorID: benefactorID:
@ -829,6 +873,143 @@ paths:
summary: Get all provinces summary: Get all provinces
tags: tags:
- Address - Address
/admin/kindboxes:
get:
consumes:
- application/json
description: Retrieves a list of all KindBoxes with filtering, sorting, and
pagination options
parameters:
- description: Filter by ID
in: query
name: filter_id
type: integer
- description: Filter by KindBox request ID
in: query
name: filter_kind_box_req_id
type: integer
- description: Filter by benefactor ID
in: query
name: filter_benefactor_id
type: integer
- description: Filter by KindBox type
enum:
- on-table
- cylindrical
- stand-up
in: query
name: filter_kind_box_type
type: string
- description: Filter by amount
in: query
name: filter_amount
type: integer
- description: Filter by serial number
in: query
name: filter_serial_number
type: string
- description: Filter by status
enum:
- delivered
- ready-to-return
- assigned-receiver-agent
- returned
- enumerated
in: query
name: filter_status
type: string
- description: Filter by delivered at
format: date-time
in: query
name: filter_delivered_at
type: string
- description: Filter by return refer time ID
in: query
name: filter_return_refer_time_id
type: integer
- description: Filter by return refer date
format: date
in: query
name: filter_return_refer_date
type: string
- description: Filter by return address ID
in: query
name: filter_return_address_id
type: integer
- description: Filter by receiver agent ID
in: query
name: filter_receiver_agent_id
type: integer
- description: Filter by returned at
format: date-time
in: query
name: filter_returned_at
type: string
- description: Filter by sender agent ID
in: query
name: filter_sender_agent_id
type: integer
- description: Filter by deliver refer date
format: date
in: query
name: filter_deliver_refer_date
type: string
- description: Filter by deliver address ID
in: query
name: filter_deliver_address_id
type: integer
- 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
- kind_box_req_id
- benefactor_id
- kind_box_type
- amount
- serial_number
- status
- delivered_at
- return_refer_time_id
- return_refer_date
- return_address_id
- receiver_agent_id
- returned_at
- sender_agent_id
- deliver_refer_date
- deliver_address_id
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/adminkindboxparam.KindBoxGetAllResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Get all KindBoxes by admin
tags:
- KindBox
/admin/kindboxes/{id}: /admin/kindboxes/{id}:
get: get:
consumes: consumes:

View File

@ -3,15 +3,16 @@ package entity
type AdminPermission string type AdminPermission string
const ( const (
AdminAdminRegisterPermission = AdminPermission("admin-register") AdminAdminRegisterPermission = AdminPermission("admin-register")
AdminKindBoxReqAcceptPermission = AdminPermission("kindboxreq-accept") AdminKindBoxReqAcceptPermission = AdminPermission("kindboxreq-accept")
AdminKindBoxReqAddPermission = AdminPermission("kindboxreq-add") AdminKindBoxReqAddPermission = AdminPermission("kindboxreq-add")
AdminKindBoxReqRejectPermission = AdminPermission("kindboxreq-reject") AdminKindBoxReqRejectPermission = AdminPermission("kindboxreq-reject")
AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall") AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall")
AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver")
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery") AdminKindBoxReqGetAwaitingDeliveryPermission = AdminPermission("kindboxreq-get_awaiting_delivery")
AdminKindBoxGetPermission = AdminPermission("kindbox-get") AdminKindBoxGetPermission = AdminPermission("kindbox-get")
AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent") AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent")
AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall")
) )

View File

@ -1,9 +1,17 @@
package adminkindboxparam package adminkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity" import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/param"
)
type KindBoxGetAllRequest struct{} type KindBoxGetAllRequest struct {
Pagination param.PaginationRequest
Sort param.SortRequest
Filter param.FilterRequest
}
type KindBoxGetAllResponse struct { type KindBoxGetAllResponse struct {
AllKindBox []entity.KindBox AllKindBox []entity.KindBox `json:"all_kind_box"`
Pagination param.PaginationResponse `json:"pagination"`
} }

View File

@ -27,8 +27,3 @@ func (d DB) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, err
return k, nil return k, nil
} }
func (d DB) GetAllKindBox(ctx context.Context, benefactorID uint) ([]entity.KindBox, error) {
// TODO implement me
panic("implement me")
}

View File

@ -0,0 +1,47 @@
package mysqlkindbox
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) {
const op = "mysqlkindbox.GetAllKindBox"
baseQuery := `SELECT * FROM kind_boxes WHERE deleted_at IS NULL`
query, args := builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...)
if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
kindBoxes := make([]entity.KindBox, 0)
for rows.Next() {
kindBox, sErr := scanKindBox(rows)
if sErr != nil {
return nil, 0, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
kindBoxes = append(kindBoxes, kindBox)
}
if rErr := rows.Err(); rErr != nil {
return nil, 0, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
var total uint
baseQuery = `SELECT COUNT(*) FROM kind_boxes WHERE deleted_at IS NULL`
query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
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)
}
return kindBoxes, total, nil
}

View File

@ -2,7 +2,6 @@ package mysqlkindboxreq
import ( import (
"context" "context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql" builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
@ -25,7 +24,6 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pa
for rows.Next() { for rows.Next() {
kindBoxReq, sErr := scanKindBoxReq(rows) kindBoxReq, sErr := scanKindBoxReq(rows)
if sErr != nil { if sErr != nil {
fmt.Println(sErr)
return nil, 0, richerror.New(op).WithErr(sErr). return nil, 0, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
} }

View File

@ -10,7 +10,9 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
'admin-getall_agent', 'admin-getall_agent',
'kindboxreq-get_awaiting_delivery', 'kindboxreq-get_awaiting_delivery',
'kindbox-get', 'kindbox-get',
'kindboxreq-add' 'kindboxreq-add',
'kindbox-assign_receiver_agent',
'kindbox-getall'
) NOT NULL; ) NOT NULL;
-- +migrate Down -- +migrate Down

View File

@ -19,7 +19,11 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`
(17, 1 , 'role','kindbox-get'), (17, 1 , 'role','kindbox-get'),
(18, 2 , 'role','kindbox-get'), (18, 2 , 'role','kindbox-get'),
(19, 1 , 'role','kindbox-assign_receiver_agent'), (19, 1 , 'role','kindbox-assign_receiver_agent'),
(20, 2 , 'role','kindbox-assign_receiver_agent'); (20, 2 , 'role','kindbox-assign_receiver_agent'),
(21, 1 , 'role','kindboxreq-add'),
(22, 2 , 'role','kindboxreq-add'),
(23, 1 , 'role','kindbox-getall'),
(24, 2 , 'role','kindbox-getall');
-- +migrate Down -- +migrate Down

View File

@ -0,0 +1,27 @@
package adminkindboxservice
import (
"context"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
const op = "adminkindboxservice.GetAll"
allKindBox, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err)
}
return param.KindBoxGetAllResponse{
AllKindBox: allKindBox,
Pagination: paginationparam.PaginationResponse{
PageSize: req.Pagination.GetPageSize(),
PageNumber: req.Pagination.GetPageNumber(),
Total: total,
},
}, nil
}

View File

@ -3,7 +3,8 @@ package adminkindboxservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
) )
type Repository interface { type Repository interface {
@ -11,6 +12,7 @@ type Repository interface {
AddKindBox(ctx context.Context, kindBox entity.KindBox) error AddKindBox(ctx context.Context, kindBox entity.KindBox) error
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error
GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error)
} }
type Service struct { type Service struct {

View File

@ -3,6 +3,7 @@ package benefactorkindboxservice
import ( import (
"context" "context"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -11,7 +12,7 @@ import (
func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
const op = "userkindboxservice.GetAll" const op = "userkindboxservice.GetAll"
allKindBox, err := s.repo.GetAllKindBox(ctx, req.BenefactorID) allKindBox, _, err := s.repo.GetAllKindBox(ctx, params.FilterRequest{}, params.PaginationRequest{}, params.SortRequest{})
if err != nil { if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }

View File

@ -4,10 +4,11 @@ import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity" entity "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
) )
type Repository interface { type Repository interface {
GetAllKindBox(ctx context.Context, benefactorID uint) ([]entity.KindBox, error) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error)
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error RegisterEmptyingRequestForKindBox(ctx context.Context, kindBox entity.KindBox) error
} }

View File

@ -0,0 +1,45 @@
package adminkindboxvalidator
import (
"errors"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]string, error) {
const op = "adminkindboxvalidator.ValidateGetAll"
validFields := []string{
"id", "kind_box_req_id", "benefactor_id", "kind_box_type", "amount", "serial_number",
"status", "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",
}
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

@ -3,9 +3,12 @@ package adminkindboxvalidator
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"git.gocasts.ir/ebhomengo/niki/entity" "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"
) )
type Repository interface { type Repository interface {
@ -73,3 +76,37 @@ func (v Validator) doesAgentExist(value interface{}) error {
return nil 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
}
}
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
}
}