From 31699f4162667cbaed08b0e003c2570bbe4d1dda Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Wed, 3 Dec 2025 11:14:26 +0700 Subject: [PATCH] FIX[BE]: fixing nonstock sometimes isn't appeared on get one --- internal/entities/project_budget.go | 4 +-- .../services/projectflock.service.go | 26 ++++++++++++++++--- .../validations/projectflock.validation.go | 4 +-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/internal/entities/project_budget.go b/internal/entities/project_budget.go index 1c339bd5..c74455b6 100644 --- a/internal/entities/project_budget.go +++ b/internal/entities/project_budget.go @@ -12,6 +12,6 @@ type ProjectBudget struct { Price float64 `gorm:"type:numeric(15,3);not null"` CreatedAt time.Time `gorm:"autoCreateTime"` - Nonstock *Nonstock `gorm:"foreignKey:Id;references:Id"` - ProjectFlock *ProjectFlock `gorm:"foreignKey:Id;references:Id"` + Nonstock *Nonstock `gorm:"foreignKey:NonstockId;references:Id"` + ProjectFlock *ProjectFlock `gorm:"foreignKey:ProjectFlockId;references:Id"` } diff --git a/internal/modules/production/project_flocks/services/projectflock.service.go b/internal/modules/production/project_flocks/services/projectflock.service.go index f38e60dd..1a7fc6f2 100644 --- a/internal/modules/production/project_flocks/services/projectflock.service.go +++ b/internal/modules/production/project_flocks/services/projectflock.service.go @@ -1086,7 +1086,7 @@ func (s projectflockService) Resubmit(c *fiber.Ctx, req *validation.Resubmit, id return nil, fiber.NewError(fiber.StatusNotFound, "Beberapa kandang tidak ditemukan") } - for _, pb := range req.ProjectBudgts { + for _, pb := range req.ProjectBudgets { if err := commonSvc.EnsureRelations(c.Context(), commonSvc.RelationCheck{Name: "Nonstock", ID: &pb.NonstockId, Exists: s.NonstockRepo.IdExists}, ); err != nil { @@ -1111,7 +1111,7 @@ 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 } - if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgts); err != nil { + if err := s.UpsertProjectBudget(c.Context(), dbTransaction, existing.Id, req.ProjectBudgets); err != nil { return err } @@ -1147,9 +1147,27 @@ func (s projectflockService) UpsertProjectBudget(ctx context.Context, dbTransact if len(budgets) == 0 { return nil } - budgetRepo := projectBudgetRepository.NewProjectBudgetRepository(dbTransaction) + nonstockMap := make(map[uint]bool) + relationChecks := make([]commonSvc.RelationCheck, 0, len(budgets)) + for _, b := range budgets { + if nonstockMap[b.NonstockId] { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Duplicate nonstock_id: %d", b.NonstockId)) + } + nonstockMap[b.NonstockId] = true + nonstockID := b.NonstockId + relationChecks = append(relationChecks, commonSvc.RelationCheck{ + Name: "Nonstock", + ID: &nonstockID, + Exists: s.NonstockRepo.IdExists, + }) + } + + if err := commonSvc.EnsureRelations(ctx, relationChecks...); err != nil { + return err + } + if err := budgetRepo.DeleteMany(ctx, func(q *gorm.DB) *gorm.DB { return q.Where("project_flock_id = ?", projectFlockID) }); err != nil && err != gorm.ErrRecordNotFound { @@ -1167,7 +1185,7 @@ func (s projectflockService) UpsertProjectBudget(ctx context.Context, dbTransact } if err := budgetRepo.CreateMany(ctx, records, nil); err != nil { - return err + return fiber.NewError(fiber.StatusInternalServerError, "Failed to save project budgets") } return nil diff --git a/internal/modules/production/project_flocks/validations/projectflock.validation.go b/internal/modules/production/project_flocks/validations/projectflock.validation.go index 607daf26..00b01456 100644 --- a/internal/modules/production/project_flocks/validations/projectflock.validation.go +++ b/internal/modules/production/project_flocks/validations/projectflock.validation.go @@ -45,6 +45,6 @@ type ProjectBudget struct { } type Resubmit struct { - KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"` - ProjectBudgts []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"` + KandangIds []uint `json:"kandang_ids" validate:"required_strict,min=1,dive,gt=0"` + ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"` }