1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:14:03 +00:00

[4.x] Add feature to ignore the resource synchronization based on provided condition. (#993)

* wip

* add test

* readability

* remove group

* DisabledSync -> ConditionalSync; test both cases with dataset

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
Abrar Ahmad 2022-11-04 19:04:29 +05:00 committed by GitHub
parent 77c5ae1f32
commit 22d1b2065b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 3 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). */ /** 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 getSyncedCreationAttributes(): array|null; // todo come up with a better name
public function shouldSync(): bool;
} }

View file

@ -13,8 +13,9 @@ trait ResourceSyncing
public static function bootResourceSyncing(): void public static function bootResourceSyncing(): void
{ {
static::saved(function (Syncable $model) { static::saved(function (Syncable $model) {
/** @var ResourceSyncing $model */ if ($model->shouldSync()) {
$model->triggerSyncEvent(); $model->triggerSyncEvent();
}
}); });
static::creating(function (self $model) { static::creating(function (self $model) {
@ -37,4 +38,9 @@ trait ResourceSyncing
{ {
return null; return null;
} }
public function shouldSync(): bool
{
return true;
}
} }

View file

@ -14,7 +14,7 @@ class TenantPivot extends Pivot
static::saved(function (self $pivot) { static::saved(function (self $pivot) {
$parent = $pivot->pivotParent; $parent = $pivot->pivotParent;
if ($parent instanceof Syncable) { if ($parent instanceof Syncable && $parent->shouldSync()) {
$parent->triggerSyncEvent(); $parent->triggerSyncEvent();
} }
}); });

3
t Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
docker-compose exec -T test vendor/bin/pest --no-coverage --filter "$@"

View file

@ -763,6 +763,43 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
expect(ResourceUser::first()->role)->toBe('commenter'); expect(ResourceUser::first()->role)->toBe('commenter');
} }
test('resources are synced only when sync is enabled', function (bool $enabled) {
app()->instance('_tenancy_test_shouldSync', $enabled);
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
migrateUsersTableForTenants();
tenancy()->initialize($tenant1);
TenantUserWithConditionalSync::create([
'global_id' => 'absd',
'name' => 'John Doe',
'email' => 'john@localhost',
'password' => 'password',
'role' => 'commenter',
]);
tenancy()->end();
expect(CentralUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
expect(CentralUserWithConditionalSync::whereGlobalId('absd')->exists())->toBe($enabled);
$centralUser = CentralUserWithConditionalSync::create([
'global_id' => 'acme',
'name' => 'John Doe',
'email' => 'john@localhost',
'password' => 'password',
'role' => 'commenter',
]);
$centralUser->tenants()->attach('t2');
$tenant2->run(function () use ($enabled) {
expect(TenantUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
expect(TenantUserWithConditionalSync::whereGlobalId('acme')->exists())->toBe($enabled);
});
})->with([[true], [false]]);
/** /**
* Create two tenants and run migrations for those tenants. * Create two tenants and run migrations for those tenants.
*/ */
@ -979,3 +1016,19 @@ class ResourceUserProvidingMixture extends ResourceUser
]; ];
} }
} }
class CentralUserWithConditionalSync extends CentralUser
{
public function shouldSync(): bool
{
return app('_tenancy_test_shouldSync');
}
}
class TenantUserWithConditionalSync extends ResourceUser
{
public function shouldSync(): bool
{
return app('_tenancy_test_shouldSync');
}
}