feat(BE-34): extend DB schema and update master data APIs [partial]

 DB Schema: product_warehouse entity and migration
 Master Data: added filter params to getall APIs
🚧 Pending: stock_logs implementation and adjustment APIs
This commit is contained in:
aguhh18
2025-10-09 13:33:30 +07:00
parent 2d49ffe4cd
commit a0bdc7b23c
33 changed files with 1203 additions and 10 deletions
@@ -0,0 +1,210 @@
package service
import (
"errors"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
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 {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.ProductWarehouseRepository
}
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate) ProductWarehouseService {
return &productWarehouseService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("Product").Preload("Warehouse").Preload("CreatedUser")
}
func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
productWarehouses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.ProductId != 0 {
db = db.Where("product_id = ?", params.ProductId)
}
if params.WarehouseId != 0 {
db = db.Where("warehouse_id = ?", params.WarehouseId)
}
// Search in related product or warehouse names
if params.Search != "" {
db = db.Joins("LEFT JOIN products ON products.id = product_warehouse.product_id").
Joins("LEFT JOIN warehouses ON warehouses.id = product_warehouse.warehouse_id").
Where("products.name ILIKE ? OR warehouses.name ILIKE ?", "%"+params.Search+"%", "%"+params.Search+"%")
}
return db.Order("created_at DESC").Order("updated_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get productWarehouses: %+v", err)
return nil, 0, err
}
return productWarehouses, total, nil
}
func (s productWarehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductWarehouse, error) {
productWarehouse, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "ProductWarehouse not found")
}
if err != nil {
s.Log.Errorf("Failed get productWarehouse by id: %+v", err)
return nil, err
}
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
}