Skip to content

Commit 3d1ec4a

Browse files
committed
fix(plugin-multi-tenant): thread hasMany through to filterDocumentsByTenants
1 parent b4f1022 commit 3d1ec4a

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

packages/plugin-multi-tenant/src/filters/filterDocumentsByTenants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Args<ConfigType = unknown> = {
1515
*/
1616
docTenantID?: number | number[] | string | string[]
1717
filterFieldName: string
18+
hasMany?: boolean
1819
req: PayloadRequest
1920
tenantsArrayFieldName?: string
2021
tenantsArrayTenantFieldName?: string
@@ -26,6 +27,7 @@ type Args<ConfigType = unknown> = {
2627
export const filterDocumentsByTenants = <ConfigType = unknown>({
2728
docTenantID,
2829
filterFieldName,
30+
hasMany,
2931
req,
3032
tenantsArrayFieldName = defaults.tenantsArrayFieldName,
3133
tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,
@@ -42,7 +44,9 @@ export const filterDocumentsByTenants = <ConfigType = unknown>({
4244
if (selectedTenant) {
4345
return {
4446
[filterFieldName]: {
45-
in: Array.isArray(selectedTenant) ? selectedTenant : [selectedTenant],
47+
// When hasMany is true and docTenantID was provided, it's already an array
48+
// When using cookie tenant, it's always a single value that needs wrapping
49+
in: hasMany && docTenantID !== undefined ? selectedTenant : [selectedTenant],
4650
},
4751
}
4852
}

packages/plugin-multi-tenant/src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ export const multiTenantPlugin =
169169
/**
170170
* Add filter options to all relationship fields
171171
*/
172+
const tenantFieldConfig = pluginConfig.collections[collection.slug]?.tenantFieldOverrides
173+
? pluginConfig.collections[collection.slug]?.tenantFieldOverrides
174+
: pluginConfig.tenantField || {}
172175
collection.fields = addFilterOptionsToFields({
173176
blockReferencesWithFilters,
174177
config: incomingConfig,
175178
fields: collection.fields,
179+
hasMany: tenantFieldConfig?.hasMany,
176180
tenantEnabledCollectionSlugs: collectionSlugs,
177181
tenantEnabledGlobalSlugs: globalCollectionSlugs,
178182
tenantFieldName,
@@ -356,10 +360,14 @@ export const multiTenantPlugin =
356360
/**
357361
* Add filter options to all relationship fields
358362
*/
363+
const tenantFieldConfig = pluginConfig.collections[collection.slug]?.tenantFieldOverrides
364+
? pluginConfig.collections[collection.slug]?.tenantFieldOverrides
365+
: pluginConfig.tenantField || {}
359366
collection.fields = addFilterOptionsToFields({
360367
blockReferencesWithFilters,
361368
config: incomingConfig,
362369
fields: collection.fields,
370+
hasMany: tenantFieldConfig?.hasMany,
363371
tenantEnabledCollectionSlugs: collectionSlugs,
364372
tenantEnabledGlobalSlugs: globalCollectionSlugs,
365373
tenantFieldName,
@@ -378,9 +386,7 @@ export const multiTenantPlugin =
378386
name: tenantFieldName,
379387
debug: pluginConfig.debug,
380388
isAutosaveEnabled: hasAutosaveEnabled(collection),
381-
overrides: pluginConfig.collections[collection.slug]?.tenantFieldOverrides
382-
? pluginConfig.collections[collection.slug]?.tenantFieldOverrides
383-
: pluginConfig.tenantField || {},
389+
overrides: tenantFieldConfig,
384390
tenantsArrayFieldName,
385391
tenantsArrayTenantFieldName,
386392
tenantsCollectionSlug,

packages/plugin-multi-tenant/src/utilities/addFilterOptionsToFields.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type AddFilterOptionsToFieldsArgs<ConfigType = unknown> = {
99
blockReferencesWithFilters: string[]
1010
config: Config | SanitizedConfig
1111
fields: Field[]
12+
hasMany?: boolean
1213
tenantEnabledCollectionSlugs: string[]
1314
tenantEnabledGlobalSlugs: string[]
1415
tenantFieldName: string
@@ -24,6 +25,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
2425
blockReferencesWithFilters,
2526
config,
2627
fields,
28+
hasMany,
2729
tenantEnabledCollectionSlugs,
2830
tenantEnabledGlobalSlugs,
2931
tenantFieldName,
@@ -66,6 +68,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
6668
if (hasTenantRelationsips) {
6769
newField = addRelationshipFilter({
6870
field: newField as RelationshipField,
71+
hasMany,
6972
tenantEnabledCollectionSlugs,
7073
tenantFieldName,
7174
tenantsArrayFieldName,
@@ -86,6 +89,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
8689
blockReferencesWithFilters,
8790
config,
8891
fields: newField.fields,
92+
hasMany,
8993
tenantEnabledCollectionSlugs,
9094
tenantEnabledGlobalSlugs,
9195
tenantFieldName,
@@ -119,6 +123,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
119123
blockReferencesWithFilters,
120124
config,
121125
fields: block.fields,
126+
hasMany,
122127
tenantEnabledCollectionSlugs,
123128
tenantEnabledGlobalSlugs,
124129
tenantFieldName,
@@ -143,6 +148,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
143148
blockReferencesWithFilters,
144149
config,
145150
fields: tab.fields,
151+
hasMany,
146152
tenantEnabledCollectionSlugs,
147153
tenantEnabledGlobalSlugs,
148154
tenantFieldName,
@@ -163,6 +169,7 @@ export function addFilterOptionsToFields<ConfigType = unknown>({
163169

164170
type AddFilterArgs<ConfigType = unknown> = {
165171
field: RelationshipField
172+
hasMany?: boolean
166173
tenantEnabledCollectionSlugs: string[]
167174
tenantFieldName: string
168175
tenantsArrayFieldName: string
@@ -174,6 +181,7 @@ type AddFilterArgs<ConfigType = unknown> = {
174181
}
175182
function addRelationshipFilter<ConfigType = unknown>({
176183
field,
184+
hasMany,
177185
tenantEnabledCollectionSlugs,
178186
tenantFieldName,
179187
tenantsArrayFieldName = defaults.tenantsArrayFieldName,
@@ -201,6 +209,7 @@ function addRelationshipFilter<ConfigType = unknown>({
201209
const tenantFilterResults = filterDocumentsByTenants({
202210
docTenantID: args.data?.[tenantFieldName],
203211
filterFieldName: tenantFieldName,
212+
hasMany,
204213
req: args.req,
205214
tenantsArrayFieldName,
206215
tenantsArrayTenantFieldName,

0 commit comments

Comments
 (0)