mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 15:55:44 +00:00
feat[BE]: Refactor Chickin create and approvals support chickin growing and chickin laying, and create get one project flock kandang API
This commit is contained in:
+147
@@ -0,0 +1,147 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
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"
|
||||
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"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProjectFlockKandangService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type projectFlockKandangService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectFlockKandangRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
}
|
||||
|
||||
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, validate *validator.Validate) ProjectFlockKandangService {
|
||||
return &projectFlockKandangService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
projectFlockKandangs, err := s.Repository.GetAll(c.Context())
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
total := int64(len(projectFlockKandangs))
|
||||
|
||||
return projectFlockKandangs, total, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error) {
|
||||
projectFlockKandang, err := s.Repository.GetByID(c.Context(), id)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get projectFlockKandang by id: %+v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if len(projectFlockKandang.Chickins) > 0 && s.ApprovalSvc != nil {
|
||||
latest, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, projectFlockKandang.Id, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch latest kandang approval for projectFlockKandang %d: %+v", projectFlockKandang.Id, err)
|
||||
}
|
||||
|
||||
if latest != nil {
|
||||
projectFlockKandang.LatestApproval = latest
|
||||
}
|
||||
}
|
||||
|
||||
availableQtys, err := s.getAvailableQuantities(c, projectFlockKandang)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch available quantities for kandang %d: %+v", projectFlockKandang.Kandang.Id, err)
|
||||
availableQtys = nil
|
||||
}
|
||||
|
||||
return projectFlockKandang, availableQtys, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, projectFlockKandang *entity.ProjectFlockKandang) ([]map[string]interface{}, error) {
|
||||
if projectFlockKandang.Kandang.Id == 0 || s.WarehouseRepo == nil || s.ProductWarehouseRepo == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectFlockKandang.Kandang.Id)
|
||||
if err != nil || warehouse == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var productCategoryCode string
|
||||
if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryGrowing) {
|
||||
productCategoryCode = "DOC"
|
||||
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
productCategoryCode = "PULLET"
|
||||
} else {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
products, err := s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
||||
if err != nil || len(products) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var result []map[string]interface{}
|
||||
for _, pw := range products {
|
||||
if pw.Quantity > 0 {
|
||||
// Build product data
|
||||
productData := map[string]interface{}{
|
||||
"id": pw.Product.Id,
|
||||
"name": pw.Product.Name,
|
||||
}
|
||||
|
||||
// Build warehouse data
|
||||
warehouseData := map[string]interface{}{
|
||||
"id": pw.Warehouse.Id,
|
||||
"name": pw.Warehouse.Name,
|
||||
"type": pw.Warehouse.Type,
|
||||
}
|
||||
|
||||
// Build product warehouse data with nested product and warehouse
|
||||
productWarehouseData := map[string]interface{}{
|
||||
"id": pw.Id,
|
||||
"quantity": pw.Quantity,
|
||||
"product": productData,
|
||||
"warehouse": warehouseData,
|
||||
}
|
||||
|
||||
result = append(result, map[string]interface{}{
|
||||
"available_qty": pw.Quantity,
|
||||
"product_warehouse": productWarehouseData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user