# Database Cleanup Scripts

This directory contains scripts to safely clean up test data while preserving production data.

## ⚠️ IMPORTANT WARNING

**These scripts will PERMANENTLY delete data. This cannot be undone!**

Before running any cleanup scripts:
1. ✅ **Backup your database** using `mongodump`
2. ✅ **Test on development environment first**
3. ✅ **Verify you have admin access to the system**

## Backup First!

```bash
# Create a backup before running any scripts
mongodump --uri="your-mongodb-uri" --out=backup-$(date +%Y%m%d)
```

## Scripts Overview

### 1. reset-facility-claims.ts
Resets all facilities to "unclaimed" status, making them available for new users to claim.

**What it does:**
- Sets `claimStatus` to `'unclaimed'`
- Removes `claimedBy` references
- Removes `facilityOwnerProfileId` references
- Clears all `claimRequests` arrays
- **Preserves all other facility data** (name, address, photos, etc.)

**Usage:**
```bash
npx tsx scripts/reset-facility-claims.ts
```

---

### 2. delete-test-users.ts
Deletes all non-admin users and their related data.

**What it deletes:**
- ✗ FacilityOwnerProfiles (subscription data)
- ✗ UserProfiles (family member profiles)
- ✗ Reviews
- ✗ Notifications
- ✗ Favorites
- ✗ Users (except admins)

**What it preserves:**
- ✓ Admin users (`role: 'admin'` or `'super_admin'`)
- ✓ All facility data

**Usage:**
```bash
npx tsx scripts/delete-test-users.ts
```

---

### 3. cleanup-orphaned-ads.ts (Optional)
Cleans up ads from deleted users.

**Modes:**
- `all` - Deletes ALL ads
- `orphaned` - Deletes only ads where the creator no longer exists

**Usage:**
```bash
# Delete all ads
npx tsx scripts/cleanup-orphaned-ads.ts all

# Delete only orphaned ads (creator doesn't exist)
npx tsx scripts/cleanup-orphaned-ads.ts orphaned
```

---

### 4. verify-cleanup.ts
Verifies that cleanup was successful and shows current database state.

**Usage:**
```bash
npx tsx scripts/verify-cleanup.ts
```

---

## Complete Cleanup Process

Follow these steps in order:

### Step 1: Backup
```bash
mongodump --uri="your-mongodb-uri" --out=backup-$(date +%Y%m%d)
```

### Step 2: Verify current state
```bash
npx tsx scripts/verify-cleanup.ts
```

### Step 3: Reset facility claims
```bash
npx tsx scripts/reset-facility-claims.ts
```

### Step 4: Delete test users
```bash
npx tsx scripts/delete-test-users.ts
```

### Step 5: (Optional) Clean up ads
```bash
# If you want to remove all ads:
npx tsx scripts/cleanup-orphaned-ads.ts all

# If you want to keep ads from existing users:
npx tsx scripts/cleanup-orphaned-ads.ts orphaned
```

### Step 6: Verify cleanup
```bash
npx tsx scripts/verify-cleanup.ts
```

---

## What Happens After Cleanup

### ✅ Preserved:
- All facility data (names, addresses, photos, amenities, etc.)
- Admin user accounts
- System configurations
- Ad campaigns (if you chose to keep them)

### ✗ Removed:
- All test user accounts
- All subscription data
- All user profiles
- All facility claims
- All reviews
- All favorites
- All notifications
- Orphaned ads (if you chose to delete them)

### 🔄 Reset:
- All facilities are now "unclaimed"
- New users can register and claim facilities
- Facility data remains intact for search/discovery

---

## Safety Features

Each script includes:
- ✓ Pre-deletion counts and statistics
- ✓ Sample data preview
- ✓ Admin user preservation
- ✓ Clear warnings before deletion
- ✓ Post-deletion verification

---

## Troubleshooting

### "No admin users found" warning
If you see this warning, **STOP!** You need at least one admin user to access the system after cleanup.

Create an admin user first:
```bash
npx tsx scripts/create-admin-user.ts
```

### Cleanup incomplete
Run the verification script to see what's remaining:
```bash
npx tsx scripts/verify-cleanup.ts
```

Then run the appropriate cleanup script again.

### Need to restore data
If you have a backup:
```bash
mongorestore --uri="your-mongodb-uri" --dir=backup-YYYYMMDD
```

---

## Support

If you encounter issues:
1. Check the terminal output for error messages
2. Run `verify-cleanup.ts` to see current state
3. Ensure you have database connection and permissions
4. Contact support with the error message

---

## Environment Considerations

### Development
Safe to run these scripts to clean up test data.

### Staging
Use caution. Ensure you have backups.

### Production
**DO NOT RUN** unless you fully understand the implications and have complete backups.

---

Generated: $(date +"%Y-%m-%d %H:%M:%S")

