Files
orion/app/modules/catalog/services/catalog_features.py
Samir Boulahtit f20266167d
Some checks failed
CI / ruff (push) Failing after 7s
CI / pytest (push) Failing after 1s
CI / architecture (push) Failing after 9s
CI / dependency-scanning (push) Successful in 27s
CI / audit (push) Successful in 8s
CI / docs (push) Has been skipped
fix(lint): auto-fix ruff violations and tune lint rules
- Auto-fixed 4,496 lint issues (import sorting, modern syntax, etc.)
- Added ignore rules for patterns intentional in this codebase:
  E402 (late imports), E712 (SQLAlchemy filters), B904 (raise from),
  SIM108/SIM105/SIM117 (readability preferences)
- Added per-file ignores for tests and scripts
- Excluded broken scripts/rename_terminology.py (has curly quotes)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 23:10:42 +01:00

121 lines
3.4 KiB
Python

# app/modules/catalog/services/catalog_features.py
"""
Catalog feature provider for the billing feature system.
Declares catalog-related billable features (product limits, import/export)
and provides usage tracking queries for feature gating.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from sqlalchemy import func
from app.modules.contracts.features import (
FeatureDeclaration,
FeatureScope,
FeatureType,
FeatureUsage,
)
if TYPE_CHECKING:
from sqlalchemy.orm import Session
class CatalogFeatureProvider:
"""Feature provider for the catalog module.
Declares:
- products_limit: quantitative per-store limit on product count
- product_import_export: binary merchant-level feature for import/export
"""
@property
def feature_category(self) -> str:
return "catalog"
def get_feature_declarations(self) -> list[FeatureDeclaration]:
return [
FeatureDeclaration(
code="products_limit",
name_key="catalog.features.products_limit.name",
description_key="catalog.features.products_limit.description",
category="catalog",
feature_type=FeatureType.QUANTITATIVE,
scope=FeatureScope.STORE,
default_limit=200,
unit_key="catalog.features.products_limit.unit",
ui_icon="package",
display_order=10,
),
FeatureDeclaration(
code="product_import_export",
name_key="catalog.features.product_import_export.name",
description_key="catalog.features.product_import_export.description",
category="catalog",
feature_type=FeatureType.BINARY,
scope=FeatureScope.MERCHANT,
ui_icon="upload-download",
display_order=20,
),
]
def get_store_usage(
self,
db: Session,
store_id: int,
) -> list[FeatureUsage]:
from app.modules.catalog.models.product import Product
count = (
db.query(func.count(Product.id))
.filter(Product.store_id == store_id)
.scalar()
or 0
)
return [
FeatureUsage(
feature_code="products_limit",
current_count=count,
label="Products",
),
]
def get_merchant_usage(
self,
db: Session,
merchant_id: int,
platform_id: int,
) -> list[FeatureUsage]:
from app.modules.catalog.models.product import Product
from app.modules.tenancy.models import Store, StorePlatform
count = (
db.query(func.count(Product.id))
.join(Store, Product.store_id == Store.id)
.join(StorePlatform, Store.id == StorePlatform.store_id)
.filter(
Store.merchant_id == merchant_id,
StorePlatform.platform_id == platform_id,
)
.scalar()
or 0
)
return [
FeatureUsage(
feature_code="products_limit",
current_count=count,
label="Products",
),
]
# Singleton instance for module registration
catalog_feature_provider = CatalogFeatureProvider()
__all__ = [
"CatalogFeatureProvider",
"catalog_feature_provider",
]