1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 06:44:04 +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:
lukinovec 2025-11-21 09:06:52 +01:00
parent f53fcb279f
commit 2b45a01018
4 changed files with 14 additions and 5 deletions

View file

@ -34,8 +34,8 @@ class DeleteResourceMapping extends QueueableListener
// or the central resource was deleted using forceDelete()
if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
Pivot::withoutEvents(function () use ($centralResource, $event) {
// $event->tenant is null when the deleted resource is a SyncMaster - all mappings are deleted in that case
// When $event->tenant is not null (= a Syncable was deleted), only delete the mapping for that tenant
// If detach() is called with null -- if $event->tenant is null -- this means a central resource was deleted and detaches all tenants.
// 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);
});
}

View file

@ -24,6 +24,4 @@ interface SyncMaster extends Syncable
public function triggerDetachEvent(TenantWithDatabase&Model $tenant): void;
public function triggerAttachEvent(TenantWithDatabase&Model $tenant): void;
public function triggerRestoredEvent(): void;
}

View file

@ -18,6 +18,8 @@ interface Syncable
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).
*

View file

@ -1102,7 +1102,16 @@ test('restoring soft deleted resources works', function () {
CentralUserWithSoftDeletes::withTrashed()->first()->restore();
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();
});
});