Files
email_reports/MIGRATION.md
Frank John Begornia d0d82aa8e1 Unified email reports
2026-01-02 01:19:07 +08:00

325 lines
7.1 KiB
Markdown

# Migration to Unified Email Reports
## Summary
**Merged `email_reports` and `email_reports_merchbay` into ONE container**
**Before:**
- Two separate directories
- Two separate containers
- Two separate cron jobs calling via HTTP
**After:**
- Single unified container
- Handles both Crew & MerchBay
- Two cron jobs (23:55, 23:56) running internally
- No HTTP calls needed
---
## What Changed
### New Unified Architecture
```
email_reports/
├── send_report.php # NEW: Unified script for both brands
├── index.php # KEPT: Legacy Crew script (backward compatible)
├── Dockerfile # UPDATED: Runs both cron jobs
├── docker-compose.yml # UPDATED: Unified container config
├── .env.example # UPDATED: Both Crew + MerchBay vars
└── .gitea/workflows/
└── deploy.yml # UPDATED: Deploys unified container
```
### Key Changes
1. **send_report.php** - Brand-agnostic report generator
- Uses `BRAND` environment variable (`crew` or `merchbay`)
- Auto-selects database, SMTP, recipients per brand
- Writes to separate CSV directories
2. **Cron Jobs** - Two jobs in one container:
```cron
55 23 * * * BRAND=crew php /app/send_report.php
56 23 * * * BRAND=merchbay php /app/send_report.php
```
3. **Separate CSV Directories**:
- `daily_order_reports_crew/`
- `daily_order_reports_merchbay/`
4. **Unified Logs** - Single `email.log` with brand prefixes:
```
2026-01-02 23:55:00 [CREW] successfully sent (5 orders)
2026-01-02 23:56:00 [MERCHBAY] No order for today
```
---
## Migration Steps
### 1. Server Preparation
SSH to your production server:
```bash
ssh user@server
cd /var/www/apps/email_reports
# Backup existing setup
tar -czf email_reports_backup_$(date +%Y%m%d).tar.gz \
daily_order_reports/ email.log .env
# Create new directories
mkdir -p daily_order_reports_crew daily_order_reports_merchbay
# Move existing Crew reports (optional)
mv daily_order_reports/* daily_order_reports_crew/ 2>/dev/null || true
```
### 2. Update .env File
Replace your `.env` with unified configuration:
```bash
cd /var/www/apps/email_reports
cat > .env << 'EOL'
# Crew Sportswear Database
DB_HOST_CREW=mysql
DB_PORT_CREW=3306
DB_NAME_CREW=custom_design
DB_USER_CREW=crew_user
DB_PASS_CREW=your_crew_password
# MerchBay Database
DB_HOST_MERCHBAY=mysql
DB_PORT_MERCHBAY=3306
DB_NAME_MERCHBAY=merchbay_laravel
DB_USER_MERCHBAY=merchbay_user
DB_PASS_MERCHBAY=your_merchbay_password
# SMTP Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
# Crew SMTP (mail@crewsportswear.com)
SMTP_PASS_CREW=your_crew_gmail_app_password
# MerchBay SMTP (support@merchbay.com)
SMTP_PASS_MERCHBAY=your_merchbay_gmail_app_password
EOL
chmod 600 .env
```
### 3. Remove Old Cron Jobs
If you had host-level cron jobs calling via HTTP:
```bash
crontab -e
# REMOVE these lines:
# 55 23 * * * wget -qO- https://www.crewsportswear.com/email_reports/index.php &> /dev/null
# 55 23 * * * wget -qO- https://www.crewsportswear.com/email_reports_merchbay/index.php &> /dev/null
```
**The container now handles scheduling internally!**
### 4. Deploy Unified Container
```bash
# From your local machine
cd email_reports
git add .
git commit -m "Migrate to unified email reports container"
git push origin main
```
Gitea workflow will automatically:
- Build unified image
- Deploy to server
- Start container with both cron jobs
### 5. Verify Deployment
```bash
# On server
docker ps | grep email_reports
# Should show:
# email_reports_unified Up X minutes
# Check cron schedule
docker exec email_reports_unified crontab -l
# Expected output:
# 55 23 * * * BRAND=crew php /app/send_report.php >> /var/log/cron.log 2>&1
# 56 23 * * * BRAND=merchbay php /app/send_report.php >> /var/log/cron.log 2>&1
```
### 6. Test Both Reports
```bash
# Test Crew report
docker exec email_reports_unified BRAND=crew php /app/send_report.php
# Test MerchBay report
docker exec email_reports_unified BRAND=merchbay php /app/send_report.php
# Check logs
docker exec email_reports_unified tail /app/email.log
# Expected:
# 2026-01-02 15:30:00 [CREW] successfully sent
# 2026-01-02 15:30:15 [MERCHBAY] successfully sent
```
### 7. Verify Email Delivery
- Check `graphics@crewsportswear.com` inbox
- Should receive **two separate emails**:
- "CREW Daily Order Report - 2026-01-02"
- "Merchbay Daily Order Report - 2026-01-02"
---
## What to Delete
### On Server
```bash
# The old email_reports_merchbay directory is no longer needed
# (if it exists on the server)
rm -rf /var/www/apps/email_reports_merchbay
# Old container (will be removed automatically by deployment)
docker rm -f email_reports_crew email_reports_merchbay
```
### In Git Repository
The `email_reports_merchbay/` directory in your repos can stay for reference, or archive it:
```bash
cd /path/to/repos
tar -czf email_reports_merchbay_archive.tar.gz email_reports_merchbay/
# Optional: rm -rf email_reports_merchbay/
```
---
## Troubleshooting
### Both Reports Not Running
```bash
# Check crontab
docker exec email_reports_unified crontab -l
# Manually trigger both
docker exec email_reports_unified BRAND=crew php /app/send_report.php
docker exec email_reports_unified BRAND=merchbay php /app/send_report.php
```
### MerchBay Database Connection Failed
```bash
# Verify MerchBay DB credentials in .env
docker exec email_reports_unified env | grep MERCHBAY
# Test connection
docker exec email_reports_unified php -r "
new PDO(
'mysql:host=' . getenv('DB_HOST_MERCHBAY') . ';dbname=' . getenv('DB_NAME_MERCHBAY'),
getenv('DB_USER_MERCHBAY'),
getenv('DB_PASS_MERCHBAY')
);
echo 'MerchBay DB: OK\n';
"
```
### Only One Email Received
```bash
# Check which brand succeeded
docker exec email_reports_unified grep "$(date +%Y-%m-%d)" /app/email.log
# Look for [CREW] or [MERCHBAY] prefixes
# If one failed, check the error message
```
### CSV Files Not Created
```bash
# Check directories exist
docker exec email_reports_unified ls -la /app/ | grep daily_order
# Should show:
# daily_order_reports_crew/
# daily_order_reports_merchbay/
# Check permissions
docker exec email_reports_unified stat -c '%a' /app/daily_order_reports_crew
# Should be 755
```
---
## Rollback Plan
If something goes wrong:
```bash
# On server
cd /var/www/apps/email_reports
# Stop unified container
docker compose down
# Restore backup
tar -xzf email_reports_backup_YYYYMMDD.tar.gz
# Restore old .env
# Start old container
```
---
## Benefits of Unified Container
**Simpler Infrastructure**
- One container instead of two
- Single deployment process
- Shared codebase
**Better Resource Usage**
- One cron daemon
- Shared Docker image
- Less memory overhead
**Easier Maintenance**
- Update both brands at once
- Unified logging
- Single point of monitoring
**Consistent Behavior**
- Same PHPMailer version
- Same PHP version
- Same timezone handling
---
## Next Steps
1. ✅ Deploy unified container
2. ✅ Test both reports manually
3. ✅ Wait for automatic execution at 23:55/23:56
4. ✅ Verify emails received
5. ✅ Monitor logs for first week
6. ✅ Remove old cron jobs
7. ✅ Archive old email_reports_merchbay directory
**Migration Complete! 🎉**