FEAT[BE]: create marketing report API

This commit is contained in:
aguhh18
2025-12-15 16:17:37 +07:00
parent efaeb89ca1
commit d5bc6838c8
9 changed files with 412 additions and 46 deletions
@@ -8,6 +8,7 @@ import (
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"
@@ -16,26 +17,29 @@ import (
)
type RepportService interface {
GetExpense(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error)
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, approvalSvc approvalService.ApprovalService) RepportService {
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.Query) ([]dto.RepportExpenseListDTO, int64, error) {
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
}
@@ -72,3 +76,40 @@ func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.Query) ([]d
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
}