STABLE: Final schema sync, optimized gitignore
This commit is contained in:
@@ -110,134 +110,61 @@ class AuthService:
|
||||
|
||||
@staticmethod
|
||||
async def complete_kyc(db: AsyncSession, user_id: int, kyc_in: UserKYCComplete):
|
||||
"""
|
||||
Step 2: Atomi Tranzakció.
|
||||
Módosított verzió: Meglévő biztonsági logika + Telephely (Branch) integráció.
|
||||
"""
|
||||
""" Step 2: Atomi Tranzakció (Person + Address + Org + Branch + Wallet). """
|
||||
try:
|
||||
# 1. User és Person betöltése
|
||||
# 1. Lekérés Eager Loadinggal a hibák elkerülésére
|
||||
stmt = select(User).options(joinedload(User.person)).where(User.id == user_id)
|
||||
res = await db.execute(stmt)
|
||||
user = res.scalar_one_or_none()
|
||||
user = (await db.execute(stmt)).scalar_one_or_none()
|
||||
if not user: return None
|
||||
|
||||
# --- BIZTONSÁG: Slug generálása ---
|
||||
if not user.folder_slug:
|
||||
user.folder_slug = generate_secure_slug(length=12)
|
||||
|
||||
if hasattr(kyc_in, 'preferred_currency') and kyc_in.preferred_currency:
|
||||
user.preferred_currency = kyc_in.preferred_currency
|
||||
|
||||
# --- SHADOW IDENTITY ELLENŐRZÉS ---
|
||||
identity_stmt = select(Person).where(and_(
|
||||
Person.mothers_last_name == kyc_in.mothers_last_name,
|
||||
Person.mothers_first_name == kyc_in.mothers_first_name,
|
||||
Person.birth_place == kyc_in.birth_place,
|
||||
Person.birth_date == kyc_in.birth_date
|
||||
))
|
||||
existing_person = (await db.execute(identity_stmt)).scalar_one_or_none()
|
||||
|
||||
if existing_person:
|
||||
user.person_id = existing_person.id
|
||||
active_person = existing_person
|
||||
else:
|
||||
active_person = user.person
|
||||
|
||||
# --- CÍM RÖGZÍTÉSE ---
|
||||
# 2. Cím rögzítése
|
||||
addr_id = await GeoService.get_or_create_full_address(
|
||||
db,
|
||||
zip_code=kyc_in.address_zip,
|
||||
city=kyc_in.address_city,
|
||||
street_name=kyc_in.address_street_name,
|
||||
street_type=kyc_in.address_street_type,
|
||||
house_number=kyc_in.address_house_number,
|
||||
parcel_id=kyc_in.address_hrsz
|
||||
db, zip_code=kyc_in.address_zip, city=kyc_in.address_city,
|
||||
street_name=kyc_in.address_street_name, street_type=kyc_in.address_street_type,
|
||||
house_number=kyc_in.address_house_number, parcel_id=kyc_in.address_hrsz
|
||||
)
|
||||
|
||||
# --- SZEMÉLYES ADATOK FRISSÍTÉSE ---
|
||||
active_person.mothers_last_name = kyc_in.mothers_last_name
|
||||
active_person.mothers_first_name = kyc_in.mothers_first_name
|
||||
active_person.birth_place = kyc_in.birth_place
|
||||
active_person.birth_date = kyc_in.birth_date
|
||||
active_person.phone = kyc_in.phone_number
|
||||
active_person.address_id = addr_id
|
||||
active_person.identity_docs = jsonable_encoder(kyc_in.identity_docs)
|
||||
active_person.ice_contact = jsonable_encoder(kyc_in.ice_contact)
|
||||
active_person.is_active = True
|
||||
# 3. Person adatok frissítése (MDM elv)
|
||||
p = user.person
|
||||
p.mothers_last_name = kyc_in.mothers_last_name
|
||||
p.mothers_first_name = kyc_in.mothers_first_name
|
||||
p.birth_place = kyc_in.birth_place
|
||||
p.birth_date = kyc_in.birth_date
|
||||
p.phone = kyc_in.phone_number
|
||||
p.address_id = addr_id
|
||||
p.identity_docs = jsonable_encoder(kyc_in.identity_docs)
|
||||
p.is_active = True
|
||||
|
||||
# --- EGYÉNI FLOTTA LÉTREHOZÁSA ---
|
||||
# 4. Individual Organization (Privát Széf) létrehozása
|
||||
new_org = Organization(
|
||||
full_name=f"{active_person.last_name} {active_person.first_name} Egyéni Flotta",
|
||||
name=f"{active_person.last_name} Flotta",
|
||||
folder_slug=generate_secure_slug(length=12),
|
||||
full_name=f"{p.last_name} {p.first_name} Magán Flotta",
|
||||
name=f"{p.last_name} Flotta",
|
||||
folder_slug=generate_secure_slug(12),
|
||||
org_type=OrgType.individual,
|
||||
owner_id=user.id,
|
||||
is_transferable=False, # Step 2: Individual flotta nem átruházható
|
||||
is_ownership_transferable=False, # A te új meződ
|
||||
is_active=True,
|
||||
status="verified",
|
||||
language=user.preferred_language,
|
||||
default_currency=user.preferred_currency or "HUF",
|
||||
country_code=user.region_code
|
||||
)
|
||||
db.add(new_org)
|
||||
await db.flush()
|
||||
|
||||
# --- ÚJ: MAIN BRANCH (KÖZPONTI TELEPHELY) LÉTREHOZÁSA ---
|
||||
# Magánszemélynél a megadott cím lesz az első telephely is.
|
||||
from app.models.address import Branch
|
||||
new_branch = Branch(
|
||||
organization_id=new_org.id,
|
||||
address_id=addr_id,
|
||||
name="Központ / Otthon",
|
||||
is_main=True,
|
||||
postal_code=kyc_in.address_zip,
|
||||
city=kyc_in.address_city,
|
||||
street_name=kyc_in.address_street_name,
|
||||
street_type=kyc_in.address_street_type,
|
||||
house_number=kyc_in.address_house_number,
|
||||
hrsz=kyc_in.address_hrsz,
|
||||
status="active"
|
||||
)
|
||||
db.add(new_branch)
|
||||
await db.flush()
|
||||
# 5. Telephely (Branch) és Tagság
|
||||
db.add(Branch(organization_id=new_org.id, address_id=addr_id, name="Otthon", is_main=True))
|
||||
db.add(OrganizationMember(organization_id=new_org.id, user_id=user.id, role="OWNER"))
|
||||
db.add(Wallet(user_id=user.id, currency=kyc_in.preferred_currency or "HUF"))
|
||||
db.add(UserStats(user_id=user.id))
|
||||
|
||||
# --- TAGSÁG, WALLET, STATS ---
|
||||
db.add(OrganizationMember(
|
||||
organization_id=new_org.id,
|
||||
user_id=user.id,
|
||||
role="owner",
|
||||
permissions={"can_add_asset": True, "can_view_costs": True, "is_admin": True}
|
||||
))
|
||||
db.add(Wallet(user_id=user.id, currency=user.preferred_currency or "HUF"))
|
||||
db.add(UserStats(user_id=user.id, total_xp=0, current_level=1))
|
||||
|
||||
# --- 7. AKTIVÁLÁS ÉS AUDIT (Ami az előzőből kimaradt) ---
|
||||
# 6. Aktiválás
|
||||
user.is_active = True
|
||||
user.folder_slug = generate_secure_slug(12)
|
||||
|
||||
await security_service.log_event(
|
||||
db,
|
||||
user_id=user.id,
|
||||
action="USER_KYC_COMPLETED",
|
||||
severity="info",
|
||||
target_type="User",
|
||||
target_id=str(user.id),
|
||||
new_data={
|
||||
"status": "active",
|
||||
"user_folder": user.folder_slug,
|
||||
"organization_id": new_org.id,
|
||||
"branch_id": str(new_branch.id), # Új telephely az auditban
|
||||
"wallet_created": True
|
||||
}
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(user)
|
||||
return user
|
||||
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
logger.error(f"KYC Atomi Tranzakció Hiba: {str(e)}")
|
||||
logger.error(f"KYC Error: {e}")
|
||||
raise e
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user