70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.db.session import get_db
|
|
from app.schemas.organization import CorpOnboardIn, CorpOnboardResponse
|
|
from app.models.organization import Organization, OrgType
|
|
from app.core.config import settings
|
|
import os
|
|
import re
|
|
import logging
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@router.post("/onboard", response_model=CorpOnboardResponse, status_code=status.HTTP_201_CREATED)
|
|
async def onboard_organization(
|
|
org_in: CorpOnboardIn,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""
|
|
Új szervezet (cég/szerviz) rögzítése.
|
|
- Magyar adószám validáció (XXXXXXXX-Y-ZZ).
|
|
- Duplikáció ellenőrzés adószám alapján.
|
|
- NAS mappa és DB rekord létrehozása.
|
|
"""
|
|
|
|
# 1. Magyar adószám validáció
|
|
if org_in.country_code == "HU":
|
|
if not re.match(r"^\d{8}-\d-\d{2}$", org_in.tax_number):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Érvénytelen magyar adószám formátum! (Példa: 12345678-1-12)"
|
|
)
|
|
|
|
# 2. Duplikáció ellenőrzés
|
|
stmt_exist = select(Organization).where(Organization.tax_number == org_in.tax_number)
|
|
result_exist = await db.execute(stmt_exist)
|
|
if result_exist.scalar_one_or_none():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail="Ezzel az adószámmal már regisztráltak céget!"
|
|
)
|
|
|
|
# 3. Mentés (Dinamikus státusszal és kisbetűs Enummal)
|
|
new_org = Organization(
|
|
name=org_in.name,
|
|
tax_number=org_in.tax_number,
|
|
reg_number=org_in.reg_number,
|
|
headquarters_address=org_in.headquarters_address,
|
|
country_code=org_in.country_code,
|
|
org_type=OrgType.business, # Most már kisbetűs 'business' kerül beküldésre
|
|
status="pending_verification"
|
|
)
|
|
|
|
db.add(new_org)
|
|
await db.flush() # ID generálás a NAS-hoz
|
|
|
|
# 4. NAS Mappa létrehozása
|
|
try:
|
|
base_path = getattr(settings, "NAS_STORAGE_PATH", "/mnt/nas/app_data")
|
|
org_path = os.path.join(base_path, "organizations", str(new_org.id))
|
|
os.makedirs(org_path, exist_ok=True)
|
|
logger.info(f"NAS mappa létrehozva szervezetnek: {org_path}")
|
|
except Exception as e:
|
|
logger.error(f"NAS hiba az onboardingnál: {e}")
|
|
|
|
await db.commit()
|
|
await db.refresh(new_org)
|
|
|
|
return {"organization_id": new_org.id, "status": new_org.status} |