mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
add feature edit periode project flock
This commit is contained in:
@@ -1225,12 +1225,34 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
|
||||
}
|
||||
}
|
||||
|
||||
// Hitung newFlockName sebelum membuka transaksi (fast-path conflict check)
|
||||
var newFlockName string
|
||||
if req.Periode != nil {
|
||||
lastSpace := strings.LastIndex(existing.FlockName, " ")
|
||||
if lastSpace < 0 {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Format flock name tidak valid")
|
||||
}
|
||||
baseName := strings.TrimSpace(existing.FlockName[:lastSpace])
|
||||
newFlockName = fmt.Sprintf("%s %03d", baseName, *req.Periode)
|
||||
|
||||
taken, err := s.Repository.ExistsByFlockName(c.Context(), newFlockName, &id)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
|
||||
}
|
||||
if taken {
|
||||
return nil, fiber.NewError(fiber.StatusConflict,
|
||||
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
|
||||
}
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
var period int = 1
|
||||
if len(existing.KandangHistory) > 0 {
|
||||
if req.Periode != nil {
|
||||
period = *req.Periode
|
||||
} else if len(existing.KandangHistory) > 0 {
|
||||
period = existing.KandangHistory[0].Period
|
||||
}
|
||||
|
||||
@@ -1242,6 +1264,40 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id
|
||||
if err := s.attachKandangs(c.Context(), dbTransaction, existing.Id, kandangIDs, periods); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update period pada SEMUA row project_flock_kandangs milik flock ini.
|
||||
// attachKandangs hanya INSERT baris baru dan melewati yang sudah ada,
|
||||
// sehingga period pada baris lama tidak terupdate tanpa langkah ini.
|
||||
if req.Periode != nil {
|
||||
if err := dbTransaction.WithContext(c.Context()).
|
||||
Model(&entity.ProjectFlockKandang{}).
|
||||
Where("project_flock_id = ?", existing.Id).
|
||||
Update("period", *req.Periode).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui periode kandang")
|
||||
}
|
||||
}
|
||||
|
||||
// Update flock_name sesuai periode baru.
|
||||
if req.Periode != nil {
|
||||
projectRepoTx := repository.NewProjectflockRepository(dbTransaction)
|
||||
|
||||
// Re-check di dalam transaksi untuk cegah race condition.
|
||||
taken, err := projectRepoTx.ExistsByFlockName(c.Context(), newFlockName, &existing.Id)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa nama flock")
|
||||
}
|
||||
if taken {
|
||||
return fiber.NewError(fiber.StatusConflict,
|
||||
fmt.Sprintf("Nama flock '%s' sudah digunakan oleh project flock lain", newFlockName))
|
||||
}
|
||||
|
||||
if err := projectRepoTx.PatchOne(c.Context(), existing.Id, map[string]any{
|
||||
"flock_name": newFlockName,
|
||||
}, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memperbarui nama flock")
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -40,4 +40,5 @@ type ProjectBudget struct {
|
||||
type Resubmit struct {
|
||||
KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
|
||||
Periode *int `json:"periode" validate:"omitempty,gt=0"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user