""" grant_admin.py — One-time script to promote a Lojiz user to admin role. Run from the AIDA/ directory: python grant_admin.py This updates the user's role to "admin" in MongoDB so they can: 1. Access all /api/admin/* endpoints (require_admin guard) 2. Log into the Lojiz Admin Dashboard with their existing credentials """ import asyncio import os import sys from motor.motor_asyncio import AsyncIOMotorClient from dotenv import load_dotenv load_dotenv() ADMIN_EMAIL = "dmldestiny7@gmail.com" MONGO_URI = os.getenv("MONGODB_URL") or os.getenv("MONGO_URI") or os.getenv("DATABASE_URL") async def grant(): if not MONGO_URI: print("❌ No MONGODB_URL found in .env — aborting.") sys.exit(1) client = AsyncIOMotorClient(MONGO_URI) # detect DB name from URI or fallback db_name = MONGO_URI.rstrip("/").split("/")[-1].split("?")[0] or "lojiz" db = client[db_name] user = await db.users.find_one({"email": ADMIN_EMAIL}) if not user: print(f"❌ No user found with email: {ADMIN_EMAIL}") client.close() sys.exit(1) current_role = user.get("role", "renter") already_admin = user.get("is_admin") is True if already_admin and current_role == "landlord": print(f"✅ {ADMIN_EMAIL} already set up as landlord+admin. Nothing to do.") client.close() return result = await db.users.update_one( {"email": ADMIN_EMAIL}, {"$set": {"role": "landlord", "is_admin": True}}, ) if result.modified_count: print(f"✅ {ADMIN_EMAIL}: role=landlord (in-app) + is_admin=True (dashboard)") else: print(f"⚠️ No modification made.") client.close() if __name__ == "__main__": asyncio.run(grant())