TaskResource.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /*
  22. id: string;
  23. title: string;
  24. description?: string | null;
  25. assignees?: IUser[] | null;
  26. assignees_id?: string[] | null;
  27. parent?: ITaskData | null;
  28. parent_id?: string | null;
  29. roles?: string[] | null;
  30. executor?: IUser | null;
  31. executor_id?: string | null;
  32. executor_relation_task?: ITaskData | null;
  33. executor_relation_task_id?: string | null;
  34. pre_task?: ITaskData | null;
  35. pre_task_id?: string | null;
  36. is_milestone: boolean;
  37. project?: IProject|null;
  38. project_id?: string | null;
  39. owner?: IStudio;
  40. owner_id?: string | null;
  41. editor?: IUser;
  42. editor_id?: string | null;
  43. status?: TTaskStatus;
  44. created_at?: string;
  45. updated_at?: string;
  46. started_at?: string | null;
  47. finished_at?: string | null;
  48. */
  49. $htmlRender = new MdRender([
  50. 'mode' => 'read',
  51. 'format'=> 'react',
  52. 'footnote' => true,
  53. 'origin' => $request->get('origin',true),
  54. 'paragraph' => $request->get('paragraph',false),
  55. ]);
  56. $data = [
  57. 'id' => $this->id,
  58. 'title' => $this->title,
  59. 'description' => $this->description,
  60. 'parent_id' => $this->parent_id,
  61. 'parent' => TaskApi::getById($this->parent_id),
  62. 'roles' => $this->roles,
  63. 'executor_id' => $this->executor_id,
  64. 'executor_relation_task_id' => $this->executor_relation_task_id,
  65. 'executor_relation_task' => TaskApi::getById($this->executor_relation_task_id),
  66. 'pre_task' => TaskApi::getPreTasks($this->id),
  67. 'next_task' => TaskApi::getNextTasks($this->id),
  68. 'is_milestone' => $this->is_milestone,
  69. 'project_id' => $this->project_id,
  70. 'project' => ProjectApi::getById($this->project_id),
  71. 'owner_id' => $this->owner_id,
  72. "owner"=> StudioApi::getById($this->owner_id),
  73. 'editor_id' => $this->editor_id,
  74. "editor"=> UserApi::getByUuid($this->editor_id),
  75. 'order' => $this->order,
  76. 'status' => $this->status,
  77. 'created_at' => $this->created_at,
  78. 'updated_at' => $this->updated_at,
  79. 'started_at' => $this->started_at,
  80. 'finished_at' => $this->finished_at,
  81. ];
  82. $assignees = TaskAssignee::where('task_id',$this->id)->select('assignee_id')->get();
  83. if(count($assignees)>0){
  84. $assignees_id = [];
  85. foreach ($assignees as $key => $value) {
  86. $assignees_id[] = $value->assignee_id;
  87. }
  88. $data['assignees_id'] = $assignees_id;
  89. $data['assignees'] = UserApi::getListByUuid($assignees_id);
  90. }
  91. if(!empty($this->description)){
  92. $data["html"] = $htmlRender->convert($this->description,[]);
  93. }
  94. if(Str::isUuid($this->executor_id)){
  95. $data["executor"] = UserApi::getByUuid($this->executor_id);
  96. }
  97. return $data;
  98. }
  99. }