fix: thread req into interal folder payload operations (#12523)
This commit is contained in:
@@ -76,9 +76,8 @@ export const buildBrowseByFolderView = async (
|
|||||||
|
|
||||||
const { breadcrumbs, documents, subfolders } = await getFolderData({
|
const { breadcrumbs, documents, subfolders } = await getFolderData({
|
||||||
folderID,
|
folderID,
|
||||||
payload: initPageResult.req.payload,
|
req: initPageResult.req,
|
||||||
search: query?.search as string,
|
search: query?.search as string,
|
||||||
user: initPageResult.req.user,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const resolvedFolderID = breadcrumbs[breadcrumbs.length - 1]?.id
|
const resolvedFolderID = breadcrumbs[breadcrumbs.length - 1]?.id
|
||||||
|
|||||||
@@ -112,9 +112,8 @@ export const buildCollectionFolderView = async (
|
|||||||
const { breadcrumbs, documents, subfolders } = await getFolderData({
|
const { breadcrumbs, documents, subfolders } = await getFolderData({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
folderID,
|
folderID,
|
||||||
payload: initPageResult.req.payload,
|
req: initPageResult.req,
|
||||||
search: query?.search as string,
|
search: query?.search as string,
|
||||||
user: initPageResult.req.user,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const resolvedFolderID = breadcrumbs[breadcrumbs.length - 1]?.id
|
const resolvedFolderID = breadcrumbs[breadcrumbs.length - 1]?.id
|
||||||
|
|||||||
@@ -33,9 +33,8 @@ export const populateFolderDataEndpoint: Endpoint = {
|
|||||||
const data = await getFolderData({
|
const data = await getFolderData({
|
||||||
collectionSlug: req.searchParams?.get('collectionSlug') || undefined,
|
collectionSlug: req.searchParams?.get('collectionSlug') || undefined,
|
||||||
folderID: req.searchParams?.get('folderID') || undefined,
|
folderID: req.searchParams?.get('folderID') || undefined,
|
||||||
payload: req.payload,
|
req,
|
||||||
search: req.searchParams?.get('search') || undefined,
|
search: req.searchParams?.get('search') || undefined,
|
||||||
user: req.user,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return Response.json(data)
|
return Response.json(data)
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import type { User } from '../../index.js'
|
import type { Document, PayloadRequest } from '../../types/index.js'
|
||||||
import type { Document, Payload } from '../../types/index.js'
|
|
||||||
import type { FolderBreadcrumb } from '../types.js'
|
import type { FolderBreadcrumb } from '../types.js'
|
||||||
|
|
||||||
type GetFolderBreadcrumbsArgs = {
|
type GetFolderBreadcrumbsArgs = {
|
||||||
breadcrumbs?: FolderBreadcrumb[]
|
breadcrumbs?: FolderBreadcrumb[]
|
||||||
folderID?: number | string
|
folderID?: number | string
|
||||||
payload: Payload
|
req: PayloadRequest
|
||||||
user?: User
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Builds breadcrumbs up from child folder
|
* Builds breadcrumbs up from child folder
|
||||||
@@ -15,9 +13,9 @@ type GetFolderBreadcrumbsArgs = {
|
|||||||
export const getFolderBreadcrumbs = async ({
|
export const getFolderBreadcrumbs = async ({
|
||||||
breadcrumbs = [],
|
breadcrumbs = [],
|
||||||
folderID,
|
folderID,
|
||||||
payload,
|
req,
|
||||||
user,
|
|
||||||
}: GetFolderBreadcrumbsArgs): Promise<FolderBreadcrumb[] | null> => {
|
}: GetFolderBreadcrumbsArgs): Promise<FolderBreadcrumb[] | null> => {
|
||||||
|
const { payload, user } = req
|
||||||
const folderFieldName: string = payload.config.folders.fieldName
|
const folderFieldName: string = payload.config.folders.fieldName
|
||||||
if (folderID) {
|
if (folderID) {
|
||||||
const folderQuery = await payload.find({
|
const folderQuery = await payload.find({
|
||||||
@@ -25,6 +23,7 @@ export const getFolderBreadcrumbs = async ({
|
|||||||
depth: 0,
|
depth: 0,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
overrideAccess: false,
|
overrideAccess: false,
|
||||||
|
req,
|
||||||
select: {
|
select: {
|
||||||
name: true,
|
name: true,
|
||||||
[folderFieldName]: true,
|
[folderFieldName]: true,
|
||||||
@@ -52,8 +51,7 @@ export const getFolderBreadcrumbs = async ({
|
|||||||
typeof folder[folderFieldName] === 'string'
|
typeof folder[folderFieldName] === 'string'
|
||||||
? folder[folderFieldName]
|
? folder[folderFieldName]
|
||||||
: folder[folderFieldName].id,
|
: folder[folderFieldName].id,
|
||||||
payload,
|
req,
|
||||||
user,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { CollectionSlug, User } from '../../index.js'
|
import type { CollectionSlug } from '../../index.js'
|
||||||
import type { Payload } from '../../types/index.js'
|
import type { PayloadRequest } from '../../types/index.js'
|
||||||
import type { GetFolderDataResult } from '../types.js'
|
import type { GetFolderDataResult } from '../types.js'
|
||||||
|
|
||||||
import { parseDocumentID } from '../../index.js'
|
import { parseDocumentID } from '../../index.js'
|
||||||
@@ -19,20 +19,11 @@ type Args = {
|
|||||||
* @default undefined
|
* @default undefined
|
||||||
*/
|
*/
|
||||||
folderID?: number | string
|
folderID?: number | string
|
||||||
/**
|
req: PayloadRequest
|
||||||
* The locale to use for the document query
|
|
||||||
* @default undefined
|
|
||||||
*/
|
|
||||||
payload: Payload
|
|
||||||
/**
|
/**
|
||||||
* Search term to filter documents by - only applicable IF `collectionSlug` exists and NO `folderID` is provided
|
* Search term to filter documents by - only applicable IF `collectionSlug` exists and NO `folderID` is provided
|
||||||
*/
|
*/
|
||||||
search?: string
|
search?: string
|
||||||
/**
|
|
||||||
* The user making the request
|
|
||||||
* @default undefined
|
|
||||||
*/
|
|
||||||
user?: User
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Query for documents, subfolders and breadcrumbs for a given folder
|
* Query for documents, subfolders and breadcrumbs for a given folder
|
||||||
@@ -40,10 +31,10 @@ type Args = {
|
|||||||
export const getFolderData = async ({
|
export const getFolderData = async ({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
folderID: _folderID,
|
folderID: _folderID,
|
||||||
payload,
|
req,
|
||||||
search,
|
search,
|
||||||
user,
|
|
||||||
}: Args): Promise<GetFolderDataResult> => {
|
}: Args): Promise<GetFolderDataResult> => {
|
||||||
|
const { payload, user } = req
|
||||||
const parentFolderID = parseDocumentID({
|
const parentFolderID = parseDocumentID({
|
||||||
id: _folderID,
|
id: _folderID,
|
||||||
collectionSlug: payload.config.folders.slug,
|
collectionSlug: payload.config.folders.slug,
|
||||||
@@ -52,8 +43,7 @@ export const getFolderData = async ({
|
|||||||
|
|
||||||
const breadcrumbsPromise = getFolderBreadcrumbs({
|
const breadcrumbsPromise = getFolderBreadcrumbs({
|
||||||
folderID: parentFolderID,
|
folderID: parentFolderID,
|
||||||
payload,
|
req,
|
||||||
user,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if (parentFolderID) {
|
if (parentFolderID) {
|
||||||
@@ -61,8 +51,7 @@ export const getFolderData = async ({
|
|||||||
const documentAndSubfolderPromise = queryDocumentsAndFoldersFromJoin({
|
const documentAndSubfolderPromise = queryDocumentsAndFoldersFromJoin({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
parentFolderID,
|
parentFolderID,
|
||||||
payload,
|
req,
|
||||||
user,
|
|
||||||
})
|
})
|
||||||
const [breadcrumbs, documentsAndSubfolders] = await Promise.all([
|
const [breadcrumbs, documentsAndSubfolders] = await Promise.all([
|
||||||
breadcrumbsPromise,
|
breadcrumbsPromise,
|
||||||
@@ -78,16 +67,14 @@ export const getFolderData = async ({
|
|||||||
// subfolders and documents are queried separately
|
// subfolders and documents are queried separately
|
||||||
const subfoldersPromise = getOrphanedDocs({
|
const subfoldersPromise = getOrphanedDocs({
|
||||||
collectionSlug: payload.config.folders.slug,
|
collectionSlug: payload.config.folders.slug,
|
||||||
payload,
|
req,
|
||||||
search,
|
search,
|
||||||
user,
|
|
||||||
})
|
})
|
||||||
const documentsPromise = collectionSlug
|
const documentsPromise = collectionSlug
|
||||||
? getOrphanedDocs({
|
? getOrphanedDocs({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
payload,
|
req,
|
||||||
search,
|
search,
|
||||||
user,
|
|
||||||
})
|
})
|
||||||
: Promise.resolve([])
|
: Promise.resolve([])
|
||||||
const [breadcrumbs, subfolders, documents] = await Promise.all([
|
const [breadcrumbs, subfolders, documents] = await Promise.all([
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { User } from '../../auth/types.js'
|
|
||||||
import type { PaginatedDocs } from '../../database/types.js'
|
import type { PaginatedDocs } from '../../database/types.js'
|
||||||
import type { CollectionSlug } from '../../index.js'
|
import type { CollectionSlug } from '../../index.js'
|
||||||
import type { Document, Payload } from '../../types/index.js'
|
import type { Document, PayloadRequest } from '../../types/index.js'
|
||||||
import type { FolderOrDocument } from '../types.js'
|
import type { FolderOrDocument } from '../types.js'
|
||||||
|
|
||||||
import { formatFolderOrDocumentItem } from './formatFolderOrDocumentItem.js'
|
import { formatFolderOrDocumentItem } from './formatFolderOrDocumentItem.js'
|
||||||
@@ -13,15 +12,14 @@ type QueryDocumentsAndFoldersResults = {
|
|||||||
type QueryDocumentsAndFoldersArgs = {
|
type QueryDocumentsAndFoldersArgs = {
|
||||||
collectionSlug?: CollectionSlug
|
collectionSlug?: CollectionSlug
|
||||||
parentFolderID: number | string
|
parentFolderID: number | string
|
||||||
payload: Payload
|
req: PayloadRequest
|
||||||
user?: User
|
|
||||||
}
|
}
|
||||||
export async function queryDocumentsAndFoldersFromJoin({
|
export async function queryDocumentsAndFoldersFromJoin({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
parentFolderID,
|
parentFolderID,
|
||||||
payload,
|
req,
|
||||||
user,
|
|
||||||
}: QueryDocumentsAndFoldersArgs): Promise<QueryDocumentsAndFoldersResults> {
|
}: QueryDocumentsAndFoldersArgs): Promise<QueryDocumentsAndFoldersResults> {
|
||||||
|
const { payload, user } = req
|
||||||
const folderCollectionSlugs: string[] = payload.config.collections.reduce<string[]>(
|
const folderCollectionSlugs: string[] = payload.config.collections.reduce<string[]>(
|
||||||
(acc, collection) => {
|
(acc, collection) => {
|
||||||
if (collection?.folders) {
|
if (collection?.folders) {
|
||||||
@@ -50,6 +48,7 @@ export async function queryDocumentsAndFoldersFromJoin({
|
|||||||
},
|
},
|
||||||
limit: 1,
|
limit: 1,
|
||||||
overrideAccess: false,
|
overrideAccess: false,
|
||||||
|
req,
|
||||||
user,
|
user,
|
||||||
where: {
|
where: {
|
||||||
id: {
|
id: {
|
||||||
|
|||||||
@@ -1,20 +1,19 @@
|
|||||||
import type { CollectionSlug, Payload, User, Where } from '../../index.js'
|
import type { CollectionSlug, PayloadRequest, Where } from '../../index.js'
|
||||||
import type { FolderOrDocument } from '../types.js'
|
import type { FolderOrDocument } from '../types.js'
|
||||||
|
|
||||||
import { formatFolderOrDocumentItem } from './formatFolderOrDocumentItem.js'
|
import { formatFolderOrDocumentItem } from './formatFolderOrDocumentItem.js'
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
collectionSlug: CollectionSlug
|
collectionSlug: CollectionSlug
|
||||||
payload: Payload
|
req: PayloadRequest
|
||||||
search?: string
|
search?: string
|
||||||
user?: User
|
|
||||||
}
|
}
|
||||||
export async function getOrphanedDocs({
|
export async function getOrphanedDocs({
|
||||||
collectionSlug,
|
collectionSlug,
|
||||||
payload,
|
req,
|
||||||
search,
|
search,
|
||||||
user,
|
|
||||||
}: Args): Promise<FolderOrDocument[]> {
|
}: Args): Promise<FolderOrDocument[]> {
|
||||||
|
const { payload, user } = req
|
||||||
let whereConstraints: Where = {
|
let whereConstraints: Where = {
|
||||||
or: [
|
or: [
|
||||||
{
|
{
|
||||||
@@ -42,6 +41,7 @@ export async function getOrphanedDocs({
|
|||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
limit: 0,
|
limit: 0,
|
||||||
overrideAccess: false,
|
overrideAccess: false,
|
||||||
|
req,
|
||||||
sort: payload.collections[collectionSlug].config.admin.useAsTitle,
|
sort: payload.collections[collectionSlug].config.admin.useAsTitle,
|
||||||
user,
|
user,
|
||||||
where: whereConstraints,
|
where: whereConstraints,
|
||||||
|
|||||||
Reference in New Issue
Block a user