From dd28cb0372e9f9338db9c821526193955f1d67a3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 2 Apr 2024 11:28:42 +1100 Subject: [PATCH] Bulk assignment of clients to a group --- app/Http/Controllers/ClientController.php | 8 ++++ .../Requests/Client/BulkClientRequest.php | 7 +++- app/Repositories/ClientRepository.php | 15 +++++++ tests/Feature/ClientTest.php | 42 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index e5098f1876d0..59986bced072 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -250,6 +250,14 @@ class ClientController extends BaseController return response()->json(['message' => $hash_or_response], 200); } + if($action == 'assign_group' && $user->can('edit', $clients->first())){ + + $this->client_repo->assignGroup($clients, $request->group_settings_id); + + return $this->listResponse(Client::query()->withTrashed()->company()->whereIn('id', $request->ids)); + + } + $clients->each(function ($client) use ($action, $user) { if ($user->can('edit', $client)) { $this->client_repo->{$action}($client); diff --git a/app/Http/Requests/Client/BulkClientRequest.php b/app/Http/Requests/Client/BulkClientRequest.php index bfe6a91cdde7..dbfad9556e35 100644 --- a/app/Http/Requests/Client/BulkClientRequest.php +++ b/app/Http/Requests/Client/BulkClientRequest.php @@ -35,10 +35,11 @@ class BulkClientRequest extends Request $user = auth()->user(); return [ - 'action' => 'required|string|in:archive,restore,delete,template', + 'action' => 'required|string|in:archive,restore,delete,template,assign_group', 'ids' => ['required','bail','array',Rule::exists('clients', 'id')->where('company_id', $user->company()->id)], 'template' => 'sometimes|string', 'template_id' => 'sometimes|string', + 'group_settings_id' => ['required_if:action,assign_group',Rule::exists('group_settings', 'id')->where('company_id', $user->company()->id)], 'send_email' => 'sometimes|bool' ]; @@ -52,6 +53,10 @@ class BulkClientRequest extends Request $input['ids'] = $this->transformKeys($input['ids']); } + if (isset($input['group_settings_id'])) { + $input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']); + } + $this->replace($input); } } diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index f7ec59ce7fba..3083a2314df9 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -126,6 +126,21 @@ class ClientRepository extends BaseRepository ClientFactory::create($user->company()->id, $user->id) ); } + + /** + * Bulk assign clients to a group. + * + * @param mixed $clients + * @param mixed $group_settings_id + * @return void + */ + public function assignGroup($clients, $group_settings_id): void + { + Client::query() + ->company() + ->whereIn('id', $clients->pluck('id')) + ->update(['group_settings_id' => $group_settings_id]); + } public function purge($client) { diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index e2850a2615df..44514d00f220 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -21,12 +21,15 @@ use App\Models\Currency; use Tests\MockAccountData; use Illuminate\Support\Str; use App\Models\CompanyToken; +use App\Models\GroupSetting; use App\Models\ClientContact; use App\Utils\Traits\MakesHash; +use Tests\Unit\GroupSettingsTest; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; use App\Factory\InvoiceItemFactory; +use App\Factory\GroupSettingFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Session; use Illuminate\Validation\ValidationException; @@ -69,6 +72,45 @@ class ClientTest extends TestCase $this->makeTestData(); } + public function testBulkGroupAssignment() + { + Client::factory()->count(5)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) { + ClientContact::factory()->create([ + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'company_id' => $this->company->id, + 'is_primary' => 1, + ]); + }); + + $gs = GroupSettingFactory::create($this->company->id, $this->user->id); + $gs->name = 'testtest'; + $gs->save(); + + $ids = Client::where('company_id', $this->company->id)->get()->pluck('hashed_id')->toArray(); + $data = [ + 'action' => 'assign_group', + 'ids' => $ids, + 'group_settings_id' => $gs->hashed_id, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients/bulk', $data); + + $arr = $response->json(); + + Client::query()->whereIn('id', $this->transformKeys($ids))->cursor()->each(function ($c) use ($gs, $arr) { + $this->assertEquals($gs->id, $c->group_settings_id); + }); + + foreach($arr['data'] as $client_response){ + + $this->assertEquals($gs->hashed_id, $client_response['group_settings_id']); + } + } + public function testClientExchangeRateCalculation() { $settings = ClientSettings::defaults();