| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Http\Controllers;
- use App\Services\SupportTicketService;
- use App\Http\Requests\SupportTicketRequest;
- use App\Http\Resources\SupportTicketResource;
- use Illuminate\Http\JsonResponse;
- class SupportTicketController extends Controller
- {
- public function __construct(
- protected SupportTicketService $service,
- ) {}
- public function index(): JsonResponse
- {
- $user = auth()->user();
- $query = \App\Models\SupportTicket::query()
- ->with(['applicantUnit', 'targetUnit'])
- ->orderBy('created_at', 'desc');
- if ($this->isMatriz($user)) {
- $query->where(fn($q) => $q->where('origin', '!=', 'unit')->orWhere('scope', '!=', 'internal'));
- } else {
- $query->visibleToUnit($user->units()->first()?->id);
- }
- return $this->successResponse(
- payload: SupportTicketResource::collection($query->get())
- );
- }
- public function store(SupportTicketRequest $request): JsonResponse
- {
- $data = $request->validated();
- $user = auth()->user();
- $isMatriz = $this->isMatriz($user);
- $data['origin'] = $isMatriz ? 'matriz' : 'unit';
- $data['applicant_user_id'] = $user->id;
- $data['responsable_user_id'] = $user->id;
- $data['applicant_unit_id'] = $isMatriz ? null : $user->units()->first()?->id;
- $data['status'] = 'in_progress';
- // Broadcast: Matriz para todas as unidades
- if ($isMatriz && ($data['scope'] ?? null) === 'all') {
- $batchId = (string) \Illuminate\Support\Str::uuid();
- $unitIds = \App\Models\Unit::query()->pluck('id');
- $created = [];
- foreach ($unitIds as $unitId) {
- $created[] = $this->service->create(array_merge($data, [
- 'target_unit_id' => $unitId,
- 'batch_id' => $batchId,
- ]));
- }
- return $this->successResponse(
- payload: SupportTicketResource::collection($created),
- message: __('messages.created'),
- code: 201
- );
- }
- // Resolução de target_unit_id por cenário
- if ($isMatriz) {
- if (($data['scope'] ?? null) === 'internal') {
- $data['target_unit_id'] = null;
- }
- // scope='specific' → target_unit_id já veio do request
- } else {
- // Franchisee
- $data['target_unit_id'] = (($data['scope'] ?? null) === 'internal')
- ? $user->units()->first()?->id
- : null; // 'specific' do Franchisee = "para Matriz"
- }
- $item = $this->service->create($data);
- return $this->successResponse(
- payload: new SupportTicketResource($item),
- message: __('messages.created'),
- code: 201
- );
- }
- public function show(int $id): JsonResponse
- {
- $item = $this->service->findById($id);
- return $this->successResponse(payload: new SupportTicketResource($item));
- }
- public function update(SupportTicketRequest $request, int $id): JsonResponse
- {
- $user = auth()->user();
- $item = $this->service->findById($id);
- if (!$this->canManage($user, $item)) {
- return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
- }
- $item = $this->service->update($id, $request->validated());
- return $this->successResponse(payload: new SupportTicketResource($item), message: __('messages.updated'));
- }
- public function destroy(int $id): JsonResponse
- {
- $user = auth()->user();
- $item = $this->service->findById($id);
- if (!$this->canManage($user, $item)) {
- return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
- }
- $this->service->delete($id);
- return $this->successResponse(message: __('messages.deleted'), code: 204);
- }
- private function isMatriz(\App\Models\User $user): bool
- {
- return $user->user_type === 'ADMIN';
- }
- private function canManage(\App\Models\User $user, \App\Models\SupportTicket $ticket): bool
- {
- if ($this->isMatriz($user)) {
- return true;
- }
- // Franchisee só pode gerenciar tickets internos que ela mesma criou
- return $ticket->origin === 'unit'
- && $ticket->scope === 'internal'
- && $ticket->applicant_unit_id === $user->units()->first()?->id;
- }
- }
|