Przeglądaj źródła

getNotificationEvents, flash message for update

sou 7 lat temu
rodzic
commit
6dbba45f04
1 zmienionych plików z 16 dodań i 24 usunięć
  1. 16 24
      lib/routes/admin.js

+ 16 - 24
lib/routes/admin.js

@@ -343,23 +343,13 @@ module.exports = function(crowi, app) {
       //   break;
       default:
         logger.error('GlobalNotificationSetting Type Error: undefined type');
-        req.flash('errorMessage', 'Error occurred in create a new global notification setting: undefined notification type');
-        break;
+        req.flash('errorMessage', 'Error occurred in creating a new global notification setting: undefined notification type');
+        return res.redirect('/admin/notification#global-notification');
     }
 
-    let triggerEvents = [];
-    const triggerEventKeys = Object.keys(form).filter(key => key.match(/^triggerEvent/));
-    triggerEventKeys.forEach(key => {
-      if (form[key]) {
-        triggerEvents.push(form[key]);
-      }
-    });
-
-    if (setting) {
-      setting.triggerPath = form.triggerPath;
-      setting.triggerEvents = triggerEvents;
-      setting.save();
-    }
+    setting.triggerPath = form.triggerPath;
+    setting.triggerEvents = getNotificationEvents(form);
+    setting.save();
 
     return res.redirect('/admin/notification#global-notification');
   };
@@ -377,9 +367,18 @@ module.exports = function(crowi, app) {
       //   break;
       default:
         logger.error('GlobalNotificationSetting Type Error: undefined type');
-        break;
+        req.flash('errorMessage', 'Error occurred in updating the global notification setting: undefined notification type');
+        return res.redirect('/admin/notification#global-notification');
     }
 
+    setting.triggerPath = form.triggerPath;
+    setting.triggerEvents = getNotificationEvents(form);
+    setting.save();
+
+    return res.redirect('/admin/notification#global-notification');
+  };
+
+  const getNotificationEvents = (form) => {
     let triggerEvents = [];
     const triggerEventKeys = Object.keys(form).filter(key => key.match(/^triggerEvent/));
     triggerEventKeys.forEach(key => {
@@ -387,14 +386,7 @@ module.exports = function(crowi, app) {
         triggerEvents.push(form[key]);
       }
     });
-
-    if (setting) {
-      setting.triggerPath = form.triggerPath;
-      setting.triggerEvents = triggerEvents;
-      setting.save();
-    }
-
-    return res.redirect('/admin/notification#global-notification');
+    return triggerEvents;
   };
 
   actions.search.buildIndex = function(req, res) {