KanbanController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Concerns\ResolvesActiveUnit;
  4. use App\Enums\NotificationType;
  5. use App\Services\KanbanService;
  6. use App\Services\NotificationService;
  7. use App\Http\Requests\KanbanRequest;
  8. use App\Http\Requests\KanbanReorderRequest;
  9. use App\Http\Resources\KanbanResource;
  10. use App\Models\Unit;
  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. /**
  74. * Notifica o outro lado quando uma atividade é criada:
  75. * franqueada → matriz; matriz → unidade alvo.
  76. */
  77. private function notifyKanbanCreated(\App\Models\Kanban $card): void
  78. {
  79. if ($card->origin === 'unit') {
  80. $unitId = $card->unit_id;
  81. $recipients = $this->notifications->matrizUserIds();
  82. } else {
  83. $unitId = $card->target_unit_id;
  84. $recipients = $this->notifications->unitUserIds($unitId);
  85. }
  86. $this->notifications->dispatch([
  87. 'origin_table' => 'kanbans',
  88. 'origin_id' => $card->id,
  89. 'unit_id' => $unitId,
  90. 'type' => NotificationType::KANBAN_CREATED->value,
  91. 'title' => 'Nova atividade',
  92. 'message' => $card->title,
  93. 'url' => '/kanban',
  94. 'action_text' => 'Ver atividade',
  95. 'priority' => $card->priority === 'alta' ? 'high' : 'normal',
  96. 'created_by_user_id' => $card->created_by_user_id,
  97. 'recipient_user_ids' => $recipients,
  98. ]);
  99. }
  100. public function show(int $id): JsonResponse
  101. {
  102. $item = $this->service->findById($id);
  103. return $this->successResponse(payload: new KanbanResource($item));
  104. }
  105. public function update(KanbanRequest $request, int $id): JsonResponse
  106. {
  107. $item = $this->service->update($id, $request->validated());
  108. return $this->successResponse(payload: new KanbanResource($item), message: __('messages.updated'));
  109. }
  110. public function destroy(int $id): JsonResponse
  111. {
  112. $this->service->delete($id);
  113. return $this->successResponse(message: __('messages.deleted'), code: 204);
  114. }
  115. public function reorder(KanbanReorderRequest $request): JsonResponse
  116. {
  117. $this->service->reorder($request->validated()['items']);
  118. return $this->successResponse(message: __('messages.updated'));
  119. }
  120. private function isMatriz(\App\Models\User $user): bool
  121. {
  122. return $user->user_type === 'ADMIN';
  123. }
  124. }