From abcd2fd1bb4e6ffe743bf384d83fb33a5b4f67ce Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 2 Dec 2019 15:18:36 +1100 Subject: [PATCH] Fixes for templates and bulk actions (#3112) * change route names from camelcase to snake case * Fixes for bulk actions * fixes for bulk actions * fixes for templates * fixes for templates --- app/Http/Controllers/InvoiceController.php | 25 ++++++++++++++++++--- app/Http/Controllers/TemplateController.php | 4 ++-- app/Http/Controllers/UserController.php | 4 ++-- app/Models/Invoice.php | 1 - database/factories/CompanyFactory.php | 2 +- routes/api.php | 4 ++-- tests/Feature/UserTest.php | 4 ++-- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 4b0b3c1e7363..8fbd0c9fd5c4 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -526,8 +526,10 @@ class InvoiceController extends BaseController $invoices->each(function ($invoice, $key) use($action){ +// $this->invoice_repo->{$action}($invoice); + if(auth()->user()->can('edit', $invoice)) - $this->invoice_repo->{$action}($invoice); + $this->performAction($invoice, $action, true); }); @@ -605,9 +607,16 @@ class InvoiceController extends BaseController public function action(ActionInvoiceRequest $request, Invoice $invoice, $action) { + return $this->performAction($invoice, $action); + + } + + private function performAction(Invoice $invoice, $action, $bulk = false) + { + /*If we are using bulk actions, we don't want to return anything */ switch ($action) { case 'clone_to_invoice': - $invoice = CloneInvoiceFactory::create($invocie, auth()->user()->id); + $invoice = CloneInvoiceFactory::create($invoice, auth()->user()->id); return $this->itemResponse($invoice); break; case 'clone_to_quote': @@ -625,10 +634,14 @@ class InvoiceController extends BaseController return $this->errorResponse(['message' => 'Invoice has no balance owing'], 400); $invoice = MarkInvoicePaid::dispatchNow($invoice); + + if(!$bulk) return $this->itemResponse($invoice); break; case 'mark_sent': $invoice->markSent(); + + if(!$bulk) return $this->itemResponse($invoice); break; case 'download': @@ -636,13 +649,19 @@ class InvoiceController extends BaseController break; case 'archive': $this->invoice_repo->archive($invoice); + + if(!$bulk) return $this->listResponse($invoice); break; case 'delete': $this->invoice_repo->delete($invoice); + + if(!$bulk) return $this->listResponse($invoice); break; case 'email': + + if(!$bulk) return response()->json(['message'=>'email sent'],200); break; @@ -651,5 +670,5 @@ class InvoiceController extends BaseController break; } } - + } diff --git a/app/Http/Controllers/TemplateController.php b/app/Http/Controllers/TemplateController.php index 60368249c88f..cfe2e51b9bed 100644 --- a/app/Http/Controllers/TemplateController.php +++ b/app/Http/Controllers/TemplateController.php @@ -103,8 +103,8 @@ class TemplateController extends BaseController if(request()->has('entity') && request()->has('entity_id')){ - $class = 'App\Models\\'.ucfirst($entity); - $entity_obj = $class::find($entity_id)->company(); + $class = 'App\Models\\'.ucfirst(request()->input('entity')); + $entity_obj = $class::whereId(request()->input('entity_id'))->company()->first(); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 97a09e7d5bb8..6892ed724a2b 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -537,7 +537,7 @@ class UserController extends BaseController * Attach an existing user to a company. * * @OA\Post( - * path="/api/v1/users/{user}/attachToCompany", + * path="/api/v1/users/{user}/attach_to_company", * operationId="attachUser", * tags={"users"}, * summary="Attach an existing user to a company", @@ -586,7 +586,7 @@ class UserController extends BaseController * Detach an existing user to a company. * * @OA\Delete( - * path="/api/v1/users/{user}/detachFromCompany", + * path="/api/v1/users/{user}/detach_from_company", * operationId="detachUser", * tags={"users"}, * summary="Detach an existing user to a company", diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 55f2ddffcdb4..350bc6943ac9 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -370,7 +370,6 @@ class Invoice extends BaseModel $balance_adjustment = floatval($balance_adjustment); - \Log::error("adjusting balance from ". $this->balance. " to ". ($this->balance + $balance_adjustment)); $this->balance = $this->balance + $balance_adjustment; if($this->balance == 0) { diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php index b4117903042d..f5ddfb9e3a80 100644 --- a/database/factories/CompanyFactory.php +++ b/database/factories/CompanyFactory.php @@ -10,7 +10,7 @@ $factory->define(App\Models\Company::class, function (Faker $faker) { 'ip' => $faker->ipv4, 'db' => config('database.default'), 'settings' => CompanySettings::defaults(), - 'custom_fields' => (object) ['custom1' => '1', 'custom2' => '2', 'custom3'=>3], + 'custom_fields' => (object) ['custom1' => '1', 'custom2' => '2', 'custom3'=>'3'], // 'address1' => $faker->secondaryAddress, // 'address2' => $faker->address, diff --git a/routes/api.php b/routes/api.php index bca82374d72f..eee4d6160813 100644 --- a/routes/api.php +++ b/routes/api.php @@ -70,8 +70,8 @@ Route::group(['middleware' => ['api_db','api_secret_check','token_auth'], 'prefi // Route::resource('users', 'UserController')->middleware('password_protected'); // name = (users. index / create / show / update / destroy / edit Route::resource('users', 'UserController'); // name = (users. index / create / show / update / destroy / edit - Route::post('users/{user}/attachToCompany', 'UserController@attach'); - Route::delete('users/{user}/detachFromCompany','UserController@detach'); + Route::post('users/{user}/attach_to_company', 'UserController@attach'); + Route::delete('users/{user}/detach_from_company','UserController@detach'); Route::post('users/bulk', 'UserController@bulk')->name('users.bulk')->middleware('password_protected'); diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 017dac9e31fd..3529d7afabe8 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -90,7 +90,7 @@ class UserTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/attachToCompany?include=company_user'); + ])->post('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/attach_to_company?include=company_user'); $response->assertStatus(200); @@ -101,7 +101,7 @@ class UserTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detachFromCompany?include=company_user'); + ])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detach_from_company?include=company_user'); $response->assertStatus(200);