Initial commit: Robot ökoszisztéma v2.0 - Stabilizált jármű és szerviz robotok
This commit is contained in:
0
backend/app/db/__init__.py
Executable file
0
backend/app/db/__init__.py
Executable file
35
backend/app/db/base.py
Executable file
35
backend/app/db/base.py
Executable file
@@ -0,0 +1,35 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/base.py
|
||||
from app.db.base_class import Base # noqa
|
||||
|
||||
# Közvetlen importok (HOZZÁADVA az audit és sales modellek)
|
||||
from app.models.address import Address, GeoPostalCode, GeoStreet, GeoStreetType, Branch, Rating # noqa
|
||||
|
||||
from app.models.identity import Person, User, Wallet, VerificationToken, SocialAccount # noqa
|
||||
|
||||
from app.models.organization import Organization, OrganizationMember, OrganizationFinancials, OrganizationSalesAssignment # noqa
|
||||
|
||||
from app.models.service import ServiceProfile, ExpertiseTag, ServiceExpertise, ServiceStaging, DiscoveryParameter # noqa
|
||||
|
||||
from app.models.vehicle_definitions import VehicleType, VehicleModelDefinition, FeatureDefinition # noqa
|
||||
|
||||
from app.models.audit import SecurityAuditLog, OperationalLog, FinancialLedger # noqa <--- KRITIKUS!
|
||||
|
||||
from app.models.asset import ( # noqa
|
||||
Asset, AssetCatalog, AssetCost, AssetEvent,
|
||||
AssetFinancials, AssetTelemetry, AssetReview, ExchangeRate
|
||||
)
|
||||
|
||||
from app.models.gamification import PointRule, LevelConfig, UserStats, Badge, UserBadge, PointsLedger # noqa
|
||||
|
||||
from app.models.system import SystemParameter # noqa (system.py használata)
|
||||
|
||||
from app.models.history import AuditLog, VehicleOwnership # noqa
|
||||
|
||||
from app.models.document import Document # noqa
|
||||
|
||||
from app.models.translation import Translation # noqa
|
||||
|
||||
from app.models.core_logic import ( # noqa
|
||||
SubscriptionTier, OrganizationSubscription, CreditTransaction, ServiceSpecialty
|
||||
)
|
||||
from app.models.security import PendingAction # noqa
|
||||
16
backend/app/db/base_class.py
Executable file
16
backend/app/db/base_class.py
Executable file
@@ -0,0 +1,16 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/base_class.py
|
||||
from typing import Any
|
||||
from sqlalchemy import MetaData
|
||||
from sqlalchemy.orm import DeclarativeBase, declared_attr
|
||||
|
||||
# Globális séma beállítása
|
||||
target_metadata = MetaData(schema="data")
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
metadata = target_metadata
|
||||
|
||||
# Automatikusan generálja a tábla nevét az osztálynévből
|
||||
@declared_attr.directive
|
||||
def __tablename__(cls) -> str:
|
||||
name = cls.__name__.lower()
|
||||
return f"{name}s" if not name.endswith('s') else name
|
||||
38
backend/app/db/context.py.old
Executable file
38
backend/app/db/context.py.old
Executable file
@@ -0,0 +1,38 @@
|
||||
from typing import Generator, Optional, Dict, Any
|
||||
from fastapi import Request
|
||||
from app.db.session import get_conn
|
||||
|
||||
def _set_config(cur, key: str, value: str) -> None:
|
||||
cur.execute("SELECT set_config(%s, %s, false);", (key, value))
|
||||
|
||||
def db_tx(request: Request) -> Generator[Dict[str, Any], None, None]:
|
||||
"""
|
||||
Egységes DB tranzakció + session context:
|
||||
BEGIN
|
||||
set_config(app.tenant_org_id, app.account_id, app.is_platform_admin)
|
||||
COMMIT/ROLLBACK
|
||||
"""
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute("BEGIN;")
|
||||
|
||||
claims: Optional[dict] = getattr(request.state, "claims", None)
|
||||
if claims:
|
||||
org_id = claims.get("org_id") or ""
|
||||
account_id = claims.get("sub") or ""
|
||||
is_platform_admin = claims.get("is_platform_admin", False)
|
||||
|
||||
# Fontos: set_config stringeket vár
|
||||
_set_config(cur, "app.tenant_org_id", str(org_id))
|
||||
_set_config(cur, "app.account_id", str(account_id))
|
||||
_set_config(cur, "app.is_platform_admin", "true" if is_platform_admin else "false")
|
||||
|
||||
yield {"conn": conn, "cur": cur}
|
||||
|
||||
conn.commit()
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
raise
|
||||
finally:
|
||||
conn.close()
|
||||
27
backend/app/db/middleware.py
Executable file
27
backend/app/db/middleware.py
Executable file
@@ -0,0 +1,27 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/middleware.py
|
||||
from fastapi import Request
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.models.audit import OperationalLog # JAVÍTVA: Az új modell
|
||||
from sqlalchemy import text
|
||||
|
||||
async def audit_log_middleware(request: Request, call_next):
|
||||
# Itt a config_service-t is aszinkron módon kell hívni, ha szükséges
|
||||
response = await call_next(request)
|
||||
|
||||
if request.method != 'GET':
|
||||
try:
|
||||
user_id = getattr(request.state, 'user_id', None)
|
||||
async with AsyncSessionLocal() as db:
|
||||
log = OperationalLog(
|
||||
user_id=user_id,
|
||||
action=f"API_CALL_{request.method}",
|
||||
resource_type="ENDPOINT",
|
||||
resource_id=str(request.url.path),
|
||||
details={"ip": request.client.host, "method": request.method}
|
||||
)
|
||||
db.add(log)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass # A naplózás nem akaszthatja meg a folyamatot
|
||||
|
||||
return response
|
||||
28
backend/app/db/session.py
Executable file
28
backend/app/db/session.py
Executable file
@@ -0,0 +1,28 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/session.py
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from app.core.config import settings
|
||||
from typing import AsyncGenerator
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=False,
|
||||
future=True,
|
||||
pool_size=30, # A robotok száma miatt
|
||||
max_overflow=20,
|
||||
pool_pre_ping=True
|
||||
)
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autoflush=False
|
||||
)
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
# JAVÍTVA: Nincs automatikus commit! Az endpoint felelőssége.
|
||||
finally:
|
||||
await session.close()
|
||||
Reference in New Issue
Block a user