@payloadcms/plugin-nested-docs
Advanced tools
Comparing version 3.20.0-canary.9f2bca1 to 3.20.0-canary.a7d4b94
@@ -5,43 +5,75 @@ import { APIError } from 'payload'; | ||
const parentSlug = pluginConfig?.parentFieldSlug || 'parent'; | ||
const parentDocIsPublished = doc._status === 'published'; | ||
const children = await req.payload.find({ | ||
collection: collection.slug, | ||
depth: 0, | ||
draft: true, | ||
limit: 0, | ||
locale: req.locale, | ||
req, | ||
where: { | ||
[parentSlug]: { | ||
equals: doc.id | ||
} | ||
} | ||
}); | ||
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'; | ||
try { | ||
for (const child of children.docs){ | ||
const childIsPublished = typeof collection.versions === 'object' && collection.versions.drafts && child._status === 'published'; | ||
if (!parentDocIsPublished && childIsPublished) { | ||
continue; | ||
if (draft) { | ||
// If the parent is a draft, don't resave children | ||
return; | ||
} else { | ||
const initialDraftChildren = await req.payload.find({ | ||
collection: collection.slug, | ||
depth: 0, | ||
draft: true, | ||
limit: 0, | ||
locale: req.locale, | ||
req, | ||
where: { | ||
[parentSlug]: { | ||
equals: doc.id | ||
} | ||
} | ||
await req.payload.update({ | ||
id: child.id, | ||
collection: collection.slug, | ||
data: { | ||
...child, | ||
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child) | ||
}, | ||
depth: 0, | ||
draft: !childIsPublished, | ||
locale: req.locale, | ||
req | ||
}); | ||
const draftChildren = initialDraftChildren.docs.filter((child)=>child._status === 'draft'); | ||
const publishedChildren = await req.payload.find({ | ||
collection: collection.slug, | ||
depth: 0, | ||
draft: false, | ||
limit: 0, | ||
locale: req.locale, | ||
req, | ||
where: { | ||
[parentSlug]: { | ||
equals: doc.id | ||
} | ||
} | ||
}); | ||
const childrenById = [ | ||
...draftChildren, | ||
...publishedChildren.docs | ||
].reduce((acc, child)=>{ | ||
acc[child.id] = acc[child.id] || []; | ||
acc[child.id].push(child); | ||
return acc; | ||
}, {}); | ||
const sortedChildren = Object.values(childrenById).flatMap((group)=>{ | ||
return group.sort((a, b)=>{ | ||
if (a.updatedAt !== b.updatedAt) { | ||
return a.updatedAt > b.updatedAt ? 1 : -1; | ||
} | ||
return a._status === 'published' ? 1 : -1; | ||
}); | ||
}); | ||
if (sortedChildren) { | ||
try { | ||
for (const child of sortedChildren){ | ||
const isDraft = child._status !== 'published'; | ||
await req.payload.update({ | ||
id: child.id, | ||
collection: collection.slug, | ||
data: { | ||
...child, | ||
[breadcrumbSlug]: await populateBreadcrumbs(req, pluginConfig, collection, child) | ||
}, | ||
depth: 0, | ||
draft: isDraft, | ||
locale: req.locale, | ||
req | ||
}); | ||
} | ||
} catch (err) { | ||
req.payload.logger.error(`Nested Docs plugin encountered an error while re-saving a child document.`); | ||
req.payload.logger.error(err); | ||
if (err?.name === 'ValidationError' && err?.data?.errors?.length) { | ||
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400); | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
req.payload.logger.error(`Nested Docs plugin has had an error while re-saving a child document${draft ? ' as draft' : ' as published'}.`); | ||
req.payload.logger.error(err); | ||
// Use type assertion until we can use instanceof reliably with our Error types | ||
if (err?.name === 'ValidationError' && err?.data?.errors?.length) { | ||
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400); | ||
} | ||
} | ||
@@ -53,15 +85,6 @@ }; | ||
doc, | ||
draft: true, | ||
draft: doc._status === 'published' ? false : true, | ||
pluginConfig, | ||
req | ||
}); | ||
if (doc._status === 'published') { | ||
await resave({ | ||
collection, | ||
doc, | ||
draft: false, | ||
pluginConfig, | ||
req | ||
}); | ||
} | ||
return undefined; | ||
@@ -68,0 +91,0 @@ }; |
{ | ||
"name": "@payloadcms/plugin-nested-docs", | ||
"version": "3.20.0-canary.9f2bca1", | ||
"version": "3.20.0-canary.a7d4b94", | ||
"description": "The official Nested Docs plugin for Payload", | ||
@@ -40,6 +40,6 @@ "homepage": "https://payloadcms.com", | ||
"@payloadcms/eslint-config": "3.9.0", | ||
"payload": "3.20.0-canary.9f2bca1" | ||
"payload": "3.20.0-canary.a7d4b94" | ||
}, | ||
"peerDependencies": { | ||
"payload": "3.20.0-canary.9f2bca1" | ||
"payload": "3.20.0-canary.a7d4b94" | ||
}, | ||
@@ -46,0 +46,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48344
381