changes to sms packages and option to create user

This commit is contained in:
Simon Pocrnjič
2025-11-06 21:54:07 +01:00
parent ad8e0d5cee
commit 1395b72ae8
102 changed files with 1386 additions and 319 deletions
+23 -3
View File
@@ -10,6 +10,7 @@
use App\Models\SmsTemplate;
use App\Services\Sms\SmsService;
use Illuminate\Bus\Queueable;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
@@ -18,7 +19,7 @@
class PackageItemSmsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public function __construct(public int $packageItemId)
{
@@ -97,7 +98,7 @@ public function handle(SmsService $sms): void
/** @var SmsSender|null $sender */
$sender = $senderId ? SmsSender::find($senderId) : null;
/** @var SmsTemplate|null $template */
$template = $templateId ? SmsTemplate::find($templateId) : null;
$template = $templateId ? SmsTemplate::with(['action', 'decision'])->find($templateId) : null;
$to = $target['number'] ?? null;
if (! is_string($to) || $to === '') {
@@ -117,7 +118,7 @@ public function handle(SmsService $sms): void
$key = $scope === 'per_profile' && $profile ? "sms:{$provider}:{$profile->id}" : "sms:{$provider}";
// Throttle
$sendClosure = function () use ($sms, $item, $package, $profile, $sender, $template, $to, $variables, $deliveryReport, $bodyOverride) {
$sendClosure = function () use ($sms, $item, $package, $profile, $sender, $template, $to, $variables, $deliveryReport, $bodyOverride, $target) {
// Idempotency key (optional external use)
if (empty($item->idempotency_key)) {
$hash = sha1(implode('|', [
@@ -188,6 +189,25 @@ public function handle(SmsService $sms): void
$item->last_error = $log->status === 'sent' ? null : ($log->meta['error_message'] ?? 'Failed');
$item->save();
// Create activity if template has action_id and decision_id configured and SMS was sent successfully
if ($newStatus === 'sent' && $template && ($template->action_id || $template->decision_id)) {
if (! empty($target['contract_id'])) {
$contract = Contract::query()->with('clientCase')->find($target['contract_id']);
if ($contract && $contract->client_case_id) {
\App\Models\Activity::create(array_filter([
'client_case_id' => $contract->client_case_id,
'contract_id' => $contract->id,
'action_id' => $template->action_id,
'decision_id' => $template->decision_id,
'note' => "SMS poslan na {$to}: {$result['message']}",
'created_at' => now(),
'updated_at' => now(),
]));
}
}
}
// Update package counters atomically
if ($newStatus === 'sent') {
$package->increment('sent_count');
+1 -1
View File
@@ -109,7 +109,7 @@ public function handle(SmsService $sms): void
}
// If no pre-created activity is provided and invoked from the case UI with a selected template, create an Activity
if (!$this->activityId && $this->templateId && $this->clientCaseId && $log) {
if (! $this->activityId && $this->templateId && $this->clientCaseId && $log) {
try {
/** @var SmsTemplate|null $template */
$template = SmsTemplate::find($this->templateId);
+7 -2
View File
@@ -75,7 +75,8 @@ protected function performSmtpAuthTest(MailProfile $profile): void
}
$remote = ($encryption === 'ssl') ? 'ssl://'.$host : $host;
$errno = 0; $errstr = '';
$errno = 0;
$errstr = '';
$socket = @fsockopen($remote, $port, $errno, $errstr, 15);
if (! $socket) {
throw new \RuntimeException("Connect failed: $errstr ($errno)");
@@ -104,7 +105,9 @@ protected function performSmtpAuthTest(MailProfile $profile): void
// Cleanly quit
$this->command($socket, "QUIT\r\n", [221], 'QUIT');
} finally {
try { fclose($socket); } catch (\Throwable) {
try {
fclose($socket);
} catch (\Throwable) {
// ignore
}
}
@@ -116,6 +119,7 @@ protected function performSmtpAuthTest(MailProfile $profile): void
protected function command($socket, string $cmd, array $expect, string $context): string
{
fwrite($socket, $cmd);
return $this->expect($socket, $expect, $context);
}
@@ -138,6 +142,7 @@ protected function expect($socket, array $expectedCodes, string $context): strin
if (! in_array($code, $expectedCodes, true)) {
throw new \RuntimeException("Unexpected SMTP code $code during $context: ".implode(' | ', $lines));
}
return $line;
}
}