Просмотр исходного кода

Merge pull request #5890 from weseek/feat/implement-methods-for-normalize-by-path

feat: Implement methods for normalize by path
Yuki Takei 3 лет назад
Родитель
Сommit
9b53d8466e

+ 1 - 1
packages/app/src/components/PrivateLegacyPages.tsx

@@ -447,7 +447,7 @@ const PrivateLegacyPages = (props: Props): JSX.Element => {
         close={() => setOpenConvertModal(false)}
         onSubmit={async(convertPath: string) => {
           try {
-            await apiv3Post<void>('/pages/legacy-pages-migration', {
+            await apiv3Post<void>('/pages/convert-pages-by-path', {
               convertPath,
             });
             toastSuccess(t('private_legacy_pages.by_path_modal.success'));

+ 26 - 11
packages/app/src/server/models/page.ts

@@ -915,20 +915,35 @@ schema.statics.removeEmptyPages = async function(pageIdsToNotRemove: ObjectIdLik
   });
 };
 
-// TODO 93939: implement this method
-// schema.statics.findNotEmptyParentByPathRecursively = async function(path: string): Promise<PageDocument | null> {
-// Find a page on the tree by path
-// Find not empty parent
-//   const parent = await this.findById(target.parent);
+/**
+ * Find a not empty parent recursively.
+ * @param {string} path
+ * @returns {Promise<PageDocument | null>}
+ */
+schema.statics.findNotEmptyParentByPathRecursively = async function(path: string): Promise<PageDocument | null> {
+  const parent = await this.findParentByPath(path);
+  if (parent == null) {
+    return null;
+  }
+
+  const recursive = async(page: PageDocument): Promise<PageDocument> => {
+    if (!page.isEmpty) {
+      return page;
+    }
 
-//   const shouldContinue = parent != null && parent.isEmpty;
+    const next = await this.findById(page.parent);
 
-//   if (shouldContinue) {
-//     return this.findNotEmptyParentByPathRecursively(parent);
-//   }
+    if (next == null || isTopPage(next.path)) {
+      return page;
+    }
 
-//   return target;
-// };
+    return recursive(next);
+  };
+
+  const notEmptyParent = await recursive(parent);
+
+  return notEmptyParent;
+};
 
 schema.statics.PageQueryBuilder = PageQueryBuilder as any; // mongoose does not support constructor type as statics attrs type
 

+ 12 - 17
packages/app/src/server/service/page.ts

@@ -1740,11 +1740,6 @@ class PageService {
     return;
   }
 
-  // TODO 93939: implement this method
-  async deleteCompletelySystematically() {
-    return;
-  }
-
   async deleteCompletelyRecursivelyMainOperation(page, user, options, pageOpId: ObjectIdLike): Promise<void> {
     await this.deleteCompletelyDescendantsWithStream(page, user, options, false);
 
@@ -2259,6 +2254,16 @@ class PageService {
 
   async normalizeParentByPath(path: string, user): Promise<void> {
     const Page = mongoose.model('Page') as unknown as PageModel;
+    const { PageQueryBuilder } = Page;
+
+    // This validation is not 100% correct since it ignores user to count
+    const builder = new PageQueryBuilder(Page.find());
+    builder.addConditionAsNotMigrated();
+    builder.addConditionToListWithDescendants(path);
+    const nEstimatedNormalizationTarget: number = await builder.query.exec('count');
+    if (nEstimatedNormalizationTarget === 0) {
+      throw Error('No page is available for conversion');
+    }
 
     const pages = await Page.findByPathAndViewer(path, user, null, false);
     if (pages == null || !Array.isArray(pages)) {
@@ -2300,7 +2305,7 @@ class PageService {
       page = systematicallyCreatedPage;
     }
     else {
-      page = page[0];
+      page = pages[0];
     }
 
     const grant = page.grant;
@@ -2317,7 +2322,6 @@ class PageService {
       isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
     }
     catch (err) {
-      // TODO 93939: delete systematicallyCreatedPage
       logger.error(`Failed to validate grant of page at "${path}"`, err);
       throw err;
     }
@@ -2340,20 +2344,11 @@ class PageService {
       });
     }
     catch (err) {
-      // TODO 93939: delete systematicallyCreatedPage
       logger.error('Failed to create PageOperation document.', err);
       throw err;
     }
 
-    // no await
-    (async() => {
-      // Remove the created page if no page has converted
-      const count = await this.normalizeParentRecursivelyMainOperation(page, user, pageOp._id);
-      if (count === 0) {
-        // TODO 93939: delete systematicallyCreatedPage
-        // await this.deleteCompletelySystematically(systematicallyCreatedPage, user, {}, false);
-      }
-    })();
+    this.normalizeParentRecursivelyMainOperation(page, user, pageOp._id);
   }
 
   async normalizeParentByPageIdsRecursively(pageIds: ObjectIdLike[], user): Promise<void> {