KanbanController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Concerns\ResolvesActiveUnit;
  4. use App\Services\KanbanService;
  5. use App\Http\Requests\KanbanRequest;
  6. use App\Http\Requests\KanbanReorderRequest;
  7. use App\Http\Resources\KanbanResource;
  8. use App\Models\Unit;
  9. use Illuminate\Http\JsonResponse;
  10. class KanbanController extends Controller
  11. {
  12. use ResolvesActiveUnit;
  13. public function __construct(
  14. protected KanbanService $service,
  15. ) {}
  16. public function index(): JsonResponse
  17. {
  18. $user = auth()->user();
  19. if ($this->isMatriz($user)) {
  20. $items = $this->service->getAll();
  21. } else {
  22. $items = $this->service->getAllForUnit($this->activeUnitId($user));
  23. }
  24. return $this->successResponse(payload: KanbanResource::collection($items));
  25. }
  26. public function store(KanbanRequest $request): JsonResponse
  27. {
  28. $data = $request->validated();
  29. $user = auth()->user();
  30. $isMatriz = $this->isMatriz($user);
  31. $data['origin'] = $isMatriz ? 'matriz' : 'unit';
  32. $data['created_by_user_id'] = $user->id;
  33. $data['unit_id'] = $isMatriz ? null : $this->activeUnitId($user);
  34. // Broadcast to all units
  35. if ($isMatriz && ($data['scope'] ?? null) === 'all') {
  36. $unitIds = Unit::query()->pluck('id');
  37. $created = [];
  38. foreach ($unitIds as $unitId) {
  39. $created[] = $this->service->create(array_merge($data, [
  40. 'target_unit_id' => $unitId,
  41. ]));
  42. }
  43. return $this->successResponse(
  44. payload: KanbanResource::collection($created),
  45. message: __('messages.created'),
  46. code: 201
  47. );
  48. }
  49. if ($isMatriz) {
  50. if (($data['scope'] ?? null) === 'internal') {
  51. $data['target_unit_id'] = null;
  52. }
  53. // scope='specific' → target_unit_id already set from request
  54. } else {
  55. // Franchisee: internal = own unit, specific = Matriz (null target)
  56. $data['target_unit_id'] = (($data['scope'] ?? null) === 'internal')
  57. ? $this->activeUnitId($user)
  58. : null;
  59. }
  60. $item = $this->service->create($data);
  61. return $this->successResponse(
  62. payload: new KanbanResource($item),
  63. message: __('messages.created'),
  64. code: 201
  65. );
  66. }
  67. public function show(int $id): JsonResponse
  68. {
  69. $item = $this->service->findById($id);
  70. return $this->successResponse(payload: new KanbanResource($item));
  71. }
  72. public function update(KanbanRequest $request, int $id): JsonResponse
  73. {
  74. $item = $this->service->update($id, $request->validated());
  75. return $this->successResponse(payload: new KanbanResource($item), message: __('messages.updated'));
  76. }
  77. public function destroy(int $id): JsonResponse
  78. {
  79. $this->service->delete($id);
  80. return $this->successResponse(message: __('messages.deleted'), code: 204);
  81. }
  82. public function reorder(KanbanReorderRequest $request): JsonResponse
  83. {
  84. $this->service->reorder($request->validated()['items']);
  85. return $this->successResponse(message: __('messages.updated'));
  86. }
  87. private function isMatriz(\App\Models\User $user): bool
  88. {
  89. return $user->user_type === 'ADMIN';
  90. }
  91. }