# Utility Scripts

This directory contains utility scripts for maintenance and troubleshooting.

## Available Scripts

### cleanupStaleOrders.ts

**Purpose:** Manually clean up very old stale orders (>24 hours) by marking them as 'unknown'.

**When to use:**
- After a prolonged bot outage
- When you have many old pending_new orders
- As part of database maintenance

**How to run:**
```bash
npm run cleanup-stale-orders
```

**What it does:**
1. Queries database for orders in `pending_new` status older than 24 hours
2. Marks each order as `unknown` status
3. Logs summary of how many orders were updated

**Example output:**
```
[Cleanup] Starting stale order cleanup...
[Cleanup] Found 15 orders older than 24 hours in pending_new status
[Cleanup] Marking order abc123 as 'unknown' (age: 48h, symbol: BTC/USDT)
[Cleanup] Marking order def456 as 'unknown' (age: 36h, symbol: ETH/USDT)
...
[Cleanup] ✅ Cleanup complete! Updated 15 stale orders to 'unknown' status
```

**Note:** This is a one-time operation. For ongoing sync, use the automatic OrderStatusSyncService (runs every 5 minutes).

## Adding New Scripts

To add a new utility script:

1. Create a new `.ts` file in this directory
2. Add the script entry to `package.json`:
   ```json
   "scripts": {
     "your-script": "tsx src/scripts/yourScript.ts"
   }
   ```
3. Document it in this README

## Related Documentation

- [Order Status Sync Documentation](../docs/ORDER_STATUS_SYNC.md)
- [Implementation Summary](../IMPLEMENTATION_SUMMARY.md)
- [Quick Reference](../ORDER_STATUS_SYNC_QUICK_REF.md)
