Refactor SMS campaign handling to dispatch a job for starting campaigns and update notification messages for improved user feedback.
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Jobs\StartSmsCampaignJob;
|
||||
use App\Models\Coupon;
|
||||
use App\Models\SmsCampaign;
|
||||
use App\Services\SmsBroadcastService;
|
||||
use App\Services\SmsMessageAnalyzer;
|
||||
use BackedEnum;
|
||||
@@ -170,52 +170,19 @@ class SendSms extends Page
|
||||
return;
|
||||
}
|
||||
|
||||
$campaign = $service->startCampaign(
|
||||
admin: auth()->user(),
|
||||
StartSmsCampaignJob::dispatch(
|
||||
adminId: auth()->id(),
|
||||
message: $data['message'],
|
||||
sendToAll: $sendToAll,
|
||||
selectedIds: $selectedIds,
|
||||
excludedIds: $excludedIds,
|
||||
);
|
||||
|
||||
if ($campaign->isComplete()) {
|
||||
$this->notifyCampaignFinished($campaign);
|
||||
} else {
|
||||
Notification::make()
|
||||
->title(__('filament.send_sms.notifications.queued'))
|
||||
->body(__('filament.send_sms.notifications.queued_body', ['count' => $campaign->recipient_count]))
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
|
||||
protected function notifyCampaignFinished(SmsCampaign $campaign): void
|
||||
{
|
||||
$campaign->refresh();
|
||||
|
||||
if ($campaign->failed_count > 0 && $campaign->sent_count === 0) {
|
||||
Notification::make()
|
||||
->title(__('filament.send_sms.notifications.failed'))
|
||||
->body(__('filament.send_sms.notifications.failed_body', ['count' => $campaign->failed_count]))
|
||||
->danger()
|
||||
->send();
|
||||
} elseif ($campaign->failed_count > 0) {
|
||||
Notification::make()
|
||||
->title(__('filament.send_sms.notifications.finished_with_errors'))
|
||||
->body(__('filament.send_sms.notifications.finished_with_errors_body', [
|
||||
'sent' => $campaign->sent_count,
|
||||
'failed' => $campaign->failed_count,
|
||||
'skipped' => $campaign->skipped_count,
|
||||
]))
|
||||
->warning()
|
||||
->send();
|
||||
} else {
|
||||
Notification::make()
|
||||
->title(__('filament.send_sms.notifications.completed'))
|
||||
->body(__('filament.send_sms.notifications.completed_body', ['count' => $campaign->sent_count]))
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
Notification::make()
|
||||
->title(__('filament.send_sms.notifications.submitted'))
|
||||
->body(__('filament.send_sms.notifications.submitted_body', ['count' => $willReceive]))
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
|
||||
protected function confirmationDescription(): string
|
||||
|
||||
38
app/Jobs/StartSmsCampaignJob.php
Normal file
38
app/Jobs/StartSmsCampaignJob.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\SmsBroadcastService;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
|
||||
class StartSmsCampaignJob implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* @param array<int, int|string> $selectedIds
|
||||
* @param array<int, int|string> $excludedIds
|
||||
*/
|
||||
public function __construct(
|
||||
public int $adminId,
|
||||
public string $message,
|
||||
public bool $sendToAll,
|
||||
public array $selectedIds = [],
|
||||
public array $excludedIds = [],
|
||||
) {}
|
||||
|
||||
public function handle(SmsBroadcastService $service): void
|
||||
{
|
||||
$admin = User::query()->findOrFail($this->adminId);
|
||||
|
||||
$service->startCampaign(
|
||||
admin: $admin,
|
||||
message: $this->message,
|
||||
sendToAll: $this->sendToAll,
|
||||
selectedIds: $this->selectedIds,
|
||||
excludedIds: $this->excludedIds,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user