From 2dd1dd6da6fbbfc7bb68adbc09f5b8221bef5a70 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 22 Mar 2024 15:16:22 +1100 Subject: [PATCH] Updates for projects --- app/Models/Project.php | 17 +++++++++ app/Transformers/ProjectTransformer.php | 45 ++++++++++++++++++++-- tests/Feature/ProjectApiTest.php | 50 +++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/app/Models/Project.php b/app/Models/Project.php index ca00a95d8ddc..3e5b3dc81748 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Laracasts\Presenter\PresentableTrait; @@ -121,6 +122,22 @@ class Project extends BaseModel return $this->hasMany(Task::class); } + public function expenses(): HasMany + { + return $this->hasMany(Expense::class); + } + + public function invoices(): HasMany + { + return $this->hasMany(Invoice::class); + } + + public function quotes(): HasMany + { + return $this->hasMany(Quote::class); + } + + public function translate_entity() { return ctrans('texts.project'); diff --git a/app/Transformers/ProjectTransformer.php b/app/Transformers/ProjectTransformer.php index 227e26571e85..69e728a854bd 100644 --- a/app/Transformers/ProjectTransformer.php +++ b/app/Transformers/ProjectTransformer.php @@ -12,10 +12,13 @@ namespace App\Transformers; -use App\Models\Client; -use App\Models\Document; -use App\Models\Project; use App\Models\Task; +use App\Models\Quote; +use App\Models\Client; +use App\Models\Project; +use App\Models\Document; +use App\Models\Expense; +use App\Models\Invoice; use App\Utils\Traits\MakesHash; /** @@ -35,6 +38,9 @@ class ProjectTransformer extends EntityTransformer protected array $availableIncludes = [ 'client', 'tasks', + 'invoices', + 'expenses', + 'quotes', ]; public function includeDocuments(Project $project) @@ -67,6 +73,39 @@ class ProjectTransformer extends EntityTransformer return $this->includeCollection($project->tasks, $transformer, Task::class); } + public function includeInvoices(Project $project): \League\Fractal\Resource\Collection + { + $transformer = new InvoiceTransformer($this->serializer); + + if(!$project->invoices) + return null; + + return $this->includeCollection($project->invoices, $transformer, Invoice::class); + } + + public function includeExpenses(Project $project): \League\Fractal\Resource\Collection + { + $transformer = new ExpenseTransformer($this->serializer); + + if(!$project->expenses) { + return null; + } + + return $this->includeCollection($project->expenses, $transformer, Expense::class); + } + + public function includeQuotes(Project $project): \League\Fractal\Resource\Collection + { + $transformer = new QuoteTransformer($this->serializer); + + if(!$project->quotes) { + return null; + } + + return $this->includeCollection($project->quotes, $transformer, Quote::class); + } + + public function transform(Project $project) { return [ diff --git a/tests/Feature/ProjectApiTest.php b/tests/Feature/ProjectApiTest.php index 3f2b6a5ad797..2dd3c183fe20 100644 --- a/tests/Feature/ProjectApiTest.php +++ b/tests/Feature/ProjectApiTest.php @@ -11,13 +11,16 @@ namespace Tests\Feature; +use App\Models\Expense; +use Tests\TestCase; +use App\Models\Invoice; +use App\Models\Quote; +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 @@ -44,6 +47,47 @@ class ProjectApiTest extends TestCase Model::reguard(); } + public function testProjectIncludes() + { + $i = Invoice::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->project->client_id, + 'project_id' => $this->project->id, + ]); + + + $e = Expense::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->project->client_id, + 'project_id' => $this->project->id, + ]); + + + $q = Quote::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->project->client_id, + 'project_id' => $this->project->id, + ]); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/projects/{$this->project->hashed_id}?include=expenses,invoices,quotes"); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals(1, count($arr['data']['invoices'])); + $this->assertEquals(1, count($arr['data']['expenses'])); + $this->assertEquals(1, count($arr['data']['quotes'])); + + } + public function testProjectValidationForBudgetedHoursPut() {