Spaces:
Running
Running
| """ | |
| 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() | |