FIX[BE]: fixing nonstock sometimes isn't appeared on get one

This commit is contained in:
aguhh18
2025-12-03 11:14:26 +07:00
parent e667d88218
commit 31699f4162
3 changed files with 26 additions and 8 deletions
+2 -2
View File
@@ -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"`
}
@@ -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
@@ -46,5 +46,5 @@ 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"`
ProjectBudgets []ProjectBudget `json:"project_budgets" validate:"required_strict,min=1,dive"`
}