TaskResource.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 'parent_id' => $this->parent_id,
  34. 'parent' => TaskApi::getById($this->parent_id),
  35. 'roles' => $this->roles,
  36. 'executor_id' => $this->executor_id,
  37. 'executor_relation_task_id' => $this->executor_relation_task_id,
  38. 'executor_relation_task' => TaskApi::getById($this->executor_relation_task_id),
  39. 'pre_task' => TaskApi::getPreTasks($this->id),
  40. 'next_task' => TaskApi::getNextTasks($this->id),
  41. 'is_milestone' => $this->is_milestone,
  42. 'project_id' => $this->project_id,
  43. 'project' => ProjectApi::getById($this->project_id),
  44. 'owner_id' => $this->owner_id,
  45. "owner" => StudioApi::getById($this->owner_id),
  46. 'editor_id' => $this->editor_id,
  47. "editor" => UserApi::getByUuid($this->editor_id),
  48. 'order' => $this->order,
  49. 'status' => $this->status,
  50. 'created_at' => $this->created_at,
  51. 'updated_at' => $this->updated_at,
  52. 'started_at' => $this->started_at,
  53. 'finished_at' => $this->finished_at,
  54. ];
  55. $assignees = TaskAssignee::where('task_id', $this->id)->select('assignee_id')->get();
  56. if (count($assignees) > 0) {
  57. $assignees_id = [];
  58. foreach ($assignees as $key => $value) {
  59. $assignees_id[] = $value->assignee_id;
  60. }
  61. $data['assignees_id'] = $assignees_id;
  62. $data['assignees'] = UserApi::getListByUuid($assignees_id);
  63. }
  64. if (!empty($this->description)) {
  65. $data["html"] = $htmlRender->convert($this->description, []);
  66. }
  67. if (Str::isUuid($this->executor_id)) {
  68. $data["executor"] = UserApi::getByUuid($this->executor_id);
  69. }
  70. return $data;
  71. }
  72. }