|
@@ -15,14 +15,71 @@ public function __construct(
|
|
|
|
|
|
|
|
public function index(): JsonResponse
|
|
public function index(): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $items = $this->service->getAll();
|
|
|
|
|
- return $this->successResponse(payload: SupportTicketResource::collection($items));
|
|
|
|
|
|
|
+ $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
|
|
public function store(SupportTicketRequest $request): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $item = $this->service->create($request->validated());
|
|
|
|
|
- return $this->successResponse(payload: new SupportTicketResource($item), message: __('messages.created'), code: 201);
|
|
|
|
|
|
|
+ $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
|
|
public function show(int $id): JsonResponse
|
|
@@ -33,13 +90,44 @@ public function show(int $id): JsonResponse
|
|
|
|
|
|
|
|
public function update(SupportTicketRequest $request, int $id): JsonResponse
|
|
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());
|
|
$item = $this->service->update($id, $request->validated());
|
|
|
return $this->successResponse(payload: new SupportTicketResource($item), message: __('messages.updated'));
|
|
return $this->successResponse(payload: new SupportTicketResource($item), message: __('messages.updated'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
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);
|
|
$this->service->delete($id);
|
|
|
return $this->successResponse(message: __('messages.deleted'), code: 204);
|
|
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;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|