Bulk assignment of clients to a group

This commit is contained in:
David Bomba 2024-04-02 11:28:42 +11:00
parent 1ed9d329a5
commit dd28cb0372
4 changed files with 71 additions and 1 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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)
{

View File

@ -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();