Просмотр исходного кода

implement executeAllRenameOperationBySystem temp

yohei0125 3 лет назад
Родитель
Сommit
ec878f3607

+ 9 - 0
packages/app/src/server/crowi/index.js

@@ -150,6 +150,13 @@ Crowi.prototype.init = async function() {
   await this.autoInstall();
 };
 
+Crowi.prototype.afterInit = async function() {
+  if (this.pageOperationService != null) {
+    await this.pageOperationService.onAfterInit();
+  }
+};
+
+
 Crowi.prototype.isPageId = function(pageId) {
   if (!pageId) {
     return false;
@@ -470,6 +477,8 @@ Crowi.prototype.start = async function() {
   // setup Global Error Handlers
   this.setupGlobalErrorHandlers();
 
+  this.afterInit();
+
   return serverListening;
 };
 

+ 23 - 0
packages/app/src/server/service/page-operation.ts

@@ -25,6 +25,29 @@ class PageOperationService {
     await PageOperation.deleteByActionTypes(types);
   }
 
+  async onAfterInit():Promise<void> {
+    await this.executeAllRenameOperationBySystem();
+  }
+
+  // サーバー起動時にDBに残っている actionType が Rename の PageOperation をすべて、createdAt の古いものから順番に実行
+  async executeAllRenameOperationBySystem(): Promise<void> {
+    const Page = this.crowi.model('Page');
+
+    const pageOps = await PageOperation.find({ actionType: PageActionType.Rename }).sort({ createdAt: 1 });
+    if (pageOps.length === 0) return;
+
+    for await (const pageOp of pageOps) {
+      const {
+        page, toPath, options, user,
+      } = pageOp;
+
+      const renamedPage = await Page.findById(pageOp.page._id);
+
+      // rename 実行
+      await this.crowi.pageService.renameSubOperation(page, toPath, user, options, renamedPage, pageOp._id);
+    }
+  }
+
   /**
    * Check if the operation is operatable
    * @param isRecursively Boolean that determines whether the operation is recursive or not