You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.2 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Activity;
  4. use App\Rules\MaxBound;
  5. use Illuminate\Http\Request;
  6. use Spatie\QueryBuilder\AllowedFilter;
  7. use Spatie\QueryBuilder\QueryBuilder;
  8. class ActivityController extends Controller
  9. {
  10. public function index($business, Request $request)
  11. {
  12. permit('businessAccess');
  13. $this->indexValidation($request);
  14. $per_page = $request->limit > 100 ? 10 : $request->limit;
  15. return $this->indexFiltering($business)->paginate($per_page);
  16. }
  17. public function indexValidation($request)
  18. {
  19. $bound = 10;
  20. $this->validate($request, [
  21. 'filter.project_id' => [new MaxBound($bound)] ,
  22. 'filter.system_id' => [new MaxBound($bound)] ,
  23. 'filter.workflow_id' => [new MaxBound($bound)] ,
  24. 'filter.status_id' => [new MaxBound($bound)] ,
  25. 'filter.sprint_id' => [new MaxBound($bound)] ,
  26. 'filter.actor_id' => [new MaxBound($bound)] ,
  27. 'filter.user_id' => [new MaxBound($bound)] ,
  28. 'filter.subject_id' => [new MaxBound($bound)] ,
  29. //todo: validation for crud_id and table_id
  30. 'filter.creates_before' => 'bail|nullable|date|date_format:Y-m-d' ,
  31. 'filter.creates_after' => 'bail|nullable|date|date_format:Y-m-d' ,
  32. 'filter.creates_in' => 'bail|nullable|numeric|max:90' ,
  33. ]);
  34. }
  35. public function indexFiltering($business)
  36. {
  37. $query = Activity::where('business_id', $business);
  38. $activityQ = QueryBuilder::for($query)
  39. ->allowedFilters([
  40. AllowedFilter::exact('project_id'),
  41. AllowedFilter::exact('system_id'),
  42. AllowedFilter::exact('workflow_id'),
  43. AllowedFilter::exact('status_id'),
  44. AllowedFilter::exact('sprint_id'),
  45. AllowedFilter::exact('task_id'),
  46. AllowedFilter::exact('actor_id'),
  47. AllowedFilter::exact('user_id'),
  48. AllowedFilter::exact('crud_id'),
  49. AllowedFilter::exact('table_id'),
  50. AllowedFilter::exact('subject_id'),
  51. AllowedFilter::scope('creates_before'),
  52. AllowedFilter::scope('creates_after'),
  53. AllowedFilter::scope('creates_in'),
  54. ])
  55. ->defaultSort('-id')
  56. ->allowedSorts('id', 'created_at');
  57. if (\request('_business_info')['info']['users'][\auth()->id()]['level'] != enum('levels.owner.id')) {
  58. $requested_projects = isset(\request('filter')['project_id']) ?
  59. array_unique(explode(',',\request('filter')['project_id'] ?? null )) :
  60. null;
  61. $requested_projects = collect($requested_projects)->keyBy(null)->toArray();
  62. $project_ids = $this->myStateProjects($requested_projects);
  63. $activityQ->where(function ($q) use ($project_ids) {
  64. $q->whereIn('project_id', $project_ids['non_guest_ids'])
  65. ->orWhere(function ($q) use ($project_ids) {
  66. $q->whereIn('project_id', $project_ids['guest_ids'])
  67. ->where('user_id', auth()->id());
  68. });
  69. });
  70. }
  71. return $activityQ;
  72. }
  73. public function myStateProjects($requested_projects)
  74. {
  75. $non_guest_ids = [];
  76. $guest_ids = [];
  77. $is_empty = empty($requested_projects);
  78. foreach (\request('_business_info')['info']['projects'] as $p_id => $p) {
  79. $level = \request('_business_info')['info']['projects'][$p_id]['members'][\auth()->id()]['level'];
  80. if (( $is_empty || isset($requested_projects[$p_id]))
  81. && $level > enum('levels.guest.id')) {
  82. array_push($non_guest_ids, $p_id);
  83. }
  84. if (( $is_empty || isset($requested_projects[$p_id]))
  85. && $level == enum('levels.guest.id')) {
  86. array_push($guest_ids, $p_id);
  87. }
  88. }
  89. return ['non_guest_ids' => $non_guest_ids, 'guest_ids' => $guest_ids];
  90. }
  91. public function store($business, Request $request)
  92. {
  93. return Activity::create($request->merge(['business_id' => $business])->all());
  94. }
  95. public function delete()
  96. {
  97. }
  98. }