1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 15:14:04 +00:00
This commit is contained in:
Abrar Ahmad 2022-11-04 12:08:35 +05:00
parent 77c5ae1f32
commit f13460d91f
3 changed files with 46 additions and 2 deletions

View file

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

View file

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

View file

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