Merge branch 'feat/BE/Sprint-5' into 'feat/BE/US-159,160/marketing'

# Conflicts:
#   internal/modules/production/project_flocks/controllers/projectflock.controller.go
#   internal/modules/production/project_flocks/dto/projectflock.dto.go
#   internal/modules/production/project_flocks/route.go
#   internal/modules/production/transfer_layings/dto/transfer_laying.dto.go
This commit is contained in:
Hafizh A. Y.
2025-11-20 02:16:12 +00:00
14 changed files with 337 additions and 176 deletions
@@ -7,6 +7,7 @@ import (
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
@@ -120,9 +121,41 @@ func (r *KandangRepositoryImpl) UpsertProjectFlockKandang(ctx context.Context, p
Where("project_flock_id = ? AND kandang_id = ?", projectFlockID, kandangID).
First(&link).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
var project entity.ProjectFlock
if err := r.db.WithContext(ctx).
Select("id, location_id").
First(&project, projectFlockID).Error; err != nil {
return err
}
var kandang entity.Kandang
if err := r.db.WithContext(ctx).
Select("id, location_id").
First(&kandang, kandangID).Error; err != nil {
return err
}
if kandang.LocationId != project.LocationId {
return fiber.NewError(fiber.StatusBadRequest, "Kandang tidak berada pada lokasi yang sama dengan project flock")
}
// Determine project's period from existing pivot rows so the new kandang
// shares the same period.
var period int
if err := r.db.WithContext(ctx).
Table("project_flock_kandangs").
Where("project_flock_id = ?", projectFlockID).
Select("COALESCE(MAX(period), 0)").
Scan(&period).Error; err != nil {
return err
}
if period <= 0 {
period = 1
}
link = entity.ProjectFlockKandang{
ProjectFlockId: projectFlockID,
KandangId: kandangID,
Period: period,
}
return r.db.WithContext(ctx).Create(&link).Error
}