From f13460d91f9b2a3a612cdcafd4d350fc984a6fde Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 4 Nov 2022 12:08:35 +0500 Subject: [PATCH] wip --- src/Contracts/Syncable.php | 2 ++ src/Database/Concerns/ResourceSyncing.php | 11 +++++-- tests/ResourceSyncingTest.php | 35 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Contracts/Syncable.php b/src/Contracts/Syncable.php index a481f318..f8e7fd84 100644 --- a/src/Contracts/Syncable.php +++ b/src/Contracts/Syncable.php @@ -18,4 +18,6 @@ interface Syncable /** Get the attributes used for creating the *other* model (i.e. tenant if this is the central one, and central if this is the tenant one). */ public function getSyncedCreationAttributes(): array|null; // todo come up with a better name + + public function shouldSync(): bool; } diff --git a/src/Database/Concerns/ResourceSyncing.php b/src/Database/Concerns/ResourceSyncing.php index fd63738d..fc7650b0 100644 --- a/src/Database/Concerns/ResourceSyncing.php +++ b/src/Database/Concerns/ResourceSyncing.php @@ -13,8 +13,10 @@ trait ResourceSyncing public static function bootResourceSyncing(): void { static::saved(function (Syncable $model) { - /** @var ResourceSyncing $model */ - $model->triggerSyncEvent(); + dump($model->shouldSync()); + if ($model->shouldSync()) { + $model->triggerSyncEvent(); + } }); static::creating(function (self $model) { @@ -37,4 +39,9 @@ trait ResourceSyncing { return null; } + + public function shouldSync(): bool + { + return true; + } } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 430c52ef..4f14756b 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -763,6 +763,25 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() expect(ResourceUser::first()->role)->toBe('commenter'); } +test('resources are synced only when sync is enabled', function () { + [$tenant1, $tenant2] = createTenantsAndRunMigrations(); + + tenancy()->initialize($tenant1); + + TenantUserWithDisabledSync::create([ + 'global_id' => 'absd', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'password', + 'role' => 'commenter', + ]); + + tenancy()->end(); + + expect(CentralUser::all())->toHaveCount(0); + +})->group('current'); + /** * Create two tenants and run migrations for those tenants. */ @@ -979,3 +998,19 @@ class ResourceUserProvidingMixture extends ResourceUser ]; } } + +class CentralUserWithDisabledSync extends CentralUser +{ + public function shouldSync(): bool + { + return false; + } +} + +class TenantUserWithDisabledSync extends CentralUser +{ + public function shouldSync(): bool + { + return false; + } +}