Updates for projects

This commit is contained in:
David Bomba 2024-03-22 15:16:22 +11:00
parent 1d5e121422
commit 2dd1dd6da6
3 changed files with 106 additions and 6 deletions

View File

@ -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');

View File

@ -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 [

View File

@ -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()
{