ryoji-s 2 лет назад
Родитель
Сommit
0916dcd73b
1 измененных файлов с 22 добавлено и 21 удалено
  1. 22 21
      apps/app/src/server/models/page.ts

+ 22 - 21
apps/app/src/server/models/page.ts

@@ -41,6 +41,7 @@ const STATUS_DELETED = 'deleted';
 export interface PageDocument extends IPage, Document {
   [x:string]: any // for obsolete methods
   getLatestRevisionBodyLength(): Promise<number | null | undefined>
+  calculateAndUpdateLatestRevisionBodyLength(this: PageDocument): Promise<void>
 }
 
 
@@ -961,40 +962,40 @@ schema.statics.findNonEmptyClosestAncestor = async function(path: string): Promi
 };
 
 /*
- * method for getLatestRevisionBodyLength
+ * get latest revision body length
  */
-async function calculateAndUpdateLatestRevisionBodyLength(pageDocument: PageDocument): Promise<void> {
-  if (!pageDocument.isLatestRevision() || pageDocument.revision == null) {
-    logger.error('revision field is required.');
-    return;
+schema.methods.getLatestRevisionBodyLength = async function(this: PageDocument): Promise<number | null | undefined> {
+  if (!this.isLatestRevision() || this.revision == null) {
+    return null;
   }
 
-  // Infer the type as Omit<PageDocument, never> due to the population
-  // Cast the type back to PageDocument to restore the original type
-  // eslint-disable-next-line rulesdir/no-populate
-  const populatedPageDocument = await pageDocument.populate('revision', 'body') as PageDocument;
-
-  if (typeof populatedPageDocument.revision === 'string') {
-    throw new Error('Failed to populate revision field in the page document.');
+  if (this.latestRevisionBodyLength == null) {
+    await this.calculateAndUpdateLatestRevisionBodyLength();
   }
 
-  pageDocument.latestRevisionBodyLength = populatedPageDocument.revision.body.length;
-  await pageDocument.save();
-}
+  return this.latestRevisionBodyLength;
+};
 
 /*
- * get latest revision body length
+ * calculate and update latestRevisionBodyLength
  */
-schema.methods.getLatestRevisionBodyLength = async function(this: PageDocument): Promise<number | null | undefined> {
+schema.methods.calculateAndUpdateLatestRevisionBodyLength = async function(this: PageDocument): Promise<void> {
   if (!this.isLatestRevision() || this.revision == null) {
-    return null;
+    logger.error('revision field is required.');
+    return;
   }
 
-  if (this.latestRevisionBodyLength == null) {
-    await calculateAndUpdateLatestRevisionBodyLength(this);
+  // Infer the type as Omit<PageDocument, never> due to the population
+  // Cast the type back to PageDocument to restore the original type
+  // eslint-disable-next-line rulesdir/no-populate
+  const populatedPageDocument = await this.populate('revision', 'body') as PageDocument;
+
+  if (typeof populatedPageDocument.revision === 'string') {
+    throw new Error('Failed to populate revision field in the page document.');
   }
 
-  return this.latestRevisionBodyLength;
+  this.latestRevisionBodyLength = populatedPageDocument.revision.body.length;
+  await this.save();
 };
 
 export type PageCreateOptions = {