sou 7 лет назад
Родитель
Сommit
90c1ebc82f

+ 53 - 21
lib/models/GlobalNotificationSetting.js

@@ -46,48 +46,80 @@ class GlobalNotificationSetting {
     this.crowi = crowi;
   }
 
-  /**
-   * enable notification setting
-   * @param {string} id
-   */
-  static async enable(id) {
-    // save
-    // return Notification
-  }
-
   /**
    * disable notification setting
    * @param {string} id
    */
-  static async disable(id) {
+  static async toggleIsEnabled(id) {
     const setting = await this.findOne({_id: id});
 
-    setting.isEnabled = false;
+    setting.isEnabled = !setting.isEnabled;
     setting.save();
 
     return setting;
   }
 
+  /**
+   * find all notification settings
+   */
+  static async findAll() {
+    const settings = await this.find().sort({ triggerPath: 1 });
+    //sort
+
+    return settings;
+  }
+
   /**
    * find a list of notification settings by path and a list of events
    * @param {string} path
    * @param {string} event
-   * @param {boolean} enabled
    */
-  static async findSettingByPathAndEvent(path, event, enabled) {
-    let settings;
+  static async findSettingByPathAndEvent(path, event) {
+    const pathsToMatch = generatePathsToMatch(path);
 
-    if (enabled == null) {
-      settings = this.find();
-    }
-    else {
-      settings = this.find({isEnabled: enabled});
-    }
+    const settings = await this.find({
+      triggerPath: {$in: pathsToMatch},
+      triggerEvents: event,
+      isEnabled: true
+    })
+    .sort({ triggerPath: 1 });
 
-    return await settings;
+    return settings;
   }
 }
 
+
+// move this to util
+// remove this from models/page
+const cutOffLastSlash = path => {
+  const lastSlash = path.lastIndexOf('/');
+  return path.substr(0, lastSlash);
+};
+
+const generatePathsOnTree = (path, pathList) => {
+  pathList.push(path);
+
+  if (path === '') {
+    return pathList;
+  }
+
+  const newPath = cutOffLastSlash(path);
+
+  return generatePathsOnTree(newPath, pathList);
+};
+
+const generatePathsToMatch = (originalPath) => {
+  const pathList = generatePathsOnTree(originalPath, []);
+  return pathList.map(path => {
+    if (path !== originalPath) {
+      return path + '/*';
+    }
+    else {
+      return path;
+    }
+  });
+};
+
 module.exports = function(crowi) {
   GlobalNotificationSetting.crowi = crowi;
   notificationSchema.loadClass(GlobalNotificationSetting);

+ 2 - 6
lib/routes/admin.js

@@ -205,7 +205,7 @@ module.exports = function(crowi, app) {
       req.session.slackSetting = null;
     }
 
-    const globalNotifications = await GlobalNotificationSetting.Parent.findSettingByPathAndEvent(null, null, null);
+    const globalNotifications = await GlobalNotificationSetting.Parent.findAll();
     const userNotifications = await UpdatePost.findAll();
 
     return res.render('admin/notification', {
@@ -1132,13 +1132,9 @@ module.exports = function(crowi, app) {
 
   actions.api.toggleIsEnabledForGlobalNotification = async(req, res) => {
     const id =req.query.id;
-    let setting;
 
     try {
-      setting= await GlobalNotificationSetting.Parent.findOne({_id: id});
-      setting.isEnabled = !setting.isEnabled;
-      setting.save();
-
+      GlobalNotificationSetting.Parent.toggleIsEnabled(id);
       return res.json(ApiResponse.success());
     }
     catch (err) {

+ 1 - 1
lib/views/admin/global-notification.html

@@ -140,7 +140,7 @@ input:checked + .slider:before {
     var id = $(event.currentTarget).closest("tr").data("updatepost-id")
     $.post('/_api/admin/global-notification/toggleIsEnabled?id=' + id, function(res) {
       if (res.ok) {
-        // alert(id, ': suucess')
+        // do something
       }
     });
   });