FEAT[BE]: implement expense report retrieval with filtering options

This commit is contained in:
aguhh18
2025-12-15 09:11:26 +07:00
parent c79e35c217
commit cbb3368141
12 changed files with 330 additions and 158 deletions
@@ -1,106 +1,74 @@
package service
import (
"strings"
"time"
"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"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type RepportService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
GetExpense(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
GetExpense(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error)
}
type repportService struct {
Log *logrus.Logger
Validate *validator.Validate
dummyData map[uint]dto.RepportListDTO
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
ApprovalSvc approvalService.ApprovalService
}
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository) RepportService {
// Initialize with dummy data
now := time.Now()
dummyData := map[uint]dto.RepportListDTO{
1: {
Id: 1,
Name: "Sales Report",
CreatedAt: now,
UpdatedAt: now,
},
2: {
Id: 2,
Name: "Inventory Report",
CreatedAt: now,
UpdatedAt: now,
},
3: {
Id: 3,
Name: "Production Report",
CreatedAt: now,
UpdatedAt: now,
},
}
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, approvalSvc approvalService.ApprovalService) RepportService {
return &repportService{
Log: utils.Log,
Validate: validate,
dummyData: dummyData,
ExpenseRealizationRepo: expenseRealizationRepo,
ApprovalSvc: approvalSvc,
}
}
func (s *repportService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error) {
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
// Convert map to slice
var results []dto.RepportListDTO
for _, v := range s.dummyData {
// Apply search filter if provided
if params.Search != "" && !strings.Contains(strings.ToLower(v.Name), strings.ToLower(params.Search)) {
continue
}
results = append(results, v)
}
// Apply pagination
total := int64(len(results))
offset := (params.Page - 1) * params.Limit
if offset >= int(total) {
return []dto.RepportListDTO{}, total, nil
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
}
end := offset + params.Limit
if end > int(total) {
end = int(total)
result := dto.ToRepportExpenseListDTOs(realizations)
expenseIDs := make([]uint, 0, len(result))
for i := range result {
expenseIDs = append(expenseIDs, uint(result[i].Id))
}
return results[offset:end], total, nil
}
func (s *repportService) GetOne(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
}
func (s *repportService) GetExpense(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
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
}