Skip to content

Commit d128fde

Browse files
fix(plugin-multi-tenant): run tenant delete cleanup inside the request transaction (#17175)
Identical to #17045, back-ported for 3.x. Co-authored-by: Yash Singh <yashs33244@gmail.com>
1 parent 624ac51 commit d128fde

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

packages/plugin-multi-tenant/src/hooks/afterTenantDelete.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const afterTenantDelete =
8888
cleanupPromises.push(
8989
req.payload.delete({
9090
collection: slug,
91+
req,
9192
where: {
9293
[tenantFieldName]: {
9394
in: [id],
@@ -102,6 +103,7 @@ export const afterTenantDelete =
102103
collection: usersSlug,
103104
depth: 0,
104105
limit: 0,
106+
req,
105107
where: {
106108
[`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
107109
in: [id],
@@ -123,6 +125,7 @@ export const afterTenantDelete =
123125
},
124126
),
125127
},
128+
req,
126129
}),
127130
)
128131
})

test/plugin-multi-tenant/int.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import type { Relationship } from './payload-types.js'
99

1010
import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
1111
import { devUser } from '../credentials.js'
12-
import { multiTenantPostsSlug, relationshipsSlug, tenantsSlug, usersSlug } from './shared.js'
12+
import {
13+
menuSlug,
14+
multiTenantPostsSlug,
15+
relationshipsSlug,
16+
tenantsSlug,
17+
usersSlug,
18+
} from './shared.js'
1319

1420
let payload: Payload
1521
let restClient: NextRESTClient
@@ -293,6 +299,31 @@ describe('@payloadcms/plugin-multi-tenant', () => {
293299
})
294300
})
295301

302+
describe('tenant cleanup on delete', () => {
303+
it('should delete a tenant that has a global collection document without hanging', async () => {
304+
const tenant = await payload.create({
305+
collection: tenantsSlug,
306+
data: { name: 'Cleanup Tenant', domain: 'cleanup-tenant.test' },
307+
})
308+
309+
await payload.create({
310+
collection: menuSlug,
311+
data: { tenant: tenant.id, title: 'Cleanup Menu' },
312+
})
313+
314+
const deletedTenant = await payload.delete({ id: tenant.id, collection: tenantsSlug })
315+
316+
expect(deletedTenant.id).toBe(tenant.id)
317+
318+
const remainingTenants = await payload.find({
319+
collection: tenantsSlug,
320+
where: { id: { equals: tenant.id } },
321+
})
322+
323+
expect(remainingTenants.docs).toHaveLength(0)
324+
}, 20000)
325+
})
326+
296327
describe('hasMany tenant field filtering', () => {
297328
it('should not double-wrap tenant arrays in filterOptions', async () => {
298329
const tenant1 = await payload.create({

0 commit comments

Comments
 (0)