diff --git a/app/Models/Project.php b/app/Models/Project.php index 62900d4e986f..e3669bc3ad7b 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -61,6 +61,11 @@ class Project extends BaseModel return $this->belongsTo(Client::class)->withTrashed(); } + public function documents() + { + return $this->morphMany(Document::class, 'documentable'); + } + // /** // * @return \Illuminate\Database\Eloquent\Relations\HasMany // */ diff --git a/app/Transformers/ExpenseTransformer.php b/app/Transformers/ExpenseTransformer.php index 7a78910a0b6a..8dcde2bfd23d 100644 --- a/app/Transformers/ExpenseTransformer.php +++ b/app/Transformers/ExpenseTransformer.php @@ -11,6 +11,7 @@ namespace App\Transformers; +use App\Models\Document; use App\Models\Expense; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\SoftDeletes; @@ -23,15 +24,23 @@ class ExpenseTransformer extends EntityTransformer use MakesHash; use SoftDeletes; protected $defaultIncludes = [ + 'documents', ]; /** * @var array */ protected $availableIncludes = [ - + 'documents', ]; + public function includeDocuments(Expense $expense) + { + $transformer = new DocumentTransformer($this->serializer); + + return $this->includeCollection($expense->documents, $transformer, Document::class); + } + /** * @param Expense $expense * diff --git a/app/Transformers/ProjectTransformer.php b/app/Transformers/ProjectTransformer.php index 8232603d14e3..6ba7d0749b97 100644 --- a/app/Transformers/ProjectTransformer.php +++ b/app/Transformers/ProjectTransformer.php @@ -11,6 +11,7 @@ namespace App\Transformers; +use App\Models\Document; use App\Models\Project; use App\Utils\Traits\MakesHash; @@ -22,14 +23,23 @@ class ProjectTransformer extends EntityTransformer use MakesHash; protected $defaultIncludes = [ + 'documents', ]; /** * @var array */ protected $availableIncludes = [ + 'documents' ]; + public function includeDocuments(Project $project) + { + $transformer = new DocumentTransformer($this->serializer); + + return $this->includeCollection($project->documents, $transformer, Document::class); + } + public function transform(Project $project) { return [ diff --git a/app/Transformers/VendorTransformer.php b/app/Transformers/VendorTransformer.php index a85e76a5af8d..e6a4c7799c03 100644 --- a/app/Transformers/VendorTransformer.php +++ b/app/Transformers/VendorTransformer.php @@ -12,6 +12,7 @@ namespace App\Transformers; use App\Models\Activity; +use App\Models\Document; use App\Models\Vendor; use App\Models\VendorContact; use App\Models\VendorGatewayToken; @@ -29,6 +30,7 @@ class VendorTransformer extends EntityTransformer protected $defaultIncludes = [ 'contacts', + 'documents' ]; /** @@ -36,6 +38,7 @@ class VendorTransformer extends EntityTransformer */ protected $availableIncludes = [ 'activities', + 'documents', ]; /** @@ -62,6 +65,13 @@ class VendorTransformer extends EntityTransformer return $this->includeCollection($vendor->contacts, $transformer, VendorContact::class); } + public function includeDocuments(Vendor $vendor) + { + $transformer = new DocumentTransformer($this->serializer); + + return $this->includeCollection($vendor->documents, $transformer, Document::class); + } + /** * @param Vendor $vendor * diff --git a/tests/Feature/DocumentsApiTest.php b/tests/Feature/DocumentsApiTest.php new file mode 100644 index 000000000000..512c918cb8f7 --- /dev/null +++ b/tests/Feature/DocumentsApiTest.php @@ -0,0 +1,143 @@ +makeTestData(); + + Session::start(); + + $this->faker = \Faker\Factory::create(); + + Model::reguard(); + } + + public function testClientDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/clients'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + + + public function testInvoiceDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + + + public function testProjectsDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/projects'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + + + public function testExpenseDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/expenses'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + + + public function testVendorDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/vendors'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + + + public function testProductDocuments() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/products'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertArrayHasKey('documents', $arr['data'][0]); + + } + +} diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 31c88f16fd76..27acbaf2b934 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -35,6 +35,7 @@ use App\Models\Expense; use App\Models\GroupSetting; use App\Models\Invoice; use App\Models\InvoiceInvitation; +use App\Models\Product; use App\Models\Project; use App\Models\Quote; use App\Models\QuoteInvitation; @@ -159,6 +160,11 @@ trait MockAccountData $company_token->save(); + Product::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + ]); + $this->client = Client::factory()->create([ 'user_id' => $this->user->id, 'company_id' => $this->company->id,