SupportReplyController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\NotificationType;
  4. use App\Http\Requests\SupportReplyRequest;
  5. use App\Http\Resources\SupportReplyResource;
  6. use App\Models\SupportTicket;
  7. use App\Services\NotificationService;
  8. use App\Services\SupportReplyService;
  9. use Illuminate\Http\JsonResponse;
  10. class SupportReplyController extends Controller
  11. {
  12. public function __construct(
  13. protected SupportReplyService $service,
  14. protected NotificationService $notifications,
  15. ) {}
  16. public function index(int $ticketId): JsonResponse
  17. {
  18. $replies = $this->service->getByTicket($ticketId);
  19. return $this->successResponse(payload: SupportReplyResource::collection($replies));
  20. }
  21. public function store(SupportReplyRequest $request, int $ticketId): JsonResponse
  22. {
  23. $reply = $this->service->create($ticketId, auth()->id(), $request->validated()['reply']);
  24. $this->notifyReplyCreated($ticketId);
  25. return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.created'), code: 201);
  26. }
  27. public function update(SupportReplyRequest $request, int $ticketId, int $id): JsonResponse
  28. {
  29. $reply = $this->service->update($ticketId, $id, $request->validated()['reply']);
  30. return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.updated'));
  31. }
  32. public function destroy(int $ticketId, int $id): JsonResponse
  33. {
  34. $this->service->delete($ticketId, $id);
  35. return $this->successResponse(message: __('messages.deleted'), code: 204);
  36. }
  37. /**
  38. * Notifica o outro lado do chamado quando há uma nova resposta.
  39. * Autor da matriz → unidade do chamado; autor da unidade → matriz.
  40. */
  41. private function notifyReplyCreated(int $ticketId): void
  42. {
  43. $ticket = SupportTicket::find($ticketId);
  44. if (!$ticket) {
  45. return;
  46. }
  47. $author = auth()->user();
  48. $authorIsMatriz = $author->user_type === 'ADMIN';
  49. $unitId = $ticket->applicant_unit_id ?? $ticket->target_unit_id;
  50. $recipients = $authorIsMatriz
  51. ? $this->notifications->unitUserIds($unitId)
  52. : $this->notifications->matrizUserIds();
  53. // Não notifica o próprio autor da resposta.
  54. $recipients = array_values(array_diff($recipients, [$author->id]));
  55. $this->notifications->dispatch([
  56. 'origin_table' => 'support_replies',
  57. 'origin_id' => $ticket->id,
  58. 'unit_id' => $unitId,
  59. 'type' => NotificationType::SUPPORT_TICKET_REPLY->value,
  60. 'title' => 'Nova resposta no chamado',
  61. 'message' => $ticket->title,
  62. 'url' => '/support',
  63. 'action_text' => 'Ver chamado',
  64. 'created_by_user_id' => $author->id,
  65. 'recipient_user_ids' => $recipients,
  66. ]);
  67. }
  68. }