Files
service-finder/backend/create_business_org.py
2026-06-04 07:26:22 +00:00

181 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""
Script to create a business organization for User 55 (Test-Robot Kft.)
"""
import sys
import asyncio
from datetime import datetime, timezone
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
sys.path.append('/app/backend')
from app.db.session import AsyncSessionLocal
from app.models.marketplace import Organization, OrganizationMember, Branch, OrgType
from app.models.identity import User
from app.core.security import generate_secure_slug
from app.services.geo_service import GeoService
async def create_business_organization():
"""Create a business organization for User 55"""
async with AsyncSessionLocal() as db:
try:
# Get User 55
user_result = await db.execute(select(User).where(User.id == 55))
user = user_result.scalar_one_or_none()
if not user:
print("ERROR: User 55 not found")
return
print(f"Found user: {user.email}")
# Check if business organization already exists for this user
org_check = await db.execute(
select(Organization).join(OrganizationMember).where(
OrganizationMember.user_id == 55,
Organization.org_type == OrgType.business
)
)
existing_business = org_check.scalar_one_or_none()
if existing_business:
print(f"Business organization already exists: {existing_business.full_name} (ID: {existing_business.id})")
return existing_business.id
# Create address for the business (using user's address or creating new)
# For simplicity, using the same address as user's person
from app.models.identity import Person
person_result = await db.execute(select(Person).where(Person.id == user.person_id))
person = person_result.scalar_one_or_none()
address_id = None
if person and person.address_id:
address_id = person.address_id
print(f"Using existing address ID: {address_id}")
else:
# Create a simple address for the business
address_data = {
"zip_code": "1132",
"city": "Budapest",
"street_name": "Váci",
"street_type": "út",
"house_number": "47",
"parcel_id": None
}
address_id = await GeoService.get_or_create_full_address(db, **address_data)
print(f"Created new address ID: {address_id}")
# Create the business organization
now = datetime.now(timezone.utc)
business_org = Organization(
full_name="Test-Robot Kft.",
name="Test-Robot Kft.",
display_name="Test-Robot",
folder_slug=generate_secure_slug(12),
org_type=OrgType.business,
owner_id=user.id,
legal_owner_id=person.id if person else None,
tax_number="12345678-1-12",
reg_number="01-09-123456",
is_active=True,
status="verified",
country_code="HU",
language="hu",
default_currency="HUF",
address_id=address_id,
address_zip="1132",
address_city="Budapest",
address_street_name="Váci",
address_street_type="út",
address_house_number="47",
first_registered_at=now,
current_lifecycle_started_at=now,
subscription_plan="FREE",
base_asset_limit=10,
purchased_extra_slots=0,
notification_settings={},
external_integration_config={},
is_ownership_transferable=True,
created_at=now
)
db.add(business_org)
await db.flush()
print(f"Created business organization: {business_org.full_name} (ID: {business_org.id})")
# Add user as OWNER of the organization
org_member = OrganizationMember(
organization_id=business_org.id,
user_id=user.id,
role="OWNER",
is_active=True,
joined_at=now
)
db.add(org_member)
# Create main branch
main_branch = Branch(
organization_id=business_org.id,
address_id=address_id,
name="Központi Telephely",
is_main=True,
phone=person.phone if person else "+36301234567",
email=user.email,
operating_hours={},
services_offered=[],
created_at=now
)
db.add(main_branch)
await db.commit()
print(f"Created main branch: {main_branch.name}")
print(f"Added user as OWNER to organization")
return business_org.id
except Exception as e:
await db.rollback()
print(f"ERROR: {e}")
import traceback
traceback.print_exc()
return None
async def verify_organizations():
"""Verify that User 55 has access to both organizations"""
async with AsyncSessionLocal() as db:
# Get all organizations for User 55
result = await db.execute(
select(Organization, OrganizationMember.role)
.join(OrganizationMember)
.where(OrganizationMember.user_id == 55)
.order_by(Organization.org_type)
)
orgs = result.all()
print("\n=== VERIFICATION ===")
print(f"User 55 has access to {len(orgs)} organizations:")
for org, role in orgs:
print(f" - {org.full_name} (ID: {org.id}, Type: {org.org_type}, Role: {role})")
# Check branches
if orgs:
for org, _ in orgs:
branch_result = await db.execute(
select(Branch).where(Branch.organization_id == org.id)
)
branches = branch_result.scalars().all()
print(f" Branches for {org.name}: {len(branches)}")
for branch in branches:
print(f" - {branch.name} (Main: {branch.is_main})")
if __name__ == "__main__":
print("=== CREATING BUSINESS ORGANIZATION FOR USER 55 ===")
org_id = asyncio.run(create_business_organization())
if org_id:
print(f"\nBusiness organization created successfully with ID: {org_id}")
asyncio.run(verify_organizations())
else:
print("\nFailed to create business organization")