Files
service-finder/tests/archive/verify_org_structure.py.old

174 lines
7.3 KiB
Python

#!/usr/bin/env python3
"""
Verify organizational structure after KYC completion for User 55
"""
import asyncio
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import text
from app.core.config import settings
async def verify_org_structure():
# Create database connection
engine = create_async_engine(settings.DATABASE_URL, echo=False)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as db:
print("🔍 Verifying organizational structure for User 55")
print("=" * 60)
# 1. User Link: Confirm User.person_id is correctly pointing to Person 56
print("\n1. User Link (User.person_id -> Person 56):")
user_query = text("""
SELECT id, email, person_id, is_active, scope_id
FROM identity.users
WHERE id = 55
""")
user_result = await db.execute(user_query)
user = user_result.fetchone()
if user:
print(f" ✅ User 55 found:")
print(f" - ID: {user.id}")
print(f" - Email: {user.email}")
print(f" - Person ID: {user.person_id} (expected: 56)")
print(f" - Is Active: {user.is_active}")
print(f" - Scope ID: {user.scope_id}")
if user.person_id == 56:
print(" ✅ Person ID correctly points to Person 56")
else:
print(f" ❌ Person ID mismatch: expected 56, got {user.person_id}")
else:
print(" ❌ User 55 not found!")
# 2. Organization: Confirm a record in fleet.organizations was created
print("\n2. Organization (fleet.organizations):")
org_query = text("""
SELECT id, full_name, name, org_type, owner_id, is_active, status
FROM fleet.organizations
WHERE owner_id = 55 AND org_type = 'individual'
ORDER BY created_at DESC
LIMIT 1
""")
org_result = await db.execute(org_query)
organization = org_result.fetchone()
if organization:
print(f" ✅ Organization found:")
print(f" - ID: {organization.id}")
print(f" - Full Name: {organization.full_name}")
print(f" - Name: {organization.name}")
print(f" - Type: {organization.org_type}")
print(f" - Owner ID: {organization.owner_id}")
print(f" - Is Active: {organization.is_active}")
print(f" - Status: {organization.status}")
# Check if scope_id matches organization id
if user and str(organization.id) == user.scope_id:
print(f" ✅ User scope_id ({user.scope_id}) matches organization ID")
else:
print(f" ⚠️ User scope_id ({user.scope_id if user else 'N/A'}) doesn't match organization ID ({organization.id})")
else:
print(" ❌ No organization found for User 55!")
# 3. Branch: Confirm a record in fleet.branches exists
print("\n3. Branch (fleet.branches):")
if organization:
branch_query = text("""
SELECT id, organization_id, name, is_main, address_id
FROM fleet.branches
WHERE organization_id = :org_id AND name = 'Home Base'
""")
branch_result = await db.execute(branch_query, {"org_id": organization.id})
branch = branch_result.fetchone()
if branch:
print(f" ✅ Branch 'Home Base' found:")
print(f" - ID: {branch.id}")
print(f" - Organization ID: {branch.organization_id}")
print(f" - Name: {branch.name}")
print(f" - Is Main: {branch.is_main}")
print(f" - Address ID: {branch.address_id}")
else:
print(" ❌ No 'Home Base' branch found for the organization!")
else:
print(" ⚠️ Cannot check branches without organization")
# 4. Infrastructure: Confirm wallets and user_stats records
print("\n4. Infrastructure (Wallets and User Stats):")
# Check wallet
wallet_query = text("""
SELECT id, user_id, currency, earned_credits, purchased_credits, service_coins
FROM identity.wallets
WHERE user_id = 55
""")
wallet_result = await db.execute(wallet_query)
wallet = wallet_result.fetchone()
if wallet:
print(f" ✅ Wallet found for User 55:")
print(f" - ID: {wallet.id}")
print(f" - User ID: {wallet.user_id}")
print(f" - Currency: {wallet.currency}")
print(f" - Earned Credits: {wallet.earned_credits}")
print(f" - Purchased Credits: {wallet.purchased_credits}")
print(f" - Service Coins: {wallet.service_coins}")
else:
print(" ❌ No wallet found for User 55!")
# Check user_stats
stats_query = text("""
SELECT id, user_id, total_xp, level, reputation_score
FROM identity.user_stats
WHERE user_id = 55
""")
stats_result = await db.execute(stats_query)
user_stats = stats_result.fetchone()
if user_stats:
print(f" ✅ User Stats found for User 55:")
print(f" - ID: {user_stats.id}")
print(f" - User ID: {user_stats.user_id}")
print(f" - Total XP: {user_stats.total_xp}")
print(f" - Level: {user_stats.level}")
print(f" - Reputation Score: {user_stats.reputation_score}")
else:
print(" ❌ No user_stats found for User 55!")
# 5. Organization Member
print("\n5. Organization Member (fleet.organization_members):")
if organization:
member_query = text("""
SELECT id, organization_id, user_id, role, is_active
FROM fleet.organization_members
WHERE organization_id = :org_id AND user_id = 55
""")
member_result = await db.execute(member_query, {"org_id": organization.id})
member = member_result.fetchone()
if member:
print(f" ✅ Organization member record found:")
print(f" - ID: {member.id}")
print(f" - Organization ID: {member.organization_id}")
print(f" - User ID: {member.user_id}")
print(f" - Role: {member.role}")
print(f" - Is Active: {member.is_active}")
else:
print(" ❌ No organization member record found!")
else:
print(" ⚠️ Cannot check organization members without organization")
print("\n" + "=" * 60)
print("Verification complete!")
return True
if __name__ == "__main__":
result = asyncio.run(verify_org_structure())
sys.exit(0 if result else 1)