fix(BE): improve product and warehouse existence check in adjustment service

This commit is contained in:
aguhh18
2025-10-17 20:43:31 +07:00
parent 79700420d4
commit a45c20d2ff
2 changed files with 25 additions and 2 deletions
@@ -83,7 +83,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
}
if !isProductExist {
return nil, fiber.NewError(fiber.StatusBadRequest, "Product not found")
return nil, fiber.NewError(fiber.StatusNotFound, "Product not found")
}
isWarehouseExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(req.WarehouseID))
@@ -92,7 +92,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
}
if !isWarehouseExist {
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse not found")
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
}
if req.Quantity <= 0 {
@@ -187,6 +187,24 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
offset := (query.Page - 1) * query.Limit
isWarehousesExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(query.WarehouseID))
if err != nil {
s.Log.Errorf("Failed to check warehouse existence: %+v", err)
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
}
if query.WarehouseID > 0 && !isWarehousesExist {
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
}
isProductsExist, err := s.ProductRepo.IdExists(c.Context(), uint(query.ProductID))
if err != nil {
s.Log.Errorf("Failed to check product existence: %+v", err)
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
}
if query.ProductID > 0 && !isProductsExist {
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
}
stockLogs, total, err := s.StockLogsRepository.GetAll(c.Context(), offset, query.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
@@ -72,6 +72,11 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
s.Log.Errorf("Failed to get productWarehouses: %+v", err)
return nil, 0, err
}
if len(productWarehouses) == 0 {
return nil, 0, fiber.NewError(fiber.StatusNotFound, "ProductWarehouses not found")
}
return productWarehouses, total, nil
}