# Deployment Commands - Customer Type Filtering

## Quick Deployment

Run these commands in order to deploy the customer type filtering:

### 1. Navigate to Backend
```bash
cd afinet-portal-backend
```

### 2. Run Migration
```bash
php artisan migrate
```

Expected output:
```
Migrating: 2026_02_16_125630_update_product_customer_types
Migrated:  2026_02_16_125630_update_product_customer_types (XX.XXms)
```

### 3. (Optional) Run Seeder
```bash
php artisan db:seed --class=ProductCustomerTypeSeeder
```

Expected output:
```
Updated: Shared Business Broadband -> customer_type: business
Updated: Dedicated Burst Internet -> customer_type: business
Updated: Dedicated Internet Access -> customer_type: business
Updated: Dark Fibre Lease -> customer_type: wholesale
Updated: International Private Line Circuit (IPLC) -> customer_type: wholesale
Updated: Magellan Metro-VPN -> customer_type: both
Updated: IP Transit -> customer_type: both
Updated: InterCity-VPN -> customer_type: both
Product customer types seeded successfully!
```

### 4. Verify Database Changes
```bash
# Using MySQL CLI
mysql -u your_user -p your_database -e "
SELECT customer_type, COUNT(*) as count, GROUP_CONCAT(code) as products
FROM product_packages
WHERE is_active = 1
GROUP BY customer_type;
"
```

Expected output:
```
+---------------+-------+------------------------------------------+
| customer_type | count | products                                 |
+---------------+-------+------------------------------------------+
| business      | 3     | SHARED_BROADBAND,DEDICATED_BURST,DIA     |
| wholesale     | 2     | DARK_FIBRE,IPLC                          |
| both          | 3     | MAGELLAN,IP_TRANSIT,INTERCITY_VPN        |
+---------------+-------+------------------------------------------+
```

### 5. Run Tests
```bash
php artisan test --filter ProductCustomerTypeFilterTest
```

Expected output:
```
PASS  Tests\Feature\ProductCustomerTypeFilterTest
✓ business customer sees business and both products
✓ wholesale customer sees wholesale and both products
✓ products with both appear for all customers
✓ without customer type filter returns all active products
✓ inactive products are not returned

Tests:  5 passed
Time:   X.XXs
```

### 6. Clear Cache (if needed)
```bash
php artisan cache:clear
php artisan config:clear
php artisan route:clear
```

---

## Verification Commands

### Check Product Distribution
```bash
mysql -u your_user -p your_database -e "
SELECT 
    code,
    name,
    customer_type,
    package_type,
    is_active
FROM product_packages
ORDER BY customer_type, code;
"
```

### Test Business Customer View
```bash
curl -X GET "http://your-api-url/api/products?customer_type=business" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Accept: application/json" | jq '.data[] | {code, customer_type}'
```

Expected: 6 products (3 business + 3 both)

### Test Wholesale Customer View
```bash
curl -X GET "http://your-api-url/api/products?customer_type=wholesale" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Accept: application/json" | jq '.data[] | {code, customer_type}'
```

Expected: 5 products (2 wholesale + 3 both)

### Check Specific Product
```bash
mysql -u your_user -p your_database -e "
SELECT code, name, customer_type, is_active
FROM product_packages
WHERE code = 'MAGELLAN';
"
```

Expected: customer_type should be 'both'

---

## Rollback Commands (if needed)

### Rollback Migration
```bash
php artisan migrate:rollback --step=1
```

This will reset all affected products to `customer_type = 'both'`

### Manual Rollback (SQL)
```sql
UPDATE product_packages
SET customer_type = 'both'
WHERE code IN (
    'SHARED_BROADBAND', 'DEDICATED_BURST', 'DIA',
    'DARK_FIBRE', 'IPLC',
    'MAGELLAN', 'IP_TRANSIT', 'INTERCITY_VPN'
);
```

---

## Manual Update Commands (if needed)

### Update Single Product to Business
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'business' 
WHERE code = 'PRODUCT_CODE';
"
```

### Update Single Product to Wholesale
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'wholesale' 
WHERE code = 'PRODUCT_CODE';
"
```

### Update Single Product to Both
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'both' 
WHERE code = 'PRODUCT_CODE';
"
```

### Bulk Update Business Products
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'business' 
WHERE code IN ('SHARED_BROADBAND', 'DEDICATED_BURST', 'DIA');
"
```

### Bulk Update Wholesale Products
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'wholesale' 
WHERE code IN ('DARK_FIBRE', 'IPLC');
"
```

### Bulk Update Both Products
```bash
mysql -u your_user -p your_database -e "
UPDATE product_packages 
SET customer_type = 'both' 
WHERE code IN ('MAGELLAN', 'IP_TRANSIT', 'INTERCITY_VPN');
"
```

---

## Monitoring Commands

### Check Application Logs
```bash
tail -f storage/logs/laravel.log
```

### Check for Errors
```bash
grep -i "error\|exception" storage/logs/laravel.log | tail -20
```

### Monitor API Requests
```bash
tail -f storage/logs/laravel.log | grep "ProductController"
```

---

## Frontend Testing

### Test with Company Customer
1. Login as company customer
2. Navigate to `/products`
3. Open browser console
4. Check network tab for API request
5. Verify `customer_type=business` parameter
6. Verify 6 products returned

### Test with Individual Customer
1. Login as individual customer
2. Navigate to `/products`
3. Open browser console
4. Check network tab for API request
5. Verify `customer_type=wholesale` parameter
6. Verify 5 products returned

---

## Database Connection Commands

### MySQL
```bash
mysql -u your_user -p your_database
```

### PostgreSQL
```bash
psql -U your_user -d your_database
```

### SQLite
```bash
sqlite3 database/database.sqlite
```

---

## Environment-Specific Commands

### Development
```bash
# Run migration
php artisan migrate

# Run tests
php artisan test --filter ProductCustomerTypeFilterTest

# Clear cache
php artisan cache:clear
```

### Staging
```bash
# Run migration with confirmation
php artisan migrate --force

# Run tests
php artisan test --filter ProductCustomerTypeFilterTest

# Verify changes
php artisan tinker
>>> ProductPackage::where('customer_type', 'both')->pluck('code');
```

### Production
```bash
# Backup database first!
mysqldump -u user -p database > backup_$(date +%Y%m%d_%H%M%S).sql

# Run migration
php artisan migrate --force

# Verify changes
php artisan tinker
>>> ProductPackage::select('customer_type', DB::raw('count(*) as count'))->groupBy('customer_type')->get();

# Clear cache
php artisan cache:clear
php artisan config:clear
php artisan route:clear
```

---

## Quick Reference

| Command | Purpose |
|---------|---------|
| `php artisan migrate` | Run migration |
| `php artisan migrate:rollback --step=1` | Rollback migration |
| `php artisan db:seed --class=ProductCustomerTypeSeeder` | Run seeder |
| `php artisan test --filter ProductCustomerTypeFilterTest` | Run tests |
| `php artisan cache:clear` | Clear cache |
| `php artisan tinker` | Open Laravel REPL |

---

## Success Criteria

✅ Migration runs without errors  
✅ 3 products have customer_type = 'business'  
✅ 2 products have customer_type = 'wholesale'  
✅ 3 products have customer_type = 'both'  
✅ All tests pass  
✅ Business customers see 6 products  
✅ Wholesale customers see 5 products  
✅ Products with 'both' appear for all customers  

---

## Support

If you encounter issues:
1. Check logs: `storage/logs/laravel.log`
2. Run verification queries from `docs/SQL_VERIFICATION_QUERIES.sql`
3. Review documentation in `docs/PRODUCT_CUSTOMER_TYPE_FILTERING.md`
4. Run test suite to identify specific failures
