# Queued Notifications System

## Overview

The notification system has been updated to use Laravel's queue system to prevent blocking the main application process when sending emails, SMS, and WhatsApp notifications.

## How It Works

Instead of sending notifications synchronously (which can block the user interface), notifications are now dispatched as background jobs. This means:

1. The notification is queued immediately
2. The user gets an instant response
3. The actual notification (email/SMS/WhatsApp) is sent in the background
4. Failed notifications are logged and can be retried

## Implementation

### Job Class
- **File**: `app/Jobs/SendNotificationJob.php`
- **Purpose**: Generic job that handles all types of notifications
- **Supports**: Both regular notifications (`autoSendNotification`) and custom notifications (`autoSendNotificationCustom`)

### Usage Examples

#### For Shipment Notifications
```php
// In OrderfulfillmentController
SendNotificationJob::dispatch($business_id, 'shipment', $transaction->contact, $transaction);
```

#### For New Sale Notifications
```php
// In SellPosController (can be updated)
SendNotificationJob::dispatch($business_id, 'new_sale', $transaction->contact, $transaction);
```

#### For Custom Notifications
```php
// For custom data notifications
SendNotificationJob::dispatch($business_id, 'registration_confirmation', $contact, null, $custom_data);
```

## Queue Configuration

### Current Setup
- **Driver**: Database (configured in `config/queue.php`)
- **Tables**: `jobs`, `failed_jobs`, `job_batches` (already migrated)
- **Default Connection**: Set via `QUEUE_CONNECTION` environment variable

### Environment Variables
Add to your `.env` file:
```env
QUEUE_CONNECTION=database
```

### Running Queue Workers
To process queued jobs, run:
```bash
php artisan queue:work
```

For production, use a process manager like Supervisor to keep the queue worker running.

### Monitoring Failed Jobs
Failed jobs are stored in the `failed_jobs` table. You can retry them using:
```bash
php artisan queue:retry all
```

## Benefits

1. **Improved Performance**: Main application doesn't wait for email/SMS delivery
2. **Better User Experience**: Instant response to users
3. **Reliability**: Failed notifications are logged and can be retried
4. **Scalability**: Can handle high notification volumes without blocking

## Migration from Synchronous to Queued

### Before (Blocking)
```php
$whatsapp_link = $this->notificationUtil->autoSendNotification($business_id, 'shipment', $transaction, $transaction->contact);
return response()->json(['status' => true, 'message' => 'Notification sent successfully.']);
```

### After (Non-blocking)
```php
SendNotificationJob::dispatch($business_id, 'shipment', $transaction->contact, $transaction);
return response()->json(['status' => true, 'message' => 'Notification queued successfully and will be sent shortly.']);
```

## Next Steps

To fully implement queued notifications across the application, consider updating these controllers:

1. **SellPosController**: Update `new_sale` notifications
2. **CustomerAuthController**: Update custom notifications
3. **Any other controllers**: That use `autoSendNotification` or `autoSendNotificationCustom`

## Troubleshooting

### Jobs Not Processing
1. Ensure queue worker is running: `php artisan queue:work`
2. Check queue connection in `.env`: `QUEUE_CONNECTION=database`
3. Verify jobs table exists and is accessible

### Failed Jobs
1. Check `failed_jobs` table for error details
2. Review Laravel logs for specific error messages
3. Retry failed jobs: `php artisan queue:retry all`

### Performance Issues
1. Consider using Redis instead of database for better performance
2. Implement job batching for multiple notifications
3. Use job priorities for critical notifications 