mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
116 lines
4.1 KiB
Go
116 lines
4.1 KiB
Go
package service
|
|
|
|
import (
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
|
|
approvalService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
|
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
|
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RepportService interface {
|
|
GetExpense(ctx *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error)
|
|
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error)
|
|
}
|
|
|
|
type repportService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
|
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
|
ApprovalSvc approvalService.ApprovalService
|
|
}
|
|
|
|
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, marketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository, approvalSvc approvalService.ApprovalService) RepportService {
|
|
return &repportService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
ExpenseRealizationRepo: expenseRealizationRepo,
|
|
MarketingDeliveryRepo: marketingDeliveryRepo,
|
|
ApprovalSvc: approvalSvc,
|
|
}
|
|
}
|
|
|
|
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
realizations, total, err := s.ExpenseRealizationRepo.GetAllWithFilters(c.Context(), offset, params.Limit, params)
|
|
if err != nil {
|
|
s.Log.Errorf("GetAllWithFilters error: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
|
|
result := dto.ToRepportExpenseListDTOs(realizations)
|
|
|
|
expenseIDs := make([]uint, 0, len(result))
|
|
for i := range result {
|
|
expenseIDs = append(expenseIDs, uint(result[i].Id))
|
|
}
|
|
|
|
approvals, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowExpense, expenseIDs, func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ActionUser")
|
|
})
|
|
if err != nil {
|
|
s.Log.Warnf("LatestByTargets error: %v", err)
|
|
}
|
|
|
|
for i := range result {
|
|
expenseIDAsUint := uint(result[i].Id)
|
|
if approval, exists := approvals[expenseIDAsUint]; exists && approval != nil {
|
|
mapped := approvalDTO.ToApprovalDTO(*approval)
|
|
result[i].LatestApproval = &mapped
|
|
}
|
|
}
|
|
|
|
return result, total, nil
|
|
}
|
|
|
|
func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
deliveryProducts, total, err := s.MarketingDeliveryRepo.GetAllWithFilters(c.Context(), offset, params.Limit, params)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
marketingIDMap := make(map[uint]bool)
|
|
marketingIDs := make([]uint, 0)
|
|
for _, dp := range deliveryProducts {
|
|
if marketingID := dp.MarketingProduct.Marketing.Id; marketingID > 0 && !marketingIDMap[marketingID] {
|
|
marketingIDs = append(marketingIDs, marketingID)
|
|
marketingIDMap[marketingID] = true
|
|
}
|
|
}
|
|
|
|
approvals, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowMarketing, marketingIDs, func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ActionUser")
|
|
})
|
|
if err != nil {
|
|
s.Log.Warnf("LatestByTargets error: %v", err)
|
|
}
|
|
|
|
for i := range deliveryProducts {
|
|
if approval, exists := approvals[deliveryProducts[i].MarketingProduct.Marketing.Id]; exists && approval != nil {
|
|
deliveryProducts[i].MarketingProduct.Marketing.LatestApproval = approval
|
|
}
|
|
}
|
|
|
|
return dto.ToRepportMarketingListDTOs(deliveryProducts), total, nil
|
|
}
|