The company must be assigned at least one user (#32wf88n)

This commit is contained in:
Cüneyt Şentürk 2022-10-07 11:39:25 +03:00
parent 95dd407643
commit 4f55de65e4

View File

@ -87,5 +87,29 @@ class UpdateUser extends Job implements ShouldUpdate
throw new \Exception($message); throw new \Exception($message);
} }
// Can't unassigned company, The company must be assigned at least one user.
$companies = $this->request->get('companies');
$user_companies = $this->model->companies()->pluck('id')->toArray();
$company_diff = array_diff($user_companies, $companies);
if ($company_diff) {
$errors = [];
foreach ($company_diff as $company_id) {
$company = Company::withCount('users')->find($company_id);
if ($company->users_count < 2) {
$errors[] = trans('auth.error.unassigned', ['company' => $company->name]);
}
}
if ($errors) {
$message = implode('\n', $errors);
throw new \Exception($message);
}
}
} }
} }