codex/fix: hidden product warehouse depletion and egg <= 0

This commit is contained in:
Adnan Zahir
2026-04-06 22:27:58 +07:00
parent 2a39342d55
commit ba4a5324ed
5 changed files with 154 additions and 10 deletions
@@ -100,6 +100,25 @@ func (s chickinService) withRelations(db *gorm.DB) *gorm.DB {
}
func resolveWarehouseForProjectFlockKandang(ctx context.Context, warehouseRepo rWarehouse.WarehouseRepository, kandangID uint, locationID uint) (*entity.Warehouse, error) {
if warehouseRepo == nil {
return nil, gorm.ErrRecordNotFound
}
if kandangID == 0 {
return nil, gorm.ErrRecordNotFound
}
if locationID != 0 {
warehouse, err := warehouseRepo.GetByKandangIDAndLocationID(ctx, kandangID, locationID)
if err == nil {
return warehouse, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
}
return warehouseRepo.GetByKandangID(ctx, kandangID)
}
func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
@@ -182,7 +201,12 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock Kandang not found")
}
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectFlockKandang.KandangId)
warehouse, err := resolveWarehouseForProjectFlockKandang(
c.Context(),
s.WarehouseRepo,
projectFlockKandang.KandangId,
projectFlockKandang.ProjectFlock.LocationId,
)
if err != nil {
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found")
}