Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/plugin-multi-tenant/src/hooks/afterTenantDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const afterTenantDelete =
cleanupPromises.push(
req.payload.delete({
collection: slug,
req,
where: {
[tenantFieldName]: {
in: [id],
Expand All @@ -102,6 +103,7 @@ export const afterTenantDelete =
collection: usersSlug,
depth: 0,
limit: 0,
req,
where: {
[`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {
in: [id],
Expand All @@ -123,6 +125,7 @@ export const afterTenantDelete =
},
),
},
req,
}),
)
})
Expand Down
33 changes: 32 additions & 1 deletion test/plugin-multi-tenant/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import type { Relationship } from './payload-types.js'

import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
import { devUser } from '../credentials.js'
import { multiTenantPostsSlug, relationshipsSlug, tenantsSlug, usersSlug } from './shared.js'
import {
menuSlug,
multiTenantPostsSlug,
relationshipsSlug,
tenantsSlug,
usersSlug,
} from './shared.js'

let payload: Payload
let restClient: NextRESTClient
Expand Down Expand Up @@ -293,6 +299,31 @@ describe('@payloadcms/plugin-multi-tenant', () => {
})
})

describe('tenant cleanup on delete', () => {
it('should delete a tenant that has a global collection document without hanging', async () => {
const tenant = await payload.create({
collection: tenantsSlug,
data: { name: 'Cleanup Tenant', domain: 'cleanup-tenant.test' },
})

await payload.create({
collection: menuSlug,
data: { tenant: tenant.id, title: 'Cleanup Menu' },
})

const deletedTenant = await payload.delete({ id: tenant.id, collection: tenantsSlug })

expect(deletedTenant.id).toBe(tenant.id)

const remainingTenants = await payload.find({
collection: tenantsSlug,
where: { id: { equals: tenant.id } },
})

expect(remainingTenants.docs).toHaveLength(0)
}, 20000)
})

describe('hasMany tenant field filtering', () => {
it('should not double-wrap tenant arrays in filterOptions', async () => {
const tenant1 = await payload.create({
Expand Down
Loading