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

show list of global notifcation

sou 7 лет назад
Родитель
Сommit
318989af4a

+ 21 - 21
lib/models/GlobalNotificationSetting.js

@@ -29,6 +29,7 @@ const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
   }, {discriminatorKey: discriminatorKey}));
 
   return {
+    Parent: Notification,
     Mail: mailNotification,
     Slack: slackNotification,
   };
@@ -49,22 +50,22 @@ class GlobalNotificationSetting {
    * enable notification setting
    * @param {string} id
    */
-  static enable(id) {
-    // return new Promise((resolve, reject) => {
-      // save
-      // return resolve(Notification)
-    //}
+  static async enable(id) {
+    // save
+    // return Notification
   }
 
   /**
    * disable notification setting
    * @param {string} id
    */
-  static disable(id) {
-    // return new Promise((resolve, reject) => {
-      // save
-      // return resolve(Notification)
-    //}
+  static async disable(id) {
+    const setting = await this.findOne({_id: id});
+
+    setting.isEnabled = false;
+    setting.save();
+
+    return setting;
   }
 
   /**
@@ -73,18 +74,17 @@ class GlobalNotificationSetting {
    * @param {string} event
    * @param {boolean} enabled
    */
-  static findSettingByPathAndEvent(path, event, enabled) {
-    // return new Promise((resolve, reject) => {
-      // if(enabled == null) {
-      //   find all
-      // }
-      // else {
-      //   find only enabled/disabled
-      // }
-      // sort by path in mongoDB
+  static async findSettingByPathAndEvent(path, event, enabled) {
+    let settings;
+
+    if (enabled == null) {
+      settings = this.find();
+    }
+    else {
+      settings = this.find({isEnabled: enabled});
+    }
 
-      // return resolve([Notification])
-    //}
+    return await settings;
   }
 }
 

+ 19 - 16
lib/routes/admin.js

@@ -189,13 +189,12 @@ module.exports = function(crowi, app) {
 
   // app.get('/admin/notification'               , admin.notification.index);
   actions.notification = {};
-  actions.notification.index = function(req, res) {
-    var config = crowi.getConfig();
-    var UpdatePost = crowi.model('UpdatePost');
-    var slackSetting = Config.setupCofigFormData('notification', config);
-    var hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
-    var hasSlackToken = Config.hasSlackToken(config);
-    var slack = crowi.slack;
+  actions.notification.index = async(req, res) => {
+    const config = crowi.getConfig();
+    const UpdatePost = crowi.model('UpdatePost');
+    let slackSetting = Config.setupCofigFormData('notification', config);
+    const hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
+    const hasSlackToken = Config.hasSlackToken(config);
 
     if (!Config.hasSlackIwhUrl(req.config)) {
       slackSetting['slack:incomingWebhookUrl'] = '';
@@ -206,14 +205,15 @@ module.exports = function(crowi, app) {
       req.session.slackSetting = null;
     }
 
-    UpdatePost.findAll()
-    .then(function(settings) {
-      return res.render('admin/notification', {
-        settings,
-        slackSetting,
-        hasSlackIwhUrl,
-        hasSlackToken,
-      });
+    const globalNotifications = await GlobalNotificationSetting.Parent.findSettingByPathAndEvent(null, null, null);
+    const userNotifications = await UpdatePost.findAll();
+
+    return res.render('admin/notification', {
+      userNotifications,
+      slackSetting,
+      hasSlackIwhUrl,
+      hasSlackToken,
+      globalNotifications,
     });
   };
 
@@ -316,6 +316,7 @@ module.exports = function(crowi, app) {
     const notificationSettingId = req.params.id;
     const renderVars = {
     };
+
     return res.render('admin/global-notification-detail', renderVars);
   };
 
@@ -340,7 +341,9 @@ module.exports = function(crowi, app) {
     let triggerEvents = [];
     const triggerEventKeys = Object.keys(form).filter(key => key.match(/^triggerEvent/));
     triggerEventKeys.forEach(key => {
-      triggerEvents.push(form[key]);
+      if (form[key]) {
+        triggerEvents.push(form[key]);
+      }
     });
 
     if (setting) {

+ 6 - 4
lib/views/admin/global-notification.html

@@ -50,9 +50,9 @@
     </tr>
     </form>
 
-    {% for setting in settings %}
+    {% for globalNotif in globalNotifications %}
     {% set detailPageUrl = '/admin/global-notification/detail' %}
-    <tr class="clickable-row" data-href="{{ detailPageUrl }}" data-updatepost-id="{{ notif._id.toString() }}">
+    <tr class="clickable-row" data-href="{{ detailPageUrl }}" data-updatepost-id="{{ globalNotif._id.toString() }}">
       <td class="unclickable">
         <label class="switch">
           <input type="checkbox">
@@ -60,7 +60,7 @@
         </label>
       </td>
       <td>
-        {{ setting.pathPattern }}
+        {{ globalNotif.triggerPath }}
       </td>
       <td>
         <table>
@@ -76,7 +76,9 @@
         </table>
       </td>
       <td>
-        {{ setting.channel }}
+        {% if globalNotif.__t == 'mail' %}emailicon {{ globalNotif.toEmail }}
+        {% elseif globalNotif.__t == 'slack' %}slackicon {{ globalNotif.slackChannels }}
+        {% endif %}
       </td>
     </tr>
     {% endfor %}

+ 5 - 5
lib/views/admin/notification.html

@@ -229,17 +229,17 @@
               </tr>
               </form>
 
-              {% for notif in settings %}
-              <tr class="admin-notif-row" data-updatepost-id="{{ notif._id.toString() }}">
+              {% for userNotif in userNotifications %}
+              <tr class="admin-notif-row" data-updatepost-id="{{ userNotif._id.toString() }}">
                 <td>
-                  {{ notif.pathPattern }}
+                  {{ userNotif.pathPattern }}
                 </td>
                 <td>
-                  {{ notif.channel }}
+                  {{ userNotif.channel }}
                 </td>
                 <td>
                   <form class="admin-remove-updatepost">
-                    <input type="hidden" name="id" value="{{ notif._id.toString() }}">
+                    <input type="hidden" name="id" value="{{ userNotif._id.toString() }}">
                     <input type="hidden" name="_csrf" value="{{ csrf() }}">
                     <input type="submit" value="Delete" class="btn btn-default">
                   </form>