54 lines
2.0 KiB
PHP
Executable File
54 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Services;
|
|
|
|
use App\Models\User;
|
|
use Modules\Helpdesk\Models\ChecklistItem;
|
|
use Modules\Helpdesk\Models\ChecklistTemplate;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
|
|
final class ChecklistService
|
|
{
|
|
public function applyTemplates(Ticket $ticket): void
|
|
{
|
|
ChecklistTemplate::query()
|
|
->where('is_active', true)
|
|
->where(static function ($query) use ($ticket): void {
|
|
$query->whereNull('company_id')->orWhere('company_id', $ticket->company_id);
|
|
})
|
|
->where(static function ($query) use ($ticket): void {
|
|
$query->whereNull('category_id')->orWhere('category_id', $ticket->category_id);
|
|
})
|
|
->where(static function ($query) use ($ticket): void {
|
|
$query->whereNull('project_id')->orWhere('project_id', $ticket->project_id);
|
|
})
|
|
->get()
|
|
->each(function (ChecklistTemplate $template) use ($ticket): void {
|
|
foreach (array_values($template->items ?? []) as $index => $item) {
|
|
ChecklistItem::query()->firstOrCreate([
|
|
'ticket_id' => $ticket->id,
|
|
'template_id' => $template->id,
|
|
'title' => is_array($item) ? (string) ($item['title'] ?? '') : (string) $item,
|
|
], [
|
|
'description' => is_array($item) ? ($item['description'] ?? null) : null,
|
|
'sort_order' => ($index + 1) * 10,
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function toggle(ChecklistItem $item, User $user): ChecklistItem
|
|
{
|
|
$completed = ! $item->is_completed;
|
|
$item->forceFill([
|
|
'is_completed' => $completed,
|
|
'completed_by' => $completed ? $user->id : null,
|
|
'completed_at' => $completed ? now() : null,
|
|
])->save();
|
|
|
|
return $item->fresh() ?? $item;
|
|
}
|
|
}
|