Explorar o código

Merge branch 'fix/normalize-parent-add-test-and-fix' into fix/save-ex-descendant-count

Taichi Masuyama %!s(int64=4) %!d(string=hai) anos
pai
achega
fb5b0cdbaf

+ 0 - 1
packages/app/src/server/interfaces/page-operation.ts

@@ -22,5 +22,4 @@ export type IUserForResuming = {
 export type IOptionsForResuming = {
   updateMetadata?: boolean,
   createRedirectPage?: boolean,
-  exDescendantCount?: number,
 };

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

@@ -72,7 +72,6 @@ const userSchemaForResuming = new Schema<IUserForResuming>({
 const optionsSchemaForResuming = new Schema<IOptionsForResuming>({
   createRedirectPage: { type: Boolean },
   updateMetadata: { type: Boolean },
-  exDescendantCount: { type: Number },
 }, { _id: false });
 
 const schema = new Schema<PageOperationDocument, PageOperationModel>({

+ 5 - 8
packages/app/src/server/models/page.ts

@@ -46,7 +46,7 @@ export type CreateMethod = (path: string, body: string, user, options) => Promis
 export interface PageModel extends Model<PageDocument> {
   [x: string]: any; // for obsolete methods
   createEmptyPagesByPaths(paths: string[], user: any | null, onlyMigratedAsExistingPages?: boolean, andFilter?): Promise<void>
-  getParentAndFillAncestors(path: string, user, pathsToExcludeNotNormalizedPages?: string[]): Promise<PageDocument & { _id: any }>
+  getParentAndFillAncestors(path: string, user): Promise<PageDocument & { _id: any }>
   findByIdsAndViewer(pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean): Promise<PageDocument[]>
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: boolean, includeEmpty?: boolean): Promise<PageDocument[]>
   findTargetAndAncestorsByPathOrId(pathOrId: string): Promise<TargetAndAncestorsResult>
@@ -550,7 +550,7 @@ schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?
  * @param path string
  * @returns Promise<PageDocument>
  */
-schema.statics.getParentAndFillAncestors = async function(path: string, user, pathsToExcludeNotNormalizedPages: string[]): Promise<PageDocument> {
+schema.statics.getParentAndFillAncestors = async function(path: string, user): Promise<PageDocument> {
   const parentPath = nodePath.dirname(path);
 
   const builder1 = new PageQueryBuilder(this.find({ path: parentPath }), true);
@@ -573,13 +573,10 @@ schema.statics.getParentAndFillAncestors = async function(path: string, user, pa
 
   // find ancestors
   const builder2 = new PageQueryBuilder(this.find(), true);
+
   // avoid including not normalized pages
-  if (pathsToExcludeNotNormalizedPages != null) {
-    builder2.addConditionToFilterByApplicableAncestors(pathsToExcludeNotNormalizedPages);
-  }
-  else {
-    builder2.addConditionToFilterByApplicableAncestors(ancestorPaths);
-  }
+  builder2.addConditionToFilterByApplicableAncestors(ancestorPaths);
+
   const ancestors = await builder2
     .addConditionToListByPathsArray(ancestorPaths)
     .addConditionToSortPagesByDescPath()

+ 4 - 13
packages/app/src/server/service/page.ts

@@ -2309,9 +2309,7 @@ class PageService {
       updatedPage = await Page.findById(page._id);
     }
     else {
-      // getParentAndFillAncestors
-      const pathsToExcludeNotNormalizedPages = collectAncestorPaths(page.path);
-      const parent = await Page.getParentAndFillAncestors(page.path, user, pathsToExcludeNotNormalizedPages);
+      const parent = await Page.getParentAndFillAncestors(page.path, user);
       updatedPage = await Page.findOneAndUpdate({ _id: page._id }, { parent: parent._id }, { new: true });
     }
 
@@ -2385,13 +2383,6 @@ class PageService {
   }
 
   async normalizeParentRecursivelyMainOperation(page, user, pageOpId: ObjectIdLike): Promise<void> {
-    // Save exDescendantCount for sub-operation
-    const Page = mongoose.model('Page') as unknown as PageModel;
-    const { PageQueryBuilder } = Page;
-    const builder = new PageQueryBuilder(Page.findOne(), true);
-    builder.addConditionAsMigrated();
-    const exPage = await builder.query.exec();
-    const options = { exDescendantCount: exPage?.descendantCount ?? 0 };
 
     try {
       await this.normalizeParentRecursively([page.path], user);
@@ -2409,10 +2400,10 @@ class PageService {
       throw Error('PageOperation document not found');
     }
 
-    await this.normalizeParentRecursivelySubOperation(page, user, pageOp._id, options);
+    await this.normalizeParentRecursivelySubOperation(page, user, pageOp._id);
   }
 
-  async normalizeParentRecursivelySubOperation(page, user, pageOpId: ObjectIdLike, options: {exDescendantCount: number}): Promise<void> {
+  async normalizeParentRecursivelySubOperation(page, user, pageOpId: ObjectIdLike): Promise<void> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
     try {
@@ -2426,7 +2417,7 @@ class PageService {
         throw Error('Page not found after updating descendantCount');
       }
 
-      const { exDescendantCount } = options;
+      const exDescendantCount = page.descendantCount;
       const newDescendantCount = pageAfterUpdatingDescendantCount.descendantCount;
       const inc = (newDescendantCount - exDescendantCount) + 1;
       await this.updateDescendantCountOfAncestors(page._id, inc, false);