KanbanController.php 3.1 KB

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