feat: Identity & Company Sync v1.2, Admin hiearchia és Pénzügyi logika véglegesítése
This commit is contained in:
Binary file not shown.
@@ -1,4 +1,3 @@
|
||||
# backend/app/models/identity.py
|
||||
import uuid
|
||||
import enum
|
||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, JSON, Numeric, text, Enum
|
||||
@@ -40,7 +39,6 @@ class User(Base):
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
|
||||
# Technikai mezők átmentése a régi user.py-ból
|
||||
role = Column(Enum(UserRole), default=UserRole.USER)
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_superuser = Column(Boolean, default=False)
|
||||
@@ -58,7 +56,6 @@ class User(Base):
|
||||
|
||||
person = relationship("Person", back_populates="users")
|
||||
wallet = relationship("Wallet", back_populates="user", uselist=False)
|
||||
# Az Organization kapcsolathoz (ha szükséges az import miatt)
|
||||
owned_organizations = relationship("Organization", backref="owner")
|
||||
|
||||
class Wallet(Base):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/models/organization.py
|
||||
import enum
|
||||
from sqlalchemy import Column, Integer, String, Boolean, Enum, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
@@ -18,11 +19,22 @@ class Organization(Base):
|
||||
name = Column(String, nullable=False)
|
||||
org_type = Column(Enum(OrgType), default=OrgType.INDIVIDUAL)
|
||||
|
||||
# Spec 2.2: Az owner_id a magánszemély flottájának tulajdonosát jelöli
|
||||
owner_id = Column(Integer, ForeignKey("data.users.id"), nullable=True)
|
||||
|
||||
# MASTER BOOK v1.2 kiegészítések
|
||||
is_active = Column(Boolean, default=True)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Kapcsolatok (UserVehicle modell megléte esetén)
|
||||
vehicles = relationship("UserVehicle", back_populates="current_org", cascade="all, delete-orphan")
|
||||
# Csak cégek (nem INDIVIDUAL) esetén adható el a flotta
|
||||
is_transferable = Column(Boolean, default=True)
|
||||
|
||||
# Hitelesítési adatok
|
||||
is_verified = Column(Boolean, default=False)
|
||||
# Türelmi idő vagy hitelesítés lejárata
|
||||
verification_expires_at = Column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Kapcsolatok
|
||||
vehicles = relationship("UserVehicle", back_populates="current_org")
|
||||
members = relationship("OrganizationMember", back_populates="organization")
|
||||
@@ -1,4 +1,6 @@
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional
|
||||
import httpx
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, and_, text
|
||||
|
||||
@@ -14,17 +16,16 @@ class AuthService:
|
||||
"""
|
||||
Master Book v1.0 szerinti atomikus regisztrációs folyamat.
|
||||
"""
|
||||
# Az AsyncSession.begin() biztosítja az ATOMICitást
|
||||
async with db.begin_nested(): # beágyazott tranzakció a biztonságért
|
||||
# 1. Person létrehozása (Identity Level)
|
||||
async with db.begin_nested():
|
||||
# 1. Person létrehozása
|
||||
new_person = Person(
|
||||
first_name=user_in.first_name,
|
||||
last_name=user_in.last_name
|
||||
)
|
||||
db.add(new_person)
|
||||
await db.flush() # ID generáláshoz
|
||||
await db.flush()
|
||||
|
||||
# 2. User létrehozása (Technical Access)
|
||||
# 2. User létrehozása
|
||||
new_user = User(
|
||||
email=user_in.email,
|
||||
hashed_password=get_password_hash(user_in.password),
|
||||
@@ -34,7 +35,7 @@ class AuthService:
|
||||
db.add(new_user)
|
||||
await db.flush()
|
||||
|
||||
# 3. Economy: Wallet inicializálás (0 Coin, 0 XP)
|
||||
# 3. Economy: Wallet inicializálás
|
||||
new_wallet = Wallet(
|
||||
user_id=new_user.id,
|
||||
coin_balance=0.00,
|
||||
@@ -42,15 +43,16 @@ class AuthService:
|
||||
)
|
||||
db.add(new_wallet)
|
||||
|
||||
# 4. Fleet: Automatikus Privát Flotta létrehozása
|
||||
# 4. Fleet: Automatikus Privát Flotta
|
||||
new_org = Organization(
|
||||
name=f"{user_in.last_name} {user_in.first_name} saját flottája",
|
||||
org_type=OrgType.INDIVIDUAL,
|
||||
owner_id=new_user.id
|
||||
owner_id=new_user.id,
|
||||
is_transferable=False # Master Book v1.1: Privát flotta nem eladható
|
||||
)
|
||||
db.add(new_org)
|
||||
|
||||
# 5. Audit Log (SQLAlchemy Core hívással a sebességért)
|
||||
# 5. Audit Log
|
||||
audit_stmt = text("""
|
||||
INSERT INTO data.audit_logs (user_id, action, endpoint, method, ip_address, created_at)
|
||||
VALUES (:uid, 'USER_REGISTERED', '/api/v1/auth/register', 'POST', :ip, :now)
|
||||
@@ -61,7 +63,7 @@ class AuthService:
|
||||
"now": datetime.now(timezone.utc)
|
||||
})
|
||||
|
||||
# 6. Üdvözlő email (Subject paraméter nélkül - Spec v1.1)
|
||||
# 6. Üdvözlő email
|
||||
try:
|
||||
await email_manager.send_email(
|
||||
recipient=user_in.email,
|
||||
@@ -70,10 +72,52 @@ class AuthService:
|
||||
user_id=new_user.id
|
||||
)
|
||||
except Exception:
|
||||
pass # Email hiba ne állítsa meg a tranzakciót
|
||||
pass
|
||||
|
||||
return new_user
|
||||
|
||||
@staticmethod
|
||||
async def verify_vies_vat(vat_number: str) -> bool:
|
||||
"""
|
||||
EU VIES API lekérdezése az adószám hitelességének ellenőrzéséhez.
|
||||
"""
|
||||
try:
|
||||
# Tisztítás: csak számok és országkód (pl. HU12345678)
|
||||
clean_vat = "".join(filter(str.isalnum, vat_number)).upper()
|
||||
async with httpx.AsyncClient() as client:
|
||||
# Mock vagy valós API hívás helye
|
||||
# Példa: response = await client.get(f"https://vies-api.eu/check/{clean_vat}")
|
||||
return True # Jelenleg elfogadjuk teszteléshez
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
async def upgrade_to_company(db: AsyncSession, user_id: int, org_id: int, vat_number: str):
|
||||
"""
|
||||
Szervezet előléptetése Verified/Unverified céggé (Master Book v1.2).
|
||||
"""
|
||||
is_valid = await AuthService.verify_vies_vat(vat_number)
|
||||
|
||||
# 30 napos türelmi idő számítása
|
||||
grace_period = datetime.now(timezone.utc) + timedelta(days=30)
|
||||
|
||||
stmt = text("""
|
||||
UPDATE data.organizations
|
||||
SET is_verified = :verified,
|
||||
verification_expires_at = :expires,
|
||||
org_type = 'fleet_owner',
|
||||
is_transferable = True
|
||||
WHERE id = :id AND owner_id = :uid
|
||||
""")
|
||||
|
||||
await db.execute(stmt, {
|
||||
"verified": is_valid,
|
||||
"expires": None if is_valid else grace_period,
|
||||
"id": org_id,
|
||||
"uid": user_id
|
||||
})
|
||||
await db.commit()
|
||||
|
||||
@staticmethod
|
||||
async def check_email_availability(db: AsyncSession, email: str) -> bool:
|
||||
query = select(User).where(and_(User.email == email, User.is_deleted == False))
|
||||
|
||||
Reference in New Issue
Block a user