takeru0001 5 лет назад
Родитель
Сommit
6e5113768a
1 измененных файлов с 15 добавлено и 0 удалено
  1. 15 0
      src/server/models/page.js

+ 15 - 0
src/server/models/page.js

@@ -1304,6 +1304,7 @@ module.exports = function(crowi) {
 
   pageSchema.statics.duplicate = async function(page, newPagePath, user) {
     const Page = this;
+    const PageTagRelation = crowi.model('PageTagRelation');
     // populate
     await page.populate({ path: 'revision', model: 'Revision', select: 'body' }).execPopulate();
 
@@ -1327,6 +1328,20 @@ module.exports = function(crowi) {
 
     return { page: crowi.pageService.serializeToObj(createdPage), tags: savedTags };
   };
+
+  pageSchema.statics.duplicateRecursively = async function(page, newPagePath, user) {
+    const Page = this;
+    const newPagePathPrefix = newPagePath;
+    const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
+
+    const pages = await Page.findManageableListWithDescendants(page, user);
+
+    const promise = pages.map(async(page) => {
+      const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
+      return this.duplicate(page, newPagePath, user);
+    });
+
+    return Promise.allSettled(promise);
   };
 
   pageSchema.statics.findListByPathsArray = async function(paths) {