FEAT[BE]: adjust api match with mock API

This commit is contained in:
aguhh18
2025-11-28 15:18:49 +07:00
parent f3b14cb8f2
commit 79c754312e
4 changed files with 48 additions and 50 deletions
@@ -147,8 +147,8 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
return nil, err
}
for _, costPerKandang := range req.CostPerKandangs {
for _, costItem := range costPerKandang.CostItems {
for _, expenseNonstock := range req.ExpenseNonstocks {
for _, costItem := range expenseNonstock.CostItems {
nonstockId := uint(costItem.NonstockID)
if err := commonSvc.EnsureRelations(c.Context(),
@@ -202,15 +202,15 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create expense")
}
if len(req.CostPerKandangs) > 0 {
if len(req.ExpenseNonstocks) > 0 {
for _, costPerKandang := range req.CostPerKandangs {
for _, expenseNonstock := range req.ExpenseNonstocks {
var projectFlockKandangId *uint64
if req.Category == "BOP" {
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(costPerKandang.KandangID))
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
@@ -221,16 +221,16 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
projectFlockKandangId = &id
}
for _, costItem := range costPerKandang.CostItems {
for _, costItem := range expenseNonstock.CostItems {
nonstockId := costItem.NonstockID
var kandangId *uint64
if req.Category == "NON-BOP" {
id := uint64(costPerKandang.KandangID)
id := uint64(expenseNonstock.KandangID)
kandangId = &id
} else if req.Category == "BOP" {
if projectFlockKandangId != nil {
kandangId = &costPerKandang.KandangID
kandangId = &expenseNonstock.KandangID
}
}
@@ -337,7 +337,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
updateBody["supplier_id"] = *req.SupplierID
}
if len(updateBody) == 0 && req.CostPerKandang == nil && len(req.Documents) == 0 {
if len(updateBody) == 0 && req.ExpenseNonstocks == nil && len(req.Documents) == 0 {
responseDTO, err := s.GetOne(c, id)
if err != nil {
@@ -422,7 +422,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
}
}
if req.CostPerKandang != nil {
if req.ExpenseNonstocks != nil {
var existingExpenseNonstocks []entity.ExpenseNonstock
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
@@ -443,12 +443,12 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get updated expense")
}
for _, cpk := range *req.CostPerKandang {
for _, expenseNonstock := range *req.ExpenseNonstocks {
var projectFlockKandangId *uint64
if updatedExpense.Category == "BOP" {
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(cpk.KandangID))
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
@@ -459,7 +459,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
projectFlockKandangId = &id
}
for _, costItem := range cpk.CostItems {
for _, costItem := range expenseNonstock.CostItems {
nonstockId := uint(costItem.NonstockID)
if err := commonSvc.EnsureRelations(c.Context(),
@@ -470,11 +470,11 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
var kandangId *uint64
if updatedExpense.Category == "NON-BOP" {
id := uint64(cpk.KandangID)
id := uint64(expenseNonstock.KandangID)
kandangId = &id
} else if updatedExpense.Category == "BOP" {
if projectFlockKandangId != nil {
kandangId = &cpk.KandangID
kandangId = &expenseNonstock.KandangID
}
}