Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
88 lines
3.0 KiB
Plaintext
88 lines
3.0 KiB
Plaintext
import { parseDocumentID } from '../../index.js';
|
|
import { getFolderBreadcrumbs } from './getFolderBreadcrumbs.js';
|
|
import { queryDocumentsAndFoldersFromJoin } from './getFoldersAndDocumentsFromJoin.js';
|
|
import { getOrphanedDocs } from './getOrphanedDocs.js';
|
|
/**
|
|
* Query for documents, subfolders and breadcrumbs for a given folder
|
|
*/ export const getFolderData = async ({ collectionSlug, documentWhere, folderID: _folderID, folderWhere, req, sort = 'name' })=>{
|
|
const { payload } = req;
|
|
if (payload.config.folders === false) {
|
|
throw new Error('Folders are not enabled');
|
|
}
|
|
const parentFolderID = parseDocumentID({
|
|
id: _folderID,
|
|
collectionSlug: payload.config.folders.slug,
|
|
payload
|
|
});
|
|
const breadcrumbsPromise = getFolderBreadcrumbs({
|
|
folderID: parentFolderID,
|
|
req
|
|
});
|
|
if (parentFolderID) {
|
|
// subfolders and documents are queried together
|
|
const documentAndSubfolderPromise = queryDocumentsAndFoldersFromJoin({
|
|
documentWhere,
|
|
folderWhere,
|
|
parentFolderID,
|
|
req
|
|
});
|
|
const [breadcrumbs, result] = await Promise.all([
|
|
breadcrumbsPromise,
|
|
documentAndSubfolderPromise
|
|
]);
|
|
return {
|
|
breadcrumbs,
|
|
documents: sortDocs({
|
|
docs: result.documents,
|
|
sort
|
|
}),
|
|
folderAssignedCollections: result.folderAssignedCollections,
|
|
subfolders: sortDocs({
|
|
docs: result.subfolders,
|
|
sort
|
|
})
|
|
};
|
|
} else {
|
|
const subfoldersPromise = getOrphanedDocs({
|
|
collectionSlug: payload.config.folders.slug,
|
|
folderFieldName: payload.config.folders.fieldName,
|
|
req,
|
|
where: folderWhere
|
|
});
|
|
const [breadcrumbs, subfolders] = await Promise.all([
|
|
breadcrumbsPromise,
|
|
subfoldersPromise
|
|
]);
|
|
return {
|
|
breadcrumbs,
|
|
documents: [],
|
|
folderAssignedCollections: collectionSlug ? [
|
|
collectionSlug
|
|
] : undefined,
|
|
subfolders: sortDocs({
|
|
docs: subfolders,
|
|
sort
|
|
})
|
|
};
|
|
}
|
|
};
|
|
function sortDocs({ docs, sort }) {
|
|
if (!sort) {
|
|
return docs;
|
|
}
|
|
const isDesc = typeof sort === 'string' && sort.startsWith('-');
|
|
const sortKey = isDesc ? sort.slice(1) : sort;
|
|
return docs.sort((a, b)=>{
|
|
let result = 0;
|
|
if (sortKey === 'name') {
|
|
result = a.value._folderOrDocumentTitle.localeCompare(b.value._folderOrDocumentTitle);
|
|
} else if (sortKey === 'createdAt') {
|
|
result = new Date(a.value.createdAt || '').getTime() - new Date(b.value.createdAt || '').getTime();
|
|
} else if (sortKey === 'updatedAt') {
|
|
result = new Date(a.value.updatedAt || '').getTime() - new Date(b.value.updatedAt || '').getTime();
|
|
}
|
|
return isDesc ? -result : result;
|
|
});
|
|
}
|
|
|
|
//# sourceMappingURL=getFolderData.js.map |