itizawa 6 лет назад
Родитель
Сommit
ec0c0c8dad

+ 7 - 1
src/client/js/components/Admin/Notification/ManageGlobalNotification.jsx

@@ -65,7 +65,13 @@ class ManageGlobalNotification extends React.Component {
 
 
     try {
     try {
       if (this.state.globalNotificationId != null) {
       if (this.state.globalNotificationId != null) {
-        // TODO put
+        await this.props.appContainer.apiv3.put(`/notification-setting/global-notification/${this.state.globalNotificationId}`, {
+          triggerPath: this.state.triggerPath,
+          notifyToType: this.state.notifyToType,
+          toEmail: this.state.emailToSend,
+          slackChannels: this.state.slackChannelToSend,
+          triggerEvents: [...this.state.triggerEvents],
+        });
       }
       }
       else {
       else {
         await this.props.appContainer.apiv3.post('/notification-setting/global-notification', {
         await this.props.appContainer.apiv3.post('/notification-setting/global-notification', {

+ 50 - 0
src/server/routes/apiv3/notification-setting.js

@@ -280,6 +280,56 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
+  // TODO swagger and validator
+  router.put('/global-notification/:id', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
+    const { id } = req.params;
+    const {
+      notifyToType, toEmail, slackChannels, triggerPath, triggerEvents,
+    } = req.body;
+
+    const models = {
+      [GlobalNotificationSetting.TYPE.MAIL]: GlobalNotificationMailSetting,
+      [GlobalNotificationSetting.TYPE.SLACK]: GlobalNotificationSlackSetting,
+    };
+
+    try {
+      let setting = await GlobalNotificationSetting.findOne({ _id: id });
+      setting = setting.toObject();
+
+      // when switching from one type to another,
+      // remove toEmail from slack setting and slackChannels from mail setting
+      if (setting.__t !== notifyToType) {
+        setting = models[setting.__t].hydrate(setting);
+        setting.toEmail = undefined;
+        setting.slackChannels = undefined;
+        await setting.save();
+        setting = setting.toObject();
+      }
+
+      if (notifyToType === GlobalNotificationSetting.TYPE.MAIL) {
+        setting = GlobalNotificationMailSetting.hydrate(setting);
+        setting.toEmail = toEmail;
+      }
+      if (notifyToType === GlobalNotificationSetting.TYPE.SLACK) {
+        setting = GlobalNotificationSlackSetting.hydrate(setting);
+        setting.slackChannels = slackChannels;
+      }
+
+      setting.__t = notifyToType;
+      setting.triggerPath = triggerPath;
+      setting.triggerEvents = triggerEvents || [];
+
+      const createdNotification = await setting.save();
+      return res.apiv3({ createdNotification });
+    }
+    catch (err) {
+      const msg = 'Error occurred in updating global notification';
+      logger.error('Error', err);
+      return res.apiv3Err(new ErrorV3(msg, 'post-globalNotification-failed'));
+    }
+
+  });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *