| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Http\Controllers;
- use App\Enums\NotificationType;
- use App\Models\SupportTicket;
- use App\Services\NotificationService;
- use App\Services\SupportReplyService;
- use App\Http\Requests\SupportReplyRequest;
- use App\Http\Resources\SupportReplyResource;
- use Illuminate\Http\JsonResponse;
- class SupportReplyController extends Controller
- {
- public function __construct(
- protected SupportReplyService $service,
- protected NotificationService $notifications,
- ) {}
- public function index(int $ticketId): JsonResponse
- {
- $replies = $this->service->getByTicket($ticketId);
- return $this->successResponse(payload: SupportReplyResource::collection($replies));
- }
- public function store(SupportReplyRequest $request, int $ticketId): JsonResponse
- {
- $reply = $this->service->create($ticketId, auth()->id(), $request->validated()['reply']);
- $this->notifyReplyCreated($ticketId);
- return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.created'), code: 201);
- }
- /**
- * Notifica o outro lado do chamado quando há uma nova resposta.
- * Autor da matriz → unidade do chamado; autor da unidade → matriz.
- */
- private function notifyReplyCreated(int $ticketId): void
- {
- $ticket = SupportTicket::find($ticketId);
- if (!$ticket) {
- return;
- }
- $author = auth()->user();
- $authorIsMatriz = $author->user_type === 'ADMIN';
- $unitId = $ticket->applicant_unit_id ?? $ticket->target_unit_id;
- $recipients = $authorIsMatriz
- ? $this->notifications->unitUserIds($unitId)
- : $this->notifications->matrizUserIds();
- // Não notifica o próprio autor da resposta.
- $recipients = array_values(array_diff($recipients, [$author->id]));
- $this->notifications->dispatch([
- 'origin_table' => 'support_replies',
- 'origin_id' => $ticket->id,
- 'unit_id' => $unitId,
- 'type' => NotificationType::SUPPORT_TICKET_REPLY->value,
- 'title' => 'Nova resposta no chamado',
- 'message' => $ticket->title,
- 'url' => '/support',
- 'action_text' => 'Ver chamado',
- 'created_by_user_id' => $author->id,
- 'recipient_user_ids' => $recipients,
- ]);
- }
- public function update(SupportReplyRequest $request, int $ticketId, int $id): JsonResponse
- {
- $reply = $this->service->update($ticketId, $id, $request->validated()['reply']);
- return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.updated'));
- }
- public function destroy(int $ticketId, int $id): JsonResponse
- {
- $this->service->delete($ticketId, $id);
- return $this->successResponse(message: __('messages.deleted'), code: 204);
- }
- }
|