AIDA / scripts /clear_bookings.py
destinyebuka's picture
fyp
5208557
Raw
History Blame Contribute Delete
5.42 kB
"""
clear_bookings.py
─────────────────
Wipes all booking data from the DB so the app looks like
bookings never happened. AIDA stops chasing paid-out stays,
review reminders, and invoice cards.
What gets cleared
─────────────────
bookings β†’ entire collection deleted
aida_sessions β†’ pending_review, review_card_sent,
review_request_sent flags unset
messages (optional) β†’ booking_invoice / booking_widget
card messages removed from chat threads
(pass --wipe-booking-messages to enable)
What is NOT touched
───────────────────
users, listings, conversations, reviews, notifications,
viewings, alerts β€” everything else stays intact.
Usage
─────
# From project root (AIDA/):
python scripts/clear_bookings.py
# Also wipe booking invoice/widget messages from chat:
python scripts/clear_bookings.py --wipe-booking-messages
# Dry run β€” see counts without deleting anything:
python scripts/clear_bookings.py --dry-run
"""
import sys
import os
from pathlib import Path
# ── Allow running from any directory ──────────────────────────
ROOT = Path(__file__).resolve().parent.parent # …/AIDA/
sys.path.insert(0, str(ROOT))
from dotenv import load_dotenv
load_dotenv(ROOT / ".env")
import pymongo
MONGODB_URL = os.getenv("MONGODB_URL")
MONGODB_DATABASE = os.getenv("MONGODB_DATABASE", "lojiz")
if not MONGODB_URL:
print("❌ MONGODB_URL not found in .env β€” aborting.")
sys.exit(1)
# ── CLI flags ─────────────────────────────────────────────────
DRY_RUN = "--dry-run" in sys.argv
WIPE_BOOKING_MESSAGES = "--wipe-booking-messages" in sys.argv
# ─────────────────────────────────────────────────────────────
def hr(): print("─" * 52)
def main():
client = pymongo.MongoClient(MONGODB_URL, serverSelectionTimeoutMS=8000)
db = client[MONGODB_DATABASE]
print()
hr()
print(" πŸ—‘ BOOKING CLEAR SCRIPT")
if DRY_RUN:
print(" ⚠️ DRY RUN β€” nothing will be deleted")
hr()
print(f" DB : {MONGODB_DATABASE}")
print()
# ── 1. bookings ──────────────────────────────────────────
booking_count = db["bookings"].count_documents({})
print(f" bookings : {booking_count} document(s) found")
if not DRY_RUN and booking_count:
result = db["bookings"].delete_many({})
print(f" βœ… deleted {result.deleted_count} booking(s)")
elif DRY_RUN:
print(f" ↳ would delete {booking_count}")
# ── 2. aida_sessions β€” clear booking flags ────────────────
session_count = db["aida_sessions"].count_documents({
"$or": [
{"pending_review": {"$exists": True}},
{"review_card_sent": {"$exists": True}},
{"review_request_sent": {"$exists": True}},
]
})
print(f"\n aida_sessions (flags): {session_count} session(s) with booking flags")
if not DRY_RUN and session_count:
result = db["aida_sessions"].update_many(
{},
{"$unset": {
"pending_review": "",
"review_card_sent": "",
"review_request_sent": "",
}},
)
print(f" βœ… cleared flags on {result.modified_count} session(s)")
elif DRY_RUN:
print(f" ↳ would clear flags on {session_count} session(s)")
# ── 3. messages β€” booking cards (optional) ────────────────
if WIPE_BOOKING_MESSAGES:
msg_count = db["messages"].count_documents({
"$or": [
{"metadata.booking_invoice": {"$exists": True}},
{"metadata.booking_widget": {"$exists": True}},
]
})
print(f"\n messages (booking) : {msg_count} booking card message(s) found")
if not DRY_RUN and msg_count:
result = db["messages"].delete_many({
"$or": [
{"metadata.booking_invoice": {"$exists": True}},
{"metadata.booking_widget": {"$exists": True}},
]
})
print(f" βœ… deleted {result.deleted_count} booking message(s)")
elif DRY_RUN:
print(f" ↳ would delete {msg_count} booking message(s)")
else:
print(f"\n messages : skipped (pass --wipe-booking-messages to also clear invoice cards from chat)")
# ── done ─────────────────────────────────────────────────
print()
hr()
if DRY_RUN:
print(" DRY RUN complete β€” run without --dry-run to apply.")
else:
print(" βœ… Done. DB looks like bookings never happened.")
hr()
print()
client.close()
if __name__ == "__main__":
main()