mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
adjust edit kandang kosong
This commit is contained in:
@@ -127,7 +127,6 @@ const (
|
|||||||
dailyChecklistCategoryEmptyKandang = "empty_kandang"
|
dailyChecklistCategoryEmptyKandang = "empty_kandang"
|
||||||
dailyChecklistStatusRejected = "REJECTED"
|
dailyChecklistStatusRejected = "REJECTED"
|
||||||
dailyChecklistStatusDraft = "DRAFT"
|
dailyChecklistStatusDraft = "DRAFT"
|
||||||
dailyChecklistErrEmptyKandangExist = "DailyChecklist cannot be created because empty_kandang already exists for at least one date in range"
|
|
||||||
dailyChecklistErrDateOverlapExist = "DailyChecklist cannot be created because at least one date in range already has a checklist"
|
dailyChecklistErrDateOverlapExist = "DailyChecklist cannot be created because at least one date in range already has a checklist"
|
||||||
dailyChecklistErrDeletedNonEmptyKandangExists = "DailyChecklist cannot be created as empty_kandang because a deleted non-empty_kandang checklist exists for this date"
|
dailyChecklistErrDeletedNonEmptyKandangExists = "DailyChecklist cannot be created as empty_kandang because a deleted non-empty_kandang checklist exists for this date"
|
||||||
)
|
)
|
||||||
@@ -544,9 +543,15 @@ func (s *dailyChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := s.validateNoEmptyKandangConflict(tx, req.KandangId, date, date); err != nil {
|
conflictID := uint(0)
|
||||||
|
|
||||||
|
if err := s.validateNoEmptyKandangConflict(tx, req.KandangId, date, date, category, status, &conflictID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if conflictID > 0 {
|
||||||
|
targetID = conflictID
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.createOrReuseSingleDailyChecklist(tx, req.KandangId, date, category, status, &targetID)
|
return s.createOrReuseSingleDailyChecklist(tx, req.KandangId, date, category, status, &targetID)
|
||||||
@@ -594,18 +599,25 @@ func (s *dailyChecklistService) validateNoChecklistOverlapForEmptyKandang(tx *go
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kandangID uint, startDate, endDate time.Time) error {
|
func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kandangID uint, startDate, endDate time.Time, newCategory, newStatus string, conflictID *uint) error {
|
||||||
var conflictCount int64
|
var existing entity.DailyChecklist
|
||||||
if err := tx.Model(&entity.DailyChecklist{}).
|
if err := tx.Where("kandang_id = ? AND date BETWEEN ? AND ? AND category = ? AND deleted_at IS NULL",
|
||||||
Where("kandang_id = ? AND date BETWEEN ? AND ? AND category = ? AND deleted_at IS NULL", kandangID, startDate, endDate, dailyChecklistCategoryEmptyKandang).
|
kandangID, startDate, endDate, dailyChecklistCategoryEmptyKandang).
|
||||||
Count(&conflictCount).Error; err != nil {
|
First(&existing).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if conflictCount > 0 {
|
if err := tx.Model(&entity.DailyChecklist{}).Where("id = ?", existing.Id).Updates(map[string]interface{}{
|
||||||
return fiber.NewError(fiber.StatusConflict, dailyChecklistErrEmptyKandangExist)
|
"category": newCategory,
|
||||||
|
"status": newStatus,
|
||||||
|
}).Error; err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*conflictID = existing.Id
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user