Explorar o código

create revertDeletedPages

zamis %!s(int64=5) %!d(string=hai) anos
pai
achega
7ccd2a0fae
Modificáronse 1 ficheiros con 22 adicións e 0 borrados
  1. 22 0
      src/server/service/page.js

+ 22 - 0
src/server/service/page.js

@@ -177,6 +177,28 @@ class PageService {
     return updatedPage;
     return updatedPage;
   }
   }
 
 
+  // revert pages recursively
+  async revertDeletedPages(page, user, options = {}) {
+    const Page = this.crowi.model('Page');
+    const newPath = Page.getRevertDeletedPageName(page.path);
+    const originPage = await Page.findByPath(newPath);
+    if (originPage != null) {
+      // When the page is deleted, it will always be created with "redirectTo" in the path of the original page.
+      // So, it's ok to delete the page
+      // However, If a page exists that is not "redirectTo", something is wrong. (Data correction is needed).
+      if (originPage.redirectTo !== page.path) {
+        throw new Error('The new page of to revert is exists and the redirect path of the page is not the deleted page.');
+      }
+      await this.completelyDeletePages(originPage, options);
+    }
+
+    page.status = STATUS_PUBLISHED;
+    page.lastUpdateUser = user;
+    debug('Revert deleted the page', page, newPath);
+    const updatedPage = await Page.rename(page, newPath, user, {});
+    return updatedPage;
+  }
+
   async handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId) {
   async handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId) {
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
     const pages = await Page.find({ grantedGroup: deletedGroup });
     const pages = await Page.find({ grantedGroup: deletedGroup });