diff --git a/src/Database/Concerns/ResourceSyncing.php b/src/Database/Concerns/ResourceSyncing.php index fc7650b0..ea9f83b4 100644 --- a/src/Database/Concerns/ResourceSyncing.php +++ b/src/Database/Concerns/ResourceSyncing.php @@ -13,7 +13,6 @@ trait ResourceSyncing public static function bootResourceSyncing(): void { static::saved(function (Syncable $model) { - dump($model->shouldSync()); if ($model->shouldSync()) { $model->triggerSyncEvent(); } diff --git a/src/Database/Models/TenantPivot.php b/src/Database/Models/TenantPivot.php index 2c7583c1..8886f09e 100644 --- a/src/Database/Models/TenantPivot.php +++ b/src/Database/Models/TenantPivot.php @@ -14,7 +14,7 @@ class TenantPivot extends Pivot static::saved(function (self $pivot) { $parent = $pivot->pivotParent; - if ($parent instanceof Syncable) { + if (($parent instanceof Syncable) && $parent->shouldSync()) { $parent->triggerSyncEvent(); } }); diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 4f14756b..32eacf46 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -765,9 +765,12 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() test('resources are synced only when sync is enabled', function () { [$tenant1, $tenant2] = createTenantsAndRunMigrations(); + migrateUsersTableForTenants(); tenancy()->initialize($tenant1); + // Tenant model returns false from `shouldSync` + // central model will not be created TenantUserWithDisabledSync::create([ 'global_id' => 'absd', 'name' => 'John Doe', @@ -779,7 +782,24 @@ test('resources are synced only when sync is enabled', function () { tenancy()->end(); expect(CentralUser::all())->toHaveCount(0); + expect(CentralUser::whereGlobalId('absd')->first())->toBeNull(); + $centralUser = CentralUserWithDisabledSync::create([ + 'global_id' => 'acme', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'password', + 'role' => 'commenter', + ]); + + // central model returns false from `shouldSync` + // resource model will not be created + $centralUser->tenants()->attach('t2'); + + $tenant2->run(function () { + expect(ResourceUser::all())->toHaveCount(0); + expect(ResourceUser::whereGlobalId('acme')->first())->toBeNull(); + }); })->group('current'); /** @@ -1007,7 +1027,7 @@ class CentralUserWithDisabledSync extends CentralUser } } -class TenantUserWithDisabledSync extends CentralUser +class TenantUserWithDisabledSync extends ResourceUser { public function shouldSync(): bool {