diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 060be29f0d..2044e6a306 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -228,7 +228,7 @@ class ClientController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $client); + $this->saveDocuments($request->file('documents'), $client, $request->input('is_public', true)); } return $this->itemResponse($client->fresh()); diff --git a/app/Http/Controllers/ClientPortal/UploadController.php b/app/Http/Controllers/ClientPortal/UploadController.php index 5099317910..d72b5c1ea3 100644 --- a/app/Http/Controllers/ClientPortal/UploadController.php +++ b/app/Http/Controllers/ClientPortal/UploadController.php @@ -34,7 +34,7 @@ class UploadController extends Controller /** @var \App\Models\ClientContact $client_contact **/ $client_contact = auth()->user(); - $this->saveDocuments($request->getFile(), $client_contact->client, true); + $this->saveDocuments($request->getFile(), $client_contact->client, $request->input('is_public', true)); return response([], 200); } diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 4347f6a28b..016485535b 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -424,7 +424,7 @@ class CompanyController extends BaseController $company = $this->company_repo->save($request->all(), $company); if ($request->has('documents')) { - $this->saveDocuments($request->input('documents'), $company, false); + $this->saveDocuments($request->input('documents'), $company, $request->input('is_public', true)); } if($request->has('e_invoice_certificate') && !is_null($request->file("e_invoice_certificate"))){ @@ -616,7 +616,7 @@ class CompanyController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $company); + $this->saveDocuments($request->file('documents'), $company, $request->input('is_public', true)); } return $this->itemResponse($company->fresh()); diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index e502f7ba29..f64b5efce9 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -776,7 +776,7 @@ class CreditController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $credit); + $this->saveDocuments($request->file('documents'), $credit, $request->input('is_public', true)); } return $this->itemResponse($credit->fresh()); diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index 476f206f5a..f7bc4e9cfd 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -564,7 +564,7 @@ class ExpenseController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $expense); + $this->saveDocuments($request->file('documents'), $expense, $request->input('is_public', true)); } return $this->itemResponse($expense->fresh()); diff --git a/app/Http/Controllers/GroupSettingController.php b/app/Http/Controllers/GroupSettingController.php index 85d141d1d7..e69d6cab7d 100644 --- a/app/Http/Controllers/GroupSettingController.php +++ b/app/Http/Controllers/GroupSettingController.php @@ -144,7 +144,7 @@ class GroupSettingController extends BaseController $this->uploadLogo($request->file('company_logo'), $group_setting->company, $group_setting); if ($request->has('documents')) { - $this->saveDocuments($request->input('documents'), $group_setting, false); + $this->saveDocuments($request->input('documents'), $group_setting, $request->input('is_public', true)); } return $this->itemResponse($group_setting); @@ -217,7 +217,7 @@ class GroupSettingController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $group_setting); + $this->saveDocuments($request->file('documents'), $group_setting, $request->input('is_public', true)); } return $this->itemResponse($group_setting->fresh()); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 626c22df9a..790755168c 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -977,7 +977,7 @@ class InvoiceController extends BaseController } if ($request->has('file')) { - $this->saveDocuments($request->file('documents'), $invoice, $request->input('is_public', true)); + $this->saveDocuments($request->file('file'), $invoice, $request->input('is_public', true)); } return $this->itemResponse($invoice->fresh()); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 8498c9d4bd..413f8213ef 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -749,7 +749,7 @@ class PaymentController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $payment); + $this->saveDocuments($request->file('documents'), $payment, $request->input('is_public', true)); } return $this->itemResponse($payment->fresh()); diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 5f4a4b38f1..d46bff3f9b 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -541,7 +541,7 @@ class ProductController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $product); + $this->saveDocuments($request->file('documents'), $product, $request->input('is_public', true)); } return $this->itemResponse($product->fresh()); diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e2be244ef0..085e7b1d7d 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -264,7 +264,7 @@ class ProjectController extends BaseController $project->saveQuietly(); if ($request->has('documents')) { - $this->saveDocuments($request->input('documents'), $project); + $this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true)); } event('eloquent.updated: App\Models\Project', $project); @@ -373,7 +373,7 @@ class ProjectController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->input('documents'), $project); + $this->saveDocuments($request->input('documents'), $project, $request->input('is_public', true)); } event('eloquent.created: App\Models\Project', $project); @@ -565,7 +565,7 @@ class ProjectController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $project); + $this->saveDocuments($request->file('documents'), $project, $request->input('is_public', true)); } return $this->itemResponse($project->fresh()); diff --git a/app/Http/Controllers/PurchaseOrderController.php b/app/Http/Controllers/PurchaseOrderController.php index 0355355781..f5eefe0fdc 100644 --- a/app/Http/Controllers/PurchaseOrderController.php +++ b/app/Http/Controllers/PurchaseOrderController.php @@ -751,7 +751,7 @@ class PurchaseOrderController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $purchase_order); + $this->saveDocuments($request->file('documents'), $purchase_order, $request->input('is_public', true)); } return $this->itemResponse($purchase_order->fresh()); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index cfbad5fe93..47eab0e968 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -905,7 +905,7 @@ class QuoteController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $quote); + $this->saveDocuments($request->file('documents'), $quote, $request->input('is_public', true)); } return $this->itemResponse($quote->fresh()); diff --git a/app/Http/Controllers/RecurringExpenseController.php b/app/Http/Controllers/RecurringExpenseController.php index 0a4d4ef593..e0d926fec8 100644 --- a/app/Http/Controllers/RecurringExpenseController.php +++ b/app/Http/Controllers/RecurringExpenseController.php @@ -609,7 +609,7 @@ class RecurringExpenseController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $recurring_expense); + $this->saveDocuments($request->file('documents'), $recurring_expense, $request->input('is_public', true)); } return $this->itemResponse($recurring_expense->fresh()); diff --git a/app/Http/Controllers/RecurringInvoiceController.php b/app/Http/Controllers/RecurringInvoiceController.php index daa59552c8..271c6e57df 100644 --- a/app/Http/Controllers/RecurringInvoiceController.php +++ b/app/Http/Controllers/RecurringInvoiceController.php @@ -550,7 +550,7 @@ class RecurringInvoiceController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $recurring_invoice); + $this->saveDocuments($request->file('documents'), $recurring_invoice, $request->input('is_public', true)); } return $this->itemResponse($recurring_invoice->fresh()); diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index c664a7a168..1d722b86a2 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -582,7 +582,7 @@ class TaskController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $task); + $this->saveDocuments($request->file('documents'), $task, $request->input('is_public', true)); } return $this->itemResponse($task->fresh()); diff --git a/app/Http/Controllers/VendorController.php b/app/Http/Controllers/VendorController.php index d13240f82d..5b074f20f4 100644 --- a/app/Http/Controllers/VendorController.php +++ b/app/Http/Controllers/VendorController.php @@ -568,7 +568,7 @@ class VendorController extends BaseController } if ($request->has('documents')) { - $this->saveDocuments($request->file('documents'), $vendor); + $this->saveDocuments($request->file('documents'), $vendor, $request->input('is_public', true)); } return $this->itemResponse($vendor->fresh()); diff --git a/app/Http/Controllers/VendorPortal/UploadController.php b/app/Http/Controllers/VendorPortal/UploadController.php index c9c6da6581..9de1e81c0e 100644 --- a/app/Http/Controllers/VendorPortal/UploadController.php +++ b/app/Http/Controllers/VendorPortal/UploadController.php @@ -33,7 +33,7 @@ class UploadController extends Controller */ public function upload(StoreUploadRequest $request, PurchaseOrder $purchase_order) { - $this->saveDocuments($request->getFile(), $purchase_order, true); + $this->saveDocuments($request->getFile(), $purchase_order, $request->input('is_public', true)); return response([], 200); } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 24cf207bad..c2ada683b2 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -31,7 +31,10 @@ class UpdateClientRequest extends Request */ public function authorize() : bool { - return auth()->user()->can('edit', $this->client); + /** @var \App\Models\User $user */ + $user = auth()->user(); + + return $user->can('edit', $this->client); } public function rules() diff --git a/app/Http/Requests/Document/StoreDocumentRequest.php b/app/Http/Requests/Document/StoreDocumentRequest.php index 6c4e1da2e4..e0eda8d112 100644 --- a/app/Http/Requests/Document/StoreDocumentRequest.php +++ b/app/Http/Requests/Document/StoreDocumentRequest.php @@ -23,12 +23,16 @@ class StoreDocumentRequest extends Request */ public function authorize() : bool { - return auth()->user()->can('create', Document::class); + /** @var \App\Models\User $user */ + $user = auth()->user(); + + return $user->can('create', Document::class); } public function rules() { return [ + 'is_public' => 'sometimes|boolean', ]; } @@ -36,6 +40,21 @@ class StoreDocumentRequest extends Request { $input = $this->all(); + if(isset($input['is_public'])) + $input['is_public'] = $this->toBoolean($input['is_public']); + $this->replace($input); } + + /** + * Convert to boolean + * + * @param $bool + * @return bool + */ + private function toBoolean($bool): bool + { + return filter_var($bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + } + } diff --git a/app/Http/Requests/Document/UpdateDocumentRequest.php b/app/Http/Requests/Document/UpdateDocumentRequest.php index ab9916a276..1843fc6586 100644 --- a/app/Http/Requests/Document/UpdateDocumentRequest.php +++ b/app/Http/Requests/Document/UpdateDocumentRequest.php @@ -34,14 +34,30 @@ class UpdateDocumentRequest extends Request public function rules() { return [ - 'name' => 'sometimes' + 'name' => 'sometimes', + 'is_public' => 'sometimes|boolean', ]; } + public function prepareForValidation() { $input = $this->all(); + if(isset($input['is_public'])) + $input['is_public'] = $this->toBoolean($input['is_public']); + $this->replace($input); } + + /** + * Convert to boolean + * + * @param $bool + * @return bool + */ + private function toBoolean($bool): bool + { + return filter_var($bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + } } diff --git a/app/Http/Requests/Invoice/UploadInvoiceRequest.php b/app/Http/Requests/Invoice/UploadInvoiceRequest.php index 8339f64aa3..b6ca345498 100644 --- a/app/Http/Requests/Invoice/UploadInvoiceRequest.php +++ b/app/Http/Requests/Invoice/UploadInvoiceRequest.php @@ -25,6 +25,7 @@ class UploadInvoiceRequest extends Request { /** @var \App\Models\User $user */ $user = auth()->user(); + return $user->can('edit', $this->invoice); } @@ -50,25 +51,6 @@ class UploadInvoiceRequest extends Request } public function prepareForValidation() - { - - //tests to see if upload via binary data works. - - // if(request()->getContent()) - // { - // // $file = new UploadedFile(request()->getContent(), request()->header('filename')); - // $file = new UploadedFile(request()->getContent(), 'something.png'); - // // request()->files->set('documents', $file); - - // $this->files->add(['file' => $file]); - - // // Merge it in request also (As I found this is not needed in every case) - // $this->merge(['file' => $file]); - - - // } - - - + { } } diff --git a/app/Models/Document.php b/app/Models/Document.php index e317e27c86..476c0f605c 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -77,6 +77,16 @@ class Document extends BaseModel 'name', ]; + /** + * @var array + */ + protected $casts = [ + 'is_public' => 'bool', + 'updated_at' => 'timestamp', + 'created_at' => 'timestamp', + 'deleted_at' => 'timestamp', + ]; + /** * @var array */ diff --git a/database/factories/DocumentFactory.php b/database/factories/DocumentFactory.php index b3183de7ff..23109a5a5d 100644 --- a/database/factories/DocumentFactory.php +++ b/database/factories/DocumentFactory.php @@ -25,7 +25,7 @@ class DocumentFactory extends Factory return [ 'is_default' => true, 'is_public' => true, - 'name' => true, + 'name' => $this->faker->word().".png", ]; } } diff --git a/tests/Feature/DocumentsApiTest.php b/tests/Feature/DocumentsApiTest.php index a7ad93e5a6..6d474df51c 100644 --- a/tests/Feature/DocumentsApiTest.php +++ b/tests/Feature/DocumentsApiTest.php @@ -11,12 +11,13 @@ namespace Tests\Feature; +use Tests\TestCase; +use App\Models\Document; +use Tests\MockAccountData; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Session; -use Tests\MockAccountData; -use Tests\TestCase; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -28,6 +29,8 @@ class DocumentsApiTest extends TestCase use DatabaseTransactions; use MockAccountData; + protected $faker; + protected function setUp() :void { parent::setUp(); @@ -41,6 +44,126 @@ class DocumentsApiTest extends TestCase Model::reguard(); } + public function testIsPublicTypesForDocumentRequest() + { + $d = Document::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get("/api/v1/documents/{$d->hashed_id}"); + + $response->assertStatus(200); + + $update = [ + 'is_public' => false, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertFalse($arr['data']['is_public']); + + $update = [ + 'is_public' => true, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertTrue($arr['data']['is_public']); + + $update = [ + 'is_public' => 'true', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertTrue($arr['data']['is_public']); + + $update = [ + 'is_public' => '1', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertTrue($arr['data']['is_public']); + + $update = [ + 'is_public' => 1, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertTrue($arr['data']['is_public']); + + $update = [ + 'is_public' => 'false', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertFalse($arr['data']['is_public']); + + $update = [ + 'is_public' => '0', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertFalse($arr['data']['is_public']); + + $update = [ + 'is_public' => 0, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson("/api/v1/documents/{$d->hashed_id}", $update); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertFalse($arr['data']['is_public']); + + } + public function testClientDocuments() { $response = $this->withHeaders([