Andrew Wallo 3 months ago
parent
commit
41730ba0ee

+ 10
- 0
app/Concerns/CompanyOwned.php View File

2
 
2
 
3
 namespace App\Concerns;
3
 namespace App\Concerns;
4
 
4
 
5
+use App\Models\User;
5
 use App\Scopes\CurrentCompanyScope;
6
 use App\Scopes\CurrentCompanyScope;
6
 use Illuminate\Database\Eloquent\ModelNotFoundException;
7
 use Illuminate\Database\Eloquent\ModelNotFoundException;
7
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
22
                     session(['current_company_id' => $companyId]);
23
                     session(['current_company_id' => $companyId]);
23
                 }
24
                 }
24
 
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;
32
+                    }
33
+                }
34
+
25
                 if ($companyId) {
35
                 if ($companyId) {
26
                     $model->company_id = $companyId;
36
                     $model->company_id = $companyId;
27
                 } else {
37
                 } else {

+ 11
- 0
app/Models/Export.php View File

1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use App\Concerns\CompanyOwned;
6
+use Filament\Actions\Exports\Models\Export as BaseExport;
7
+
8
+class Export extends BaseExport
9
+{
10
+    use CompanyOwned;
11
+}

+ 11
- 0
app/Models/Import.php View File

1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use App\Concerns\CompanyOwned;
6
+use Filament\Actions\Imports\Models\Import as BaseImport;
7
+
8
+class Import extends BaseImport
9
+{
10
+    use CompanyOwned;
11
+}

+ 11
- 0
app/Models/Notification.php View File

1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use App\Concerns\CompanyOwned;
6
+use Illuminate\Notifications\DatabaseNotification;
7
+
8
+class Notification extends DatabaseNotification
9
+{
10
+    use CompanyOwned;
11
+}

+ 5
- 0
app/Models/User.php View File

124
 
124
 
125
         return true;
125
         return true;
126
     }
126
     }
127
+
128
+    public function notifications(): MorphMany
129
+    {
130
+        return $this->morphMany(Notification::class, 'notifiable')->latest();
131
+    }
127
 }
132
 }

+ 13
- 0
app/Providers/AppServiceProvider.php View File

3
 namespace App\Providers;
3
 namespace App\Providers;
4
 
4
 
5
 use App\Http\Responses\LoginRedirectResponse;
5
 use App\Http\Responses\LoginRedirectResponse;
6
+use App\Models\Export;
7
+use App\Models\Import;
8
+use App\Models\Notification;
6
 use App\Services\DateRangeService;
9
 use App\Services\DateRangeService;
10
+use Filament\Actions\Exports\Models\Export as BaseExport;
11
+use Filament\Actions\Imports\Models\Import as BaseImport;
7
 use Filament\Http\Responses\Auth\Contracts\LoginResponse;
12
 use Filament\Http\Responses\Auth\Contracts\LoginResponse;
8
 use Filament\Notifications\Livewire\Notifications;
13
 use Filament\Notifications\Livewire\Notifications;
9
 use Filament\Support\Assets\Js;
14
 use Filament\Support\Assets\Js;
10
 use Filament\Support\Enums\Alignment;
15
 use Filament\Support\Enums\Alignment;
11
 use Filament\Support\Facades\FilamentAsset;
16
 use Filament\Support\Facades\FilamentAsset;
17
+use Illuminate\Notifications\DatabaseNotification;
12
 use Illuminate\Support\ServiceProvider;
18
 use Illuminate\Support\ServiceProvider;
13
 
19
 
14
 class AppServiceProvider extends ServiceProvider
20
 class AppServiceProvider extends ServiceProvider
27
      */
33
      */
28
     public function boot(): void
34
     public function boot(): void
29
     {
35
     {
36
+        // Bind custom Import and Export models
37
+        $this->app->bind(BaseImport::class, Import::class);
38
+        $this->app->bind(BaseExport::class, Export::class);
39
+
40
+        // Bind custom Notification model
41
+        $this->app->bind(DatabaseNotification::class, Notification::class);
42
+
30
         Notifications::alignment(Alignment::Center);
43
         Notifications::alignment(Alignment::Center);
31
 
44
 
32
         FilamentAsset::register([
45
         FilamentAsset::register([

+ 29
- 0
database/migrations/2025_07_13_042000_add_company_id_to_notifications_table.php View File

1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\DB;
6
+use Illuminate\Support\Facades\Schema;
7
+
8
+return new class extends Migration
9
+{
10
+    /**
11
+     * Run the migrations.
12
+     */
13
+    public function up(): void
14
+    {
15
+        // Clear existing notifications since we're adding company scoping
16
+        DB::table('notifications')->truncate();
17
+
18
+        Schema::table('notifications', function (Blueprint $table) {
19
+            $table->foreignId('company_id')
20
+                ->after('notifiable_id')
21
+                ->nullable()
22
+                ->constrained()
23
+                ->cascadeOnDelete();
24
+
25
+            $table->index(['company_id', 'created_at']);
26
+            $table->index(['company_id', 'notifiable_type', 'notifiable_id']);
27
+        });
28
+    }
29
+};

+ 42
- 0
database/migrations/2025_07_13_043000_add_company_id_to_imports_exports_tables.php View File

1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\DB;
6
+use Illuminate\Support\Facades\Schema;
7
+
8
+return new class extends Migration
9
+{
10
+    /**
11
+     * Run the migrations.
12
+     */
13
+    public function up(): void
14
+    {
15
+        // Disable foreign key checks and clear existing data
16
+        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
17
+        DB::table('failed_import_rows')->truncate();
18
+        DB::table('exports')->truncate();
19
+        DB::table('imports')->truncate();
20
+        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
21
+
22
+        Schema::table('imports', function (Blueprint $table) {
23
+            $table->foreignId('company_id')
24
+                ->after('id')
25
+                ->nullable()
26
+                ->constrained()
27
+                ->cascadeOnDelete();
28
+
29
+            $table->index(['company_id', 'created_at']);
30
+        });
31
+
32
+        Schema::table('exports', function (Blueprint $table) {
33
+            $table->foreignId('company_id')
34
+                ->after('id')
35
+                ->nullable()
36
+                ->constrained()
37
+                ->cascadeOnDelete();
38
+
39
+            $table->index(['company_id', 'created_at']);
40
+        });
41
+    }
42
+};

Loading…
Cancel
Save