Merge pull request 'feat(admin): add benefactor aggregator service(#198)' (#207) from stage/hamed/agregate-service into develop

Reviewed-on: ebhomengo/niki#207
This commit is contained in:
hossein 2024-11-19 15:51:12 +00:00
commit 54da7e48fa
14 changed files with 232 additions and 39 deletions

View File

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

View File

@ -2,27 +2,31 @@ 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"
)
type Handler struct {
authSvc authservice.Service
adminKindBoxSvc adminkindboxservice.Service
adminAuthorizeSvc adminauthorizationservice.Service
notificationSvc notification.Service
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,
adminAuthorizeSvc: adminAuthorizeSvc,
notificationSvc: notificationSvc,
authSvc: authSvc,
adminKindBoxSvc: adminKindBoxSvc,
adminBenefactorAggSvc: adminBenefactorAggSvc,
adminAuthorizeSvc: adminAuthorizeSvc,
notificationSvc: notificationSvc,
}
}

View File

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

View File

@ -2,26 +2,31 @@ 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"
)
type Handler struct {
authSvc authservice.Service
adminKindBoxReqSvc adminkindboxreqservice.Service
adminAuthorizeSvc adminauthorizationservice.Service
notificationSvc notification.Service
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,
adminAuthorizeSvc: adminAuthorizeSvc,
notificationSvc: notificationSvc,
authSvc: authSvc,
adminKindBoxReqSvc: adminKindBoxReqSvc,
adminBenefactorAggSvc: adminBenefactorAggSvc,
adminAuthorizeSvc: adminAuthorizeSvc,
notificationSvc: notificationSvc,
}
}

View File

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

View File

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

View File

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

9
pkg/array_func/map.go Normal file
View File

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

View File

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

View File

@ -23,6 +23,7 @@ const (
StatementKeyAdminGetByPhoneNumber
StatementKeyAdminAgentGetAll
StatementKeyBenefactorGetByID
StatementKeyBenefactorGetByIDs
StatementKeyBenefactorGetByPhoneNumber
StatementKeyBenefactorCreate
StatementKeyBenefactorGetAll

View File

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

View File

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

View File

@ -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"
)
@ -17,13 +18,15 @@ type Repository interface {
}
type Service struct {
repo Repository
vld validator.Validator
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,
vld: vld,
repo: repo,
benefactorAggSvc: benefactorAggSvc,
vld: vld,
}
}

View File

@ -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 {
@ -76,19 +78,20 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont
)
redisOtp := redisotp.New(rds)
var (
AdminAuthSvc = auth.New(cfg.AdminAuth)
AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo)
AdminReferTimeVld = adminrefertimevalidator.New()
AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo, AdminReferTimeVld)
AdminAddressSvc = adminaddressservice.New(addressRepo)
AdminBenefactorVld = adminbenefactorvalidator.New(benefactorRepo)
AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld)
AdminAgentSvc = adminagentservice.New(agentRepo)
AdminAuthSvc = auth.New(cfg.AdminAuth)
AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo)
AdminReferTimeVld = adminrefertimevalidator.New()
AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo, AdminReferTimeVld)
AdminAddressSvc = adminaddressservice.New(addressRepo)
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,
}
}