diff --git a/app/Transformers/TaskTransformer.php b/app/Transformers/TaskTransformer.php index 71b6d7c3436b..eb077d2e1d87 100644 --- a/app/Transformers/TaskTransformer.php +++ b/app/Transformers/TaskTransformer.php @@ -12,6 +12,7 @@ namespace App\Transformers; use App\Models\Document; +use App\Models\Project; use App\Models\Task; use App\Models\TaskStatus; use App\Utils\Traits\MakesHash; @@ -33,7 +34,8 @@ class TaskTransformer extends EntityTransformer */ protected $availableIncludes = [ 'client', - 'status' + 'status', + 'project', ]; public function includeDocuments(Task $task) @@ -65,6 +67,16 @@ class TaskTransformer extends EntityTransformer return $this->includeItem($task->status, $transformer, TaskStatus::class); } + public function includeProject(Task $task): ?Item + { + $transformer = new ProjectTransformer($this->serializer); + + if (!$task->project) { + return null; + } + + return $this->includeItem($task->project, $transformer, Project::class); + } public function transform(Task $task) { diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 2f0d54a4c7da..236f2fa66291 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -11,14 +11,15 @@ namespace Tests\Feature; +use Tests\TestCase; use App\Models\Task; +use App\Models\Project; +use Tests\MockAccountData; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Session; use Illuminate\Validation\ValidationException; -use Tests\MockAccountData; -use Tests\TestCase; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -358,6 +359,34 @@ class TaskApiTest extends TestCase $this->assertTrue($this->checkTimeLog($log)); } + public function testTaskListWithProjects() + { + + $project = Project::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'name' => 'proggy', + ]); + + $data = [ + 'project_id' => $this->encodePrimaryKey($project->id), + 'timelog' => [[1,2,'a'],[3,4,'d']], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/tasks?include=project', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals('proggy', $arr['data']['project']['name']); + + } + public function testTaskListClientStatus() { $response = $this->withHeaders([