feat[BE]: add flock response to project flock and projectflockkandang getone and getall API

This commit is contained in:
aguhh18
2025-11-11 12:16:39 +07:00
parent fd0943dfaf
commit 6b5d27ae8e
7 changed files with 220 additions and 58 deletions
@@ -6,9 +6,12 @@ import (
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
flockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/validations"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
@@ -18,8 +21,8 @@ import (
)
type ProjectFlockKandangService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, error)
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, map[uint]*flockDTO.FlockBaseDTO, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, *flockDTO.FlockBaseDTO, error)
}
// Note: map[uint]float64 adalah mapping dari ProductWarehouse ID ke calculated available quantity
@@ -32,9 +35,10 @@ type projectFlockKandangService struct {
WarehouseRepo rWarehouse.WarehouseRepository
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
PopulationRepo repository.ProjectFlockPopulationRepository
FlockRepo flockRepository.FlockRepository
}
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, validate *validator.Validate) ProjectFlockKandangService {
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, flockRepo flockRepository.FlockRepository, validate *validator.Validate) ProjectFlockKandangService {
return &projectFlockKandangService{
Log: utils.Log,
Validate: validate,
@@ -43,12 +47,13 @@ func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository
WarehouseRepo: warehouseRepo,
ProductWarehouseRepo: productWarehouseRepo,
PopulationRepo: populationRepo,
FlockRepo: flockRepo,
}
}
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error) {
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, map[uint]*flockDTO.FlockBaseDTO, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
return nil, 0, nil, err
}
offset := (params.Page - 1) * params.Limit
@@ -57,7 +62,7 @@ func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Quer
if err != nil {
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
return nil, 0, err
return nil, 0, nil, err
}
if s.ApprovalSvc != nil {
@@ -80,16 +85,36 @@ func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Quer
}
}
return projectFlockKandangs, total, nil
// Fetch Flock master data for each ProjectFlockKandang
flockMap := make(map[uint]*flockDTO.FlockBaseDTO)
for i := range projectFlockKandangs {
if projectFlockKandangs[i].ProjectFlock.Id != 0 {
baseName := pfutils.DeriveBaseName(projectFlockKandangs[i].ProjectFlock.FlockName)
if baseName != "" {
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
} else if flock != nil {
// Convert to DTO and store in map with ProjectFlockId as key
flockMap[projectFlockKandangs[i].ProjectFlock.Id] = &flockDTO.FlockBaseDTO{
Id: flock.Id,
Name: flock.Name,
}
}
}
}
}
return projectFlockKandangs, total, flockMap, nil
}
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, error) {
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, *flockDTO.FlockBaseDTO, error) {
projectFlockKandang, err := s.Repository.GetByID(c.Context(), id)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
return nil, nil, nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
}
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, nil, err
}
if len(projectFlockKandang.Chickins) > 0 && s.ApprovalSvc != nil {
@@ -123,8 +148,23 @@ func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Proje
productWarehouses = []entity.ProductWarehouse{}
}
}
var flockResult *flockDTO.FlockBaseDTO
if projectFlockKandang.ProjectFlock.Id != 0 {
baseName := pfutils.DeriveBaseName(projectFlockKandang.ProjectFlock.FlockName)
if baseName != "" {
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
} else if flock != nil {
flockResult = &flockDTO.FlockBaseDTO{
Id: flock.Id,
Name: flock.Name,
}
}
}
}
return projectFlockKandang, availableQtyMap, productWarehouses, nil
return projectFlockKandang, availableQtyMap, productWarehouses, flockResult, nil
}
func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, projectFlockKandang *entity.ProjectFlockKandang) (map[uint]float64, error) {