forked from ebhomengo/niki
feat(admin): add benefactor aggregator service(#198)
This commit is contained in:
parent
bf8a141f68
commit
1d14e86602
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
||||
arrayfunc "git.gocasts.ir/ebhomengo/niki/pkg/array_func"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
|
@ -50,18 +51,46 @@ func (h Handler) GetAll(c echo.Context) error {
|
|||
|
||||
req.Filter = queryparam.GetFilterParams(c)
|
||||
|
||||
resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req)
|
||||
kindboxes, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req)
|
||||
if sErr != nil {
|
||||
msg, code := httpmsg.Error(sErr)
|
||||
if resp.FieldErrors != nil {
|
||||
if kindboxes.FieldErrors != nil {
|
||||
return c.JSON(code, echo.Map{
|
||||
"message": msg,
|
||||
"errors": resp.FieldErrors,
|
||||
"errors": kindboxes.FieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
benefactors, bErr := h.adminBenefactorAggSvc.GetByIDs(c.Request().Context(), getBenefactorIDs(kindboxes.Data))
|
||||
if bErr != nil {
|
||||
msg, code := httpmsg.Error(sErr)
|
||||
if kindboxes.FieldErrors != nil {
|
||||
return c.JSON(code, echo.Map{
|
||||
"message": msg,
|
||||
"errors": kindboxes.FieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
resp := param.KindBoxGetAllResponse{
|
||||
Data: kindboxes.Data,
|
||||
Info: param.Info{
|
||||
Benefactors: benefactors,
|
||||
},
|
||||
Pagination: kindboxes.Pagination,
|
||||
}
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func getBenefactorIDs(kindBoxes []param.Data) []any {
|
||||
benefactorsMap := make(map[uint]bool)
|
||||
for _, kindBox := range kindBoxes {
|
||||
benefactorsMap[kindBox.BenefactorID] = true
|
||||
}
|
||||
return arrayfunc.MapToSlice(benefactorsMap)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package adminkindboxhandler
|
|||
|
||||
import (
|
||||
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
|
||||
adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator"
|
||||
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
|
||||
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
|
||||
"git.gocasts.ir/ebhomengo/niki/service/notification"
|
||||
|
|
@ -10,18 +11,21 @@ import (
|
|||
type Handler struct {
|
||||
authSvc authservice.Service
|
||||
adminKindBoxSvc adminkindboxservice.Service
|
||||
adminBenefactorAggSvc adminbenefactoraggsvc.Service
|
||||
adminAuthorizeSvc adminauthorizationservice.Service
|
||||
notificationSvc notification.Service
|
||||
}
|
||||
|
||||
func New(authSvc authservice.Service,
|
||||
adminKindBoxSvc adminkindboxservice.Service,
|
||||
adminBenefactorAggSvc adminbenefactoraggsvc.Service,
|
||||
adminAuthorizeSvc adminauthorizationservice.Service,
|
||||
notificationSvc notification.Service,
|
||||
) Handler {
|
||||
return Handler{
|
||||
authSvc: authSvc,
|
||||
adminKindBoxSvc: adminKindBoxSvc,
|
||||
adminBenefactorAggSvc: adminBenefactorAggSvc,
|
||||
adminAuthorizeSvc: adminAuthorizeSvc,
|
||||
notificationSvc: notificationSvc,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
arrayfunc "git.gocasts.ir/ebhomengo/niki/pkg/array_func"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
|
||||
echo "github.com/labstack/echo/v4"
|
||||
|
|
@ -48,18 +49,46 @@ func (h Handler) GetAll(c echo.Context) error {
|
|||
|
||||
req.Filter = queryparam.GetFilterParams(c)
|
||||
|
||||
resp, err := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req)
|
||||
kindBoxReqs, err := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req)
|
||||
if err != nil {
|
||||
msg, code := httpmsg.Error(err)
|
||||
if resp.FieldErrors != nil {
|
||||
if kindBoxReqs.FieldErrors != nil {
|
||||
return c.JSON(code, httpmsg.ErrorResponse{
|
||||
Message: msg,
|
||||
Errors: resp.FieldErrors,
|
||||
Errors: kindBoxReqs.FieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
benefactors, bErr := h.adminBenefactorAggSvc.GetByIDs(c.Request().Context(), getBenefactorIDs(kindBoxReqs.Data))
|
||||
if bErr != nil {
|
||||
msg, code := httpmsg.Error(bErr)
|
||||
if kindBoxReqs.FieldErrors != nil {
|
||||
return c.JSON(code, echo.Map{
|
||||
"message": msg,
|
||||
"errors": kindBoxReqs.FieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
resp := param.KindBoxReqGetAllResponse{
|
||||
Data: kindBoxReqs.Data,
|
||||
Info: param.Info{
|
||||
Benefactors: benefactors,
|
||||
},
|
||||
Pagination: kindBoxReqs.Pagination,
|
||||
}
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func getBenefactorIDs(kindBoxReqs []param.Data) []any {
|
||||
benefactorsMap := make(map[uint]bool)
|
||||
for _, kindBox := range kindBoxReqs {
|
||||
benefactorsMap[kindBox.BenefactorID] = true
|
||||
}
|
||||
return arrayfunc.MapToSlice(benefactorsMap)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package adminkindboxreqhandler
|
|||
|
||||
import (
|
||||
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
|
||||
adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator"
|
||||
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
|
||||
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
|
||||
"git.gocasts.ir/ebhomengo/niki/service/notification"
|
||||
|
|
@ -10,17 +11,21 @@ import (
|
|||
type Handler struct {
|
||||
authSvc authservice.Service
|
||||
adminKindBoxReqSvc adminkindboxreqservice.Service
|
||||
adminBenefactorAggSvc adminbenefactoraggsvc.Service
|
||||
adminAuthorizeSvc adminauthorizationservice.Service
|
||||
notificationSvc notification.Service
|
||||
}
|
||||
|
||||
func New(authSvc authservice.Service,
|
||||
adminKindBoxReqSvc adminkindboxreqservice.Service,
|
||||
adminAuthorizeSvc adminauthorizationservice.Service, notificationSvc notification.Service,
|
||||
adminBenefactorAggSvc adminbenefactoraggsvc.Service,
|
||||
adminAuthorizeSvc adminauthorizationservice.Service,
|
||||
notificationSvc notification.Service,
|
||||
) Handler {
|
||||
return Handler{
|
||||
authSvc: authSvc,
|
||||
adminKindBoxReqSvc: adminKindBoxReqSvc,
|
||||
adminBenefactorAggSvc: adminBenefactorAggSvc,
|
||||
adminAuthorizeSvc: adminAuthorizeSvc,
|
||||
notificationSvc: notificationSvc,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,8 +50,8 @@ func New(
|
|||
Router: echo.New(),
|
||||
config: cfg,
|
||||
adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc),
|
||||
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
||||
adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
||||
adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminBenefactorAggSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
||||
adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminBenefactorAggSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc),
|
||||
adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc),
|
||||
adminBenefactorHandler: adminbenefactorhandler.New(svc.AdminAuthSvc, svc.AdminAuthorizeSvc, svc.AdminBenefactorSvc, svc.AdminAddressSvc, svc.AdminKindBoxSvc, svc.AdminKindBoxReqSvc),
|
||||
adminReferTimeHandler: adminrefertimehandler.New(svc.AdminAuthSvc, svc.AdminReferTimeSvc, svc.AdminAuthorizeSvc),
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package adminkindboxparam
|
|||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/param"
|
||||
adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||
)
|
||||
|
||||
type KindBoxGetAllRequest struct {
|
||||
|
|
@ -11,8 +12,13 @@ type KindBoxGetAllRequest struct {
|
|||
Search param.SearchRequest
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Benefactors []adminbenefactoreparam.Data `json:"benefactors"`
|
||||
}
|
||||
|
||||
type KindBoxGetAllResponse struct {
|
||||
Data []Data `json:"data"`
|
||||
Info Info `json:"info"`
|
||||
Pagination param.PaginationResponse `json:"pagination"`
|
||||
FieldErrors map[string]string `json:"field_errors,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package adminkindboxreqparam
|
|||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/param"
|
||||
adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||
)
|
||||
|
||||
type KindBoxReqGetAllRequest struct {
|
||||
|
|
@ -11,8 +12,13 @@ type KindBoxReqGetAllRequest struct {
|
|||
Search param.SearchRequest
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Benefactors []adminbenefactoreparam.Data `json:"benefactors"`
|
||||
}
|
||||
|
||||
type KindBoxReqGetAllResponse struct {
|
||||
Data []Data `json:"data"`
|
||||
Info Info `json:"info"`
|
||||
Pagination param.PaginationResponse `json:"pagination"`
|
||||
FieldErrors map[string]string `json:"field_errors,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
package arrayfunc
|
||||
|
||||
func MapToSlice(mapList map[uint]bool) []any {
|
||||
arrayList := make([]any, 0, len(mapList))
|
||||
for id := range mapList {
|
||||
arrayList = append(arrayList, id)
|
||||
}
|
||||
return arrayList
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package mysqlbenefactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
)
|
||||
|
||||
func (d *DB) GetBenefactorByIds(ctx context.Context, benefactorIDs []any) (map[uint]entity.Benefactor, error) {
|
||||
const op = "mysqlbenefactor.GetBenefactorByIds"
|
||||
|
||||
if len(benefactorIDs) <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
query := `select * from benefactors where id in (%s)`
|
||||
param := "?"
|
||||
for i := 1; i < len(benefactorIDs); i++ {
|
||||
param += ",?"
|
||||
}
|
||||
query = fmt.Sprintf(query, param)
|
||||
rows, qErr := d.conn.Conn().QueryContext(ctx, query, benefactorIDs...)
|
||||
if qErr != nil {
|
||||
return nil, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
benefactors := make(map[uint]entity.Benefactor)
|
||||
for rows.Next() {
|
||||
benefactor, sErr := scanBenefactor(rows)
|
||||
if sErr != nil {
|
||||
return nil, richerror.New(op).WithErr(sErr).
|
||||
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
benefactors[benefactor.ID] = benefactor
|
||||
}
|
||||
if rErr := rows.Err(); rErr != nil {
|
||||
return nil, richerror.New(op).WithErr(rErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
return benefactors, nil
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ const (
|
|||
StatementKeyAdminGetByPhoneNumber
|
||||
StatementKeyAdminAgentGetAll
|
||||
StatementKeyBenefactorGetByID
|
||||
StatementKeyBenefactorGetByIDs
|
||||
StatementKeyBenefactorGetByPhoneNumber
|
||||
StatementKeyBenefactorCreate
|
||||
StatementKeyBenefactorGetAll
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package adminbenefactoraggregatorservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||
response "git.gocasts.ir/ebhomengo/niki/pkg/response_builder"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
)
|
||||
|
||||
func (s Service) GetByIDs(ctx context.Context, benefactorIDs []any) ([]param.Data, error) {
|
||||
const op = "adminbenefactoraggregatorservice.GetByIDs"
|
||||
var data []param.Data
|
||||
|
||||
benefactors, err := s.repo.GetBenefactorByIds(ctx, benefactorIDs)
|
||||
if err != nil {
|
||||
return nil, richerror.New(op).WithErr(err)
|
||||
}
|
||||
for _, benefactor := range benefactors {
|
||||
data = append(data, param.Data{
|
||||
ID: benefactor.ID,
|
||||
FirstName: benefactor.FirstName,
|
||||
LastName: benefactor.LastName,
|
||||
PhoneNumber: benefactor.PhoneNumber,
|
||||
Description: benefactor.Description,
|
||||
Email: benefactor.Email,
|
||||
Gender: benefactor.Gender,
|
||||
BirthDate: response.GetNullDate(benefactor.BirthDate),
|
||||
Status: benefactor.Status,
|
||||
})
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package adminbenefactoraggregatorservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetBenefactorByIds(ctx context.Context, benefactorIDs []any) (map[uint]entity.Benefactor, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func New(repo Repository) Service {
|
||||
return Service{repo: repo}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
params "git.gocasts.ir/ebhomengo/niki/param"
|
||||
adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator"
|
||||
validator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
|
||||
)
|
||||
|
||||
|
|
@ -18,12 +19,14 @@ type Repository interface {
|
|||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
benefactorAggSvc adminbenefactoraggsvc.Service
|
||||
vld validator.Validator
|
||||
}
|
||||
|
||||
func New(repo Repository, vld validator.Validator) Service {
|
||||
func New(repo Repository, benefactorAggSvc adminbenefactoraggsvc.Service, vld validator.Validator) Service {
|
||||
return Service{
|
||||
repo: repo,
|
||||
benefactorAggSvc: benefactorAggSvc,
|
||||
vld: vld,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
adminagentservice "git.gocasts.ir/ebhomengo/niki/service/admin/agent"
|
||||
adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization"
|
||||
adminbenefactorservice "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor"
|
||||
adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator"
|
||||
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
|
||||
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
|
||||
adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time"
|
||||
|
|
@ -62,6 +63,7 @@ type Service struct {
|
|||
NotificationSvc notification.Service
|
||||
BenefactorReferTimeSvc benefactorrefertimeservice.Service
|
||||
AdminReferTimeSvc adminrefertimeservice.Service
|
||||
AdminBenefactorAggSvc adminbenefactoraggsvc.Service
|
||||
}
|
||||
|
||||
func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscontract.SmsAdapter) *Service {
|
||||
|
|
@ -84,11 +86,12 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
|
|||
AdminBenefactorVld = adminbenefactorvalidator.New(benefactorRepo)
|
||||
AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld)
|
||||
AdminAgentSvc = adminagentservice.New(agentRepo)
|
||||
AdminBenefactorAggSvc = adminbenefactoraggsvc.New(benefactorRepo)
|
||||
|
||||
AdminVld = adminvalidator.New(adminRepo)
|
||||
AdminSvc = adminservice.New(adminRepo, AdminAuthSvc, AdminVld)
|
||||
AdminKindBoxVld = adminkindboxvalidator.New(kindBoxRepo, AdminAgentSvc, AdminBenefactorSvc, AdminReferTimeSvc, AdminAddressSvc)
|
||||
AdminKindBoxSvc = adminkindboxservice.New(kindBoxRepo, AdminKindBoxVld)
|
||||
AdminKindBoxSvc = adminkindboxservice.New(kindBoxRepo, AdminBenefactorAggSvc, AdminKindBoxVld)
|
||||
AdminKindBoxReqVld = adminkindboxreqvalidator.New(kindBoxReqRepo, AdminSvc, AdminAgentSvc, AdminBenefactorSvc, AdminReferTimeSvc, AdminAddressSvc)
|
||||
AdminKindBoxReqSvc = adminkindboxreqservice.New(kindBoxReqRepo, AdminKindBoxReqVld)
|
||||
)
|
||||
|
|
@ -132,5 +135,6 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
|
|||
NotificationSvc: NotificationSvc,
|
||||
BenefactorReferTimeSvc: BenefactorReferTimeSvc,
|
||||
AdminReferTimeSvc: AdminReferTimeSvc,
|
||||
AdminBenefactorAggSvc: AdminBenefactorAggSvc,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue