Taichi Masuyama 4 سال پیش
والد
کامیت
652bebc421
2فایلهای تغییر یافته به همراه10 افزوده شده و 8 حذف شده
  1. 1 5
      packages/app/src/server/models/page.ts
  2. 9 3
      packages/app/src/server/util/compare-objectId.ts

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

@@ -151,10 +151,6 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[], publicO
   }
 };
 
-schema.statics.findOneParentByParentPath = async function(parentPath: string): Promise<PageDocument | null> {
-  return this.findOne({ path: parentPath }); // find the oldest parent which must always be the true parent
-};
-
 /*
  * Find the parent and update if the parent exists.
  * If not,
@@ -434,7 +430,7 @@ export default (crowi: Crowi): any => {
 
     let parentId: string | null = null;
     const parentPath = nodePath.dirname(path);
-    const parent = await this.findOneParentByParentPath(parentPath);
+    const parent = await this.findOne({ path: parentPath }); // find the oldest parent which must always be the true parent
     if (!isTopPage(path)) {
       parentId = await Page.getParentIdAndFillAncestors(path, parent);
     }

+ 9 - 3
packages/app/src/server/util/compare-objectId.ts

@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
 
 type IObjectId = mongoose.Types.ObjectId;
 const ObjectId = mongoose.Types.ObjectId;
+type ObjectIdLike = IObjectId | string;
 
 export const isIncludesObjectId = (arr: (IObjectId | string)[], id: IObjectId | string): boolean => {
   const _arr = arr.map(i => i.toString());
@@ -16,16 +17,21 @@ export const isIncludesObjectId = (arr: (IObjectId | string)[], id: IObjectId |
  * @param testIds Array of mongoose.Types.ObjectId
  * @returns Array of mongoose.Types.ObjectId
  */
-export const excludeTestIdsFromTargetIds = (targetIds: (IObjectId | string)[], testIds: (IObjectId | string)[]): IObjectId[] => {
+export const excludeTestIdsFromTargetIds = <T extends { toString: any } = IObjectId>(
+  targetIds: T[], testIds: (IObjectId | string)[],
+): T[] => {
   // cast to string
   const arr1 = targetIds.map(e => e.toString());
   const arr2 = testIds.map(e => e.toString());
 
   // filter
   const excluded = arr1.filter(e => !arr2.includes(e));
-
   // cast to ObjectId
-  return excluded.map(e => new ObjectId(e));
+  const shouldReturnString = (arr: any[]): arr is string[] => {
+    return typeof arr[0] === 'string';
+  };
+
+  return shouldReturnString(targetIds) ? excluded : excluded.map(e => new ObjectId(e));
 };
 
 export const removeDuplicates = (objectIds: (IObjectId | string)[]): IObjectId[] => {