Add more permissions
This commit is contained in:
@@ -1778,7 +1778,31 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
|
||||
return back()->with('error', 'Unable to verify SMS credits.');
|
||||
}
|
||||
|
||||
// Queue the SMS send; activity will be created in the job on success if a template is provided
|
||||
// Create an activity before sending
|
||||
$activityNote = sprintf('Št: %s | Telo: %s', (string) $phone->nu, (string) $validated['message']);
|
||||
$activityData = [
|
||||
'note' => $activityNote,
|
||||
'user_id' => optional($request->user())->id,
|
||||
];
|
||||
// If template provided, attach its action/decision to the activity
|
||||
if (! empty($validated['template_id'])) {
|
||||
$tpl = \App\Models\SmsTemplate::find((int) $validated['template_id']);
|
||||
if ($tpl) {
|
||||
$activityData['action_id'] = $tpl->action_id;
|
||||
$activityData['decision_id'] = $tpl->decision_id;
|
||||
}
|
||||
}
|
||||
// Attach contract_id if contract_uuid is provided and belongs to this case
|
||||
if (! empty($validated['contract_uuid'])) {
|
||||
$contract = $clientCase->contracts()->where('uuid', $validated['contract_uuid'])->first();
|
||||
if ($contract) {
|
||||
$activityData['contract_id'] = $contract->id;
|
||||
}
|
||||
}
|
||||
|
||||
$activity = $clientCase->activities()->create($activityData);
|
||||
|
||||
// Queue the SMS send; pass activity_id so the job can update note on failure and skip creating a new activity
|
||||
\App\Jobs\SendSmsJob::dispatch(
|
||||
profileId: $profile->id,
|
||||
to: (string) $phone->nu,
|
||||
@@ -1790,6 +1814,7 @@ public function sendSmsToPhone(ClientCase $clientCase, Request $request, int $ph
|
||||
templateId: $validated['template_id'] ?? null,
|
||||
clientCaseId: $clientCase->id,
|
||||
userId: optional($request->user())->id,
|
||||
activityId: $activity?->id,
|
||||
);
|
||||
|
||||
return back()->with('success', 'SMS je bil dodan v čakalno vrsto.');
|
||||
|
||||
+24
-2
@@ -33,6 +33,8 @@ public function __construct(
|
||||
public ?int $templateId = null,
|
||||
public ?int $clientCaseId = null,
|
||||
public ?int $userId = null,
|
||||
// If provided, update this activity on failure instead of creating a new one
|
||||
public ?int $activityId = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -86,8 +88,28 @@ public function handle(SmsService $sms): void
|
||||
clientReference: $this->clientReference,
|
||||
);
|
||||
}
|
||||
// If invoked from the case UI with a selected template, create an Activity
|
||||
if ($this->templateId && $this->clientCaseId && $log) {
|
||||
// Update an existing pre-created activity ONLY on failure when activityId is provided
|
||||
if ($this->activityId && $log && ($log->status === 'failed')) {
|
||||
try {
|
||||
$activity = \App\Models\Activity::find($this->activityId);
|
||||
if ($activity) {
|
||||
$note = (string) ($activity->note ?? '');
|
||||
$append = sprintf(
|
||||
' | Napaka: %s',
|
||||
'SMS ni bil poslan!'
|
||||
);
|
||||
$activity->update(['note' => $note.$append]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
\Log::warning('SendSmsJob activity update failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'activity_id' => $this->activityId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
try {
|
||||
/** @var SmsTemplate|null $template */
|
||||
$template = SmsTemplate::find($this->templateId);
|
||||
|
||||
Reference in New Issue
Block a user