1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 14:34:04 +00:00
This commit is contained in:
Abrar Ahmad 2022-11-04 12:43:09 +05:00
parent f13460d91f
commit 3187a9c5a8
3 changed files with 22 additions and 3 deletions

View file

@ -13,7 +13,6 @@ trait ResourceSyncing
public static function bootResourceSyncing(): void
{
static::saved(function (Syncable $model) {
dump($model->shouldSync());
if ($model->shouldSync()) {
$model->triggerSyncEvent();
}

View file

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

View file

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