sou 7 лет назад
Родитель
Сommit
4920a202cc

+ 1 - 18
lib/models/GlobalNotificationSetting.js

@@ -1,25 +1,8 @@
-const debug = require('debug')('growi:models:GlobalNotificationSetting');
 const mongoose = require('mongoose');
 const GlobalNotificationSetting = require('./GlobalNotificationSetting/index');
 
-/**
- * create child schemas inherited from parentSchema
- * all child schemas are stored in globalnotificationsettings collection
- * @link{http://url.com module_name}
- * @param {object} parentSchema
- * @param {string} modelName
- * @param {string} discriminatorKey
- */
-const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
-  const Notification = mongoose.model(modelName, parentSchema);
-
-  return {
-    Parent: Notification,
-  };
-};
-
 module.exports = function(crowi) {
   GlobalNotificationSetting.class.crowi = crowi;
   GlobalNotificationSetting.schema.loadClass(GlobalNotificationSetting.class);
-  return createChildSchemas(GlobalNotificationSetting.schema, 'GlobalNotificationSetting', 'type');
+  return mongoose.model('GlobalNotificationSetting', GlobalNotificationSetting.schema);
 };

+ 10 - 19
lib/models/GlobalNotificationSetting/GlobalNotificationMailSetting.js

@@ -1,25 +1,16 @@
 const mongoose = require('mongoose');
 const GlobalNotificationSetting = require('./index');
+const GlobalNotificationSettingClass = GlobalNotificationSetting.class;
+const GlobalNotificationSettingSchema = GlobalNotificationSetting.schema;
 
-/**
- * create child schemas inherited from parentSchema
- * all child schemas are stored in globalnotificationsettings collection
- * @link{http://url.com module_name}
- * @param {object} parentSchema
- * @param {string} modelName
- * @param {string} discriminatorKey
- */
-const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
-  const Notification = mongoose.model(modelName, parentSchema);
-  const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
-    toEmail: String,
-  }, {discriminatorKey: discriminatorKey}));
+module.exports = function(crowi) {
+  GlobalNotificationSettingClass.crowi = crowi;
+  GlobalNotificationSettingSchema.loadClass(GlobalNotificationSettingClass);
 
-  return mailNotification;
-};
+  const GlobalNotificationSettingModel = mongoose.model('GlobalNotificationSetting', GlobalNotificationSettingSchema);
+  const GlobalNotificationMailSettingModel = GlobalNotificationSettingModel.discriminator('mail', new mongoose.Schema({
+    toEmail: String,
+  }, {discriminatorKey: 'type'}));
 
-module.exports = function(crowi) {
-  GlobalNotificationSetting.class.crowi = crowi;
-  GlobalNotificationSetting.schema.loadClass(GlobalNotificationSetting.class);
-  return createChildSchemas(GlobalNotificationSetting.schema, 'GlobalNotificationSetting', 'type');
+  return GlobalNotificationMailSettingModel;
 };

+ 10 - 19
lib/models/GlobalNotificationSetting/GlobalNotificationSlackSetting.js

@@ -1,25 +1,16 @@
 const mongoose = require('mongoose');
 const GlobalNotificationSetting = require('./index');
+const GlobalNotificationSettingClass = GlobalNotificationSetting.class;
+const GlobalNotificationSettingSchema = GlobalNotificationSetting.schema;
 
-/**
- * create child schemas inherited from parentSchema
- * all child schemas are stored in globalnotificationsettings collection
- * @link{http://url.com module_name}
- * @param {object} parentSchema
- * @param {string} modelName
- * @param {string} discriminatorKey
- */
-const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
-  const Notification = mongoose.model(modelName, parentSchema);
-  const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
-    slackChannels: String,
-  }, {discriminatorKey: discriminatorKey}));
+module.exports = function(crowi) {
+  GlobalNotificationSettingClass.crowi = crowi;
+  GlobalNotificationSettingSchema.loadClass(GlobalNotificationSettingClass);
 
-  return slackNotification;
-};
+  const GlobalNotificationSettingModel = mongoose.model('GlobalNotificationSetting', GlobalNotificationSettingSchema);
+  const GlobalNotificationSlackSettingModel = GlobalNotificationSettingModel.discriminator('slack', new mongoose.Schema({
+    slackChannels: String,
+  }, {discriminatorKey: 'type'}));
 
-module.exports = function(crowi) {
-  GlobalNotificationSetting.class.crowi = crowi;
-  GlobalNotificationSetting.schema.loadClass(GlobalNotificationSetting.class);
-  return createChildSchemas(GlobalNotificationSetting.schema, 'GlobalNotificationSetting', 'type');
+  return GlobalNotificationSlackSettingModel;
 };

+ 1 - 1
lib/models/index.js

@@ -14,5 +14,5 @@ module.exports = {
   UpdatePost: require('./updatePost'),
   GlobalNotificationSetting: require('./GlobalNotificationSetting'),
   GlobalNotificationMailSetting: require('./GlobalNotificationSetting/GlobalNotificationMailSetting'),
-  // GlobalNotificationSlackSetting: require('./GlobalNotificationSetting/GlobalNotificationSlackSetting'),
+  GlobalNotificationSlackSetting: require('./GlobalNotificationSetting/GlobalNotificationSlackSetting'),
 };

+ 8 - 7
lib/routes/admin.js

@@ -14,6 +14,7 @@ module.exports = function(crowi, app) {
     , Config = models.Config
     , GlobalNotificationSetting = models.GlobalNotificationSetting
     , GlobalNotificationMailSetting = models.GlobalNotificationMailSetting
+    , GlobalNotificationSlackSetting = models.GlobalNotificationSlackSetting
     , PluginUtils = require('../plugins/plugin-utils')
     , pluginUtils = new PluginUtils()
     , ApiResponse = require('../util/apiResponse')
@@ -206,7 +207,7 @@ module.exports = function(crowi, app) {
       req.session.slackSetting = null;
     }
 
-    const globalNotifications = await GlobalNotificationSetting.Parent.findAll();
+    const globalNotifications = await GlobalNotificationSetting.findAll();
     const userNotifications = await UpdatePost.findAll();
 
     return res.render('admin/notification', {
@@ -319,7 +320,7 @@ module.exports = function(crowi, app) {
 
     if (notificationSettingId) {
       try {
-        renderVars.setting = await GlobalNotificationSetting.Parent.findOne({_id: notificationSettingId});
+        renderVars.setting = await GlobalNotificationSetting.findOne({_id: notificationSettingId});
       }
       catch (err) {
         logger.error(`Error in finding a global notification setting with {_id: ${notificationSettingId}}`);
@@ -339,7 +340,7 @@ module.exports = function(crowi, app) {
         setting.toEmail = form.toEmail;
         break;
       // case 'slack':
-      //   setting = new GlobalNotificationSetting.Slack(crowi);
+      //   setting = new GlobalNotificationSlackSetting(crowi);
       //   setting.slackChannels = form.slackChannels;
       //   break;
       default:
@@ -357,7 +358,7 @@ module.exports = function(crowi, app) {
 
   actions.globalNotification.update = async(req, res) => {
     const form = req.form.notificationGlobal;
-    const setting = await GlobalNotificationSetting.Parent.findOne({_id: form.id});
+    const setting = await GlobalNotificationSetting.findOne({_id: form.id});
 
     switch (form.notifyToType) {
       case 'mail':
@@ -1157,10 +1158,10 @@ module.exports = function(crowi, app) {
 
     try {
       if (isEnabled) {
-        await GlobalNotificationSetting.Parent.disable(id);
+        await GlobalNotificationSetting.disable(id);
       }
       else {
-        await GlobalNotificationSetting.Parent.enable(id);
+        await GlobalNotificationSetting.enable(id);
       }
 
       return res.json(ApiResponse.success());
@@ -1174,7 +1175,7 @@ module.exports = function(crowi, app) {
     const id = req.query.id;
 
     try {
-      await GlobalNotificationSetting.Parent.findOneAndRemove({_id: id});
+      await GlobalNotificationSetting.findOneAndRemove({_id: id});
       return res.json(ApiResponse.success());
     }
     catch (err) {