1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Add form request for product bulk actions

This commit is contained in:
David Bomba 2023-04-19 17:31:29 +10:00
parent 78e429d559
commit f144252097
4 changed files with 107 additions and 6 deletions

View File

@ -13,6 +13,7 @@ namespace App\Http\Controllers;
use App\Factory\ProductFactory;
use App\Filters\ProductFilters;
use App\Http\Requests\Product\BulkProductRequest;
use App\Http\Requests\Product\CreateProductRequest;
use App\Http\Requests\Product\DestroyProductRequest;
use App\Http\Requests\Product\EditProductRequest;
@ -455,21 +456,32 @@ class ProductController extends BaseController
* ),
* )
*/
public function bulk()
public function bulk(BulkProductRequest $request)
{
$action = request()->input('action');
$action = $request->input('action');
$ids = request()->input('ids');
$ids = $request->input('ids');
$products = Product::withTrashed()->whereIn('id', $this->transformKeys($ids))->cursor();
$products = Product::withTrashed()->whereIn('id', $ids);
$products->each(function ($product, $key) use ($action) {
nlog($products->count());
if($action == 'set_tax_id'){
$tax_id = $request->input('tax_id');
$products->update(['tax_id' => $tax_id]);
return $this->listResponse(Product::withTrashed()->whereIn('id', $ids));
}
$products->cursor()->each(function ($product, $key) use ($action) {
if (auth()->user()->can('edit', $product)) {
$this->product_repo->{$action}($product);
}
});
return $this->listResponse(Product::withTrashed()->whereIn('id', $this->transformKeys($ids)));
return $this->listResponse(Product::withTrashed()->whereIn('id', $ids));
}
/**

View File

@ -0,0 +1,51 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Product;
use App\Http\Requests\Request;
use App\Utils\Traits\MakesHash;
use Illuminate\Validation\Rule;
class BulkProductRequest extends Request
{
use MakesHash;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return true;
}
public function rules()
{
return [
'ids' => ['required','bail','array',Rule::exists('products', 'id')->where('company_id', auth()->user()->company()->id)],
'action' => 'in:archive,restore,delete,set_tax_id',
'tax_id' => 'nullable|required_if:action,set_tax_id,in:1,2,3,4,5,6,7,8,9',
];
}
public function prepareForValidation()
{
$input = $this->all();
nlog($input);
if (isset($input['ids'])) {
$input['ids'] = $this->transformKeys($input['ids']);
}
$this->replace($input);
}
}

View File

@ -33,6 +33,7 @@ class ProductFactory extends Factory
'custom_value3' => $this->faker->text(20),
'custom_value4' => $this->faker->text(20),
'is_deleted' => false,
'tax_id' => 1,
];
}
}

View File

@ -45,6 +45,43 @@ class ProductTest extends TestCase
);
$this->makeTestData();
$this->withoutExceptionHandling();
}
public function testSetTaxId()
{
$p = Product::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id
]);
$this->assertEquals(1, $p->tax_id);
$update = [
'ids' => [$p->hashed_id],
'action' => 'set_tax_id',
'tax_id' => 6,
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/products/bulk', $update)
->assertStatus(200);
}
catch(\Exception $e){
}
$p = $p->fresh();
$this->assertEquals(6, $p->tax_id);
}
public function testProductGetProductKeyFilter()