Browse Source

Fix create pages recusrively

https://youtrack.weseek.co.jp/issue/GW-7936
- Put removeLeafEmptyPagesRecursively method back
- Add optional user parameter to createEmptyPagesByPaths
- Apply revisions to page on create empty pages if user is exists in params
- Add initial descendantCount to created pages
- Set isEmpty to false to created pages
- Move pushRevision method
Mudana-Grune 3 years ago
parent
commit
7789a63856
2 changed files with 46 additions and 17 deletions
  1. 39 15
      packages/app/src/server/models/page.ts
  2. 7 2
      packages/app/src/server/service/page.ts

+ 39 - 15
packages/app/src/server/models/page.ts

@@ -671,18 +671,55 @@ schema.statics.findTargetAndAncestorsByPathOrId = async function(pathOrId: strin
   return { targetAndAncestors, rootPage };
 };
 
+/*
+ * Utils from obsolete-page.js
+ */
+export async function pushRevision(pageData, newRevision, user) {
+  await newRevision.save();
+
+  pageData.revision = newRevision;
+  pageData.lastUpdateUser = user?._id ?? user;
+  pageData.updatedAt = Date.now();
+
+  return pageData.save();
+}
+
 /**
  * Create empty pages at paths at which no pages exist
  * @param paths Page paths
  * @param aggrPipelineForExistingPages AggregationPipeline object to find existing pages at paths
  */
-schema.statics.createEmptyPagesByPaths = async function(paths: string[], aggrPipelineForExistingPages: any[]): Promise<void> {
+schema.statics.createEmptyPagesByPaths = async function(paths: string[], aggrPipelineForExistingPages: any[], user?: IUserHasId): Promise<void> {
   const existingPages = await this.aggregate(aggrPipelineForExistingPages);
 
   const existingPagePaths = existingPages.map(page => page.path);
   const notExistingPagePaths = paths.filter(path => !existingPagePaths.includes(path));
+  if (user != null) {
+    const Revision = mongoose.model('Revision') as any;
+    // Create and save pages
+    const createPagesAndRevisions = notExistingPagePaths.map(async(path) => {
+      const page = await this.create({ path, isEmpty: false, descendantCount: 0 });
+
+      // Get number of descendants
+      const descendantCount = await this.countDocuments({ path: { $regex: `^${path}/` } });
+
+      // Update descendantCount
+      page.descendantCount = descendantCount;
+      await page.save();
+
+      // Create revision and push it to the page
+      const newRevision = Revision.prepareRevision(page, '', null, user, { format: 'markdown' });
+      await pushRevision(page, newRevision, user);
+      await page.populateDataToShowRevision();
+
+      return page;
+    });
 
-  await this.insertMany(notExistingPagePaths.map(path => ({ path, isEmpty: true })));
+    await Promise.all(createPagesAndRevisions);
+  }
+  else {
+    await this.insertMany(notExistingPagePaths.map(path => ({ path, isEmpty: true })));
+  }
 };
 
 /**
@@ -706,19 +743,6 @@ schema.statics.findParentByPath = async function(path: string): Promise<PageDocu
   return null;
 };
 
-/*
- * Utils from obsolete-page.js
- */
-export async function pushRevision(pageData, newRevision, user) {
-  await newRevision.save();
-
-  pageData.revision = newRevision;
-  pageData.lastUpdateUser = user?._id ?? user;
-  pageData.updatedAt = Date.now();
-
-  return pageData.save();
-}
-
 /**
  * add/subtract descendantCount of pages with provided paths by increment.
  * increment can be negative number

+ 7 - 2
packages/app/src/server/service/page.ts

@@ -561,6 +561,12 @@ class PageService {
     const nToIncrease = (renamedPage.isEmpty ? 0 : 1) + page.descendantCount;
     await this.updateDescendantCountOfAncestors(renamedPage._id, nToIncrease, false);
 
+    // Remove leaf empty pages if not moving to under the ex-target position
+    if (!this.isRenamingToUnderTarget(page.path, newPagePath)) {
+      // remove empty pages at leaf position
+      await Page.removeLeafEmptyPagesRecursively(page.parent);
+    }
+
     await PageOperation.findByIdAndDelete(pageOpId);
   }
 
@@ -3341,8 +3347,7 @@ class PageService {
 
     // Fill ancestors
     const aggregationPipeline: any[] = await this.buildPipelineToCreateEmptyPagesByUser(user, ancestorPaths);
-
-    await Page.createEmptyPagesByPaths(ancestorPaths, aggregationPipeline);
+    await Page.createEmptyPagesByPaths(ancestorPaths, aggregationPipeline, user);
 
     // Connect ancestors
     await this.connectPageTree(path);