Browse Source

Implemented findNotEmptyParentByPathRecursively

Taichi Masuyama 3 years ago
parent
commit
677197fcac
1 changed files with 26 additions and 11 deletions
  1. 26 11
      packages/app/src/server/models/page.ts

+ 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