mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:04:03 +00:00
Move triggerRestoredEvent(), test restoring Syncables
Update comments in DeleteResourceMapping to explain the detach() behavior. Move triggerRestoredEvent() from SyncMaster to Syncable interface, test restoring tenant resources.
This commit is contained in:
parent
f53fcb279f
commit
2b45a01018
4 changed files with 14 additions and 5 deletions
|
|
@ -34,8 +34,8 @@ class DeleteResourceMapping extends QueueableListener
|
||||||
// or the central resource was deleted using forceDelete()
|
// or the central resource was deleted using forceDelete()
|
||||||
if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
|
if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
|
||||||
Pivot::withoutEvents(function () use ($centralResource, $event) {
|
Pivot::withoutEvents(function () use ($centralResource, $event) {
|
||||||
// $event->tenant is null when the deleted resource is a SyncMaster - all mappings are deleted in that case
|
// If detach() is called with null -- if $event->tenant is null -- this means a central resource was deleted and detaches all tenants.
|
||||||
// When $event->tenant is not null (= a Syncable was deleted), only delete the mapping for that tenant
|
// If detach() is called with a specific tenant, it means the resource was deleted in that tenant, and we only delete that single mapping.
|
||||||
$centralResource->tenants()->detach($event->tenant);
|
$centralResource->tenants()->detach($event->tenant);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,4 @@ interface SyncMaster extends Syncable
|
||||||
public function triggerDetachEvent(TenantWithDatabase&Model $tenant): void;
|
public function triggerDetachEvent(TenantWithDatabase&Model $tenant): void;
|
||||||
|
|
||||||
public function triggerAttachEvent(TenantWithDatabase&Model $tenant): void;
|
public function triggerAttachEvent(TenantWithDatabase&Model $tenant): void;
|
||||||
|
|
||||||
public function triggerRestoredEvent(): void;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ interface Syncable
|
||||||
|
|
||||||
public function triggerDeleteEvent(bool $forceDelete = false): void;
|
public function triggerDeleteEvent(bool $forceDelete = false): void;
|
||||||
|
|
||||||
|
public function triggerRestoredEvent(): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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).
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -1102,7 +1102,16 @@ test('restoring soft deleted resources works', function () {
|
||||||
CentralUserWithSoftDeletes::withTrashed()->first()->restore();
|
CentralUserWithSoftDeletes::withTrashed()->first()->restore();
|
||||||
|
|
||||||
tenancy()->runForMultiple([$tenant1, $tenant2], function () {
|
tenancy()->runForMultiple([$tenant1, $tenant2], function () {
|
||||||
expect(TenantUserWithSoftDeletes::withTrashed()->first()->trashed())->toBeFalse();
|
$tenantResource = TenantUserWithSoftDeletes::withTrashed()->first();
|
||||||
|
|
||||||
|
expect($tenantResource->trashed())->toBeFalse();
|
||||||
|
|
||||||
|
$tenantResource->delete();
|
||||||
|
|
||||||
|
// Restoring a tenant resource works as expected
|
||||||
|
$tenantResource->restore();
|
||||||
|
|
||||||
|
expect($tenantResource->trashed())->toBeFalse();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue