SMS service
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\ClientCase;
|
||||
use App\Models\SmsProfile;
|
||||
use App\Models\SmsSender;
|
||||
use App\Models\SmsTemplate;
|
||||
use App\Services\Sms\SmsService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class SendSmsJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public int $profileId,
|
||||
public string $to,
|
||||
public string $content,
|
||||
public ?int $senderId = null,
|
||||
public ?string $countryCode = null,
|
||||
public bool $deliveryReport = false,
|
||||
public ?string $clientReference = null,
|
||||
// For optional activity creation (case UI only)
|
||||
public ?int $templateId = null,
|
||||
public ?int $clientCaseId = null,
|
||||
public ?int $userId = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(SmsService $sms): void
|
||||
{
|
||||
// Resolve models
|
||||
/** @var SmsProfile|null $profile */
|
||||
$profile = SmsProfile::find($this->profileId);
|
||||
if (! $profile) {
|
||||
return; // nothing to do
|
||||
}
|
||||
|
||||
/** @var SmsSender|null $sender */
|
||||
$sender = $this->senderId ? SmsSender::find($this->senderId) : null;
|
||||
|
||||
// Send and get log (handles queued->sent/failed transitions internally)
|
||||
$log = $sms->sendRaw(
|
||||
profile: $profile,
|
||||
to: $this->to,
|
||||
content: $this->content,
|
||||
sender: $sender,
|
||||
countryCode: $this->countryCode,
|
||||
deliveryReport: $this->deliveryReport,
|
||||
clientReference: $this->clientReference,
|
||||
);
|
||||
// If invoked from the case UI with a selected template, create an Activity
|
||||
if ($this->templateId && $this->clientCaseId && $log) {
|
||||
try {
|
||||
/** @var SmsTemplate|null $template */
|
||||
$template = SmsTemplate::find($this->templateId);
|
||||
/** @var ClientCase|null $case */
|
||||
$case = ClientCase::find($this->clientCaseId);
|
||||
if ($template && $case) {
|
||||
$note = '';
|
||||
if ($log->status === 'sent') {
|
||||
$note = sprintf('Št: %s | Telo: %s', (string) $this->to, (string) $this->content);
|
||||
} elseif ($log->status === 'failed') {
|
||||
$note = sprintf(
|
||||
'Št: %s | Telo: %s | Napaka: %s',
|
||||
(string) $this->to,
|
||||
(string) $this->content,
|
||||
'SMS ni bil poslan!'
|
||||
);
|
||||
}
|
||||
|
||||
$case->activities()->create([
|
||||
'note' => $note,
|
||||
'action_id' => $template->action_id,
|
||||
'decision_id' => $template->decision_id,
|
||||
'user_id' => $this->userId,
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
\Log::warning('SendSmsJob activity creation failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'client_case_id' => $this->clientCaseId,
|
||||
'template_id' => $this->templateId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user