mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
Merge branch 'fix/kandang-kosong' into 'development'
[FIX][BE]: adjust edit kandang kosong DAILY CHECKLIST See merge request mbugroup/lti-api!527
This commit is contained in:
@@ -127,7 +127,6 @@ const (
|
||||
dailyChecklistCategoryEmptyKandang = "empty_kandang"
|
||||
dailyChecklistStatusRejected = "REJECTED"
|
||||
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"
|
||||
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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
if conflictID > 0 {
|
||||
targetID = conflictID
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return s.createOrReuseSingleDailyChecklist(tx, req.KandangId, date, category, status, &targetID)
|
||||
@@ -594,18 +599,25 @@ func (s *dailyChecklistService) validateNoChecklistOverlapForEmptyKandang(tx *go
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kandangID uint, startDate, endDate time.Time) error {
|
||||
var conflictCount int64
|
||||
if err := tx.Model(&entity.DailyChecklist{}).
|
||||
Where("kandang_id = ? AND date BETWEEN ? AND ? AND category = ? AND deleted_at IS NULL", kandangID, startDate, endDate, dailyChecklistCategoryEmptyKandang).
|
||||
Count(&conflictCount).Error; err != nil {
|
||||
func (s *dailyChecklistService) validateNoEmptyKandangConflict(tx *gorm.DB, kandangID uint, startDate, endDate time.Time, newCategory, newStatus string, conflictID *uint) error {
|
||||
var existing entity.DailyChecklist
|
||||
if err := tx.Where("kandang_id = ? AND date BETWEEN ? AND ? AND category = ? AND deleted_at IS NULL",
|
||||
kandangID, startDate, endDate, dailyChecklistCategoryEmptyKandang).
|
||||
First(&existing).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if conflictCount > 0 {
|
||||
return fiber.NewError(fiber.StatusConflict, dailyChecklistErrEmptyKandangExist)
|
||||
if err := tx.Model(&entity.DailyChecklist{}).Where("id = ?", existing.Id).Updates(map[string]interface{}{
|
||||
"category": newCategory,
|
||||
"status": newStatus,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*conflictID = existing.Id
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user