refactor(arch): eliminate all cross-module model imports in service layer
Some checks failed
CI / ruff (push) Successful in 9s
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / pytest (push) Has been cancelled

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:
2026-02-27 06:13:15 +01:00
parent e3a52f6536
commit 86e85a98b8
66 changed files with 2242 additions and 1295 deletions

View File

@@ -12,6 +12,7 @@ import logging
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import func
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
@@ -188,6 +189,74 @@ class TeamService:
logger.error(f"Error removing team member: {str(e)}")
raise TeamValidationException("Failed to remove team member")
# ========================================================================
# Cross-module public API methods
# ========================================================================
def get_store_owner(self, db: Session, store_id: int) -> StoreUser | None:
"""
Get the owner StoreUser for a store.
Args:
db: Database session
store_id: Store ID
Returns:
StoreUser with is_owner=True, or None
"""
return (
db.query(StoreUser)
.filter(
StoreUser.store_id == store_id,
StoreUser.is_owner == True, # noqa: E712
)
.first()
)
def get_active_team_member_count(self, db: Session, store_id: int) -> int:
"""
Count active team members for a store.
Args:
db: Database session
store_id: Store ID
Returns:
Number of active team members
"""
return (
db.query(func.count(StoreUser.id))
.filter(
StoreUser.store_id == store_id,
StoreUser.is_active == True, # noqa: E712
)
.scalar()
or 0
)
def get_store_users_with_user(
self, db: Session, store_id: int, active_only: bool = True
) -> list[tuple[User, StoreUser]]:
"""
Get User and StoreUser pairs for a store.
Args:
db: Database session
store_id: Store ID
active_only: Only active users
Returns:
List of (User, StoreUser) tuples
"""
query = (
db.query(User, StoreUser)
.join(StoreUser, User.id == StoreUser.user_id)
.filter(StoreUser.store_id == store_id)
)
if active_only:
query = query.filter(User.is_active == True) # noqa: E712
return query.all()
def get_store_roles(self, db: Session, store_id: int) -> list[dict[str, Any]]:
"""
Get available roles for store.
@@ -216,5 +285,20 @@ class TeamService:
raise TeamValidationException("Failed to retrieve roles")
def get_total_active_team_member_count(self, db: Session) -> int:
"""
Count active team members across all stores.
Returns:
Total number of active team members platform-wide
"""
return (
db.query(func.count(StoreUser.id))
.filter(StoreUser.is_active == True) # noqa: E712
.scalar()
or 0
)
# Create service instance
team_service = TeamService()