payload 3.80.0-internal.cdd7ef7 → 3.80.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (265) hide show
  1. package/dist/admin/elements/Nav.d.ts +0 -13
  2. package/dist/admin/elements/Nav.d.ts.map +1 -1
  3. package/dist/admin/elements/Nav.js.map +1 -1
  4. package/dist/admin/functions/index.d.ts +38 -1
  5. package/dist/admin/functions/index.d.ts.map +1 -1
  6. package/dist/admin/functions/index.js.map +1 -1
  7. package/dist/admin/types.d.ts +4 -5
  8. package/dist/admin/types.d.ts.map +1 -1
  9. package/dist/admin/types.js.map +1 -1
  10. package/dist/admin/views/folderList.d.ts +56 -0
  11. package/dist/admin/views/folderList.d.ts.map +1 -0
  12. package/dist/admin/views/folderList.js +3 -0
  13. package/dist/admin/views/folderList.js.map +1 -0
  14. package/dist/admin/views/index.d.ts +3 -1
  15. package/dist/admin/views/index.d.ts.map +1 -1
  16. package/dist/admin/views/index.js.map +1 -1
  17. package/dist/admin/views/list.d.ts +2 -43
  18. package/dist/admin/views/list.d.ts.map +1 -1
  19. package/dist/admin/views/list.js.map +1 -1
  20. package/dist/bin/generateImportMap/iterateCollections.d.ts.map +1 -1
  21. package/dist/bin/generateImportMap/iterateCollections.js +0 -1
  22. package/dist/bin/generateImportMap/iterateCollections.js.map +1 -1
  23. package/dist/bin/generateImportMap/iterateConfig.d.ts.map +1 -1
  24. package/dist/bin/generateImportMap/iterateConfig.js +0 -7
  25. package/dist/bin/generateImportMap/iterateConfig.js.map +1 -1
  26. package/dist/collections/config/client.d.ts +1 -3
  27. package/dist/collections/config/client.d.ts.map +1 -1
  28. package/dist/collections/config/client.js +0 -11
  29. package/dist/collections/config/client.js.map +1 -1
  30. package/dist/collections/config/defaults.js +1 -1
  31. package/dist/collections/config/defaults.js.map +1 -1
  32. package/dist/collections/config/sanitize.d.ts.map +1 -1
  33. package/dist/collections/config/sanitize.js +7 -3
  34. package/dist/collections/config/sanitize.js.map +1 -1
  35. package/dist/collections/config/types.d.ts +7 -29
  36. package/dist/collections/config/types.d.ts.map +1 -1
  37. package/dist/collections/config/types.js.map +1 -1
  38. package/dist/collections/operations/create.js +17 -17
  39. package/dist/collections/operations/create.js.map +1 -1
  40. package/dist/collections/operations/findByID.js +3 -3
  41. package/dist/collections/operations/findByID.js.map +1 -1
  42. package/dist/collections/operations/update.js +1 -1
  43. package/dist/collections/operations/update.js.map +1 -1
  44. package/dist/collections/operations/utilities/update.d.ts.map +1 -1
  45. package/dist/collections/operations/utilities/update.js +6 -6
  46. package/dist/collections/operations/utilities/update.js.map +1 -1
  47. package/dist/config/client.d.ts.map +1 -1
  48. package/dist/config/client.js +10 -0
  49. package/dist/config/client.js.map +1 -1
  50. package/dist/config/defaults.d.ts.map +1 -1
  51. package/dist/config/defaults.js +15 -0
  52. package/dist/config/defaults.js.map +1 -1
  53. package/dist/config/sanitize.d.ts.map +1 -1
  54. package/dist/config/sanitize.js +26 -3
  55. package/dist/config/sanitize.js.map +1 -1
  56. package/dist/config/types.d.ts +12 -26
  57. package/dist/config/types.d.ts.map +1 -1
  58. package/dist/config/types.js.map +1 -1
  59. package/dist/exports/shared.d.ts +3 -2
  60. package/dist/exports/shared.d.ts.map +1 -1
  61. package/dist/exports/shared.js +2 -1
  62. package/dist/exports/shared.js.map +1 -1
  63. package/dist/fields/baseFields/slug/index.d.ts +7 -0
  64. package/dist/fields/baseFields/slug/index.d.ts.map +1 -1
  65. package/dist/fields/baseFields/slug/index.js +2 -2
  66. package/dist/fields/baseFields/slug/index.js.map +1 -1
  67. package/dist/fields/config/sanitize.d.ts.map +1 -1
  68. package/dist/fields/config/sanitize.js +0 -4
  69. package/dist/fields/config/sanitize.js.map +1 -1
  70. package/dist/fields/config/sanitizeJoinField.d.ts.map +1 -1
  71. package/dist/fields/config/sanitizeJoinField.js +0 -3
  72. package/dist/fields/config/sanitizeJoinField.js.map +1 -1
  73. package/dist/folders/addFolderCollection.d.ts +10 -0
  74. package/dist/folders/addFolderCollection.d.ts.map +1 -0
  75. package/dist/folders/addFolderCollection.js +26 -0
  76. package/dist/folders/addFolderCollection.js.map +1 -0
  77. package/dist/folders/addFolderFieldToCollection.d.ts +8 -0
  78. package/dist/folders/addFolderFieldToCollection.d.ts.map +1 -0
  79. package/dist/folders/addFolderFieldToCollection.js +20 -0
  80. package/dist/folders/addFolderFieldToCollection.js.map +1 -0
  81. package/dist/folders/buildFolderField.d.ts +8 -0
  82. package/dist/folders/buildFolderField.d.ts.map +1 -0
  83. package/dist/folders/buildFolderField.js +87 -0
  84. package/dist/folders/buildFolderField.js.map +1 -0
  85. package/dist/folders/constants.d.ts +3 -0
  86. package/dist/folders/constants.d.ts.map +1 -0
  87. package/dist/folders/constants.js +4 -0
  88. package/dist/folders/constants.js.map +1 -0
  89. package/dist/folders/createFolderCollection.d.ts +11 -0
  90. package/dist/folders/createFolderCollection.d.ts.map +1 -0
  91. package/dist/folders/createFolderCollection.js +115 -0
  92. package/dist/folders/createFolderCollection.js.map +1 -0
  93. package/dist/folders/hooks/deleteSubfoldersAfterDelete.d.ts +8 -0
  94. package/dist/folders/hooks/deleteSubfoldersAfterDelete.d.ts.map +1 -0
  95. package/dist/folders/hooks/deleteSubfoldersAfterDelete.js +15 -0
  96. package/dist/folders/hooks/deleteSubfoldersAfterDelete.js.map +1 -0
  97. package/dist/folders/hooks/dissasociateAfterDelete.d.ts +8 -0
  98. package/dist/folders/hooks/dissasociateAfterDelete.d.ts.map +1 -0
  99. package/dist/folders/hooks/dissasociateAfterDelete.js +20 -0
  100. package/dist/folders/hooks/dissasociateAfterDelete.js.map +1 -0
  101. package/dist/folders/hooks/ensureSafeCollectionsChange.d.ts +5 -0
  102. package/dist/folders/hooks/ensureSafeCollectionsChange.d.ts.map +1 -0
  103. package/dist/folders/hooks/ensureSafeCollectionsChange.js +107 -0
  104. package/dist/folders/hooks/ensureSafeCollectionsChange.js.map +1 -0
  105. package/dist/folders/hooks/reparentChildFolder.d.ts +24 -0
  106. package/dist/folders/hooks/reparentChildFolder.d.ts.map +1 -0
  107. package/dist/folders/hooks/reparentChildFolder.js +72 -0
  108. package/dist/folders/hooks/reparentChildFolder.js.map +1 -0
  109. package/dist/folders/types.d.ts +118 -0
  110. package/dist/folders/types.d.ts.map +1 -0
  111. package/dist/folders/types.js +3 -0
  112. package/dist/folders/types.js.map +1 -0
  113. package/dist/folders/utils/buildFolderWhereConstraints.d.ts +13 -0
  114. package/dist/folders/utils/buildFolderWhereConstraints.d.ts.map +1 -0
  115. package/dist/folders/utils/buildFolderWhereConstraints.js +45 -0
  116. package/dist/folders/utils/buildFolderWhereConstraints.js.map +1 -0
  117. package/dist/folders/utils/formatFolderOrDocumentItem.d.ts +12 -0
  118. package/dist/folders/utils/formatFolderOrDocumentItem.d.ts.map +1 -0
  119. package/dist/folders/utils/formatFolderOrDocumentItem.js +30 -0
  120. package/dist/folders/utils/formatFolderOrDocumentItem.js.map +1 -0
  121. package/dist/folders/utils/getFolderBreadcrumbs.d.ts +14 -0
  122. package/dist/folders/utils/getFolderBreadcrumbs.d.ts.map +1 -0
  123. package/dist/folders/utils/getFolderBreadcrumbs.js +45 -0
  124. package/dist/folders/utils/getFolderBreadcrumbs.js.map +1 -0
  125. package/dist/folders/utils/getFolderData.d.ts +33 -0
  126. package/dist/folders/utils/getFolderData.d.ts.map +1 -0
  127. package/dist/folders/utils/getFolderData.js +88 -0
  128. package/dist/folders/utils/getFolderData.js.map +1 -0
  129. package/dist/folders/utils/getFoldersAndDocumentsFromJoin.d.ts +24 -0
  130. package/dist/folders/utils/getFoldersAndDocumentsFromJoin.d.ts.map +1 -0
  131. package/dist/folders/utils/getFoldersAndDocumentsFromJoin.js +66 -0
  132. package/dist/folders/utils/getFoldersAndDocumentsFromJoin.js.map +1 -0
  133. package/dist/folders/utils/getOrphanedDocs.d.ts +15 -0
  134. package/dist/folders/utils/getOrphanedDocs.d.ts.map +1 -0
  135. package/dist/folders/utils/getOrphanedDocs.js +40 -0
  136. package/dist/folders/utils/getOrphanedDocs.js.map +1 -0
  137. package/dist/globals/config/types.d.ts +0 -4
  138. package/dist/globals/config/types.d.ts.map +1 -1
  139. package/dist/globals/config/types.js.map +1 -1
  140. package/dist/globals/operations/update.d.ts.map +1 -1
  141. package/dist/globals/operations/update.js +2 -1
  142. package/dist/globals/operations/update.js.map +1 -1
  143. package/dist/index.bundled.d.ts +288 -577
  144. package/dist/index.d.ts +9 -23
  145. package/dist/index.d.ts.map +1 -1
  146. package/dist/index.js +5 -13
  147. package/dist/index.js.map +1 -1
  148. package/dist/preferences/keys.d.ts +4 -8
  149. package/dist/preferences/keys.d.ts.map +1 -1
  150. package/dist/preferences/keys.js +4 -7
  151. package/dist/preferences/keys.js.map +1 -1
  152. package/dist/types/index.d.ts +0 -1
  153. package/dist/types/index.d.ts.map +1 -1
  154. package/dist/types/index.js.map +1 -1
  155. package/dist/utilities/addDataAndFileToRequest.d.ts.map +1 -1
  156. package/dist/utilities/addDataAndFileToRequest.js +7 -1
  157. package/dist/utilities/addDataAndFileToRequest.js.map +1 -1
  158. package/dist/utilities/extractID.js +1 -1
  159. package/dist/utilities/extractID.js.map +1 -1
  160. package/dist/utilities/formatAdminURL.d.ts +2 -13
  161. package/dist/utilities/formatAdminURL.d.ts.map +1 -1
  162. package/dist/utilities/formatAdminURL.js.map +1 -1
  163. package/dist/versions/saveVersion.d.ts +1 -0
  164. package/dist/versions/saveVersion.d.ts.map +1 -1
  165. package/dist/versions/saveVersion.js +16 -66
  166. package/dist/versions/saveVersion.js.map +1 -1
  167. package/dist/versions/updateLatestVersion.d.ts +24 -0
  168. package/dist/versions/updateLatestVersion.d.ts.map +1 -0
  169. package/dist/versions/updateLatestVersion.js +64 -0
  170. package/dist/versions/updateLatestVersion.js.map +1 -0
  171. package/package.json +4 -4
  172. package/dist/admin/views/hierarchyList.d.ts +0 -9
  173. package/dist/admin/views/hierarchyList.d.ts.map +0 -1
  174. package/dist/admin/views/hierarchyList.js +0 -3
  175. package/dist/admin/views/hierarchyList.js.map +0 -1
  176. package/dist/hierarchy/addHierarchyToCollection.d.ts +0 -8
  177. package/dist/hierarchy/addHierarchyToCollection.d.ts.map +0 -1
  178. package/dist/hierarchy/addHierarchyToCollection.js +0 -62
  179. package/dist/hierarchy/addHierarchyToCollection.js.map +0 -1
  180. package/dist/hierarchy/buildParentField.d.ts +0 -11
  181. package/dist/hierarchy/buildParentField.d.ts.map +0 -1
  182. package/dist/hierarchy/buildParentField.js +0 -42
  183. package/dist/hierarchy/buildParentField.js.map +0 -1
  184. package/dist/hierarchy/constants.d.ts +0 -15
  185. package/dist/hierarchy/constants.d.ts.map +0 -1
  186. package/dist/hierarchy/constants.js +0 -11
  187. package/dist/hierarchy/constants.js.map +0 -1
  188. package/dist/hierarchy/createFolderField.d.ts +0 -39
  189. package/dist/hierarchy/createFolderField.d.ts.map +0 -1
  190. package/dist/hierarchy/createFolderField.js +0 -54
  191. package/dist/hierarchy/createFolderField.js.map +0 -1
  192. package/dist/hierarchy/createFoldersCollection.d.ts +0 -57
  193. package/dist/hierarchy/createFoldersCollection.d.ts.map +0 -1
  194. package/dist/hierarchy/createFoldersCollection.js +0 -63
  195. package/dist/hierarchy/createFoldersCollection.js.map +0 -1
  196. package/dist/hierarchy/createTagField.d.ts +0 -44
  197. package/dist/hierarchy/createTagField.d.ts.map +0 -1
  198. package/dist/hierarchy/createTagField.js +0 -48
  199. package/dist/hierarchy/createTagField.js.map +0 -1
  200. package/dist/hierarchy/createTagsCollection.d.ts +0 -54
  201. package/dist/hierarchy/createTagsCollection.d.ts.map +0 -1
  202. package/dist/hierarchy/createTagsCollection.js +0 -56
  203. package/dist/hierarchy/createTagsCollection.js.map +0 -1
  204. package/dist/hierarchy/getInitialTreeData.d.ts +0 -27
  205. package/dist/hierarchy/getInitialTreeData.d.ts.map +0 -1
  206. package/dist/hierarchy/getInitialTreeData.js +0 -125
  207. package/dist/hierarchy/getInitialTreeData.js.map +0 -1
  208. package/dist/hierarchy/hooks/collectionAfterDelete.d.ts +0 -14
  209. package/dist/hierarchy/hooks/collectionAfterDelete.d.ts.map +0 -1
  210. package/dist/hierarchy/hooks/collectionAfterDelete.js +0 -21
  211. package/dist/hierarchy/hooks/collectionAfterDelete.js.map +0 -1
  212. package/dist/hierarchy/hooks/collectionAfterRead.d.ts +0 -27
  213. package/dist/hierarchy/hooks/collectionAfterRead.d.ts.map +0 -1
  214. package/dist/hierarchy/hooks/collectionAfterRead.js +0 -64
  215. package/dist/hierarchy/hooks/collectionAfterRead.js.map +0 -1
  216. package/dist/hierarchy/hooks/collectionBeforeChange.d.ts +0 -19
  217. package/dist/hierarchy/hooks/collectionBeforeChange.d.ts.map +0 -1
  218. package/dist/hierarchy/hooks/collectionBeforeChange.js +0 -90
  219. package/dist/hierarchy/hooks/collectionBeforeChange.js.map +0 -1
  220. package/dist/hierarchy/hooks/collectionBeforeDelete.d.ts +0 -15
  221. package/dist/hierarchy/hooks/collectionBeforeDelete.d.ts.map +0 -1
  222. package/dist/hierarchy/hooks/collectionBeforeDelete.js +0 -20
  223. package/dist/hierarchy/hooks/collectionBeforeDelete.js.map +0 -1
  224. package/dist/hierarchy/hooks/ensureSafeCollectionsChange.d.ts +0 -8
  225. package/dist/hierarchy/hooks/ensureSafeCollectionsChange.d.ts.map +0 -1
  226. package/dist/hierarchy/hooks/ensureSafeCollectionsChange.js +0 -108
  227. package/dist/hierarchy/hooks/ensureSafeCollectionsChange.js.map +0 -1
  228. package/dist/hierarchy/injectHierarchyButton.d.ts +0 -14
  229. package/dist/hierarchy/injectHierarchyButton.d.ts.map +0 -1
  230. package/dist/hierarchy/injectHierarchyButton.js +0 -37
  231. package/dist/hierarchy/injectHierarchyButton.js.map +0 -1
  232. package/dist/hierarchy/resolveHierarchyCollections.d.ts +0 -23
  233. package/dist/hierarchy/resolveHierarchyCollections.d.ts.map +0 -1
  234. package/dist/hierarchy/resolveHierarchyCollections.js +0 -312
  235. package/dist/hierarchy/resolveHierarchyCollections.js.map +0 -1
  236. package/dist/hierarchy/sanitizeHierarchyCollection.d.ts +0 -14
  237. package/dist/hierarchy/sanitizeHierarchyCollection.d.ts.map +0 -1
  238. package/dist/hierarchy/sanitizeHierarchyCollection.js +0 -112
  239. package/dist/hierarchy/sanitizeHierarchyCollection.js.map +0 -1
  240. package/dist/hierarchy/types.d.ts +0 -137
  241. package/dist/hierarchy/types.d.ts.map +0 -1
  242. package/dist/hierarchy/types.js +0 -6
  243. package/dist/hierarchy/types.js.map +0 -1
  244. package/dist/hierarchy/utils/buildLocalizedHierarchyPaths.d.ts +0 -71
  245. package/dist/hierarchy/utils/buildLocalizedHierarchyPaths.d.ts.map +0 -1
  246. package/dist/hierarchy/utils/buildLocalizedHierarchyPaths.js +0 -65
  247. package/dist/hierarchy/utils/buildLocalizedHierarchyPaths.js.map +0 -1
  248. package/dist/hierarchy/utils/computePaths.d.ts +0 -31
  249. package/dist/hierarchy/utils/computePaths.d.ts.map +0 -1
  250. package/dist/hierarchy/utils/computePaths.js +0 -371
  251. package/dist/hierarchy/utils/computePaths.js.map +0 -1
  252. package/dist/hierarchy/utils/findUseAsTitle.d.ts +0 -6
  253. package/dist/hierarchy/utils/findUseAsTitle.d.ts.map +0 -1
  254. package/dist/hierarchy/utils/findUseAsTitle.js +0 -72
  255. package/dist/hierarchy/utils/findUseAsTitle.js.map +0 -1
  256. package/dist/hierarchy/utils/getAncestors.d.ts +0 -34
  257. package/dist/hierarchy/utils/getAncestors.d.ts.map +0 -1
  258. package/dist/hierarchy/utils/getAncestors.js +0 -94
  259. package/dist/hierarchy/utils/getAncestors.js.map +0 -1
  260. package/dist/hierarchy/utils/getLocalizedValue.d.ts +0 -30
  261. package/dist/hierarchy/utils/getLocalizedValue.d.ts.map +0 -1
  262. package/dist/hierarchy/utils/getLocalizedValue.js +0 -46
  263. package/dist/hierarchy/utils/getLocalizedValue.js.map +0 -1
  264. package/dist/hierarchy/utils/getLocalizedValue.spec.js +0 -250
  265. package/dist/hierarchy/utils/getLocalizedValue.spec.js.map +0 -1
@@ -1,125 +0,0 @@
1
- import { DEFAULT_HIERARCHY_TREE_LIMIT } from '../hierarchy/constants.js';
2
- export const getInitialTreeData = async ({ baseFilter, collectionSlug, expandedNodeIds = [], filterByCollections, limit, payload, selectedNodeId, selectedNodeParentId, user })=>{
3
- const collectionConfig = payload.collections[collectionSlug]?.config;
4
- if (!collectionConfig || !collectionConfig.hierarchy) {
5
- throw new Error(`Collection ${collectionSlug} is not a hierarchy`);
6
- }
7
- const hierarchyConfig = collectionConfig.hierarchy;
8
- const parentFieldName = hierarchyConfig.parentFieldName;
9
- const useAsTitle = collectionConfig.admin?.useAsTitle ?? 'id';
10
- // Get typeFieldName for filtering
11
- const typeFieldName = hierarchyConfig.collectionSpecific && typeof hierarchyConfig.collectionSpecific === 'object' ? hierarchyConfig.collectionSpecific.fieldName : undefined;
12
- // Build filter condition if filterByCollections is provided
13
- // Exclude the hierarchy collection itself (folders always show folders)
14
- const filteredTypes = filterByCollections?.filter((t)=>t !== collectionSlug);
15
- // Get all possible type values from relatedCollections for detecting empty arrays
16
- const allPossibleTypes = hierarchyConfig.relatedCollections ? Object.keys(hierarchyConfig.relatedCollections) : [];
17
- const filterCondition = filteredTypes?.length && typeFieldName ? {
18
- or: [
19
- {
20
- [typeFieldName]: {
21
- in: filteredTypes
22
- }
23
- },
24
- {
25
- [typeFieldName]: {
26
- exists: false
27
- }
28
- },
29
- // Include unrestricted folders with empty allowedTypes array
30
- // Using not_in with all possible values matches empty arrays in both MongoDB and Postgres
31
- ...allPossibleTypes.length > 0 ? [
32
- {
33
- [typeFieldName]: {
34
- not_in: allPossibleTypes
35
- }
36
- }
37
- ] : []
38
- ]
39
- } : null;
40
- // Use limit from config if not provided, fallback to config's treeLimit
41
- const effectiveLimit = limit ?? hierarchyConfig.admin?.treeLimit ?? DEFAULT_HIERARCHY_TREE_LIMIT;
42
- const allDocs = [];
43
- const loadedParents = {};
44
- // Normalize selectedNodeParentId: treat null/undefined as 'null' (root level)
45
- const normalizedSelectedParentId = selectedNodeParentId === null || selectedNodeParentId === undefined ? 'null' : String(selectedNodeParentId);
46
- // Helper to check if selectedNodeId is among siblings at a given parent level
47
- const needsSelectedNodeIncluded = (parentKey)=>{
48
- return !!selectedNodeId && normalizedSelectedParentId === parentKey;
49
- };
50
- // Helper to fetch children with optional selectedNodeId inclusion
51
- const fetchChildrenForParent = async (parentKey, parentCondition)=>{
52
- const mustIncludeSelected = needsSelectedNodeIncluded(parentKey);
53
- let accumulatedDocs = [];
54
- let currentPage = 1;
55
- let hasMore = true;
56
- let totalDocs = 0;
57
- let foundSelected = false;
58
- // Combine parent condition with filter condition and baseFilter
59
- const conditions = [
60
- parentCondition
61
- ];
62
- if (filterCondition) {
63
- conditions.push(filterCondition);
64
- }
65
- if (baseFilter) {
66
- conditions.push(baseFilter);
67
- }
68
- const whereClause = conditions.length > 1 ? {
69
- and: conditions
70
- } : parentCondition;
71
- while(hasMore){
72
- const result = await payload.find({
73
- collection: collectionSlug,
74
- depth: 0,
75
- limit: effectiveLimit,
76
- overrideAccess: false,
77
- page: currentPage,
78
- sort: useAsTitle,
79
- user,
80
- where: whereClause
81
- });
82
- accumulatedDocs = [
83
- ...accumulatedDocs,
84
- ...result.docs
85
- ];
86
- totalDocs = result.totalDocs;
87
- hasMore = result.hasNextPage;
88
- // Check if selectedNodeId is in this page's results
89
- if (mustIncludeSelected && !foundSelected) {
90
- foundSelected = result.docs.some((doc)=>String(doc.id) === String(selectedNodeId));
91
- }
92
- // Stop if we've found the selected node OR we only need first page
93
- if (!mustIncludeSelected || foundSelected || !hasMore) {
94
- break;
95
- }
96
- currentPage++;
97
- }
98
- allDocs.push(...accumulatedDocs);
99
- loadedParents[parentKey] = {
100
- hasMore,
101
- loadedCount: accumulatedDocs.length,
102
- totalDocs
103
- };
104
- };
105
- // Query 1: Fetch root nodes
106
- await fetchChildrenForParent('null', {
107
- [parentFieldName]: {
108
- exists: false
109
- }
110
- });
111
- // Query 2: For each expanded node, fetch its children
112
- for (const parentId of expandedNodeIds){
113
- await fetchChildrenForParent(String(parentId), {
114
- [parentFieldName]: {
115
- equals: parentId
116
- }
117
- });
118
- }
119
- return {
120
- docs: allDocs,
121
- loadedParents
122
- };
123
- };
124
-
125
- //# sourceMappingURL=getInitialTreeData.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/hierarchy/getInitialTreeData.ts"],"sourcesContent":["import type { TypeWithID } from '../collections/config/types.js'\nimport type { PayloadRequest, Where } from '../types/index.js'\n\nimport { DEFAULT_HIERARCHY_TREE_LIMIT } from '../hierarchy/constants.js'\n\nexport type GetInitialTreeDataArgs = {\n /** Base filter to apply to all queries (e.g., tenant filter) */\n baseFilter?: null | Where\n collectionSlug: string\n expandedNodeIds?: (number | string)[]\n /** Filter tree to only show folders that allow these collection types (or are unrestricted) */\n filterByCollections?: string[]\n limit?: number\n payload: PayloadRequest['payload']\n /** The currently selected node ID. When provided, ensures siblings are loaded to include this node. */\n selectedNodeId?: null | number | string\n /** The parent ID of the selected node. Required when selectedNodeId is provided. */\n selectedNodeParentId?: null | number | string\n user: PayloadRequest['user']\n}\n\nexport type InitialTreeData = {\n docs: TypeWithID[]\n // Metadata about what was loaded - keyed by parent ID ('null' for root)\n loadedParents: Record<string, { hasMore: boolean; loadedCount?: number; totalDocs: number }>\n}\n\nexport const getInitialTreeData = async ({\n baseFilter,\n collectionSlug,\n expandedNodeIds = [],\n filterByCollections,\n limit,\n payload,\n selectedNodeId,\n selectedNodeParentId,\n user,\n}: GetInitialTreeDataArgs): Promise<InitialTreeData> => {\n const collectionConfig = payload.collections[collectionSlug]?.config\n\n if (!collectionConfig || !collectionConfig.hierarchy) {\n throw new Error(`Collection ${collectionSlug} is not a hierarchy`)\n }\n\n const hierarchyConfig = collectionConfig.hierarchy\n const parentFieldName = hierarchyConfig.parentFieldName\n const useAsTitle = collectionConfig.admin?.useAsTitle ?? 'id'\n\n // Get typeFieldName for filtering\n const typeFieldName =\n hierarchyConfig.collectionSpecific && typeof hierarchyConfig.collectionSpecific === 'object'\n ? hierarchyConfig.collectionSpecific.fieldName\n : undefined\n\n // Build filter condition if filterByCollections is provided\n // Exclude the hierarchy collection itself (folders always show folders)\n const filteredTypes = filterByCollections?.filter((t) => t !== collectionSlug)\n\n // Get all possible type values from relatedCollections for detecting empty arrays\n const allPossibleTypes = hierarchyConfig.relatedCollections\n ? Object.keys(hierarchyConfig.relatedCollections)\n : []\n\n const filterCondition =\n filteredTypes?.length && typeFieldName\n ? {\n or: [\n { [typeFieldName]: { in: filteredTypes } },\n { [typeFieldName]: { exists: false } }, // Include unrestricted folders (field doesn't exist)\n // Include unrestricted folders with empty allowedTypes array\n // Using not_in with all possible values matches empty arrays in both MongoDB and Postgres\n ...(allPossibleTypes.length > 0\n ? [{ [typeFieldName]: { not_in: allPossibleTypes } }]\n : []),\n ],\n }\n : null\n\n // Use limit from config if not provided, fallback to config's treeLimit\n const effectiveLimit = limit ?? hierarchyConfig.admin?.treeLimit ?? DEFAULT_HIERARCHY_TREE_LIMIT\n\n const allDocs: TypeWithID[] = []\n const loadedParents: Record<\n string,\n { hasMore: boolean; loadedCount: number; totalDocs: number }\n > = {}\n\n // Normalize selectedNodeParentId: treat null/undefined as 'null' (root level)\n const normalizedSelectedParentId =\n selectedNodeParentId === null || selectedNodeParentId === undefined\n ? 'null'\n : String(selectedNodeParentId)\n\n // Helper to check if selectedNodeId is among siblings at a given parent level\n const needsSelectedNodeIncluded = (parentKey: string): boolean => {\n return !!selectedNodeId && normalizedSelectedParentId === parentKey\n }\n\n // Helper to fetch children with optional selectedNodeId inclusion\n const fetchChildrenForParent = async (\n parentKey: string,\n parentCondition: Where,\n ): Promise<void> => {\n const mustIncludeSelected = needsSelectedNodeIncluded(parentKey)\n let accumulatedDocs: TypeWithID[] = []\n let currentPage = 1\n let hasMore = true\n let totalDocs = 0\n let foundSelected = false\n\n // Combine parent condition with filter condition and baseFilter\n const conditions: Where[] = [parentCondition]\n if (filterCondition) {\n conditions.push(filterCondition)\n }\n if (baseFilter) {\n conditions.push(baseFilter)\n }\n const whereClause = conditions.length > 1 ? { and: conditions } : parentCondition\n\n while (hasMore) {\n const result = await payload.find({\n collection: collectionSlug,\n depth: 0,\n limit: effectiveLimit,\n overrideAccess: false,\n page: currentPage,\n sort: useAsTitle,\n user,\n where: whereClause,\n })\n\n accumulatedDocs = [...accumulatedDocs, ...result.docs]\n totalDocs = result.totalDocs\n hasMore = result.hasNextPage\n\n // Check if selectedNodeId is in this page's results\n if (mustIncludeSelected && !foundSelected) {\n foundSelected = result.docs.some(\n (doc: TypeWithID) => String(doc.id) === String(selectedNodeId),\n )\n }\n\n // Stop if we've found the selected node OR we only need first page\n if (!mustIncludeSelected || foundSelected || !hasMore) {\n break\n }\n\n currentPage++\n }\n\n allDocs.push(...accumulatedDocs)\n loadedParents[parentKey] = {\n hasMore,\n loadedCount: accumulatedDocs.length,\n totalDocs,\n }\n }\n\n // Query 1: Fetch root nodes\n await fetchChildrenForParent('null', {\n [parentFieldName]: { exists: false },\n })\n\n // Query 2: For each expanded node, fetch its children\n for (const parentId of expandedNodeIds) {\n await fetchChildrenForParent(String(parentId), {\n [parentFieldName]: { equals: parentId },\n })\n }\n\n return {\n docs: allDocs,\n loadedParents,\n }\n}\n"],"names":["DEFAULT_HIERARCHY_TREE_LIMIT","getInitialTreeData","baseFilter","collectionSlug","expandedNodeIds","filterByCollections","limit","payload","selectedNodeId","selectedNodeParentId","user","collectionConfig","collections","config","hierarchy","Error","hierarchyConfig","parentFieldName","useAsTitle","admin","typeFieldName","collectionSpecific","fieldName","undefined","filteredTypes","filter","t","allPossibleTypes","relatedCollections","Object","keys","filterCondition","length","or","in","exists","not_in","effectiveLimit","treeLimit","allDocs","loadedParents","normalizedSelectedParentId","String","needsSelectedNodeIncluded","parentKey","fetchChildrenForParent","parentCondition","mustIncludeSelected","accumulatedDocs","currentPage","hasMore","totalDocs","foundSelected","conditions","push","whereClause","and","result","find","collection","depth","overrideAccess","page","sort","where","docs","hasNextPage","some","doc","id","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uBK,iBAAiB,CAACF,SAAS;gBACrD;YACF;YAEAD;QACF;QAEAV,QAAQe,IAAI,IAAIN;QAChBR,aAAa,CAACI,UAAU,GAAG;YACzBM;YACAoB,aAAatB,gBAAgBhB,MAAM;YACnCmB;QACF;IACF;IAEA,4BAA4B;IAC5B,MAAMN,uBAAuB,QAAQ;QACnC,CAAC5B,gBAAgB,EAAE;YAAEkB,QAAQ;QAAM;IACrC;IAEA,sDAAsD;IACtD,KAAK,MAAMoC,YAAYnE,gBAAiB;QACtC,MAAMyC,uBAAuBH,OAAO6B,WAAW;YAC7C,CAACtD,gBAAgB,EAAE;gBAAEuD,QAAQD;YAAS;QACxC;IACF;IAEA,OAAO;QACLN,MAAM1B;QACNC;IACF;AACF,EAAC"}
@@ -1,14 +0,0 @@
1
- /**
2
- * afterDelete Hook Responsibilities:
3
- * - Clear folder references from related documents when folder is deleted
4
- */
5
- import type { CollectionAfterDeleteHook } from '../../index.js';
6
- type Args = {
7
- /**
8
- * Map of collection slugs to their field names
9
- */
10
- relatedCollections: Record<string, string>;
11
- };
12
- export declare const hierarchyCollectionAfterDelete: ({ relatedCollections }: Args) => CollectionAfterDeleteHook;
13
- export {};
14
- //# sourceMappingURL=collectionAfterDelete.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collectionAfterDelete.d.ts","sourceRoot":"","sources":["../../../src/hierarchy/hooks/collectionAfterDelete.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAA;AAE/D,KAAK,IAAI,GAAG;IACV;;OAEG;IACH,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC3C,CAAA;AAED,eAAO,MAAM,8BAA8B,2BAChB,IAAI,KAAG,yBAgB/B,CAAA"}
@@ -1,21 +0,0 @@
1
- /**
2
- * afterDelete Hook Responsibilities:
3
- * - Clear folder references from related documents when folder is deleted
4
- */ export const hierarchyCollectionAfterDelete = ({ relatedCollections })=>async ({ id, req })=>{
5
- for (const [collectionSlug, fieldName] of Object.entries(relatedCollections)){
6
- await req.payload.update({
7
- collection: collectionSlug,
8
- data: {
9
- [fieldName]: null
10
- },
11
- req,
12
- where: {
13
- [fieldName]: {
14
- equals: id
15
- }
16
- }
17
- });
18
- }
19
- };
20
-
21
- //# sourceMappingURL=collectionAfterDelete.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/hierarchy/hooks/collectionAfterDelete.ts"],"sourcesContent":["/**\n * afterDelete Hook Responsibilities:\n * - Clear folder references from related documents when folder is deleted\n */\n\nimport type { CollectionAfterDeleteHook } from '../../index.js'\n\ntype Args = {\n /**\n * Map of collection slugs to their field names\n */\n relatedCollections: Record<string, string>\n}\n\nexport const hierarchyCollectionAfterDelete =\n ({ relatedCollections }: Args): CollectionAfterDeleteHook =>\n async ({ id, req }) => {\n for (const [collectionSlug, fieldName] of Object.entries(relatedCollections)) {\n await req.payload.update({\n collection: collectionSlug,\n data: {\n [fieldName]: null,\n },\n req,\n where: {\n [fieldName]: {\n equals: id,\n },\n },\n })\n }\n }\n"],"names":["hierarchyCollectionAfterDelete","relatedCollections","id","req","collectionSlug","fieldName","Object","entries","payload","update","collection","data","where","equals"],"mappings":"AAAA;;;CAGC,GAWD,OAAO,MAAMA,iCACX,CAAC,EAAEC,kBAAkB,EAAQ,GAC7B,OAAO,EAAEC,EAAE,EAAEC,GAAG,EAAE;QAChB,KAAK,MAAM,CAACC,gBAAgBC,UAAU,IAAIC,OAAOC,OAAO,CAACN,oBAAqB;YAC5E,MAAME,IAAIK,OAAO,CAACC,MAAM,CAAC;gBACvBC,YAAYN;gBACZO,MAAM;oBACJ,CAACN,UAAU,EAAE;gBACf;gBACAF;gBACAS,OAAO;oBACL,CAACP,UAAU,EAAE;wBACXQ,QAAQX;oBACV;gBACF;YACF;QACF;IACF,EAAC"}
@@ -1,27 +0,0 @@
1
- /**
2
- * afterRead Hook Responsibilities:
3
- * - Automatically compute and attach _h_slugPath and _h_titlePath to documents
4
- * - Use cached ancestors to optimize queries when reading multiple documents
5
- * - Only computes when explicitly requested via:
6
- * 1. context.computeHierarchyPaths flag
7
- * 2. ?computeHierarchyPaths=true query param
8
- * 3. Selecting path fields in query (select parameter)
9
- */
10
- import type { CollectionAfterReadHook } from '../../index.js';
11
- type Args = {
12
- /**
13
- * The name of the field that contains the parent document ID
14
- */
15
- parentFieldName: string;
16
- /**
17
- * The name of the field to populate with the slug-based path
18
- */
19
- slugPathFieldName: string;
20
- /**
21
- * The name of the field to populate with the title-based path
22
- */
23
- titlePathFieldName: string;
24
- };
25
- export declare const hierarchyCollectionAfterRead: ({ parentFieldName, slugPathFieldName, titlePathFieldName }: Args) => CollectionAfterReadHook;
26
- export {};
27
- //# sourceMappingURL=collectionAfterRead.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collectionAfterRead.d.ts","sourceRoot":"","sources":["../../../src/hierarchy/hooks/collectionAfterRead.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,uBAAuB,EAAkB,MAAM,gBAAgB,CAAA;AAgC7E,KAAK,IAAI,GAAG;IACV;;OAEG;IACH,eAAe,EAAE,MAAM,CAAA;IACvB;;OAEG;IACH,iBAAiB,EAAE,MAAM,CAAA;IACzB;;OAEG;IACH,kBAAkB,EAAE,MAAM,CAAA;CAC3B,CAAA;AAED,eAAO,MAAM,4BAA4B,+DACsB,IAAI,KAAG,uBA8CnE,CAAA"}
@@ -1,64 +0,0 @@
1
- /**
2
- * afterRead Hook Responsibilities:
3
- * - Automatically compute and attach _h_slugPath and _h_titlePath to documents
4
- * - Use cached ancestors to optimize queries when reading multiple documents
5
- * - Only computes when explicitly requested via:
6
- * 1. context.computeHierarchyPaths flag
7
- * 2. ?computeHierarchyPaths=true query param
8
- * 3. Selecting path fields in query (select parameter)
9
- */ import { computePaths } from '../utils/computePaths.js';
10
- import { findUseAsTitleField } from '../utils/findUseAsTitle.js';
11
- /**
12
- * Checks if path fields are being selected in the query
13
- */ function isPathFieldSelected(req, slugPathFieldName, titlePathFieldName) {
14
- // Check query.select parameter (REST API)
15
- const selectParam = req.query?.select;
16
- if (selectParam) {
17
- if (typeof selectParam === 'string') {
18
- const selectedFields = selectParam.split(',').map((f)=>f.trim());
19
- return selectedFields.includes(slugPathFieldName) || selectedFields.includes(titlePathFieldName);
20
- }
21
- // Could be an array or object depending on query parser
22
- if (Array.isArray(selectParam)) {
23
- return selectParam.includes(slugPathFieldName) || selectParam.includes(titlePathFieldName);
24
- }
25
- }
26
- return false;
27
- }
28
- export const hierarchyCollectionAfterRead = ({ parentFieldName, slugPathFieldName, titlePathFieldName })=>async ({ collection, context, doc, req })=>{
29
- // Skip if deleting
30
- if (context?.isDeleting) {
31
- return doc;
32
- }
33
- // Determine if paths should be computed
34
- const shouldComputePaths = context?.computeHierarchyPaths === true || // Explicit flag
35
- req.query?.computeHierarchyPaths === 'true' || // Query parameter
36
- isPathFieldSelected(req, slugPathFieldName, titlePathFieldName) // Field selection detection
37
- ;
38
- if (!shouldComputePaths) {
39
- return doc;
40
- }
41
- try {
42
- const { localized: isTitleLocalized } = findUseAsTitleField(collection);
43
- const { slugPath, titlePath } = await computePaths({
44
- collection,
45
- doc,
46
- draft: doc._status === 'draft',
47
- locale: isTitleLocalized && req.locale === 'all' ? 'all' : req.locale === 'all' ? undefined : req.locale || undefined,
48
- parentFieldName,
49
- req,
50
- slugPathFieldName,
51
- titlePathFieldName
52
- });
53
- // Attach computed paths to document using configured field names
54
- doc[slugPathFieldName] = slugPath;
55
- doc[titlePathFieldName] = titlePath;
56
- } catch (error) {
57
- // If path computation fails, log but don't break the document read
58
- // eslint-disable-next-line no-console
59
- console.error(`Failed to compute hierarchy paths for document ${doc.id}:`, error);
60
- }
61
- return doc;
62
- };
63
-
64
- //# sourceMappingURL=collectionAfterRead.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/hierarchy/hooks/collectionAfterRead.ts"],"sourcesContent":["/**\n * afterRead Hook Responsibilities:\n * - Automatically compute and attach _h_slugPath and _h_titlePath to documents\n * - Use cached ancestors to optimize queries when reading multiple documents\n * - Only computes when explicitly requested via:\n * 1. context.computeHierarchyPaths flag\n * 2. ?computeHierarchyPaths=true query param\n * 3. Selecting path fields in query (select parameter)\n */\n\nimport type { CollectionAfterReadHook, PayloadRequest } from '../../index.js'\n\nimport { computePaths } from '../utils/computePaths.js'\nimport { findUseAsTitleField } from '../utils/findUseAsTitle.js'\n\n/**\n * Checks if path fields are being selected in the query\n */\nfunction isPathFieldSelected(\n req: PayloadRequest,\n slugPathFieldName: string,\n titlePathFieldName: string,\n): boolean {\n // Check query.select parameter (REST API)\n const selectParam = req.query?.select\n\n if (selectParam) {\n if (typeof selectParam === 'string') {\n const selectedFields = selectParam.split(',').map((f) => f.trim())\n return (\n selectedFields.includes(slugPathFieldName) || selectedFields.includes(titlePathFieldName)\n )\n }\n // Could be an array or object depending on query parser\n if (Array.isArray(selectParam)) {\n return selectParam.includes(slugPathFieldName) || selectParam.includes(titlePathFieldName)\n }\n }\n\n return false\n}\n\ntype Args = {\n /**\n * The name of the field that contains the parent document ID\n */\n parentFieldName: string\n /**\n * The name of the field to populate with the slug-based path\n */\n slugPathFieldName: string\n /**\n * The name of the field to populate with the title-based path\n */\n titlePathFieldName: string\n}\n\nexport const hierarchyCollectionAfterRead =\n ({ parentFieldName, slugPathFieldName, titlePathFieldName }: Args): CollectionAfterReadHook =>\n async ({ collection, context, doc, req }) => {\n // Skip if deleting\n if (context?.isDeleting) {\n return doc\n }\n\n // Determine if paths should be computed\n const shouldComputePaths =\n context?.computeHierarchyPaths === true || // Explicit flag\n req.query?.computeHierarchyPaths === 'true' || // Query parameter\n isPathFieldSelected(req, slugPathFieldName, titlePathFieldName) // Field selection detection\n\n if (!shouldComputePaths) {\n return doc\n }\n\n try {\n const { localized: isTitleLocalized } = findUseAsTitleField(collection)\n\n const { slugPath, titlePath } = await computePaths({\n collection,\n doc,\n draft: doc._status === 'draft',\n locale:\n isTitleLocalized && req.locale === 'all'\n ? 'all'\n : req.locale === 'all'\n ? undefined\n : req.locale || undefined,\n parentFieldName,\n req,\n slugPathFieldName,\n titlePathFieldName,\n })\n\n // Attach computed paths to document using configured field names\n doc[slugPathFieldName] = slugPath\n doc[titlePathFieldName] = titlePath\n } catch (error) {\n // If path computation fails, log but don't break the document read\n // eslint-disable-next-line no-console\n console.error(`Failed to compute hierarchy paths for document ${doc.id}:`, error)\n }\n\n return doc\n }\n"],"names":["computePaths","findUseAsTitleField","isPathFieldSelected","req","slugPathFieldName","titlePathFieldName","selectParam","query","select","selectedFields","split","map","f","trim","includes","Array","isArray","hierarchyCollectionAfterRead","parentFieldName","collection","context","doc","isDeleting","shouldComputePaths","computeHierarchyPaths","localized","isTitleLocalized","slugPath","titlePath","draft","_status","locale","undefined","error","console","id"],"mappings":"AAAA;;;;;;;;CAQC,GAID,SAASA,YAAY,QAAQ,2BAA0B;AACvD,SAASC,mBAAmB,QAAQ,6BAA4B;AAEhE;;CAEC,GACD,SAASC,oBACPC,GAAmB,EACnBC,iBAAyB,EACzBC,kBAA0B;IAE1B,0CAA0C;IAC1C,MAAMC,cAAcH,IAAII,KAAK,EAAEC;IAE/B,IAAIF,aAAa;QACf,IAAI,OAAOA,gBAAgB,UAAU;YACnC,MAAMG,iBAAiBH,YAAYI,KAAK,CAAC,KAAKC,GAAG,CAAC,CAACC,IAAMA,EAAEC,IAAI;YAC/D,OACEJ,eAAeK,QAAQ,CAACV,sBAAsBK,eAAeK,QAAQ,CAACT;QAE1E;QACA,wDAAwD;QACxD,IAAIU,MAAMC,OAAO,CAACV,cAAc;YAC9B,OAAOA,YAAYQ,QAAQ,CAACV,sBAAsBE,YAAYQ,QAAQ,CAACT;QACzE;IACF;IAEA,OAAO;AACT;AAiBA,OAAO,MAAMY,+BACX,CAAC,EAAEC,eAAe,EAAEd,iBAAiB,EAAEC,kBAAkB,EAAQ,GACjE,OAAO,EAAEc,UAAU,EAAEC,OAAO,EAAEC,GAAG,EAAElB,GAAG,EAAE;QACtC,mBAAmB;QACnB,IAAIiB,SAASE,YAAY;YACvB,OAAOD;QACT;QAEA,wCAAwC;QACxC,MAAME,qBACJH,SAASI,0BAA0B,QAAQ,gBAAgB;QAC3DrB,IAAII,KAAK,EAAEiB,0BAA0B,UAAU,kBAAkB;QACjEtB,oBAAoBC,KAAKC,mBAAmBC,oBAAoB,4BAA4B;;QAE9F,IAAI,CAACkB,oBAAoB;YACvB,OAAOF;QACT;QAEA,IAAI;YACF,MAAM,EAAEI,WAAWC,gBAAgB,EAAE,GAAGzB,oBAAoBkB;YAE5D,MAAM,EAAEQ,QAAQ,EAAEC,SAAS,EAAE,GAAG,MAAM5B,aAAa;gBACjDmB;gBACAE;gBACAQ,OAAOR,IAAIS,OAAO,KAAK;gBACvBC,QACEL,oBAAoBvB,IAAI4B,MAAM,KAAK,QAC/B,QACA5B,IAAI4B,MAAM,KAAK,QACbC,YACA7B,IAAI4B,MAAM,IAAIC;gBACtBd;gBACAf;gBACAC;gBACAC;YACF;YAEA,iEAAiE;YACjEgB,GAAG,CAACjB,kBAAkB,GAAGuB;YACzBN,GAAG,CAAChB,mBAAmB,GAAGuB;QAC5B,EAAE,OAAOK,OAAO;YACd,mEAAmE;YACnE,sCAAsC;YACtCC,QAAQD,KAAK,CAAC,CAAC,+CAA+C,EAAEZ,IAAIc,EAAE,CAAC,CAAC,CAAC,EAAEF;QAC7E;QAEA,OAAOZ;IACT,EAAC"}
@@ -1,19 +0,0 @@
1
- /**
2
- * beforeChange Hook Responsibilities:
3
- * - Validate circular references when parent changes
4
- * - Prevent moving a folder into its own subfolder
5
- *
6
- * Does NOT handle:
7
- * - Tree structure (no stored tree anymore)
8
- * - Path computation (done in afterRead)
9
- */
10
- import type { CollectionBeforeChangeHook } from '../../index.js';
11
- type Args = {
12
- /**
13
- * The name of the field that contains the parent document ID
14
- */
15
- parentFieldName: string;
16
- };
17
- export declare const hierarchyCollectionBeforeChange: ({ parentFieldName }: Args) => CollectionBeforeChangeHook;
18
- export {};
19
- //# sourceMappingURL=collectionBeforeChange.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collectionBeforeChange.d.ts","sourceRoot":"","sources":["../../../src/hierarchy/hooks/collectionBeforeChange.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,0BAA0B,EAI3B,MAAM,gBAAgB,CAAA;AAEvB,KAAK,IAAI,GAAG;IACV;;OAEG;IACH,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,eAAO,MAAM,+BAA+B,wBACpB,IAAI,KAAG,0BAgC5B,CAAA"}
@@ -1,90 +0,0 @@
1
- /**
2
- * beforeChange Hook Responsibilities:
3
- * - Validate circular references when parent changes
4
- * - Prevent moving a folder into its own subfolder
5
- *
6
- * Does NOT handle:
7
- * - Tree structure (no stored tree anymore)
8
- * - Path computation (done in afterRead)
9
- */ export const hierarchyCollectionBeforeChange = ({ parentFieldName })=>async ({ collection, data, operation, originalDoc, req })=>{
10
- // Determine the new parent ID
11
- const newParentID = data[parentFieldName] !== undefined ? data[parentFieldName] : originalDoc?.[parentFieldName];
12
- const parentChanged = operation === 'update' && data[parentFieldName] !== undefined && data[parentFieldName] !== originalDoc?.[parentFieldName];
13
- // Validate circular references when parent is changing
14
- if (parentChanged && newParentID) {
15
- // Extract parent ID (could be plain ID or populated object with id)
16
- const parentId = typeof newParentID === 'object' && 'id' in newParentID ? newParentID.id : newParentID;
17
- // Prevent self-referential parent
18
- if (parentId === (originalDoc?.id || data.id)) {
19
- throw new Error('Document cannot be its own parent');
20
- }
21
- // Check for true circular references (loops in the chain), but allow
22
- // moving into a child - that will be handled by afterChange reparenting
23
- await validateNoCircularReference({
24
- collection,
25
- currentDocId: originalDoc?.id,
26
- parentId,
27
- req
28
- });
29
- }
30
- return data;
31
- };
32
- /**
33
- * Walks up the parent chain to detect true circular references (loops).
34
- * Does NOT throw when moving into a child - that case is handled by afterChange
35
- * which will automatically reparent the child.
36
- */ async function validateNoCircularReference({ collection, currentDocId, parentId, req }) {
37
- const parentFieldName = collection.hierarchy && collection.hierarchy !== true ? collection.hierarchy.parentFieldName : undefined;
38
- if (!parentFieldName) {
39
- return;
40
- }
41
- const fieldName = parentFieldName;
42
- async function checkAncestor(ancestorId, visitedNodes = new Set()) {
43
- // Create unique key for this node
44
- const nodeKey = `${collection.slug}:${ancestorId}`;
45
- // Check if we've visited this node before (true loop in the chain)
46
- if (visitedNodes.has(nodeKey)) {
47
- throw new Error(`Circular reference detected: the parent chain contains a loop`);
48
- }
49
- // If we've reached the current document, this means we're trying to move into a child
50
- if (ancestorId === currentDocId) {
51
- throw new Error('Cannot move folder into its own subfolder');
52
- }
53
- // Add this node to visited set
54
- visitedNodes.add(nodeKey);
55
- try {
56
- const ancestor = await req.payload.findByID({
57
- id: ancestorId,
58
- collection: collection.slug,
59
- depth: 0,
60
- req,
61
- select: {
62
- [fieldName]: true
63
- }
64
- });
65
- const nextParent = ancestor?.[fieldName];
66
- if (!nextParent) {
67
- return; // No parent, end of chain
68
- }
69
- // Extract next parent ID (could be plain ID or populated object)
70
- let nextParentId = nextParent;
71
- if (typeof nextParent === 'object' && 'id' in nextParent) {
72
- nextParentId = nextParent.id;
73
- }
74
- // Continue traversal if parent exists
75
- if (nextParentId !== null && nextParentId !== undefined && (typeof nextParentId === 'string' || typeof nextParentId === 'number')) {
76
- return checkAncestor(nextParentId, visitedNodes);
77
- }
78
- } catch (error) {
79
- // If it's our validation error, re-throw it
80
- if (error instanceof Error && error.message?.includes('Circular reference detected')) {
81
- throw error;
82
- }
83
- // Non-existent parent can't create a circular reference
84
- return;
85
- }
86
- }
87
- await checkAncestor(parentId);
88
- }
89
-
90
- //# sourceMappingURL=collectionBeforeChange.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/hierarchy/hooks/collectionBeforeChange.ts"],"sourcesContent":["/**\n * beforeChange Hook Responsibilities:\n * - Validate circular references when parent changes\n * - Prevent moving a folder into its own subfolder\n *\n * Does NOT handle:\n * - Tree structure (no stored tree anymore)\n * - Path computation (done in afterRead)\n */\n\nimport type {\n CollectionBeforeChangeHook,\n CollectionConfig,\n JsonObject,\n PayloadRequest,\n} from '../../index.js'\n\ntype Args = {\n /**\n * The name of the field that contains the parent document ID\n */\n parentFieldName: string\n}\n\nexport const hierarchyCollectionBeforeChange =\n ({ parentFieldName }: Args): CollectionBeforeChangeHook =>\n async ({ collection, data, operation, originalDoc, req }) => {\n // Determine the new parent ID\n const newParentID =\n data[parentFieldName] !== undefined ? data[parentFieldName] : originalDoc?.[parentFieldName]\n const parentChanged =\n operation === 'update' &&\n data[parentFieldName] !== undefined &&\n data[parentFieldName] !== originalDoc?.[parentFieldName]\n\n // Validate circular references when parent is changing\n if (parentChanged && newParentID) {\n // Extract parent ID (could be plain ID or populated object with id)\n const parentId =\n typeof newParentID === 'object' && 'id' in newParentID ? newParentID.id : newParentID\n\n // Prevent self-referential parent\n if (parentId === (originalDoc?.id || data.id)) {\n throw new Error('Document cannot be its own parent')\n }\n\n // Check for true circular references (loops in the chain), but allow\n // moving into a child - that will be handled by afterChange reparenting\n await validateNoCircularReference({\n collection,\n currentDocId: originalDoc?.id,\n parentId,\n req,\n })\n }\n\n return data\n }\n\n/**\n * Walks up the parent chain to detect true circular references (loops).\n * Does NOT throw when moving into a child - that case is handled by afterChange\n * which will automatically reparent the child.\n */\nasync function validateNoCircularReference({\n collection,\n currentDocId,\n parentId,\n req,\n}: {\n collection: CollectionConfig\n currentDocId: number | string\n parentId: number | string\n req: PayloadRequest\n}) {\n const parentFieldName =\n collection.hierarchy && collection.hierarchy !== true\n ? collection.hierarchy.parentFieldName\n : undefined\n\n if (!parentFieldName) {\n return\n }\n\n const fieldName = parentFieldName\n\n async function checkAncestor(\n ancestorId: number | string,\n visitedNodes: Set<string> = new Set(),\n ): Promise<void> {\n // Create unique key for this node\n const nodeKey = `${collection.slug}:${ancestorId}`\n\n // Check if we've visited this node before (true loop in the chain)\n if (visitedNodes.has(nodeKey)) {\n throw new Error(`Circular reference detected: the parent chain contains a loop`)\n }\n\n // If we've reached the current document, this means we're trying to move into a child\n if (ancestorId === currentDocId) {\n throw new Error('Cannot move folder into its own subfolder')\n }\n\n // Add this node to visited set\n visitedNodes.add(nodeKey)\n\n try {\n const ancestor = (await req.payload.findByID({\n id: ancestorId,\n collection: collection.slug,\n depth: 0,\n req,\n select: {\n [fieldName]: true,\n },\n })) as JsonObject\n\n const nextParent = ancestor?.[fieldName]\n\n if (!nextParent) {\n return // No parent, end of chain\n }\n\n // Extract next parent ID (could be plain ID or populated object)\n let nextParentId = nextParent\n if (typeof nextParent === 'object' && 'id' in nextParent) {\n nextParentId = nextParent.id\n }\n\n // Continue traversal if parent exists\n if (\n nextParentId !== null &&\n nextParentId !== undefined &&\n (typeof nextParentId === 'string' || typeof nextParentId === 'number')\n ) {\n return checkAncestor(nextParentId, visitedNodes)\n }\n } catch (error) {\n // If it's our validation error, re-throw it\n if (error instanceof Error && error.message?.includes('Circular reference detected')) {\n throw error\n }\n // Non-existent parent can't create a circular reference\n return\n }\n }\n\n await checkAncestor(parentId)\n}\n"],"names":["hierarchyCollectionBeforeChange","parentFieldName","collection","data","operation","originalDoc","req","newParentID","undefined","parentChanged","parentId","id","Error","validateNoCircularReference","currentDocId","hierarchy","fieldName","checkAncestor","ancestorId","visitedNodes","Set","nodeKey","slug","has","add","ancestor","payload","findByID","depth","select","nextParent","nextParentId","error","message","includes"],"mappings":"AAAA;;;;;;;;CAQC,GAgBD,OAAO,MAAMA,kCACX,CAAC,EAAEC,eAAe,EAAQ,GAC1B,OAAO,EAAEC,UAAU,EAAEC,IAAI,EAAEC,SAAS,EAAEC,WAAW,EAAEC,GAAG,EAAE;QACtD,8BAA8B;QAC9B,MAAMC,cACJJ,IAAI,CAACF,gBAAgB,KAAKO,YAAYL,IAAI,CAACF,gBAAgB,GAAGI,aAAa,CAACJ,gBAAgB;QAC9F,MAAMQ,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C3BM,OAAO;gBACPtB;gBACAuB,QAAQ;oBACN,CAACb,UAAU,EAAE;gBACf;YACF;YAEA,MAAMc,aAAaL,UAAU,CAACT,UAAU;YAExC,IAAI,CAACc,YAAY;gBACf,QAAO,0BAA0B;YACnC;YAEA,iEAAiE;YACjE,IAAIC,eAAeD;YACnB,IAAI,OAAOA,eAAe,YAAY,QAAQA,YAAY;gBACxDC,eAAeD,WAAWnB,EAAE;YAC9B;YAEA,sCAAsC;YACtC,IACEoB,iBAAiB,QACjBA,iBAAiBvB,aAChB,CAAA,OAAOuB,iBAAiB,YAAY,OAAOA,iBAAiB,QAAO,GACpE;gBACA,OAAOd,cAAcc,cAAcZ;YACrC;QACF,EAAE,OAAOa,OAAO;YACd,4CAA4C;YAC5C,IAAIA,iBAAiBpB,SAASoB,MAAMC,OAAO,EAAEC,SAAS,gCAAgC;gBACpF,MAAMF;YACR;YACA,wDAAwD;YACxD;QACF;IACF;IAEA,MAAMf,cAAcP;AACtB"}
@@ -1,15 +0,0 @@
1
- /**
2
- * beforeDelete Hook Responsibilities:
3
- * - Delete child folders when parent is deleted (cascade delete)
4
- * - Set context flag for deletion tracking
5
- */
6
- import type { CollectionBeforeDeleteHook } from '../../index.js';
7
- type Args = {
8
- /**
9
- * The name of the field that contains the parent document ID
10
- */
11
- parentFieldName: string;
12
- };
13
- export declare const hierarchyCollectionBeforeDelete: ({ parentFieldName }: Args) => CollectionBeforeDeleteHook;
14
- export {};
15
- //# sourceMappingURL=collectionBeforeDelete.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collectionBeforeDelete.d.ts","sourceRoot":"","sources":["../../../src/hierarchy/hooks/collectionBeforeDelete.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAEhE,KAAK,IAAI,GAAG;IACV;;OAEG;IACH,eAAe,EAAE,MAAM,CAAA;CACxB,CAAA;AAED,eAAO,MAAM,+BAA+B,wBACpB,IAAI,KAAG,0BAe5B,CAAA"}
@@ -1,20 +0,0 @@
1
- /**
2
- * beforeDelete Hook Responsibilities:
3
- * - Delete child folders when parent is deleted (cascade delete)
4
- * - Set context flag for deletion tracking
5
- */ export const hierarchyCollectionBeforeDelete = ({ parentFieldName })=>async ({ id, collection, req })=>{
6
- req.context = req.context || {};
7
- req.context.isDeleting = true;
8
- // Delete all child folders (cascade delete)
9
- await req.payload.delete({
10
- collection: collection.slug,
11
- req,
12
- where: {
13
- [parentFieldName]: {
14
- equals: id
15
- }
16
- }
17
- });
18
- };
19
-
20
- //# sourceMappingURL=collectionBeforeDelete.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/hierarchy/hooks/collectionBeforeDelete.ts"],"sourcesContent":["/**\n * beforeDelete Hook Responsibilities:\n * - Delete child folders when parent is deleted (cascade delete)\n * - Set context flag for deletion tracking\n */\n\nimport type { CollectionBeforeDeleteHook } from '../../index.js'\n\ntype Args = {\n /**\n * The name of the field that contains the parent document ID\n */\n parentFieldName: string\n}\n\nexport const hierarchyCollectionBeforeDelete =\n ({ parentFieldName }: Args): CollectionBeforeDeleteHook =>\n async ({ id, collection, req }) => {\n req.context = req.context || {}\n req.context.isDeleting = true\n\n // Delete all child folders (cascade delete)\n await req.payload.delete({\n collection: collection.slug,\n req,\n where: {\n [parentFieldName]: {\n equals: id,\n },\n },\n })\n }\n"],"names":["hierarchyCollectionBeforeDelete","parentFieldName","id","collection","req","context","isDeleting","payload","delete","slug","where","equals"],"mappings":"AAAA;;;;CAIC,GAWD,OAAO,MAAMA,kCACX,CAAC,EAAEC,eAAe,EAAQ,GAC1B,OAAO,EAAEC,EAAE,EAAEC,UAAU,EAAEC,GAAG,EAAE;QAC5BA,IAAIC,OAAO,GAAGD,IAAIC,OAAO,IAAI,CAAC;QAC9BD,IAAIC,OAAO,CAACC,UAAU,GAAG;QAEzB,4CAA4C;QAC5C,MAAMF,IAAIG,OAAO,CAACC,MAAM,CAAC;YACvBL,YAAYA,WAAWM,IAAI;YAC3BL;YACAM,OAAO;gBACL,CAACT,gBAAgB,EAAE;oBACjBU,QAAQT;gBACV;YACF;QACF;IACF,EAAC"}
@@ -1,8 +0,0 @@
1
- import { type CollectionBeforeValidateHook, type CollectionSlug } from '../../index.js';
2
- export declare const ensureSafeCollectionsChange: ({ folderFieldName, foldersSlug, parentFieldName, typeFieldName, }: {
3
- folderFieldName: string;
4
- foldersSlug: CollectionSlug;
5
- parentFieldName?: string;
6
- typeFieldName?: string;
7
- }) => CollectionBeforeValidateHook;
8
- //# sourceMappingURL=ensureSafeCollectionsChange.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ensureSafeCollectionsChange.d.ts","sourceRoot":"","sources":["../../../src/hierarchy/hooks/ensureSafeCollectionsChange.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,4BAA4B,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIjG,eAAO,MAAM,2BAA2B,sEAMnC;IACD,eAAe,EAAE,MAAM,CAAA;IACvB,WAAW,EAAE,cAAc,CAAA;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB,KAAG,4BAmIH,CAAA"}
@@ -1,108 +0,0 @@
1
- import { APIError } from '../../index.js';
2
- import { extractID } from '../../utilities/extractID.js';
3
- import { getTranslatedLabel } from '../../utilities/getTranslatedLabel.js';
4
- export const ensureSafeCollectionsChange = ({ folderFieldName, foldersSlug, parentFieldName = 'folder', typeFieldName = 'hierarchyType' })=>async ({ data, originalDoc, req })=>{
5
- const currentFolderID = extractID(originalDoc || {});
6
- const parentFolderID = extractID(data?.[parentFieldName] || originalDoc?.[parentFieldName] || {});
7
- if (Array.isArray(data?.[typeFieldName]) && data[typeFieldName].length > 0) {
8
- const typeFieldValue = data[typeFieldName];
9
- const currentlyAssignedCollections = Array.isArray(originalDoc?.[typeFieldName]) && originalDoc[typeFieldName].length > 0 ? originalDoc[typeFieldName] : undefined;
10
- /**
11
- * Check if the assigned collections have changed.
12
- * example:
13
- * - originalAssignedCollections: ['posts', 'pages']
14
- * - folderType: ['posts']
15
- *
16
- * The user is narrowing the types of documents that can be associated with this folder.
17
- * If the user is only expanding the types of documents that can be associated with this folder,
18
- * we do not need to do anything.
19
- */ const newCollections = currentlyAssignedCollections ? currentlyAssignedCollections.filter((c)=>!typeFieldValue.includes(c)) : typeFieldValue;
20
- if (newCollections && newCollections.length > 0) {
21
- let dependentCollection = null;
22
- if (typeof currentFolderID === 'string' || typeof currentFolderID === 'number') {
23
- // Check each collection being removed for dependent documents
24
- for (const collectionSlug of newCollections){
25
- const result = await req.payload.find({
26
- collection: collectionSlug,
27
- limit: 1,
28
- overrideAccess: true,
29
- req,
30
- where: {
31
- [folderFieldName]: {
32
- equals: currentFolderID
33
- }
34
- }
35
- });
36
- if (result.totalDocs > 0) {
37
- dependentCollection = collectionSlug;
38
- break;
39
- }
40
- }
41
- // Also check for child folders with these types
42
- if (!dependentCollection) {
43
- const childFoldersResult = await req.payload.find({
44
- collection: foldersSlug,
45
- limit: 1,
46
- overrideAccess: true,
47
- req,
48
- where: {
49
- and: [
50
- {
51
- [typeFieldName]: {
52
- in: newCollections
53
- }
54
- },
55
- {
56
- [parentFieldName]: {
57
- equals: currentFolderID
58
- }
59
- }
60
- ]
61
- }
62
- });
63
- if (childFoldersResult.totalDocs > 0) {
64
- dependentCollection = foldersSlug;
65
- }
66
- }
67
- }
68
- if (dependentCollection) {
69
- const translatedLabels = newCollections.map((collectionSlug)=>{
70
- if (req.payload.collections[collectionSlug]?.config.labels.singular) {
71
- return getTranslatedLabel(req.payload.collections[collectionSlug]?.config.labels.plural, req.i18n);
72
- }
73
- return collectionSlug;
74
- });
75
- const isFolder = dependentCollection === foldersSlug;
76
- throw new APIError(`The folder "${data.name || originalDoc.name}" contains ${isFolder ? 'folders' : 'documents'} that still belong to the following collections: ${translatedLabels.join(', ')}`, 400);
77
- }
78
- return data;
79
- }
80
- } else if ((data?.[typeFieldName] === null || Array.isArray(data?.[typeFieldName]) && data?.[typeFieldName].length === 0) && parentFolderID) {
81
- // attempting to set the type to catch-all, so we need to ensure that the parent allows this
82
- let parentFolder;
83
- if (typeof parentFolderID === 'string' || typeof parentFolderID === 'number') {
84
- try {
85
- parentFolder = await req.payload.findByID({
86
- id: parentFolderID,
87
- collection: foldersSlug,
88
- overrideAccess: true,
89
- req,
90
- select: {
91
- name: true,
92
- [typeFieldName]: true
93
- },
94
- user: req.user
95
- });
96
- } catch (_) {
97
- // parent folder does not exist
98
- }
99
- }
100
- const parentTypeValue = parentFolder?.[typeFieldName];
101
- if (parentFolder && parentTypeValue && Array.isArray(parentTypeValue) && parentTypeValue.length > 0) {
102
- throw new APIError(`The folder "${data?.name || originalDoc.name}" must have folder-type set since its parent folder ${parentFolder?.name ? `"${parentFolder?.name}" ` : ''}has a folder-type set.`, 400);
103
- }
104
- }
105
- return data;
106
- };
107
-
108
- //# sourceMappingURL=ensureSafeCollectionsChange.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/hierarchy/hooks/ensureSafeCollectionsChange.ts"],"sourcesContent":["import { APIError, type CollectionBeforeValidateHook, type CollectionSlug } from '../../index.js'\nimport { extractID } from '../../utilities/extractID.js'\nimport { getTranslatedLabel } from '../../utilities/getTranslatedLabel.js'\n\nexport const ensureSafeCollectionsChange =\n ({\n folderFieldName,\n foldersSlug,\n parentFieldName = 'folder',\n typeFieldName = 'hierarchyType',\n }: {\n folderFieldName: string\n foldersSlug: CollectionSlug\n parentFieldName?: string\n typeFieldName?: string\n }): CollectionBeforeValidateHook =>\n async ({ data, originalDoc, req }) => {\n const currentFolderID = extractID(originalDoc || {})\n const parentFolderID = extractID(\n data?.[parentFieldName] || originalDoc?.[parentFieldName] || {},\n )\n if (Array.isArray(data?.[typeFieldName]) && data[typeFieldName].length > 0) {\n const typeFieldValue = data[typeFieldName] as string[]\n const currentlyAssignedCollections: string[] | undefined =\n Array.isArray(originalDoc?.[typeFieldName]) && originalDoc[typeFieldName].length > 0\n ? originalDoc[typeFieldName]\n : undefined\n /**\n * Check if the assigned collections have changed.\n * example:\n * - originalAssignedCollections: ['posts', 'pages']\n * - folderType: ['posts']\n *\n * The user is narrowing the types of documents that can be associated with this folder.\n * If the user is only expanding the types of documents that can be associated with this folder,\n * we do not need to do anything.\n */\n const newCollections = currentlyAssignedCollections\n ? // user is narrowing the current scope of the folder\n currentlyAssignedCollections.filter((c) => !typeFieldValue.includes(c))\n : // user is adding a scope to the folder\n typeFieldValue\n\n if (newCollections && newCollections.length > 0) {\n let dependentCollection: null | string = null\n\n if (typeof currentFolderID === 'string' || typeof currentFolderID === 'number') {\n // Check each collection being removed for dependent documents\n for (const collectionSlug of newCollections) {\n const result = await req.payload.find({\n collection: collectionSlug,\n limit: 1,\n overrideAccess: true,\n req,\n where: {\n [folderFieldName]: { equals: currentFolderID },\n },\n })\n\n if (result.totalDocs > 0) {\n dependentCollection = collectionSlug\n break\n }\n }\n\n // Also check for child folders with these types\n if (!dependentCollection) {\n const childFoldersResult = await req.payload.find({\n collection: foldersSlug,\n limit: 1,\n overrideAccess: true,\n req,\n where: {\n and: [\n { [typeFieldName]: { in: newCollections } },\n { [parentFieldName]: { equals: currentFolderID } },\n ],\n },\n })\n\n if (childFoldersResult.totalDocs > 0) {\n dependentCollection = foldersSlug\n }\n }\n }\n\n if (dependentCollection) {\n const translatedLabels = newCollections.map((collectionSlug) => {\n if (req.payload.collections[collectionSlug]?.config.labels.singular) {\n return getTranslatedLabel(\n req.payload.collections[collectionSlug]?.config.labels.plural,\n req.i18n,\n )\n }\n return collectionSlug\n })\n\n const isFolder = dependentCollection === foldersSlug\n throw new APIError(\n `The folder \"${data.name || originalDoc.name}\" contains ${isFolder ? 'folders' : 'documents'} that still belong to the following collections: ${translatedLabels.join(', ')}`,\n 400,\n )\n }\n\n return data\n }\n } else if (\n (data?.[typeFieldName] === null ||\n (Array.isArray(data?.[typeFieldName]) && data?.[typeFieldName].length === 0)) &&\n parentFolderID\n ) {\n // attempting to set the type to catch-all, so we need to ensure that the parent allows this\n let parentFolder\n if (typeof parentFolderID === 'string' || typeof parentFolderID === 'number') {\n try {\n parentFolder = await req.payload.findByID({\n id: parentFolderID,\n collection: foldersSlug,\n overrideAccess: true,\n req,\n select: {\n name: true,\n [typeFieldName]: true,\n },\n user: req.user,\n })\n } catch (_) {\n // parent folder does not exist\n }\n }\n\n const parentTypeValue = parentFolder?.[typeFieldName]\n if (\n parentFolder &&\n parentTypeValue &&\n Array.isArray(parentTypeValue) &&\n parentTypeValue.length > 0\n ) {\n throw new APIError(\n `The folder \"${data?.name || originalDoc.name}\" must have folder-type set since its parent folder ${parentFolder?.name ? `\"${parentFolder?.name}\" ` : ''}has a folder-type set.`,\n 400,\n )\n }\n }\n\n return data\n }\n"],"names":["APIError","extractID","getTranslatedLabel","ensureSafeCollectionsChange","folderFieldName","foldersSlug","parentFieldName","typeFieldName","data","originalDoc","req","currentFolderID","parentFolderID","Array","isArray","length","typeFieldValue","currentlyAssignedCollections","undefined","newCollections","filter","c","includes","dependentCollection","collectionSlug","result","payload","find","collection","limit","overrideAccess","where","equals","totalDocs","childFoldersResult","and","in","translatedLabels","map","collections","config","labels","singular","plural","i18n","isFolder","name","join","parentFolder","findByID","id","select","user","_","parentTypeValue"],"mappings":"AAAA,SAASA,QAAQ,QAAgE,iBAAgB;AACjG,SAASC,SAAS,QAAQ,+BAA8B;AACxD,SAASC,kBAAkB,QAAQ,wCAAuC;AAE1E,OAAO,MAAMC,8BACX,CAAC,EACCC,eAAe,EACfC,WAAW,EACXC,kBAAkB,QAAQ,EAC1BC,gBAAgB,eAAe,EAMhC,GACD,OAAO,EAAEC,IAAI,EAAEC,WAAW,EAAEC,GAAG,EAAE;QAC/B,MAAMC,kBAAkBV,UAAUQ,eAAe,CAAC;QAClD,MAAMG,iBAAiBX,UACrBO,MAAM,CAACF,gBAAgB,IAAIG,aAAa,CAACH,gBAAgB,IAAI,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