@payloadcms/plugin-nested-docs 3.43.0-internal.c5bbc84 → 3.44.0-canary.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIError } from 'payload';
|
|
1
|
+
import { APIError, ValidationErrorName } from 'payload';
|
|
2
2
|
import { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js';
|
|
3
3
|
const resave = async ({ collection, doc, draft, pluginConfig, req })=>{
|
|
4
4
|
const parentSlug = pluginConfig?.parentFieldSlug || 'parent';
|
|
@@ -66,7 +66,7 @@ const resave = async ({ collection, doc, draft, pluginConfig, req })=>{
|
|
|
66
66
|
} catch (err) {
|
|
67
67
|
req.payload.logger.error(`Nested Docs plugin encountered an error while re-saving a child document.`);
|
|
68
68
|
req.payload.logger.error(err);
|
|
69
|
-
if (err?.name ===
|
|
69
|
+
if (err?.name === ValidationErrorName && err?.data?.errors?.length) {
|
|
70
70
|
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/resaveChildren.ts"],"sourcesContent":["import type {\n CollectionAfterChangeHook,\n CollectionConfig,\n JsonObject,\n PayloadRequest,\n ValidationError,\n} from 'payload'\n\nimport { APIError } from 'payload'\n\nimport type { NestedDocsPluginConfig } from '../types.js'\n\nimport { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js'\n\ntype ResaveArgs = {\n collection: CollectionConfig\n doc: JsonObject\n draft: boolean\n pluginConfig: NestedDocsPluginConfig\n req: PayloadRequest\n}\n\nconst resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs) => {\n const parentSlug = pluginConfig?.parentFieldSlug || 'parent'\n\n if (draft) {\n // If the parent is a draft, don't resave children\n return\n } else {\n const initialDraftChildren = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: true,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const draftChildren = initialDraftChildren.docs.filter((child) => child._status === 'draft')\n\n const publishedChildren = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: false,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const childrenById = [...draftChildren, ...publishedChildren.docs].reduce<\n Record<string, JsonObject[]>\n >((acc, child) => {\n acc[child.id] = acc[child.id] || []\n acc[child.id]!.push(child)\n return acc\n }, {})\n\n const sortedChildren = Object.values(childrenById).flatMap((group: JsonObject[]) => {\n return group.sort((a, b) => {\n if (a.updatedAt !== b.updatedAt) {\n return a.updatedAt > b.updatedAt ? 1 : -1\n }\n return a._status === 'published' ? 1 : -1\n })\n })\n\n if (sortedChildren) {\n try {\n for (const child of sortedChildren) {\n const isDraft = child._status !== 'published'\n\n await req.payload.update({\n id: child.id,\n collection: collection.slug,\n data: populateBreadcrumbs(req, pluginConfig, collection, child),\n depth: 0,\n draft: isDraft,\n locale: req.locale,\n req,\n })\n }\n } catch (err: unknown) {\n req.payload.logger.error(\n `Nested Docs plugin encountered an error while re-saving a child document.`,\n )\n req.payload.logger.error(err)\n\n if (\n (err as ValidationError)?.name ===
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/resaveChildren.ts"],"sourcesContent":["import type {\n CollectionAfterChangeHook,\n CollectionConfig,\n JsonObject,\n PayloadRequest,\n ValidationError,\n} from 'payload'\n\nimport { APIError, ValidationErrorName } from 'payload'\n\nimport type { NestedDocsPluginConfig } from '../types.js'\n\nimport { populateBreadcrumbs } from '../utilities/populateBreadcrumbs.js'\n\ntype ResaveArgs = {\n collection: CollectionConfig\n doc: JsonObject\n draft: boolean\n pluginConfig: NestedDocsPluginConfig\n req: PayloadRequest\n}\n\nconst resave = async ({ collection, doc, draft, pluginConfig, req }: ResaveArgs) => {\n const parentSlug = pluginConfig?.parentFieldSlug || 'parent'\n\n if (draft) {\n // If the parent is a draft, don't resave children\n return\n } else {\n const initialDraftChildren = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: true,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const draftChildren = initialDraftChildren.docs.filter((child) => child._status === 'draft')\n\n const publishedChildren = await req.payload.find({\n collection: collection.slug,\n depth: 0,\n draft: false,\n limit: 0,\n locale: req.locale,\n req,\n where: {\n [parentSlug]: {\n equals: doc.id,\n },\n },\n })\n\n const childrenById = [...draftChildren, ...publishedChildren.docs].reduce<\n Record<string, JsonObject[]>\n >((acc, child) => {\n acc[child.id] = acc[child.id] || []\n acc[child.id]!.push(child)\n return acc\n }, {})\n\n const sortedChildren = Object.values(childrenById).flatMap((group: JsonObject[]) => {\n return group.sort((a, b) => {\n if (a.updatedAt !== b.updatedAt) {\n return a.updatedAt > b.updatedAt ? 1 : -1\n }\n return a._status === 'published' ? 1 : -1\n })\n })\n\n if (sortedChildren) {\n try {\n for (const child of sortedChildren) {\n const isDraft = child._status !== 'published'\n\n await req.payload.update({\n id: child.id,\n collection: collection.slug,\n data: populateBreadcrumbs(req, pluginConfig, collection, child),\n depth: 0,\n draft: isDraft,\n locale: req.locale,\n req,\n })\n }\n } catch (err: unknown) {\n req.payload.logger.error(\n `Nested Docs plugin encountered an error while re-saving a child document.`,\n )\n req.payload.logger.error(err)\n\n if (\n (err as ValidationError)?.name === ValidationErrorName &&\n (err as ValidationError)?.data?.errors?.length\n ) {\n throw new APIError(\n 'Could not publish or save changes: One or more children are invalid.',\n 400,\n )\n }\n }\n }\n }\n}\n\nexport const resaveChildren =\n (pluginConfig: NestedDocsPluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>\n async ({ doc, req }) => {\n await resave({\n collection,\n doc,\n draft: doc._status === 'published' ? false : true,\n pluginConfig,\n req,\n })\n\n return undefined\n }\n"],"names":["APIError","ValidationErrorName","populateBreadcrumbs","resave","collection","doc","draft","pluginConfig","req","parentSlug","parentFieldSlug","initialDraftChildren","payload","find","slug","depth","limit","locale","where","equals","id","draftChildren","docs","filter","child","_status","publishedChildren","childrenById","reduce","acc","push","sortedChildren","Object","values","flatMap","group","sort","a","b","updatedAt","isDraft","update","data","err","logger","error","name","errors","length","resaveChildren","undefined"],"mappings":"AAQA,SAASA,QAAQ,EAAEC,mBAAmB,QAAQ,UAAS;AAIvD,SAASC,mBAAmB,QAAQ,sCAAqC;AAUzE,MAAMC,SAAS,OAAO,EAAEC,UAAU,EAAEC,GAAG,EAAEC,KAAK,EAAEC,YAAY,EAAEC,GAAG,EAAc;IAC7E,MAAMC,aAAaF,cAAcG,mBAAmB;IAEpD,IAAIJ,OAAO;QACT,kDAAkD;QAClD;IACF,OAAO;QACL,MAAMK,uBAAuB,MAAMH,IAAII,OAAO,CAACC,IAAI,CAAC;YAClDT,YAAYA,WAAWU,IAAI;YAC3BC,OAAO;YACPT,OAAO;YACPU,OAAO;YACPC,QAAQT,IAAIS,MAAM;YAClBT;YACAU,OAAO;gBACL,CAACT,WAAW,EAAE;oBACZU,QAAQd,IAAIe,EAAE;gBAChB;YACF;QACF;QAEA,MAAMC,gBAAgBV,qBAAqBW,IAAI,CAACC,MAAM,CAAC,CAACC,QAAUA,MAAMC,OAAO,KAAK;QAEpF,MAAMC,oBAAoB,MAAMlB,IAAII,OAAO,CAACC,IAAI,CAAC;YAC/CT,YAAYA,WAAWU,IAAI;YAC3BC,OAAO;YACPT,OAAO;YACPU,OAAO;YACPC,QAAQT,IAAIS,MAAM;YAClBT;YACAU,OAAO;gBACL,CAACT,WAAW,EAAE;oBACZU,QAAQd,IAAIe,EAAE;gBAChB;YACF;QACF;QAEA,MAAMO,eAAe;eAAIN;eAAkBK,kBAAkBJ,IAAI;SAAC,CAACM,MAAM,CAEvE,CAACC,KAAKL;YACNK,GAAG,CAACL,MAAMJ,EAAE,CAAC,GAAGS,GAAG,CAACL,MAAMJ,EAAE,CAAC,IAAI,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C;IACT,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-nested-docs",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.44.0-canary.0",
|
|
4
4
|
"description": "The official Nested Docs plugin for Payload",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@payloadcms/eslint-config": "3.28.0",
|
|
41
|
-
"payload": "3.
|
|
41
|
+
"payload": "3.44.0-canary.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"payload": "3.
|
|
44
|
+
"payload": "3.44.0-canary.0"
|
|
45
45
|
},
|
|
46
46
|
"publishConfig": {
|
|
47
47
|
"registry": "https://registry.npmjs.org/"
|