feat(niki): agent get all delivery awaiting kindboxreqs

This commit is contained in:
Erfan Mohammadi 2024-06-12 11:45:28 +03:30 committed by Iman Mirazimi
parent 5eb962dc43
commit 857d26fd10
24 changed files with 738 additions and 82 deletions

View File

@ -1,13 +1,10 @@
package adminkindboxreqhandler package adminkindboxreqhandler
import ( import (
"net/http"
"strconv"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
"net/http"
) )
// GetAll godoc // GetAll godoc
@ -26,17 +23,7 @@ func (h Handler) GetAll(c echo.Context) error {
if bErr := c.Bind(&req); bErr != nil { if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) return echo.NewHTTPError(http.StatusBadRequest)
} }
resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req)
var paginationReq paginationparam.PaginationRequest
// TODO : pkg convert string to uint
//nolint
pageNumber, _ := strconv.ParseUint(c.QueryParam("page_number"), 0, 64)
//nolint
pageSize, _ := strconv.ParseUint(c.QueryParam("page_size"), 0, 64)
paginationReq.PageSize = uint(pageSize)
paginationReq.PageNumber = uint(pageNumber)
resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req, paginationReq)
if sErr != nil { if sErr != nil {
msg, code := httpmsg.Error(sErr) msg, code := httpmsg.Error(sErr)

View File

@ -0,0 +1,64 @@
package adminkindboxreqhandler
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
"github.com/labstack/echo/v4"
)
// GetAllAwaitingDelivery godoc
// @Summary Get all awaiting delivery KindBox requests
// @Description Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options
// @Tags KindBoxReq
// @Accept json
// @Produce json
// @Param filter_id query int false "Filter by ID"
// @Param filter_benefactor_id query int false "Filter by benefactor ID"
// @Param filter_kind_box_type query entity.KindBoxType false "Filter by KindBox type" Format(enum)
// @Param filter_count_requested query int false "Filter by count requested"
// @Param filter_count_accepted query int false "Filter by count accepted"
// @Param filter_deliver_refer_time_id query int false "Filter by deliver refer time 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,benefactor_id,kind_box_type,count_requested,count_accepted,deliver_refer_time_id,deliver_refer_date,deliver_address_id)
// @Param sort_direction query string false "Sort order" Enums(asc,desc)
// @Success 200 {object} param.DeliveryAwaitingGetAllResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admin/kindboxreqs/awaiting-delivery [get]
func (h Handler) GetAllAwaitingDelivery(c echo.Context) error {
var req param.DeliveryAwaitingGetAllRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
req.Filter = queryparam.GetFilterParams(c)
if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetAllAwaitingDelivery(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
req.Filter["sender_agent_id"] = claim.GetClaimsFromEchoContext(c).UserID
req.Filter["status"] = entity.KindBoxReqAssignedSenderAgentStatus.String()
resp, sErr := h.adminKindBoxReqSvc.GetAllAwaitingDelivery(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

@ -16,4 +16,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.PATCH("/assign-sender-agent/:id", h.AssignSenderAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqAssignSenderAgentPermission)) r.PATCH("/assign-sender-agent/:id", h.AssignSenderAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqAssignSenderAgentPermission))
r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission)) r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission))
r.GET("/awaiting-delivery/:id", h.GetAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission)) r.GET("/awaiting-delivery/:id", h.GetAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission))
r.GET("/awaiting-delivery", h.GetAllAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission))
} }

View File

@ -416,6 +416,135 @@ const docTemplate = `{
} }
} }
}, },
"/admin/kindboxreqs/awaiting-delivery": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "Get all awaiting delivery KindBox requests",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
1,
2,
3
],
"type": "integer",
"format": "enum",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by count requested",
"name": "filter_count_requested",
"in": "query"
},
{
"type": "integer",
"description": "Filter by count accepted",
"name": "filter_count_accepted",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_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",
"benefactor_id",
"kind_box_type",
"count_requested",
"count_accepted",
"deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs/awaiting-delivery/{id}": { "/admin/kindboxreqs/awaiting-delivery/{id}": {
"get": { "get": {
"security": [ "security": [
@ -1073,6 +1202,20 @@ const docTemplate = `{
"adminkindboxreqparam.DeliverKindBoxReqResponse": { "adminkindboxreqparam.DeliverKindBoxReqResponse": {
"type": "object" "type": "object"
}, },
"adminkindboxreqparam.DeliveryAwaitingGetAllResponse": {
"type": "object",
"properties": {
"all_awaiting_kind_box_req": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBoxReq"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"adminkindboxreqparam.DeliveryAwaitingGetResponse": { "adminkindboxreqparam.DeliveryAwaitingGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1148,7 +1291,7 @@ const docTemplate = `{
"adminkindboxreqparam.KindBoxReqGetAllResponse": { "adminkindboxreqparam.KindBoxReqGetAllResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
"all_kind_box_req": { "all_awaiting_kind_box_req": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/entity.KindBoxReq" "$ref": "#/definitions/entity.KindBoxReq"

View File

@ -405,6 +405,135 @@
} }
} }
}, },
"/admin/kindboxreqs/awaiting-delivery": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"description": "Retrieves a list of all awaiting KindBox requests with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"KindBoxReq"
],
"summary": "Get all awaiting delivery KindBox requests",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
1,
2,
3
],
"type": "integer",
"format": "enum",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by count requested",
"name": "filter_count_requested",
"in": "query"
},
{
"type": "integer",
"description": "Filter by count accepted",
"name": "filter_count_accepted",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_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",
"benefactor_id",
"kind_box_type",
"count_requested",
"count_accepted",
"deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
}
}
}
},
"/admin/kindboxreqs/awaiting-delivery/{id}": { "/admin/kindboxreqs/awaiting-delivery/{id}": {
"get": { "get": {
"security": [ "security": [
@ -1062,6 +1191,20 @@
"adminkindboxreqparam.DeliverKindBoxReqResponse": { "adminkindboxreqparam.DeliverKindBoxReqResponse": {
"type": "object" "type": "object"
}, },
"adminkindboxreqparam.DeliveryAwaitingGetAllResponse": {
"type": "object",
"properties": {
"all_awaiting_kind_box_req": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBoxReq"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"adminkindboxreqparam.DeliveryAwaitingGetResponse": { "adminkindboxreqparam.DeliveryAwaitingGetResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1137,7 +1280,7 @@
"adminkindboxreqparam.KindBoxReqGetAllResponse": { "adminkindboxreqparam.KindBoxReqGetAllResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
"all_kind_box_req": { "all_awaiting_kind_box_req": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/entity.KindBoxReq" "$ref": "#/definitions/entity.KindBoxReq"

View File

@ -81,6 +81,15 @@ definitions:
type: object type: object
adminkindboxreqparam.DeliverKindBoxReqResponse: adminkindboxreqparam.DeliverKindBoxReqResponse:
type: object type: object
adminkindboxreqparam.DeliveryAwaitingGetAllResponse:
properties:
all_awaiting_kind_box_req:
items:
$ref: '#/definitions/entity.KindBoxReq'
type: array
pagination:
$ref: '#/definitions/param.PaginationResponse'
type: object
adminkindboxreqparam.DeliveryAwaitingGetResponse: adminkindboxreqparam.DeliveryAwaitingGetResponse:
properties: properties:
benefactorID: benefactorID:
@ -130,7 +139,7 @@ definitions:
type: object type: object
adminkindboxreqparam.KindBoxReqGetAllResponse: adminkindboxreqparam.KindBoxReqGetAllResponse:
properties: properties:
all_kind_box_req: all_awaiting_kind_box_req:
items: items:
$ref: '#/definitions/entity.KindBoxReq' $ref: '#/definitions/entity.KindBoxReq'
type: array type: array
@ -826,6 +835,95 @@ paths:
summary: Admin Assign Sender Agent to kindboxreq summary: Admin Assign Sender Agent to kindboxreq
tags: tags:
- KindBoxReq - KindBoxReq
/admin/kindboxreqs/awaiting-delivery:
get:
consumes:
- application/json
description: Retrieves a list of all awaiting KindBox requests with filtering,
sorting, and pagination options
parameters:
- description: Filter by ID
in: query
name: filter_id
type: integer
- description: Filter by benefactor ID
in: query
name: filter_benefactor_id
type: integer
- description: Filter by KindBox type
enum:
- 1
- 2
- 3
format: enum
in: query
name: filter_kind_box_type
type: integer
- description: Filter by count requested
in: query
name: filter_count_requested
type: integer
- description: Filter by count accepted
in: query
name: filter_count_accepted
type: integer
- description: Filter by deliver refer time ID
in: query
name: filter_deliver_refer_time_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
- benefactor_id
- kind_box_type
- count_requested
- count_accepted
- deliver_refer_time_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/adminkindboxreqparam.DeliveryAwaitingGetAllResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Get all awaiting delivery KindBox requests
tags:
- KindBoxReq
/admin/kindboxreqs/awaiting-delivery/{id}: /admin/kindboxreqs/awaiting-delivery/{id}:
get: get:
consumes: consumes:

View File

@ -1,13 +1,17 @@
package adminkindboxreqparam package adminkindboxreqparam
import ( import (
entity "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
paginationparam "git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
) )
type KindBoxReqGetAllRequest struct{} type KindBoxReqGetAllRequest struct {
Pagination param.PaginationRequest
Sort param.SortRequest
Filter param.FilterRequest
}
type KindBoxReqGetAllResponse struct { type KindBoxReqGetAllResponse struct {
AllKindBoxReq []entity.KindBoxReq `json:"all_kind_box_req"` AllKindBoxReq []entity.KindBoxReq `json:"all_awaiting_kind_box_req"`
Pagination paginationparam.PaginationResponse `json:"pagination"` Pagination param.PaginationResponse `json:"pagination"`
} }

View File

@ -0,0 +1,17 @@
package adminkindboxreqparam
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/param"
)
type DeliveryAwaitingGetAllRequest struct {
Pagination param.PaginationRequest
Sort param.SortRequest
Filter param.FilterRequest
}
type DeliveryAwaitingGetAllResponse struct {
AllAwaitingKindBoxReq []entity.KindBoxReq `json:"all_awaiting_kind_box_req"`
Pagination param.PaginationResponse `json:"pagination"`
}

3
param/filter.go Normal file
View File

@ -0,0 +1,3 @@
package param
type FilterRequest map[string]any

View File

@ -2,12 +2,12 @@ package param
const ( const (
defaultPageNumber = 1 defaultPageNumber = 1
dafaultPageSize = 10 defaultPageSize = 10
) )
type PaginationRequest struct { type PaginationRequest struct {
PageSize uint PageSize uint `query:"page_size" example:"10"`
PageNumber uint PageNumber uint `query:"page_number" example:"1"`
} }
type PaginationResponse struct { type PaginationResponse struct {
@ -32,9 +32,10 @@ func (p *PaginationRequest) GetPageSize() uint {
validPageSizes := []uint{10, 25, 50, 100} validPageSizes := []uint{10, 25, 50, 100}
for _, size := range validPageSizes { for _, size := range validPageSizes {
if p.PageSize == size { if p.PageSize == size {
return size return p.PageSize
} }
} }
p.PageSize = defaultPageSize
return dafaultPageSize return p.PageSize
} }

13
param/sort.go Normal file
View File

@ -0,0 +1,13 @@
package param
type SortDirection string
const (
AscSortDirection = SortDirection("asc")
DescSortDirection = SortDirection("desc")
)
type SortRequest struct {
Field string `query:"sort_field"`
Direction SortDirection `query:"sort_direction"`
}

View File

@ -21,12 +21,16 @@ const (
ErrorMsgAssignSenderAgentKindBoxReqStatus = "only accepted kind_box_reqs will have the ability to be assign sender agent" ErrorMsgAssignSenderAgentKindBoxReqStatus = "only accepted kind_box_reqs will have the ability to be assign sender agent"
ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered" ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered"
ErrorMsgAdminIsNotAgent = "admin is not agent" ErrorMsgAdminIsNotAgent = "admin is not agent"
ErrorMsgCountAcceptedOverflow = "count accepted is greater than count requested" ErrorMsgCountAcceptedOverflow = "count accepted is greater than count requested"
ErrorMsgCantInsertRecord = "can't insert record" ErrorMsgCantInsertRecord = "can't insert record"
ErrorMsgCantRetrieveLastInsertID = "can't retrieve last insert id" ErrorMsgCantRetrieveLastInsertID = "can't retrieve last insert id"
ErrorMsgCantUpdateRecord = "can't update record" ErrorMsgCantUpdateRecord = "can't update record"
ErrorMsgReferTimeNotFound = "refer time not found" ErrorMsgReferTimeNotFound = "refer time not found"
ErrorMsgReferTimeIsNotActive = "refer time is not active" ErrorMsgReferTimeIsNotActive = "refer time is not active"
ErrorMsgKindBoxReqDoesntBelongToBenefactor = "kind box req doesnt belong to benefactor" ErrorMsgKindBoxReqDoesntBelongToBenefactor = "kind box req doesnt belong to benefactor"
ErrorMsgCantDeleteAddress = "can't delete address" ErrorMsgCantDeleteAddress = "can't delete address"
ErrorMsgFiltersAreNotValid = "filters are not valid"
ErrorMsgSortFieldIsRequired = "sort field is required"
ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc"
ErrorMsgSortFieldIsNotValid = "sort field is not valid"
) )

View File

@ -0,0 +1,17 @@
package mysqlquerybuilder
import "fmt"
func BuildFilterQuery(filter map[string]interface{}) (string, []any) {
var (
filterQuery string
args = []any{}
)
for key, value := range filter {
filterQuery += fmt.Sprintf("AND %s = ? ", key)
args = append(args, value)
}
return filterQuery, args
}

View File

@ -0,0 +1,31 @@
package mysqlquerybuilder
import (
"fmt"
"git.gocasts.ir/ebhomengo/niki/param"
)
func BuildGetAllQuery(baseQuery string, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) (string, []any) {
filterQuery, fArgs := BuildFilterQuery(filter)
paginationQuery, pArgs := BuildPaginationQuery(pagination)
sortQuery := BuildSortQuery(sort)
args := []any{}
args = append(args, fArgs...)
args = append(args, pArgs...)
query := baseQuery
if filterQuery != "" {
query = fmt.Sprintf("%s %s", query, filterQuery)
}
if sortQuery != "" {
query = fmt.Sprintf("%s %s", query, sortQuery)
}
if paginationQuery != "" {
query = fmt.Sprintf("%s %s", query, paginationQuery)
}
return query, args
}

View File

@ -0,0 +1,9 @@
package mysqlquerybuilder
import (
"git.gocasts.ir/ebhomengo/niki/param"
)
func BuildPaginationQuery(pagination param.PaginationRequest) (string, []any) {
return "LIMIT ? OFFSET ?", []any{pagination.GetPageSize(), pagination.GetOffset()}
}

View File

@ -0,0 +1,14 @@
package mysqlquerybuilder
import (
"fmt"
"git.gocasts.ir/ebhomengo/niki/param"
)
func BuildSortQuery(sort param.SortRequest) string {
if sort.Field == "" && sort.Direction == "" {
return ""
}
return fmt.Sprintf("ORDER BY %s %s", sort.Field, sort.Direction)
}

18
pkg/query_param/echo.go Normal file
View File

@ -0,0 +1,18 @@
package queryparam
import (
"strings"
"github.com/labstack/echo/v4"
)
func GetFilterParams(c echo.Context) map[string]any {
queryParams := make(map[string]any)
for key, values := range c.QueryParams() {
if len(values) > 0 && strings.HasPrefix(key, "filter_") {
queryParams[strings.TrimPrefix(key, "filter_")] = values[0]
}
}
return queryParams
}

View File

@ -2,68 +2,48 @@ package mysqlkindboxreq
import ( import (
"context" "context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/param"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
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"
) )
func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) { func (d DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest) ([]entity.KindBoxReq, uint, error) {
const op = "mysqlkindboxreq.GetAllKindBoxReq" const op = "mysqlkindboxreq.GetAllKindBoxReq"
// TODO: create getCount function baseQuery := `SELECT * FROM kind_box_reqs WHERE deleted_at IS NULL`
var count uint query, args := builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
rows, err := d.conn.Conn().QueryContext(ctx, "SELECT COUNT(*) FROM kind_box_reqs where deleted_at is null") rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...)
if err != nil { if qErr != nil {
return nil, paginationparam.PaginationResponse{}, return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)
} }
defer rows.Close() defer rows.Close()
kindBoxReqs := make([]entity.KindBoxReq, 0)
// Iterate through the rows (should only be one) and extract the count:
for rows.Next() {
err := rows.Scan(&count)
if err != nil {
panic(err)
}
}
if rErr := rows.Err(); rErr != nil {
return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
// TODO - add sort and filter
rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs where deleted_at is null limit ? offset ?", pagination.GetPageSize(), pagination.GetOffset())
if err != nil {
return nil, paginationparam.PaginationResponse{},
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
// An album slice to hold data from returned rows.
var kindBoxReqs []entity.KindBoxReq
// Loop through rows, using Scan to assign column data to struct fields.
for rows.Next() { for rows.Next() {
kindBoxReq, sErr := scanKindBoxReq(rows) kindBoxReq, sErr := scanKindBoxReq(rows)
if sErr != nil { if sErr != nil {
return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(sErr). fmt.Println(sErr)
return nil, 0, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
} }
kindBoxReqs = append(kindBoxReqs, kindBoxReq) kindBoxReqs = append(kindBoxReqs, kindBoxReq)
} }
if rErr := rows.Err(); rErr != nil { if rErr := rows.Err(); rErr != nil {
return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(rErr). return nil, 0, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
} }
return kindBoxReqs, paginationparam.PaginationResponse{ var total uint
PageSize: pagination.GetPageSize(), baseQuery = `SELECT COUNT(*) FROM kind_box_reqs WHERE deleted_at IS NULL`
PageNumber: pagination.GetPageNumber(), query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
Total: count, qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
}, nil if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return kindBoxReqs, total, nil
} }

View File

@ -30,6 +30,7 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
&countAccepted, &countAccepted,
&description, &description,
&status, &status,
&kindBoxReq.DeliverReferTimeID,
&kindBoxReq.DeliverReferDate, &kindBoxReq.DeliverReferDate,
&kindBoxReq.DeliverAddressID, &kindBoxReq.DeliverAddressID,
&senderAgentID, &senderAgentID,

View File

@ -8,11 +8,10 @@ import (
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
// TODO: Pagination, Filters, Sort. func (s Service) GetAll(ctx context.Context, req param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) {
func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest, paginationreq paginationparam.PaginationRequest) (param.KindBoxReqGetAllResponse, error) {
const op = "adminkindboxreqservice.GetAll" const op = "adminkindboxreqservice.GetAll"
allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq) allKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil { if err != nil {
return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
@ -20,9 +19,9 @@ func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest, pa
return param.KindBoxReqGetAllResponse{ return param.KindBoxReqGetAllResponse{
AllKindBoxReq: allKindBoxReq, AllKindBoxReq: allKindBoxReq,
Pagination: paginationparam.PaginationResponse{ Pagination: paginationparam.PaginationResponse{
PageSize: pagination.PageSize, PageSize: req.Pagination.GetPageSize(),
PageNumber: pagination.PageNumber, PageNumber: req.Pagination.GetPageNumber(),
Total: pagination.Total, Total: total,
}, },
}, nil }, nil
} }

View File

@ -0,0 +1,28 @@
package adminkindboxreqservice
import (
"context"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAllAwaitingDelivery(ctx context.Context, req param.DeliveryAwaitingGetAllRequest) (param.DeliveryAwaitingGetAllResponse, error) {
const op = "adminkindboxreqservice.GetAllAwaitingDelivery"
allAwaitingKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil {
return param.DeliveryAwaitingGetAllResponse{}, richerror.New(op).WithErr(err)
}
return param.DeliveryAwaitingGetAllResponse{
AllAwaitingKindBoxReq: allAwaitingKindBoxReq,
Pagination: paginationparam.PaginationResponse{
PageSize: req.Pagination.GetPageSize(),
PageNumber: req.Pagination.GetPageNumber(),
Total: total,
},
}, nil
}

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
paginationparam "git.gocasts.ir/ebhomengo/niki/param" params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
) )
@ -13,9 +13,9 @@ type Repository interface {
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error
RollbackKindBoxRequestStatus(ctx context.Context, id uint) error RollbackKindBoxRequestStatus(ctx context.Context, id uint) error
GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error)
AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID uint, senderAgentID uint) error AssignSenderAgentToKindBoxReq(ctx context.Context, kindBoxReqID uint, senderAgentID uint) error
DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error DeliverKindBoxReq(ctx context.Context, kindBoxReqID uint) error
GetAllKindBoxReq(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBoxReq, uint, error)
GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error)
} }

View File

@ -0,0 +1,43 @@
package adminkindboxreqvalidator
import (
"errors"
kbrparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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) ValidateGetAllAwaitingDelivery(req kbrparam.DeliveryAwaitingGetAllRequest) (map[string]string, error) {
const op = "adminkindboxreqvalidator.ValidateGetAllAwaitingDelivery"
validFields := []string{
"id", "benefactor_id", "kind_box_type",
"count_requested", "count_accepted",
"deliver_refer_time_id", "deliver_refer_date", "deliver_address_id",
}
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

@ -4,8 +4,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"slices"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" param "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"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
@ -82,6 +84,7 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
return nil return nil
} }
func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error { func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
kindboxreqID, ok := value.(uint) kindboxreqID, ok := value.(uint)
if !ok { if !ok {
@ -97,6 +100,7 @@ func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
return nil return nil
} }
func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error { func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error {
kindboxreqID, ok := value.(uint) kindboxreqID, ok := value.(uint)
if !ok { if !ok {
@ -147,3 +151,37 @@ func (v Validator) doesAgentAdminExist(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
}
}