From 7bd5a73d704c7efde6e25730f746bf00de3cd467 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 22 Jan 2023 15:11:33 +1100 Subject: [PATCH] Bulk action request for company gateways --- .../Controllers/CompanyGatewayController.php | 20 ++++----- .../BulkCompanyGatewayRequest.php | 45 +++++++++++++++++++ .../Feature/Bank/BankTransactionRuleTest.php | 1 - tests/Feature/CompanyGatewayApiTest.php | 44 ++++++++++++++++++ 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 app/Http/Requests/CompanyGateway/BulkCompanyGatewayRequest.php diff --git a/app/Http/Controllers/CompanyGatewayController.php b/app/Http/Controllers/CompanyGatewayController.php index 016884938f..a0239261fb 100644 --- a/app/Http/Controllers/CompanyGatewayController.php +++ b/app/Http/Controllers/CompanyGatewayController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers; use App\DataMapper\FeesAndLimits; use App\Factory\CompanyGatewayFactory; use App\Filters\CompanyGatewayFilters; +use App\Http\Requests\CompanyGateway\BulkCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\CreateCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\DestroyCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\EditCompanyGatewayRequest; @@ -496,19 +497,18 @@ class CompanyGatewayController extends BaseController * ), * ) */ - public function bulk() + public function bulk(BulkCompanyGatewayRequest $request) { - $action = request()->input('action'); + $action = $request->input('action'); - $ids = request()->input('ids'); + $ids = $request->input('ids'); - $company_gateways = CompanyGateway::withTrashed()->find($this->transformKeys($ids)); - - $company_gateways->each(function ($company_gateway, $key) use ($action) { - if (auth()->user()->can('edit', $company_gateway)) { - $this->company_repo->{$action}($company_gateway); - } - }); + $company_gateways = CompanyGateway::withTrashed() + ->whereIn('id',$this->transformKeys($ids)) + ->cursor() + ->each(function ($company_gateway, $key) use ($action) { + $this->company_repo->{$action}($company_gateway); + }); return $this->listResponse(CompanyGateway::withTrashed()->whereIn('id', $this->transformKeys($ids))); } diff --git a/app/Http/Requests/CompanyGateway/BulkCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/BulkCompanyGatewayRequest.php new file mode 100644 index 0000000000..3f28c367e0 --- /dev/null +++ b/app/Http/Requests/CompanyGateway/BulkCompanyGatewayRequest.php @@ -0,0 +1,45 @@ +user()->isAdmin(); + } + + public function rules() + { + + return [ + 'ids' => 'required|bail|array', + 'action' => 'in:archive,restore,delete' + ]; + + } + + +} diff --git a/tests/Feature/Bank/BankTransactionRuleTest.php b/tests/Feature/Bank/BankTransactionRuleTest.php index 87f0be350f..299c693998 100644 --- a/tests/Feature/Bank/BankTransactionRuleTest.php +++ b/tests/Feature/Bank/BankTransactionRuleTest.php @@ -76,7 +76,6 @@ class BankTransactionRuleTest extends TestCase ])->post('/api/v1/bank_transaction_rules/bulk', $data) ->assertStatus(200); - } public function testValidationContainsRule() diff --git a/tests/Feature/CompanyGatewayApiTest.php b/tests/Feature/CompanyGatewayApiTest.php index b036dd35c8..f976408188 100644 --- a/tests/Feature/CompanyGatewayApiTest.php +++ b/tests/Feature/CompanyGatewayApiTest.php @@ -12,6 +12,7 @@ namespace Tests\Feature; use App\DataMapper\FeesAndLimits; +use App\Factory\CompanyGatewayFactory; use App\Models\CompanyGateway; use App\Models\GatewayType; use App\Utils\Traits\CompanyGatewayFeesAndLimitsSaver; @@ -46,6 +47,49 @@ class CompanyGatewayApiTest extends TestCase Model::reguard(); } + public function testBulkActions() + { + $cg = CompanyGatewayFactory::create($this->company->id, $this->user->id); + $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $cg->save(); + + $data = [ + 'action' => 'archive', + 'ids' => [$cg->hashed_id] + ]; + + $response = $this->withHeaders([ + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/company_gateways/bulk', $data) + ->assertStatus(200); + + + $data = [ + 'ids' => [$cg->hashed_id], + 'action' => 'restore' + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/company_gateways/bulk', $data) + ->assertStatus(200); + + $data = [ + 'ids' => [$cg->hashed_id], + 'action' => 'delete' + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/company_gateways/bulk', $data) + ->assertStatus(200); + + } + + + public function testCompanyGatewayEndPointsWithIncorrectFields() { $data = [