From 3594d1bea0bdf6a9e2af533489fbb13d986c13c0 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 4 Jul 2017 12:04:21 +0300 Subject: [PATCH] API optimizations --- app/Http/Controllers/BaseAPIController.php | 2 ++ app/Ninja/Transformers/DocumentTransformer.php | 4 ++-- app/Ninja/Transformers/ExpenseTransformer.php | 5 +++++ app/Ninja/Transformers/InvoiceTransformer.php | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/BaseAPIController.php b/app/Http/Controllers/BaseAPIController.php index 2de99be007..0219a7f165 100644 --- a/app/Http/Controllers/BaseAPIController.php +++ b/app/Http/Controllers/BaseAPIController.php @@ -206,6 +206,8 @@ class BaseAPIController extends Controller $data[] = 'clients.contacts'; } elseif ($include == 'vendors') { $data[] = 'vendors.vendor_contacts'; + } elseif ($include == 'documents' && $this->entityType == ENTITY_INVOICE) { + $data[] = 'documents.expense'; } elseif ($include) { $data[] = $include; } diff --git a/app/Ninja/Transformers/DocumentTransformer.php b/app/Ninja/Transformers/DocumentTransformer.php index 0237a306c0..df412106fa 100644 --- a/app/Ninja/Transformers/DocumentTransformer.php +++ b/app/Ninja/Transformers/DocumentTransformer.php @@ -25,8 +25,8 @@ class DocumentTransformer extends EntityTransformer 'name' => $document->name, 'type' => $document->type, 'path' => $document->path, - 'invoice_id' => isset($document->invoice->public_id) ? (int) $document->invoice->public_id : null, - 'expense_id' => isset($document->expense->public_id) ? (int) $document->expense->public_id : null, + 'invoice_id' => $document->invoice_id && $document->invoice ? (int) $document->invoice->public_id : null, + 'expense_id' => $document->expense_id && $document->expense ? (int) $document->expense->public_id : null, 'updated_at' => $this->getTimestamp($document->updated_at), ]); } diff --git a/app/Ninja/Transformers/ExpenseTransformer.php b/app/Ninja/Transformers/ExpenseTransformer.php index 60dbda1885..5a8317f70d 100644 --- a/app/Ninja/Transformers/ExpenseTransformer.php +++ b/app/Ninja/Transformers/ExpenseTransformer.php @@ -49,6 +49,11 @@ class ExpenseTransformer extends EntityTransformer { $transformer = new DocumentTransformer($this->account, $this->serializer); + $expense->documents->each(function ($document) use ($expense) { + $document->setRelation('expense', $expense); + $document->setRelation('invoice', $expense->invoice); + }); + return $this->includeCollection($expense->documents, $transformer, ENTITY_DOCUMENT); } diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index 5659919126..84aa450534 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -77,6 +77,10 @@ class InvoiceTransformer extends EntityTransformer { $transformer = new DocumentTransformer($this->account, $this->serializer); + $invoice->documents->each(function ($document) use ($invoice) { + $document->setRelation('invoice', $invoice); + }); + return $this->includeCollection($invoice->documents, $transformer, ENTITY_DOCUMENT); }