v1.0.0
This commit is contained in:
138
src/app/Livewire/Operator/NewTicket.php
Normal file
138
src/app/Livewire/Operator/NewTicket.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Operator;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Subcategory;
|
||||
use App\Models\User;
|
||||
use App\Services\TicketService;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
class NewTicket extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public int $step = 1;
|
||||
|
||||
public ?int $customerId = null;
|
||||
|
||||
public ?int $categoryId = null;
|
||||
|
||||
public ?int $subcategoryId = null;
|
||||
|
||||
public string $subject = '';
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public array $customValues = [];
|
||||
|
||||
public array $attachments = [];
|
||||
|
||||
#[Computed]
|
||||
public function clients()
|
||||
{
|
||||
return User::query()->withRole('client')->orderBy('name')->get();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function categories()
|
||||
{
|
||||
return Category::query()->with('subcategories')->get();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function selectedCategory(): ?Category
|
||||
{
|
||||
return $this->categoryId ? Category::query()->with('subcategories')->find($this->categoryId) : null;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function selectedSubcategory(): ?Subcategory
|
||||
{
|
||||
return $this->subcategoryId ? Subcategory::query()->with('customFields')->find($this->subcategoryId) : null;
|
||||
}
|
||||
|
||||
public function selectCategory(int $id): void
|
||||
{
|
||||
$this->categoryId = $id;
|
||||
$this->subcategoryId = null;
|
||||
$this->step = 2;
|
||||
}
|
||||
|
||||
public function selectSubcategory(int $id): void
|
||||
{
|
||||
$this->subcategoryId = $id;
|
||||
$this->step = 3;
|
||||
}
|
||||
|
||||
public function backToCategory(): void
|
||||
{
|
||||
$this->step = 1;
|
||||
}
|
||||
|
||||
public function backToSubcategory(): void
|
||||
{
|
||||
$this->step = 2;
|
||||
}
|
||||
|
||||
public function updatedAttachments(): void
|
||||
{
|
||||
if (! $this->attachments) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($error = Settings::validateAttachments($this->attachments)) {
|
||||
$this->attachments = [];
|
||||
$this->addError('attachments', $error);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeAttachment(int $index): void
|
||||
{
|
||||
unset($this->attachments[$index]);
|
||||
$this->attachments = array_values($this->attachments);
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$rules = [
|
||||
'customerId' => 'required|exists:users,id',
|
||||
'subject' => 'required|string|max:255',
|
||||
'body' => 'required|string',
|
||||
];
|
||||
|
||||
foreach ($this->selectedSubcategory?->customFields ?? [] as $field) {
|
||||
$rules["customValues.{$field->id}"] = $field->required ? 'required' : 'nullable';
|
||||
}
|
||||
|
||||
$this->validate($rules);
|
||||
|
||||
$customer = User::query()->findOrFail($this->customerId);
|
||||
|
||||
$ticket = app(TicketService::class)->create([
|
||||
'email' => $customer->email,
|
||||
'subcategory_id' => $this->subcategoryId,
|
||||
'subject' => $this->subject,
|
||||
'body' => $this->body,
|
||||
'custom_values' => $this->customValues,
|
||||
'assignee_id' => Auth::id(),
|
||||
], $customer);
|
||||
|
||||
$ticket->messages()->first()?->update([
|
||||
'body' => $ticket->body.' (zgłoszenie utworzone przez operatora w imieniu klienta)',
|
||||
]);
|
||||
|
||||
app(TicketService::class)->attachFiles($ticket, $ticket->messages()->first(), $this->attachments);
|
||||
|
||||
$this->redirect(route('operator.ticket', $ticket), navigate: true);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.operator.new-ticket');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user