KanbanController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\NotificationType;
  4. use App\Http\Controllers\Concerns\ResolvesActiveUnit;
  5. use App\Http\Requests\KanbanReorderRequest;
  6. use App\Http\Requests\KanbanRequest;
  7. use App\Http\Resources\KanbanResource;
  8. use App\Models\Unit;
  9. use App\Services\KanbanService;
  10. use App\Services\NotificationService;
  11. use Illuminate\Http\JsonResponse;
  12. class KanbanController extends Controller
  13. {
  14. use ResolvesActiveUnit;
  15. public function __construct(
  16. protected KanbanService $service,
  17. protected NotificationService $notifications,
  18. ) {}
  19. public function index(): JsonResponse
  20. {
  21. $user = auth()->user();
  22. if ($this->isMatriz($user)) {
  23. $items = $this->service->getAll();
  24. } else {
  25. $items = $this->service->getAllForUnit($this->activeUnitId($user));
  26. }
  27. return $this->successResponse(payload: KanbanResource::collection($items));
  28. }
  29. public function store(KanbanRequest $request): JsonResponse
  30. {
  31. $data = $request->validated();
  32. $user = auth()->user();
  33. $isMatriz = $this->isMatriz($user);
  34. $data['origin'] = $isMatriz ? 'matriz' : 'unit';
  35. $data['created_by_user_id'] = $user->id;
  36. $data['unit_id'] = $isMatriz ? null : $this->activeUnitId($user);
  37. // Broadcast to all units
  38. if ($isMatriz && ($data['scope'] ?? null) === 'all') {
  39. $unitIds = Unit::query()->pluck('id');
  40. $created = [];
  41. foreach ($unitIds as $unitId) {
  42. $card = $this->service->create(array_merge($data, [
  43. 'target_unit_id' => $unitId,
  44. ]));
  45. $this->notifyKanbanCreated($card);
  46. $created[] = $card;
  47. }
  48. return $this->successResponse(
  49. payload: KanbanResource::collection($created),
  50. message: __('messages.created'),
  51. code: 201
  52. );
  53. }
  54. if ($isMatriz) {
  55. if (($data['scope'] ?? null) === 'internal') {
  56. $data['target_unit_id'] = null;
  57. }
  58. // scope='specific' → target_unit_id already set from request
  59. } else {
  60. // Franchisee: internal = own unit, specific = Matriz (null target)
  61. $data['target_unit_id'] = (($data['scope'] ?? null) === 'internal')
  62. ? $this->activeUnitId($user)
  63. : null;
  64. }
  65. $item = $this->service->create($data);
  66. $this->notifyKanbanCreated($item);
  67. return $this->successResponse(
  68. payload: new KanbanResource($item),
  69. message: __('messages.created'),
  70. code: 201
  71. );
  72. }
  73. public function show(int $id): JsonResponse
  74. {
  75. $item = $this->service->findById($id);
  76. return $this->successResponse(payload: new KanbanResource($item));
  77. }
  78. public function update(KanbanRequest $request, int $id): JsonResponse
  79. {
  80. $item = $this->service->update($id, $request->validated());
  81. return $this->successResponse(payload: new KanbanResource($item), message: __('messages.updated'));
  82. }
  83. public function destroy(int $id): JsonResponse
  84. {
  85. $this->service->delete($id);
  86. return $this->successResponse(message: __('messages.deleted'), code: 204);
  87. }
  88. //
  89. public function reorder(KanbanReorderRequest $request): JsonResponse
  90. {
  91. $this->service->reorder($request->validated()['items']);
  92. return $this->successResponse(message: __('messages.updated'));
  93. }
  94. //
  95. private function isMatriz(\App\Models\User $user): bool
  96. {
  97. return $user->user_type === 'ADMIN';
  98. }
  99. /**
  100. * Notifica o outro lado quando uma atividade é criada:
  101. * franqueada → matriz; matriz → unidade alvo.
  102. */
  103. private function notifyKanbanCreated(\App\Models\Kanban $card): void
  104. {
  105. if ($card->origin === 'unit') {
  106. $unitId = $card->unit_id;
  107. $recipients = $this->notifications->matrizUserIds();
  108. } else {
  109. $unitId = $card->target_unit_id;
  110. $recipients = $this->notifications->unitUserIds($unitId);
  111. }
  112. $this->notifications->dispatch([
  113. 'origin_table' => 'kanbans',
  114. 'origin_id' => $card->id,
  115. 'unit_id' => $unitId,
  116. 'type' => NotificationType::KANBAN_CREATED->value,
  117. 'title' => 'Nova atividade',
  118. 'message' => $card->title,
  119. 'url' => '/kanban',
  120. 'action_text' => 'Ver atividade',
  121. 'priority' => $card->priority === 'alta' ? 'high' : 'normal',
  122. 'created_by_user_id' => $card->created_by_user_id,
  123. 'recipient_user_ids' => $recipients,
  124. ]);
  125. }
  126. }