mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 02:54:36 -04:00
Bulk assignment of clients to a group
This commit is contained in:
parent
1ed9d329a5
commit
dd28cb0372
@ -250,6 +250,14 @@ class ClientController extends BaseController
|
|||||||
return response()->json(['message' => $hash_or_response], 200);
|
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) {
|
$clients->each(function ($client) use ($action, $user) {
|
||||||
if ($user->can('edit', $client)) {
|
if ($user->can('edit', $client)) {
|
||||||
$this->client_repo->{$action}($client);
|
$this->client_repo->{$action}($client);
|
||||||
|
@ -35,10 +35,11 @@ class BulkClientRequest extends Request
|
|||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
return [
|
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)],
|
'ids' => ['required','bail','array',Rule::exists('clients', 'id')->where('company_id', $user->company()->id)],
|
||||||
'template' => 'sometimes|string',
|
'template' => 'sometimes|string',
|
||||||
'template_id' => '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'
|
'send_email' => 'sometimes|bool'
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -52,6 +53,10 @@ class BulkClientRequest extends Request
|
|||||||
$input['ids'] = $this->transformKeys($input['ids']);
|
$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);
|
$this->replace($input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,6 +126,21 @@ class ClientRepository extends BaseRepository
|
|||||||
ClientFactory::create($user->company()->id, $user->id)
|
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)
|
public function purge($client)
|
||||||
{
|
{
|
||||||
|
@ -21,12 +21,15 @@ use App\Models\Currency;
|
|||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use App\Models\CompanyToken;
|
use App\Models\CompanyToken;
|
||||||
|
use App\Models\GroupSetting;
|
||||||
use App\Models\ClientContact;
|
use App\Models\ClientContact;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Tests\Unit\GroupSettingsTest;
|
||||||
use App\DataMapper\ClientSettings;
|
use App\DataMapper\ClientSettings;
|
||||||
use App\DataMapper\CompanySettings;
|
use App\DataMapper\CompanySettings;
|
||||||
use App\DataMapper\DefaultSettings;
|
use App\DataMapper\DefaultSettings;
|
||||||
use App\Factory\InvoiceItemFactory;
|
use App\Factory\InvoiceItemFactory;
|
||||||
|
use App\Factory\GroupSettingFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
@ -69,6 +72,45 @@ class ClientTest extends TestCase
|
|||||||
$this->makeTestData();
|
$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()
|
public function testClientExchangeRateCalculation()
|
||||||
{
|
{
|
||||||
$settings = ClientSettings::defaults();
|
$settings = ClientSettings::defaults();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user