refactor(arch): eliminate all cross-module model imports in service layer
Some checks failed
Some checks failed
Enforce MOD-025/MOD-026 rules: zero top-level cross-module model imports remain in any service file. All 66 files migrated using deferred import patterns (method-body, _get_model() helpers, instance-cached self._Model) and new cross-module service methods in tenancy. Documentation updated with Pattern 6 (deferred imports), migration plan marked complete, and violations status reflects 84→0 service-layer violations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,10 +10,12 @@ Handles:
|
||||
- PDF generation (via separate module)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import UTC, datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from sqlalchemy import and_, func
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -36,7 +38,9 @@ from app.modules.orders.schemas.invoice import (
|
||||
StoreInvoiceSettingsCreate,
|
||||
StoreInvoiceSettingsUpdate,
|
||||
)
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -143,18 +143,20 @@ class OrderFeatureProvider:
|
||||
platform_id: int,
|
||||
) -> list[FeatureUsage]:
|
||||
from app.modules.orders.models.order import Order
|
||||
from app.modules.tenancy.models import Store, StorePlatform
|
||||
from app.modules.tenancy.services.platform_service import platform_service
|
||||
from app.modules.tenancy.services.store_service import store_service
|
||||
|
||||
now = datetime.now(UTC)
|
||||
period_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
merchant_stores = store_service.get_stores_by_merchant_id(db, merchant_id)
|
||||
platform_store_ids = platform_service.get_store_ids_for_platform(db, platform_id)
|
||||
store_ids = [s.id for s in merchant_stores if s.id in platform_store_ids]
|
||||
|
||||
count = (
|
||||
db.query(func.count(Order.id))
|
||||
.join(Store, Order.store_id == Store.id)
|
||||
.join(StorePlatform, Store.id == StorePlatform.store_id)
|
||||
.filter(
|
||||
Store.merchant_id == merchant_id,
|
||||
StorePlatform.platform_id == platform_id,
|
||||
Order.store_id.in_(store_ids),
|
||||
Order.created_at >= period_start,
|
||||
)
|
||||
.scalar()
|
||||
|
||||
@@ -18,11 +18,6 @@ from app.modules.inventory.exceptions import (
|
||||
InsufficientInventoryException,
|
||||
InventoryNotFoundException,
|
||||
)
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
from app.modules.inventory.models.inventory_transaction import (
|
||||
InventoryTransaction,
|
||||
TransactionType,
|
||||
)
|
||||
from app.modules.inventory.schemas.inventory import InventoryReserve
|
||||
from app.modules.inventory.services.inventory_service import inventory_service
|
||||
from app.modules.orders.exceptions import (
|
||||
@@ -61,6 +56,8 @@ class OrderInventoryService:
|
||||
"""
|
||||
Find the location with available inventory for a product.
|
||||
"""
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
|
||||
inventory = (
|
||||
db.query(Inventory)
|
||||
.filter(
|
||||
@@ -83,13 +80,17 @@ class OrderInventoryService:
|
||||
db: Session,
|
||||
store_id: int,
|
||||
product_id: int,
|
||||
inventory: Inventory,
|
||||
transaction_type: TransactionType,
|
||||
inventory,
|
||||
transaction_type,
|
||||
quantity_change: int,
|
||||
order: Order,
|
||||
reason: str | None = None,
|
||||
) -> InventoryTransaction:
|
||||
):
|
||||
"""Create an inventory transaction record for audit trail."""
|
||||
from app.modules.inventory.models.inventory_transaction import (
|
||||
InventoryTransaction,
|
||||
)
|
||||
|
||||
transaction = InventoryTransaction.create_transaction(
|
||||
store_id=store_id,
|
||||
product_id=product_id,
|
||||
@@ -116,6 +117,7 @@ class OrderInventoryService:
|
||||
skip_missing: bool = True,
|
||||
) -> dict:
|
||||
"""Reserve inventory for all items in an order."""
|
||||
from app.modules.inventory.models.inventory_transaction import TransactionType
|
||||
order = self.get_order_with_items(db, store_id, order_id)
|
||||
|
||||
reserved_count = 0
|
||||
@@ -199,6 +201,8 @@ class OrderInventoryService:
|
||||
skip_missing: bool = True,
|
||||
) -> dict:
|
||||
"""Fulfill (deduct) inventory when an order is shipped."""
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
from app.modules.inventory.models.inventory_transaction import TransactionType
|
||||
order = self.get_order_with_items(db, store_id, order_id)
|
||||
|
||||
fulfilled_count = 0
|
||||
@@ -304,6 +308,8 @@ class OrderInventoryService:
|
||||
skip_missing: bool = True,
|
||||
) -> dict:
|
||||
"""Fulfill (deduct) inventory for a specific order item."""
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
from app.modules.inventory.models.inventory_transaction import TransactionType
|
||||
order = self.get_order_with_items(db, store_id, order_id)
|
||||
|
||||
item = None
|
||||
@@ -430,6 +436,9 @@ class OrderInventoryService:
|
||||
skip_missing: bool = True,
|
||||
) -> dict:
|
||||
"""Release reserved inventory when an order is cancelled."""
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
from app.modules.inventory.models.inventory_transaction import TransactionType
|
||||
|
||||
order = self.get_order_with_items(db, store_id, order_id)
|
||||
|
||||
released_count = 0
|
||||
|
||||
@@ -16,7 +16,6 @@ from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from app.modules.catalog.exceptions import ProductNotFoundException
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.orders.exceptions import (
|
||||
ExceptionAlreadyResolvedException,
|
||||
InvalidProductForExceptionException,
|
||||
@@ -211,12 +210,14 @@ class OrderItemExceptionService:
|
||||
store_id: int | None = None,
|
||||
) -> OrderItemException:
|
||||
"""Resolve an exception by assigning a product."""
|
||||
from app.modules.catalog.services.product_service import product_service
|
||||
|
||||
exception = self.get_exception_by_id(db, exception_id, store_id)
|
||||
|
||||
if exception.status == "resolved":
|
||||
raise ExceptionAlreadyResolvedException(exception_id)
|
||||
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
raise ProductNotFoundException(product_id)
|
||||
|
||||
@@ -310,7 +311,9 @@ class OrderItemExceptionService:
|
||||
if not pending:
|
||||
return []
|
||||
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
from app.modules.catalog.services.product_service import product_service
|
||||
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
logger.warning(f"Product {product_id} not found for auto-match")
|
||||
return []
|
||||
@@ -415,7 +418,9 @@ class OrderItemExceptionService:
|
||||
notes: str | None = None,
|
||||
) -> int:
|
||||
"""Bulk resolve all pending exceptions for a GTIN."""
|
||||
product = db.query(Product).filter(Product.id == product_id).first()
|
||||
from app.modules.catalog.services.product_service import product_service
|
||||
|
||||
product = product_service.get_product_by_id(db, product_id)
|
||||
if not product:
|
||||
raise ProductNotFoundException(product_id)
|
||||
|
||||
|
||||
@@ -177,18 +177,11 @@ class OrderMetricsProvider:
|
||||
Aggregates order data across all stores.
|
||||
"""
|
||||
from app.modules.orders.models import Order
|
||||
from app.modules.tenancy.models import StorePlatform
|
||||
from app.modules.tenancy.services.platform_service import platform_service
|
||||
|
||||
try:
|
||||
# Get all store IDs for this platform using StorePlatform junction table
|
||||
store_ids = (
|
||||
db.query(StorePlatform.store_id)
|
||||
.filter(
|
||||
StorePlatform.platform_id == platform_id,
|
||||
StorePlatform.is_active == True,
|
||||
)
|
||||
.subquery()
|
||||
)
|
||||
# Get all store IDs for this platform via platform service
|
||||
store_ids = platform_service.get_store_ids_for_platform(db, platform_id)
|
||||
|
||||
# Total orders
|
||||
total_orders = (
|
||||
|
||||
@@ -27,14 +27,8 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.billing.exceptions import TierLimitExceededException
|
||||
from app.modules.billing.services.subscription_service import subscription_service
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.customers.exceptions import CustomerNotFoundException
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.inventory.exceptions import InsufficientInventoryException
|
||||
from app.modules.marketplace.models import ( # IMPORT-002
|
||||
MarketplaceProduct,
|
||||
MarketplaceProductTranslation,
|
||||
)
|
||||
from app.modules.orders.exceptions import (
|
||||
OrderNotFoundException,
|
||||
OrderValidationException,
|
||||
@@ -44,7 +38,6 @@ from app.modules.orders.schemas.order import (
|
||||
OrderCreate,
|
||||
OrderUpdate,
|
||||
)
|
||||
from app.modules.tenancy.models import Store
|
||||
from app.utils.money import Money, cents_to_euros, euros_to_cents
|
||||
from app.utils.vat import (
|
||||
VATResult,
|
||||
@@ -135,10 +128,16 @@ class OrderService:
|
||||
self,
|
||||
db: Session,
|
||||
store_id: int,
|
||||
) -> Product:
|
||||
):
|
||||
"""
|
||||
Get or create the store's placeholder product for unmatched items.
|
||||
"""
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.marketplace.models import (
|
||||
MarketplaceProduct,
|
||||
MarketplaceProductTranslation,
|
||||
)
|
||||
|
||||
# Check for existing placeholder product for this store
|
||||
placeholder = (
|
||||
db.query(Product)
|
||||
@@ -217,47 +216,27 @@ class OrderService:
|
||||
last_name: str,
|
||||
phone: str | None = None,
|
||||
is_active: bool = False,
|
||||
) -> Customer:
|
||||
):
|
||||
"""
|
||||
Find existing customer by email or create new one.
|
||||
"""
|
||||
# Look for existing customer by email within store scope
|
||||
customer = (
|
||||
db.query(Customer)
|
||||
.filter(
|
||||
and_(
|
||||
Customer.store_id == store_id,
|
||||
Customer.email == email,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
from app.modules.customers.services.customer_service import customer_service
|
||||
|
||||
# Look for existing customer by email within store scope
|
||||
customer = customer_service.get_customer_by_email(db, store_id, email)
|
||||
if customer:
|
||||
return customer
|
||||
|
||||
# Generate a unique customer number
|
||||
timestamp = datetime.now(UTC).strftime("%Y%m%d%H%M%S")
|
||||
random_suffix = "".join(random.choices(string.digits, k=4))
|
||||
customer_number = f"CUST-{store_id}-{timestamp}-{random_suffix}"
|
||||
|
||||
# Create new customer
|
||||
customer = Customer(
|
||||
store_id=store_id,
|
||||
email=email,
|
||||
# Create new customer via customer service
|
||||
customer = customer_service.create_customer_for_enrollment(
|
||||
db, store_id, email,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
phone=phone,
|
||||
customer_number=customer_number,
|
||||
hashed_password="",
|
||||
is_active=is_active,
|
||||
)
|
||||
db.add(customer)
|
||||
db.flush()
|
||||
|
||||
logger.info(
|
||||
f"Created {'active' if is_active else 'inactive'} customer "
|
||||
f"{customer.id} for store {store_id}: {email}"
|
||||
f"Created customer {customer.id} for store {store_id}: {email}"
|
||||
)
|
||||
|
||||
return customer
|
||||
@@ -279,20 +258,12 @@ class OrderService:
|
||||
subscription_service.check_order_limit(db, store_id)
|
||||
|
||||
try:
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.customers.services.customer_service import customer_service
|
||||
|
||||
# Get or create customer
|
||||
if order_data.customer_id:
|
||||
customer = (
|
||||
db.query(Customer)
|
||||
.filter(
|
||||
and_(
|
||||
Customer.id == order_data.customer_id,
|
||||
Customer.store_id == store_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not customer:
|
||||
raise CustomerNotFoundException(str(order_data.customer_id))
|
||||
customer = customer_service.get_customer(db, store_id, order_data.customer_id)
|
||||
else:
|
||||
# Create customer from snapshot
|
||||
customer = self.find_or_create_customer(
|
||||
@@ -481,6 +452,7 @@ class OrderService:
|
||||
"""
|
||||
Create an order from Letzshop shipment data.
|
||||
"""
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.orders.services.order_item_exception_service import (
|
||||
order_item_exception_service,
|
||||
)
|
||||
@@ -1097,7 +1069,8 @@ class OrderService:
|
||||
search: str | None = None,
|
||||
) -> tuple[list[dict], int]:
|
||||
"""Get orders across all stores for admin."""
|
||||
query = db.query(Order).join(Store)
|
||||
from sqlalchemy.orm import joinedload
|
||||
query = db.query(Order).options(joinedload(Order.store))
|
||||
|
||||
if store_id:
|
||||
query = query.filter(Order.store_id == store_id)
|
||||
@@ -1234,28 +1207,31 @@ class OrderService:
|
||||
|
||||
def get_stores_with_orders_admin(self, db: Session) -> list[dict]:
|
||||
"""Get list of stores that have orders (admin only)."""
|
||||
results = (
|
||||
from app.modules.tenancy.services.store_service import store_service
|
||||
|
||||
# Get store IDs with order counts
|
||||
store_order_counts = (
|
||||
db.query(
|
||||
Store.id,
|
||||
Store.name,
|
||||
Store.store_code,
|
||||
Order.store_id,
|
||||
func.count(Order.id).label("order_count"),
|
||||
)
|
||||
.join(Order, Order.store_id == Store.id)
|
||||
.group_by(Store.id, Store.name, Store.store_code)
|
||||
.group_by(Order.store_id)
|
||||
.order_by(func.count(Order.id).desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
return [
|
||||
{
|
||||
"id": row.id,
|
||||
"name": row.name,
|
||||
"store_code": row.store_code,
|
||||
"order_count": row.order_count,
|
||||
}
|
||||
for row in results
|
||||
]
|
||||
result = []
|
||||
for store_id, order_count in store_order_counts:
|
||||
store = store_service.get_store_by_id_optional(db, store_id)
|
||||
if store:
|
||||
result.append({
|
||||
"id": store.id,
|
||||
"name": store.name,
|
||||
"store_code": store.store_code,
|
||||
"order_count": order_count,
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
def mark_as_shipped_admin(
|
||||
self,
|
||||
@@ -1324,5 +1300,65 @@ class OrderService:
|
||||
}
|
||||
|
||||
|
||||
# ========================================================================
|
||||
# Cross-module public API methods
|
||||
# ========================================================================
|
||||
|
||||
def get_order_by_id(
|
||||
self, db: Session, order_id: int, store_id: int | None = None
|
||||
) -> Order | None:
|
||||
"""
|
||||
Get order by ID, optionally scoped to a store.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
order_id: Order ID
|
||||
store_id: Optional store scope
|
||||
|
||||
Returns:
|
||||
Order object or None
|
||||
"""
|
||||
query = db.query(Order).filter(Order.id == order_id)
|
||||
if store_id is not None:
|
||||
query = query.filter(Order.store_id == store_id)
|
||||
return query.first()
|
||||
|
||||
def get_store_order_count(self, db: Session, store_id: int) -> int:
|
||||
"""
|
||||
Count orders for a store.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
|
||||
Returns:
|
||||
Order count
|
||||
"""
|
||||
return (
|
||||
db.query(func.count(Order.id))
|
||||
.filter(Order.store_id == store_id)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
def get_total_order_count(
|
||||
self, db: Session, date_from: "datetime | None" = None
|
||||
) -> int:
|
||||
"""
|
||||
Get total order count, optionally filtered by date.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
date_from: Optional start date filter
|
||||
|
||||
Returns:
|
||||
Total order count
|
||||
"""
|
||||
query = db.query(func.count(Order.id))
|
||||
if date_from is not None:
|
||||
query = query.filter(Order.created_at >= date_from)
|
||||
return query.scalar() or 0
|
||||
|
||||
|
||||
# Create service instance
|
||||
order_service = OrderService()
|
||||
|
||||
Reference in New Issue
Block a user