TaskResource.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\TaskApi;
  7. use App\Http\Api\ProjectApi;
  8. use App\Http\Api\MdRender;
  9. use App\Models\TaskAssignee;
  10. use Illuminate\Support\Str;
  11. class TaskResource extends JsonResource
  12. {
  13. /**
  14. * Transform the resource into an array.
  15. *
  16. * @param \Illuminate\Http\Request $request
  17. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  18. */
  19. public function toArray($request)
  20. {
  21. $htmlRender = new MdRender([
  22. 'mode' => 'read',
  23. 'format' => 'react',
  24. 'footnote' => true,
  25. 'origin' => $request->get('origin', true),
  26. 'paragraph' => $request->get('paragraph', false),
  27. ]);
  28. $data = [
  29. 'id' => $this->id,
  30. 'title' => $this->title,
  31. 'description' => $this->description,
  32. 'type' => $this->type,
  33. 'category' => $this->category,
  34. 'progress' => $this->progress,
  35. 'parent_id' => $this->parent_id,
  36. 'parent' => TaskApi::getById($this->parent_id),
  37. 'roles' => $this->roles,
  38. 'executor_id' => $this->executor_id,
  39. 'executor_relation_task_id' => $this->executor_relation_task_id,
  40. 'executor_relation_task' => TaskApi::getById($this->executor_relation_task_id),
  41. 'pre_task' => TaskApi::getPreTasks($this->id),
  42. 'next_task' => TaskApi::getNextTasks($this->id),
  43. 'is_milestone' => $this->is_milestone,
  44. 'project_id' => $this->project_id,
  45. 'project' => ProjectApi::getById($this->project_id),
  46. 'owner_id' => $this->owner_id,
  47. "owner" => StudioApi::getById($this->owner_id),
  48. 'editor_id' => $this->editor_id,
  49. "editor" => UserApi::getByUuid($this->editor_id),
  50. 'order' => $this->order,
  51. 'status' => $this->status,
  52. 'created_at' => $this->created_at,
  53. 'updated_at' => $this->updated_at,
  54. 'started_at' => $this->started_at,
  55. 'finished_at' => $this->finished_at,
  56. ];
  57. $assignees = TaskAssignee::where('task_id', $this->id)->select('assignee_id')->get();
  58. if (count($assignees) > 0) {
  59. $assignees_id = [];
  60. foreach ($assignees as $key => $value) {
  61. $assignees_id[] = $value->assignee_id;
  62. }
  63. $data['assignees_id'] = $assignees_id;
  64. $data['assignees'] = UserApi::getListByUuid($assignees_id);
  65. }
  66. if (!empty($this->description)) {
  67. $data["html"] = $htmlRender->convert($this->description, []);
  68. }
  69. if (Str::isUuid($this->executor_id)) {
  70. $data["executor"] = UserApi::getByUuid($this->executor_id);
  71. }
  72. return $data;
  73. }
  74. }