Taichi Masuyama 4 лет назад
Родитель
Сommit
9dc51335e7

+ 1 - 1
packages/app/src/server/models/page.ts

@@ -497,7 +497,7 @@ schema.statics.findAncestorsUsingParentRecursively = async function(pageId: Obje
       return ancestors;
     }
 
-    return findAncestorsRecursively(parent);
+    return findAncestorsRecursively(parent, [...ancestors, parent]);
   }
 
   return findAncestorsRecursively(target);

+ 6 - 1
packages/app/src/server/routes/page.js

@@ -1213,7 +1213,12 @@ module.exports = function(crowi, app) {
         await crowi.pageService.deleteCompletely(page, req.user, options, isRecursively);
       }
       else {
-        if (!page.isEmpty && !page.isUpdatable(previousRevision)) {
+        const notRecursivelyAndEmpty = page.isEmpty && !isRecursively;
+        if (notRecursivelyAndEmpty) {
+          return res.json(ApiResponse.error(`Page '${pageId}' is not found.`, 'notfound'));
+        }
+
+        if (!page.isUpdatable(previousRevision)) {
           return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
         }
 

+ 6 - 9
packages/app/src/server/service/page.ts

@@ -1001,12 +1001,6 @@ class PageService {
       throw new Error('Page is not deletable.');
     }
 
-    // replace with an empty page
-    const shouldReplace = !isRecursively && await Page.exists({ parent: page._id });
-    if (shouldReplace) {
-      await Page.replaceTargetWithPage(page);
-    }
-
     if (isRecursively) {
       // no await for deleteDescendantsWithStream and updateDescendantCountOfAncestors
       (async() => {
@@ -1024,9 +1018,12 @@ class PageService {
       // replace with an empty page
       const shouldReplace = await Page.exists({ parent: page._id });
       if (shouldReplace) {
-        await Page.replaceTargetWithEmptyPage(page);
+        await Page.replaceTargetWithPage(page);
       }
 
+      // update descendantCount of ancestors'
+      await this.updateDescendantCountOfAncestors(page.parent, -1, true);
+
       const shouldDeleteLeafEmptyPages = !shouldReplace;
       if (shouldDeleteLeafEmptyPages) {
         // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
@@ -1449,7 +1446,7 @@ class PageService {
 
         // update descendantCount of ancestors'
         if (page.parent != null) {
-          await this.updateDescendantCountOfAncestors(page.parent, (revertedDescendantCount + 1) * -1, true);
+          await this.updateDescendantCountOfAncestors(page.parent, revertedDescendantCount + 1, true);
 
           // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
         }
@@ -2108,7 +2105,7 @@ class PageService {
     await streamToPromise(recountWriteStream);
   }
 
-  // update descendantCount of all pages that are ancestors of a provided path by count
+  // update descendantCount of all pages that are ancestors of a provided pageId by count
   async updateDescendantCountOfAncestors(pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean): Promise<void> {
     const Page = this.crowi.model('Page');
     const ancestors = await Page.findAncestorsUsingParentRecursively(pageId, shouldIncludeTarget);