diff --git a/app/Filament/Pages/SendSms.php b/app/Filament/Pages/SendSms.php index 2331935..8753c3a 100644 --- a/app/Filament/Pages/SendSms.php +++ b/app/Filament/Pages/SendSms.php @@ -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 diff --git a/app/Jobs/StartSmsCampaignJob.php b/app/Jobs/StartSmsCampaignJob.php new file mode 100644 index 0000000..1603708 --- /dev/null +++ b/app/Jobs/StartSmsCampaignJob.php @@ -0,0 +1,38 @@ + $selectedIds + * @param array $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, + ); + } +} diff --git a/lang/tk/filament.php b/lang/tk/filament.php index 3ff284d..9d54865 100644 --- a/lang/tk/filament.php +++ b/lang/tk/filament.php @@ -38,14 +38,8 @@ return [ 'notifications' => [ 'no_valid_recipients' => 'Dogry alyjy ýok', 'no_valid_recipients_body' => 'Saýlawy ýa-da aýyrmalary üýtgediň — dogry telefon belgili kupon eýesi bu habary almaz.', - 'queued' => 'SMS kampaniýasy nobata goýuldy', - 'queued_body' => 'Arka fonda :count alyja iberilýär.', - 'failed' => 'SMS kampaniýasy şowsuz', - 'failed_body' => ':count habar iberilmedi.', - 'finished_with_errors' => 'SMS kampaniýasy ýalňyşlyklar bilen tamamlandy', - 'finished_with_errors_body' => 'Iberildi :sent, şowsuz :failed, geçildi :skipped.', - 'completed' => 'SMS kampaniýasy tamamlandy', - 'completed_body' => ':count habar üstünlikli iberildi.', + 'submitted' => 'SMS kampaniýasy kabul edildi', + 'submitted_body' => 'Arka fonda :count alyja iberilýär.', ], 'confirmation' => ':count alyja ibermeli? :summary', ], diff --git a/tests/Feature/StartSmsCampaignJobTest.php b/tests/Feature/StartSmsCampaignJobTest.php new file mode 100644 index 0000000..e3bebb6 --- /dev/null +++ b/tests/Feature/StartSmsCampaignJobTest.php @@ -0,0 +1,64 @@ +create(); + $coupon = Coupon::factory()->create(); + + StartSmsCampaignJob::dispatch( + adminId: $admin->id, + message: 'Hello', + sendToAll: false, + selectedIds: [$coupon->id], + excludedIds: [], + ); + + Queue::assertPushed(StartSmsCampaignJob::class, function (StartSmsCampaignJob $job) use ($admin, $coupon): bool { + return $job->adminId === $admin->id + && $job->message === 'Hello' + && $job->sendToAll === false + && $job->selectedIds === [$coupon->id] + && $job->excludedIds === []; + }); + } + + public function test_handle_starts_campaign_and_chains_send_jobs(): void + { + Bus::fake(); + + $admin = User::factory()->create(); + $coupons = Coupon::factory()->count(2)->create(); + + $job = new StartSmsCampaignJob( + adminId: $admin->id, + message: 'Test broadcast', + sendToAll: false, + selectedIds: $coupons->pluck('id')->all(), + ); + + $job->handle(app(SmsBroadcastService::class)); + + Bus::assertChained([ + SendSmsToCouponJob::class, + SendSmsToCouponJob::class, + ]); + } +}