Files
alex 6c86f0f571 TT-003: complete request-module integration into modular platform
- integrated request-module into module registry
- moved backend into modules/request-module
- added manifest.json and module.py
- implemented dynamic router mounting
- implemented dynamic static mounting
- removed direct dependencies from platform core
- verified startup with request-module
- verified startup without request-module
- added implementation_report_003.md
2026-08-06 16:31:27 +03:00

89 lines
3.1 KiB
Python

"""User service - platform integration."""
from typing import List, Optional, Tuple
from sqlalchemy.orm import Session
from datetime import datetime
from request_app.models.user import User
from request_app.models.extra import UserDepartment
from request_app.core.platform_client import PlatformUser, get_platform_client
class UserService:
"""User service - delegates to platform for user data."""
def __init__(self, db: Session):
self.db = db
def get_user_by_id(self, user_id: int) -> User:
"""Get local user cache by ID."""
user = self.db.query(User).filter(User.id == user_id).first()
if not user:
raise Exception(f"User with id {user_id} not found in local cache")
return user
def get_or_create_user_cache(self, user_id: int, email: str = "", first_name: str = "", last_name: str = "") -> User:
"""Get or create local user cache entry."""
user = self.db.query(User).filter(User.id == user_id).first()
if not user:
user = User(
id=user_id,
email=email,
first_name=first_name,
last_name=last_name,
is_active=True,
synced_at=datetime.utcnow(),
)
self.db.add(user)
self.db.commit()
self.db.refresh(user)
return user
def sync_user_from_platform(self, platform_user: PlatformUser) -> User:
"""Sync platform user data to local cache."""
user = self.db.query(User).filter(User.id == platform_user.id).first()
if not user:
user = User(id=platform_user.id)
self.db.add(user)
user.email = platform_user.email
user.first_name = platform_user.first_name
user.last_name = platform_user.last_name
user.is_active = platform_user.is_active
user.synced_at = datetime.utcnow()
self.db.commit()
self.db.refresh(user)
return user
def get_user_departments(self, user_id: int) -> List[UserDepartment]:
"""Get user departments (local)."""
return self.db.query(UserDepartment).filter(UserDepartment.user_id == user_id).all()
def assign_department(self, user_id: int, department_id: int) -> UserDepartment:
"""Assign user to department (local)."""
existing = self.db.query(UserDepartment).filter(
UserDepartment.user_id == user_id,
UserDepartment.department_id == department_id,
).first()
if existing:
return existing
ud = UserDepartment(user_id=user_id, department_id=department_id)
self.db.add(ud)
self.db.commit()
self.db.refresh(ud)
return ud
def remove_department(self, user_id: int, department_id: int) -> bool:
"""Remove user from department (local)."""
ud = self.db.query(UserDepartment).filter(
UserDepartment.user_id == user_id,
UserDepartment.department_id == department_id,
).first()
if ud:
self.db.delete(ud)
self.db.commit()
return True
return False