Parcourir la source

modify method

yohei0125 il y a 4 ans
Parent
commit
03229599e4

+ 3 - 4
packages/app/src/server/models/page.ts

@@ -99,7 +99,7 @@ schema.plugin(uniqueValidator);
 /*
  * Methods
  */
-const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
+export const collectAncestorPaths = (path: string, ancestorPaths: string[] = []): string[] => {
   if (isTopPage(path)) return ancestorPaths;
 
   const parentPath = nodePath.dirname(path);
@@ -375,9 +375,8 @@ schema.statics.getAggrConditionForPageWithProvidedPathAndDescendants = function(
   ];
 };
 
-// update descendantCount of ancestors of the provided path by count
-schema.statics.recountDescendantCountOfAncestors = async function(path:string, count: number):Promise<void> {
-  const paths = collectAncestorPaths(path);
+// update descendantCount of pages with provided paths by count
+schema.statics.recountDescendantCountOfPathsByCount = async function(paths:string[], count: number):Promise<void> {
   const pages = await this.aggregate([{ $match: { path: { $in: paths } } }]);
   const operations = pages.map((page) => {
     return {

+ 4 - 3
packages/app/src/server/service/page.js

@@ -1,7 +1,7 @@
 import { pagePathUtils } from '@growi/core';
 
 import loggerFactory from '~/utils/logger';
-import { generateGrantCondition } from '~/server/models/page';
+import { generateGrantCondition, collectAncestorPaths } from '~/server/models/page';
 
 import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
 
@@ -1290,10 +1290,11 @@ class PageService {
     await streamToPromise(recountWriteStream);
   }
 
-  // update descendantCount of all pages that are ancestors of path by passed count
+  // update descendantCount of all pages that are ancestors of a provided path by count
   async updateDescendantCountOfAncestors(path = '/', count = 0) {
     const Page = this.crowi.model('Page');
-    await Page.recountDescendantCountOfAncestors(path, count);
+    const ancestors = collectAncestorPaths(path);
+    await Page.recountDescendantCountOfPathsByCount(ancestors, count);
   }
 
 }