From 63cf0c6fac41b9fffc22ffd757b81cc5e723817a Mon Sep 17 00:00:00 2001 From: giovanni Date: Fri, 27 Mar 2026 12:11:17 +0700 Subject: [PATCH] add restrict delete master data kandang group --- .../repositories/kandang_group.repository.go | 19 +++++++++++++++++++ .../services/kandang_group.service.go | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/internal/modules/master/kandang-groups/repositories/kandang_group.repository.go b/internal/modules/master/kandang-groups/repositories/kandang_group.repository.go index a7c579b7..8aa58cd1 100644 --- a/internal/modules/master/kandang-groups/repositories/kandang_group.repository.go +++ b/internal/modules/master/kandang-groups/repositories/kandang_group.repository.go @@ -2,6 +2,7 @@ package repository import ( "context" + "errors" "gorm.io/gorm" @@ -14,6 +15,7 @@ type KandangGroupRepository interface { LocationExists(ctx context.Context, locationId uint) (bool, error) PicExists(ctx context.Context, picId uint) (bool, error) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) + HasDailyChecklistRelation(ctx context.Context, kandangGroupId uint) (bool, error) } type KandangGroupRepositoryImpl struct { @@ -39,3 +41,20 @@ func (r *KandangGroupRepositoryImpl) PicExists(ctx context.Context, picId uint) func (r *KandangGroupRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) { return repository.ExistsByName[entity.KandangGroup](ctx, r.db, name, excludeID) } + +func (r *KandangGroupRepositoryImpl) HasDailyChecklistRelation(ctx context.Context, kandangGroupId uint) (bool, error) { + var marker int + err := r.db.WithContext(ctx). + Model(&entity.DailyChecklist{}). + Select("1"). + Where("kandang_id = ?", kandangGroupId). + Limit(1). + Take(&marker).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + if err != nil { + return false, err + } + return true, nil +} diff --git a/internal/modules/master/kandang-groups/services/kandang_group.service.go b/internal/modules/master/kandang-groups/services/kandang_group.service.go index 276c20b5..b569ca97 100644 --- a/internal/modules/master/kandang-groups/services/kandang_group.service.go +++ b/internal/modules/master/kandang-groups/services/kandang_group.service.go @@ -226,6 +226,16 @@ func (s kandangGroupService) DeleteOne(c *fiber.Ctx, id uint) error { if err != nil { return err } + + hasDailyChecklistRelation, err := s.Repository.HasDailyChecklistRelation(c.Context(), id) + if err != nil { + s.Log.Errorf("Failed to check daily checklist relation for kandang group %d: %+v", id, err) + return fiber.NewError(fiber.StatusInternalServerError, "Failed to check kandang group relation") + } + if hasDailyChecklistRelation { + return fiber.NewError(fiber.StatusConflict, "Kandang group tidak boleh dihapus karena masih memiliki relasi daily checklist") + } + if len(kandangGroup.Kandangs) > 0 { return fiber.NewError(fiber.StatusConflict, "Kandang group tidak boleh dihapus karena masih memiliki relasi kandang") }