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

This commit is contained in:
Adnan Zahir
2026-04-06 22:27:58 +07:00
committed by giovanni
parent 491fe0abef
commit e6dc658046
5 changed files with 154 additions and 10 deletions
@@ -1075,6 +1075,25 @@ func (s projectflockService) kandangRepoWithTx(tx *gorm.DB) kandangRepository.Ka
return kandangRepository.NewKandangRepository(s.Repository.DB())
}
func resolveWarehouseByKandangAndLocation(ctx context.Context, warehouseRepo warehouseRepository.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 projectflockService) ensureProjectFlockKandangProductWarehouses(ctx context.Context, dbTransaction *gorm.DB, records []*entity.ProjectFlockKandang) error {
if len(records) == 0 {
return nil
@@ -1103,20 +1122,24 @@ func (s projectflockService) ensureProjectFlockKandangProductWarehouses(ctx cont
if dbTransaction != nil {
db = dbTransaction
}
var category string
type projectFlockMeta struct {
Category string `gorm:"column:category"`
LocationId uint `gorm:"column:location_id"`
}
var flockMeta projectFlockMeta
if err := db.WithContext(ctx).
Model(&entity.ProjectFlock{}).
Select("category").
Select("category, location_id").
Where("id = ?", projectFlockID).
Scan(&category).Error; err != nil {
Scan(&flockMeta).Error; err != nil {
return err
}
if strings.TrimSpace(category) == "" {
if strings.TrimSpace(flockMeta.Category) == "" {
return fiber.NewError(fiber.StatusBadRequest, "Project flock category tidak ditemukan")
}
prefixes := []string{"AYAM-"}
if strings.EqualFold(category, string(utils.ProjectFlockCategoryLaying)) {
if strings.EqualFold(flockMeta.Category, string(utils.ProjectFlockCategoryLaying)) {
prefixes = append(prefixes, "TELUR")
}
@@ -1134,7 +1157,7 @@ func (s projectflockService) ensureProjectFlockKandangProductWarehouses(ctx cont
continue
}
warehouse, err := warehouseRepo.GetByKandangID(ctx, record.KandangId)
warehouse, err := resolveWarehouseByKandangAndLocation(ctx, warehouseRepo, record.KandangId, flockMeta.LocationId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Warehouse untuk kandang %d belum tersedia", record.KandangId))