feat(BE-48): auto-create product_warehouse on stock adjustment & remove unused APIs

- Change logic: automatically create product_warehouse if it does not exist during stock adjustment
- Remove unnecessary/unused API endpoints
- Ensure adjustment process continues even if product_warehouse was not previously available
This commit is contained in:
aguhh18
2025-10-13 11:38:05 +07:00
parent ce28429efd
commit 5283aed996
8 changed files with 69 additions and 207 deletions
@@ -17,9 +17,6 @@ import (
type ProductWarehouseService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProductWarehouse, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProductWarehouse, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProductWarehouse, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type productWarehouseService struct {
@@ -79,125 +76,3 @@ func (s productWarehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductW
}
return productWarehouse, nil
}
func (s *productWarehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.ProductWarehouse, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
isProductExist, err := s.Repository.IsProductExist(c.Context(), req.ProductId)
if err != nil {
s.Log.Errorf("Failed to check product existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check product existence")
}
if !isProductExist {
return nil, fiber.NewError(fiber.StatusBadRequest, "Product not found")
}
isWarehouseExist, err := s.Repository.IsWarehouseExist(c.Context(), req.WarehouseId)
if err != nil {
s.Log.Errorf("Failed to check warehouse existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse existence")
}
if !isWarehouseExist {
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse not found")
}
// chceking if productWarehouse with same product_id and warehouse_id already
exists, err := s.Repository.ProductWarehouseExists(c.Context(), req.ProductId, req.WarehouseId, nil)
if err != nil {
s.Log.Errorf("Failed to check productWarehouse existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check productWarehouse existence")
}
if exists {
return nil, fiber.NewError(fiber.StatusConflict, "ProductWarehouse already exists")
}
createBody := &entity.ProductWarehouse{
ProductId: req.ProductId,
WarehouseId: req.WarehouseId,
Quantity: req.Quantity,
CreatedBy: 1, // TODO: Ganti dengan user ID dari context setelah middleware auth diimplementasi
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create productWarehouse: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s productWarehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProductWarehouse, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
// validation Id exist
if exists, err := s.Repository.ExistsByID(c.Context(), id); err != nil {
s.Log.Errorf("Failed to check productWarehouse existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check productWarehouse existence")
} else if !exists {
return nil, fiber.NewError(fiber.StatusNotFound, "ProductWarehouse not found")
}
// validation productId and warehouseId exist
if req.ProductId != nil {
isProductExist, err := s.Repository.IsProductExist(c.Context(), *req.ProductId)
if err != nil {
s.Log.Errorf("Failed to check product existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check product existence")
}
if !isProductExist {
return nil, fiber.NewError(fiber.StatusBadRequest, "Product not found")
}
}
if req.WarehouseId != nil {
isWarehouseExist, err := s.Repository.IsWarehouseExist(c.Context(), *req.WarehouseId)
if err != nil {
s.Log.Errorf("Failed to check warehouse existence: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse existence")
}
if !isWarehouseExist {
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
}
}
updateBody := make(map[string]any)
if req.ProductId != nil {
updateBody["product_id"] = *req.ProductId
}
if req.WarehouseId != nil {
updateBody["warehouse_id"] = *req.WarehouseId
}
if req.Quantity != nil {
updateBody["quantity"] = *req.Quantity
}
if len(updateBody) == 0 {
return s.GetOne(c, id)
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "ProductWarehouse not found")
}
s.Log.Errorf("Failed to update productWarehouse: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s productWarehouseService) DeleteOne(c *fiber.Ctx, id uint) error {
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "ProductWarehouse not found")
}
s.Log.Errorf("Failed to delete productWarehouse: %+v", err)
return err
}
return nil
}