Browse Source

project gantt

moell 1 year ago
parent
commit
42d52f99da

+ 16 - 1
app/Http/Controllers/API/ProjectController.php

@@ -28,6 +28,7 @@ use App\Models\Task;
 use App\Models\User;
 use App\Repositories\ActionRepository;
 use App\Services\Project\ProjectKanbanService;
+use App\Services\Project\ProjectGanttService;
 use App\Services\Project\ProjectTaskGroupViewService;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -357,9 +358,23 @@ class ProjectController extends Controller
         ) ? $request->get("group") : "requirement_id";
 
 
-
         return $this->success([
             'data' => (new ProjectTaskGroupViewService())->groupView($project, $group, $request->all())
         ]);
     }
+
+    public function gantt(Request $request, string $id)
+    {
+        $project = Project::findOrFail($id);
+
+        $group = in_array(
+            $request->get("group"),
+            ['requirement_id','assign','task_type']
+        ) ? $request->get("group") : "task_type";
+
+
+        return $this->success([
+            'data' => (new ProjectGanttService())->gantt($project, $group)
+        ]);
+    }
 }

+ 26 - 0
app/Http/Resources/API/ProjectGanttResource.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ProjectGanttResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @return array<string, mixed>
+     */
+    public function toArray(Request $request): array
+    {
+        return [
+            "id" => $this->id,
+            "name" => $this->name,
+            "status" => $this->status,
+            "assign" => new UserProfileResource($this->assignTo),
+            "begin" => (string)$this->begin,
+            "end" => (string)$this->end,
+        ];
+    }
+}

+ 63 - 0
app/Services/Project/ProjectGanttService.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Services\Project;
+
+use App\Http\Resources\API\ProjectGanttResource;
+use App\Http\Resources\API\ProjectGroupViewTaskResource;
+use App\Models\Enums\ProjectStatus;
+use App\Models\Project;
+use App\Models\Requirement;
+use App\Models\Task;
+use App\Models\User;
+use Illuminate\Support\Collection;
+
+class ProjectGanttService
+{
+    public function gantt(Project $project, string $group): array
+    {
+        $groupTasks = $this->getGroupTask($project, $group);
+
+        $groups = $groupTasks->keys()->filter();
+
+        $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group);
+
+        $items = [];
+        foreach(["", ...$groups] as $groupKey) {
+            if (! isset($groupTasks[$groupKey])) {
+                continue;
+            }
+
+            $items[] = [
+                'group' => $group,
+                'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
+                'tasks' => ProjectGanttResource::collection($groupTasks[$groupKey]),
+            ];
+        }
+
+        return $items;
+    }
+
+    protected function getGroupNamesKeyBy(Collection $groups, string $group): array
+    {
+        $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]);
+        $groupNames = match ($group) {
+            "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(),
+            "task_type" => $groupsFormat,
+            "assign" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
+        };
+
+        return $groupNames->keyBy("id")->toArray();
+    }
+
+    protected function getGroupTask(
+        Project $project,
+        string $group,
+    ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
+    {
+        return Task::query()
+            ->with(['assignTo', 'finishedBy'])
+            ->where("project_id", $project->id)
+            ->get()
+            ->groupBy($group);
+    }
+}

+ 1 - 0
routes/api.php

@@ -47,6 +47,7 @@ Route::middleware(['auth:sanctum'])->group(function () {
         Route::get("project/{project}/dynamic", [API\ProjectController::class, "dynamic"])->name("project.dynamic");
         Route::get("project/{project}/kanban", [API\ProjectController::class, "kanban"])->name("project.kanban");
         Route::get("project/{project}/group-view", [API\ProjectController::class, "groupView"])->name("project.group-view");
+        Route::get("project/{project}/gantt", [API\ProjectController::class, "gantt"])->name("project.gantt");
         Route::patch("project/{project}/closed", [API\ProjectController::class, "closed"])->name("project.closed");
         Route::patch("project/{project}/start", [API\ProjectController::class, "start"])->name("project.start");
         Route::patch("project/{project}/pause", [API\ProjectController::class, "pause"])->name("project.pause");