|
@@ -2,6 +2,7 @@
|
2
|
2
|
|
3
|
3
|
namespace App\Concerns;
|
4
|
4
|
|
|
5
|
+use App\Models\Notification;
|
5
|
6
|
use App\Models\User;
|
6
|
7
|
use App\Scopes\CurrentCompanyScope;
|
7
|
8
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
@@ -18,17 +19,15 @@ trait CompanyOwned
|
18
|
19
|
if (empty($model->company_id)) {
|
19
|
20
|
$companyId = session('current_company_id');
|
20
|
21
|
|
21
|
|
- if (! $companyId && Auth::check()) {
|
22
|
|
- $companyId = Auth::user()->currentCompany->id;
|
|
22
|
+ if (! $companyId && ($user = Auth::user()) && ($companyId = $user->current_company_id)) {
|
23
|
23
|
session(['current_company_id' => $companyId]);
|
24
|
24
|
}
|
25
|
25
|
|
26
|
|
- // Special handling for notifications in job context
|
27
|
|
- if (! $companyId && $model->getTable() === 'notifications' && isset($model->notifiable_id)) {
|
28
|
|
- // Get company_id from the notifiable user's current_company_id
|
29
|
|
- $user = User::find($model->notifiable_id);
|
30
|
|
- if ($user?->current_company_id) {
|
31
|
|
- $companyId = $user->current_company_id;
|
|
26
|
+ // For notifications in job context, get company_id from the notifiable user
|
|
27
|
+ if (! $companyId && $model instanceof Notification && $model->notifiable_type === User::class) {
|
|
28
|
+ $notifiable = $model->notifiable;
|
|
29
|
+ if ($notifiable instanceof User) {
|
|
30
|
+ $companyId = $notifiable->current_company_id;
|
32
|
31
|
}
|
33
|
32
|
}
|
34
|
33
|
|