@@ -25,7 +25,7 @@ type Args = {
25
25
view : ViewTypes
26
26
}
27
27
export async function getGlobalViewRedirect ( {
28
- slug,
28
+ slug : collectionSlug ,
29
29
basePath,
30
30
docID,
31
31
headers,
@@ -64,45 +64,67 @@ export async function getGlobalViewRedirect({
64
64
tenant = tenantOptions [ 0 ] ?. value || null
65
65
}
66
66
67
- try {
68
- const { docs } = await payload . find ( {
69
- collection : slug ,
70
- depth : 0 ,
71
- limit : 1 ,
72
- overrideAccess : false ,
73
- pagination : false ,
74
- user,
75
- where : {
76
- [ tenantFieldName ] : {
77
- equals : tenant ,
67
+ if ( tenant ) {
68
+ try {
69
+ const globalTenantDocQuery = await payload . find ( {
70
+ collection : collectionSlug ,
71
+ depth : 0 ,
72
+ limit : 1 ,
73
+ pagination : false ,
74
+ select : {
75
+ id : true ,
78
76
} ,
79
- } ,
80
- } )
77
+ where : {
78
+ [ tenantFieldName ] : {
79
+ equals : tenant ,
80
+ } ,
81
+ } ,
82
+ } )
81
83
82
- const tenantDocID = docs ?. [ 0 ] ?. id
84
+ const globalTenantDocID = globalTenantDocQuery ?. docs ?. [ 0 ] ?. id
83
85
84
- if ( view === 'document' ) {
85
- if ( docID && ! tenantDocID ) {
86
- // viewing a document with an id but does not match the selected tenant, redirect to create route
87
- redirectRoute = `/collections/${ slug } /create`
88
- } else if ( tenantDocID && docID !== tenantDocID ) {
89
- // tenant document already exists but does not match current route doc ID, redirect to matching tenant doc
90
- redirectRoute = `/collections/${ slug } /${ tenantDocID } `
91
- }
92
- } else if ( view === 'list' ) {
93
- if ( tenantDocID ) {
94
- // tenant document exists, redirect to edit view
95
- redirectRoute = `/collections/${ slug } /${ tenantDocID } `
96
- } else {
97
- // tenant document does not exist, redirect to create route
98
- redirectRoute = `/collections/${ slug } /create`
86
+ if ( view === 'document' ) {
87
+ // global tenant document edit view
88
+ if ( globalTenantDocID && docID !== globalTenantDocID ) {
89
+ // tenant document already exists but does not match current route docID
90
+ // redirect to matching tenant docID from query
91
+ redirectRoute = `/collections/${ collectionSlug } /${ globalTenantDocID } `
92
+ } else if ( docID && ! globalTenantDocID ) {
93
+ // a docID was found in the route but no global document with this tenant exists
94
+ // so we need to generate a redirect to the create route
95
+ redirectRoute = await generateCreateRedirect ( {
96
+ collectionSlug,
97
+ payload,
98
+ tenantID : tenant ,
99
+ } )
100
+ }
101
+ } else if ( view === 'list' ) {
102
+ // global tenant document list view
103
+ if ( globalTenantDocID ) {
104
+ // tenant document exists, redirect from list view to the document edit view
105
+ redirectRoute = `/collections/${ collectionSlug } /${ globalTenantDocID } `
106
+ } else {
107
+ // no matching document was found for the current tenant
108
+ // so we need to generate a redirect to the create route
109
+ redirectRoute = await generateCreateRedirect ( {
110
+ collectionSlug,
111
+ payload,
112
+ tenantID : tenant ,
113
+ } )
114
+ }
99
115
}
116
+ } catch ( e : unknown ) {
117
+ const prefix = `${ e && typeof e === 'object' && 'message' in e && typeof e . message === 'string' ? `${ e . message } - ` : '' } `
118
+ payload . logger . error ( e , `${ prefix } Multi Tenant Redirect Error` )
100
119
}
101
- } catch ( e : unknown ) {
102
- payload . logger . error (
103
- e ,
104
- `${ typeof e === 'object' && e && 'message' in e ? `e?.message - ` : '' } Multi Tenant Redirect Error` ,
105
- )
120
+ } else {
121
+ // no tenants were found, redirect to the admin view
122
+ return formatAdminURL ( {
123
+ adminRoute : payload . config . routes . admin ,
124
+ basePath,
125
+ path : '' ,
126
+ serverURL : payload . config . serverURL ,
127
+ } )
106
128
}
107
129
108
130
if ( redirectRoute ) {
@@ -114,5 +136,57 @@ export async function getGlobalViewRedirect({
114
136
} )
115
137
}
116
138
139
+ // no redirect is needed
140
+ // the current route is valid
117
141
return undefined
118
142
}
143
+
144
+ type GenerateCreateArgs = {
145
+ collectionSlug : string
146
+ payload : Payload
147
+ tenantID : number | string
148
+ }
149
+ /**
150
+ * Generate a redirect URL for creating a new document in a multi-tenant collection.
151
+ *
152
+ * If autosave is enabled on the collection, we need to create the document and then redirect to it.
153
+ * Otherwise we can redirect to the default create route.
154
+ */
155
+ async function generateCreateRedirect ( {
156
+ collectionSlug,
157
+ payload,
158
+ tenantID,
159
+ } : GenerateCreateArgs ) : Promise < `/${string } ` | undefined > {
160
+ const collection = payload . collections [ collectionSlug ]
161
+ if (
162
+ collection ?. config . versions ?. drafts &&
163
+ typeof collection . config . versions . drafts === 'object' &&
164
+ collection . config . versions . drafts . autosave
165
+ ) {
166
+ // Autosave is enabled, create a document first
167
+ try {
168
+ const doc = await payload . create ( {
169
+ collection : collectionSlug ,
170
+ data : {
171
+ tenant : tenantID ,
172
+ } ,
173
+ depth : 0 ,
174
+ draft : true ,
175
+ select : {
176
+ id : true ,
177
+ } ,
178
+ } )
179
+ return `/collections/${ collectionSlug } /${ doc . id } `
180
+ } catch ( error ) {
181
+ payload . logger . error (
182
+ error ,
183
+ `Error creating autosave global multi tenant document for ${ collectionSlug } ` ,
184
+ )
185
+ }
186
+
187
+ return '/'
188
+ }
189
+
190
+ // Autosave is not enabled, redirect to default create route
191
+ return `/collections/${ collectionSlug } /create`
192
+ }
0 commit comments