sou 7 лет назад
Родитель
Сommit
c604ef37e1
2 измененных файлов с 54 добавлено и 36 удалено
  1. 35 25
      lib/models/global-notification-setting.js
  2. 19 11
      lib/routes/admin.js

+ 35 - 25
lib/models/global-notification-setting.js

@@ -1,37 +1,38 @@
 const debug = require('debug')('growi:models:global-notification-setting');
 const mongoose = require('mongoose');
-const Notification = this;
+// const Notification = this;
 
 /*
- * define schema
+ * parent schema
  */
-
-const notifyToSchema = new mongoose.Schema({
-  type: { type: String, required: true },
-  extended: {
-    type: String,
-    default: '{}',
-    get: function(data) {
-      try {
-        return JSON.parse(data);
-      }
-      catch (e) {
-        return data;
-      }
-    },
-    set: function(data) {
-      return JSON.stringify(data);
-    }
-  },
-});
-
 const notificationSchema = new mongoose.Schema({
   isEnabled: { type: Boolean, required: true, default: true },
   triggerPath: { type: String, required: true },
   triggerEvents: { type: [String] },
-  notifyTo: notifyToSchema
 });
 
+/*
+ * child schema inherited from notificationSchema
+ * stored in globalnotificationsettings collection
+ */
+const createChildSchemas = (parentSchema, className, modelName, discriminatorKey) => {
+  parentSchema.loadClass(className);
+  const Notification = mongoose.model(modelName, parentSchema);
+  const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
+    toEmail: String,
+  }, {discriminatorKey: discriminatorKey}));
+
+  const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
+    slackChannels: String,
+  }, {discriminatorKey: discriminatorKey}));
+
+  return {
+    Mail: mailNotification,
+    Slack: slackNotification,
+  };
+};
+
+
 /**
  * GlobalNotificationSetting Class
  * @class GlobalNotificationSetting
@@ -79,10 +80,19 @@ class GlobalNotificationSetting {
       // return resolve([Notification])
     //}
   }
+
+  // DELETEME
+  test(s) {
+    console.log(s)
+  }
 }
 
 module.exports = function(crowi) {
   GlobalNotificationSetting.crowi = crowi;
-  notificationSchema.loadClass(GlobalNotificationSetting);
-  return mongoose.model('GlobalNotificationSetting', notificationSchema);
+  return createChildSchemas(
+    notificationSchema,
+    GlobalNotificationSetting,
+    'GlobalNotificationSetting',
+    'type',
+  );
 };

+ 19 - 11
lib/routes/admin.js

@@ -1131,29 +1131,37 @@ module.exports = function(crowi, app) {
     }, callback);
   }
 
-  //DELETEME
+  // DELETEME
   actions.test = (req, res) => {
-    let notif = new GlobalNotification();
+    let notif = new GlobalNotification.Mail();
     notif.isEnabled = true;
     notif.triggerPath = '/*',
     notif.triggerEvents = 'comment, pageCreate';
-    notif.notifyTo = {
-      type: 'mail',
-      extended: {
-        toEmail: 'email here',
-      }
-    };
+    notif.toEmail = 'email@email.com';
 
     notif.save(function(err, notif) {
       if(err) {
         console.log(err);
         return;
       }
-      console.log(notif);
-      return res.json(ApiResponse.success());
     });
-  };
 
+    let snotif = new GlobalNotification.Slack();
+    snotif.isEnabled = true;
+    snotif.triggerPath = '/*',
+    snotif.triggerEvents = 'comment, pageCreate';
+    snotif.slackChannels = 'slack, slack';
+
+    snotif.save(function(err, snotif) {
+      if(err) {
+        console.log(err);
+        return;
+      }
+    });
+    notif.test('wwwwwwwwwwwwwwwwwww');
+    snotif.test('sssssssssssss');
+    return res.json(ApiResponse.success());
+  };
 
   return actions;
 };