mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
fix get detail kandang kosong
This commit is contained in:
@@ -1030,6 +1030,7 @@ func (s *dailyChecklistService) UpdateByPut(c *fiber.Ctx, req *validation.Create
|
||||
}
|
||||
}
|
||||
|
||||
var wasBranchC bool // non-empty_kandang → empty_kandang transition
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.lockKandangForChecklistCreation(tx, req.KandangId); err != nil {
|
||||
return err
|
||||
@@ -1088,6 +1089,23 @@ func (s *dailyChecklistService) UpdateByPut(c *fiber.Ctx, req *validation.Create
|
||||
if err := s.upsertEmptyKandangRange(tx, id, req.KandangId, date, emptyEndDate, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
// Branch C: non-empty → empty_kandang, hard-delete task/progress data
|
||||
if !existingIsEmpty {
|
||||
wasBranchC = true
|
||||
if err := tx.Exec(`
|
||||
DELETE FROM daily_checklist_activity_task_assignments
|
||||
WHERE task_id IN (
|
||||
SELECT id FROM daily_checklist_activity_tasks WHERE checklist_id = ?
|
||||
)`, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("checklist_id = ?", id).Delete(&entity.DailyChecklistActivityTask{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("daily_checklist_id = ?", id).Delete(&entity.DailyChecklistTask{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if existingIsEmpty {
|
||||
updates := map[string]any{
|
||||
"deleted_at": time.Now(),
|
||||
@@ -1108,6 +1126,20 @@ func (s *dailyChecklistService) UpdateByPut(c *fiber.Ctx, req *validation.Create
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Branch C: delete DC documents outside transaction (storage is external)
|
||||
if wasBranchC && s.DocumentSvc != nil {
|
||||
docs, docErr := s.DocumentSvc.ListByTarget(c.Context(), string(utils.DocumentTypeDailyChecklist), uint64(id))
|
||||
if docErr == nil && len(docs) > 0 {
|
||||
docIDs := make([]uint, 0, len(docs))
|
||||
for _, doc := range docs {
|
||||
docIDs = append(docIDs, doc.Id)
|
||||
}
|
||||
if delErr := s.DocumentSvc.DeleteDocuments(c.Context(), docIDs, true); delErr != nil {
|
||||
s.Log.Errorf("Failed to delete documents for DC %d during empty_kandang conversion: %+v", id, delErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user