update category
This commit is contained in:
parent
91960f0e57
commit
d0378b5ac4
@ -191,7 +191,6 @@ func (h *CategoryHandler) ListCategories(c *gin.Context) {
|
|||||||
req.OutletID = &outletID
|
req.OutletID = &outletID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validationError, validationErrorCode := h.categoryValidator.ValidateListCategoriesRequest(req)
|
validationError, validationErrorCode := h.categoryValidator.ValidateListCategoriesRequest(req)
|
||||||
if validationError != nil {
|
if validationError != nil {
|
||||||
logger.FromContext(ctx).WithError(validationError).Error("CategoryHandler::ListCategories -> request validation failed")
|
logger.FromContext(ctx).WithError(validationError).Error("CategoryHandler::ListCategories -> request validation failed")
|
||||||
|
|||||||
@ -39,6 +39,7 @@ type ProductRepository interface {
|
|||||||
ExistsBySKU(ctx context.Context, organizationID uuid.UUID, sku string, excludeID *uuid.UUID) (bool, error)
|
ExistsBySKU(ctx context.Context, organizationID uuid.UUID, sku string, excludeID *uuid.UUID) (bool, error)
|
||||||
GetByName(ctx context.Context, organizationID uuid.UUID, name string) (*entities.Product, error)
|
GetByName(ctx context.Context, organizationID uuid.UUID, name string) (*entities.Product, error)
|
||||||
ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error)
|
ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error)
|
||||||
|
ExistsByNameInOutlet(ctx context.Context, organizationID uuid.UUID, outletID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error)
|
||||||
UpdateActiveStatus(ctx context.Context, id uuid.UUID, isActive bool) error
|
UpdateActiveStatus(ctx context.Context, id uuid.UUID, isActive bool) error
|
||||||
GetLowCostProducts(ctx context.Context, organizationID uuid.UUID, maxCost float64) ([]*entities.Product, error)
|
GetLowCostProducts(ctx context.Context, organizationID uuid.UUID, maxCost float64) ([]*entities.Product, error)
|
||||||
}
|
}
|
||||||
@ -79,12 +80,12 @@ func (p *ProductProcessorImpl) CreateProduct(ctx context.Context, req *models.Cr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := p.productRepo.ExistsByName(ctx, req.OrganizationID, req.Name, nil)
|
exists, err := p.productRepo.ExistsByNameInOutlet(ctx, req.OrganizationID, req.OutletID, req.Name, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to check product name uniqueness: %w", err)
|
return nil, fmt.Errorf("failed to check product name uniqueness: %w", err)
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return nil, fmt.Errorf("product with name '%s' already exists for this organization", req.Name)
|
return nil, fmt.Errorf("product with name '%s' already exists for this outlet", req.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
productEntity := mappers.CreateProductRequestToEntity(req)
|
productEntity := mappers.CreateProductRequestToEntity(req)
|
||||||
@ -173,12 +174,12 @@ func (p *ProductProcessorImpl) UpdateProduct(ctx context.Context, id uuid.UUID,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if req.Name != nil && *req.Name != existingProduct.Name {
|
if req.Name != nil && *req.Name != existingProduct.Name {
|
||||||
exists, err := p.productRepo.ExistsByName(ctx, existingProduct.OrganizationID, *req.Name, &id)
|
exists, err := p.productRepo.ExistsByNameInOutlet(ctx, existingProduct.OrganizationID, req.OutletID, *req.Name, &id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to check product name uniqueness: %w", err)
|
return nil, fmt.Errorf("failed to check product name uniqueness: %w", err)
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return nil, fmt.Errorf("product with name '%s' already exists for this organization", *req.Name)
|
return nil, fmt.Errorf("product with name '%s' already exists for this outlet", *req.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -72,6 +72,9 @@ func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]in
|
|||||||
case "search":
|
case "search":
|
||||||
searchValue := "%" + value.(string) + "%"
|
searchValue := "%" + value.(string) + "%"
|
||||||
query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
|
query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
|
||||||
|
case "outlet_id":
|
||||||
|
// Include outlet-specific categories AND global categories (outlet_id IS NULL)
|
||||||
|
query = query.Where("outlet_id = ? OR outlet_id IS NULL", value)
|
||||||
default:
|
default:
|
||||||
query = query.Where(key+" = ?", value)
|
query = query.Where(key+" = ?", value)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -178,6 +178,26 @@ func (r *ProductRepositoryImpl) ExistsByName(ctx context.Context, organizationID
|
|||||||
return count > 0, err
|
return count > 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExistsByNameInOutlet checks name uniqueness scoped to a specific outlet via product_outlet_prices.
|
||||||
|
// Falls back to organization-scoped check when outletID is zero.
|
||||||
|
func (r *ProductRepositoryImpl) ExistsByNameInOutlet(ctx context.Context, organizationID uuid.UUID, outletID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error) {
|
||||||
|
if outletID == uuid.Nil {
|
||||||
|
return r.ExistsByName(ctx, organizationID, name, excludeID)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := r.db.WithContext(ctx).Model(&entities.Product{}).
|
||||||
|
Joins("INNER JOIN product_outlet_prices pop ON pop.product_id = products.id AND pop.outlet_id = ?", outletID).
|
||||||
|
Where("products.organization_id = ? AND products.name = ?", organizationID, name)
|
||||||
|
|
||||||
|
if excludeID != nil {
|
||||||
|
query = query.Where("products.id != ?", *excludeID)
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
err := query.Count(&count).Error
|
||||||
|
return count > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
func (r *ProductRepositoryImpl) UpdateActiveStatus(ctx context.Context, id uuid.UUID, isActive bool) error {
|
func (r *ProductRepositoryImpl) UpdateActiveStatus(ctx context.Context, id uuid.UUID, isActive bool) error {
|
||||||
return r.db.WithContext(ctx).Model(&entities.Product{}).
|
return r.db.WithContext(ctx).Model(&entities.Product{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user