Andrew Wallo 3 månader sedan
förälder
incheckning
0b1046aecd
2 ändrade filer med 11 tillägg och 17 borttagningar
  1. 7
    8
      app/Concerns/CompanyOwned.php
  2. 4
    9
      app/Scopes/CurrentCompanyScope.php

+ 7
- 8
app/Concerns/CompanyOwned.php Visa fil

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

+ 4
- 9
app/Scopes/CurrentCompanyScope.php Visa fil

@@ -16,19 +16,14 @@ class CurrentCompanyScope implements Scope
16 16
      */
17 17
     public function apply(Builder $builder, Model $model): void
18 18
     {
19
-        $companyId = session('current_company_id');
20
-
21
-        if (! $companyId && app()->runningInConsole()) {
19
+        if (app()->runningInConsole()) {
22 20
             return;
23 21
         }
24 22
 
25
-        if (! $companyId && Auth::check() && Auth::user()->currentCompany) {
26
-            $companyId = Auth::user()->currentCompany->id;
27
-            session(['current_company_id' => $companyId]);
28
-        }
23
+        $companyId = session('current_company_id');
29 24
 
30
-        if (! $companyId) {
31
-            $companyId = Auth::user()->currentCompany->id;
25
+        if (! $companyId && ($user = Auth::user()) && ($companyId = $user->current_company_id)) {
26
+            session(['current_company_id' => $companyId]);
32 27
         }
33 28
 
34 29
         if ($companyId) {

Laddar…
Avbryt
Spara