From 4f55de65e426dc1c60488cc6f9b571d995fab17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Fri, 7 Oct 2022 11:39:25 +0300 Subject: [PATCH] The company must be assigned at least one user (#32wf88n) --- app/Jobs/Auth/UpdateUser.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/Jobs/Auth/UpdateUser.php b/app/Jobs/Auth/UpdateUser.php index 56cc7c3d5..1e2a044c6 100644 --- a/app/Jobs/Auth/UpdateUser.php +++ b/app/Jobs/Auth/UpdateUser.php @@ -87,5 +87,29 @@ class UpdateUser extends Job implements ShouldUpdate 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); + } + } } }