Sfoglia il codice sorgente

Merge pull request #3720 from weseek/imprv/gw5917-save-bot-type-to-mongodb

Imprv/gw5917 save bot type to mongodb
Kaori Tokashiki 4 anni fa
parent
commit
9e10a658bd

+ 4 - 4
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettings.jsx

@@ -18,10 +18,10 @@ const CustomBotWithoutProxySettings = (props) => {
 
 
   const deleteSlackSettingsHandler = async() => {
   const deleteSlackSettingsHandler = async() => {
     try {
     try {
-      await appContainer.apiv3.put('/slack-integration-settings/custom-bot-without-proxy', {
-        slackSigningSecret: '',
-        slackBotToken: '',
-        currentBotType: '',
+      await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
+        slackSigningSecret: null,
+        slackBotToken: null,
+        currentBotType: null,
       });
       });
       toastSuccess('success');
       toastSuccess('success');
     }
     }

+ 1 - 1
src/client/js/components/Admin/SlackIntegration/CustomBotWithoutProxySettingsAccordion.jsx

@@ -34,7 +34,7 @@ const CustomBotWithoutProxySettingsAccordion = ({
 
 
   const updateSecretTokenHandler = async() => {
   const updateSecretTokenHandler = async() => {
     try {
     try {
-      await appContainer.apiv3.put('/slack-integration-settings/custom-bot-without-proxy', {
+      await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
         slackSigningSecret,
         slackSigningSecret,
         slackBotToken,
         slackBotToken,
         currentBotType,
         currentBotType,

+ 2 - 4
src/client/js/components/Admin/SlackIntegration/SlackIntegration.jsx

@@ -71,12 +71,10 @@ const SlackIntegration = (props) => {
 
 
   const changeCurrentBotSettingsHandler = async() => {
   const changeCurrentBotSettingsHandler = async() => {
     try {
     try {
-      const res = await appContainer.apiv3.put('/slack-integration-settings/custom-bot-without-proxy', {
-        slackSigningSecret: '',
-        slackBotToken: '',
+      const res = await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
         currentBotType: selectedBotType,
         currentBotType: selectedBotType,
       });
       });
-      setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
+      setCurrentBotType(res.data.slackBotTypeParam.slackBotType);
       setSelectedBotType(null);
       setSelectedBotType(null);
       toastSuccess(t('admin:slack_integration.bot_reset_successful'));
       toastSuccess(t('admin:slack_integration.bot_reset_successful'));
       setIsRegisterSlackCredentials(false);
       setIsRegisterSlackCredentials(false);

+ 47 - 17
src/server/routes/apiv3/slack-integration-settings.js

@@ -51,9 +51,7 @@ module.exports = (crowi) => {
   const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
   const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
 
 
   const validator = {
   const validator = {
-    CustomBotWithoutProxy: [
-      body('slackSigningSecret').isString(),
-      body('slackBotToken').isString(),
+    BotType: [
       body('currentBotType').isString(),
       body('currentBotType').isString(),
     ],
     ],
     SlackIntegration: [
     SlackIntegration: [
@@ -66,6 +64,17 @@ module.exports = (crowi) => {
     ],
     ],
   };
   };
 
 
+  async function resetAllBotSettings() {
+    const params = {
+      'slackbot:currentBotType': null,
+      'slackbot:signingSecret': null,
+      'slackbot:token': null,
+    };
+    const { configManager } = crowi;
+    // update config without publishing S2sMessage
+    return configManager.updateConfigsInTheSameNamespace('crowi', params, true);
+  }
+
   async function updateSlackBotSettings(params) {
   async function updateSlackBotSettings(params) {
     const { configManager } = crowi;
     const { configManager } = crowi;
     // update config without publishing S2sMessage
     // update config without publishing S2sMessage
@@ -188,6 +197,7 @@ module.exports = (crowi) => {
       }
       }
     });
     });
 
 
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *
@@ -207,25 +217,45 @@ module.exports = (crowi) => {
    *           200:
    *           200:
    *             description: Succeeded to put CustomBotWithoutProxy setting.
    *             description: Succeeded to put CustomBotWithoutProxy setting.
    */
    */
-  router.put('/custom-bot-without-proxy',
-    accessTokenParser, loginRequiredStrictly, adminRequired, csrf, validator.CustomBotWithoutProxy, apiV3FormValidator, async(req, res) => {
-      const { slackSigningSecret, slackBotToken, currentBotType } = req.body;
-      const requestParams = {
-        'slackbot:signingSecret': slackSigningSecret,
-        'slackbot:token': slackBotToken,
-        'slackbot:currentBotType': currentBotType,
-      };
+  router.put('/bot-type',
+    accessTokenParser, loginRequiredStrictly, adminRequired, csrf, validator.BotType, apiV3FormValidator, async(req, res) => {
+      const { currentBotType } = req.body;
+
+      await resetAllBotSettings();
+      const requestParams = { 'slackbot:currentBotType': currentBotType };
+
       try {
       try {
         await updateSlackBotSettings(requestParams);
         await updateSlackBotSettings(requestParams);
         crowi.slackBotService.publishUpdatedMessage();
         crowi.slackBotService.publishUpdatedMessage();
 
 
         // TODO Impl to delete AccessToken both of Proxy and GROWI when botType changes.
         // TODO Impl to delete AccessToken both of Proxy and GROWI when botType changes.
-        const customBotWithoutProxySettingParams = {
-          slackSigningSecret: crowi.configManager.getConfig('crowi', 'slackbot:signingSecret'),
-          slackBotToken: crowi.configManager.getConfig('crowi', 'slackbot:token'),
-          slackBotType: crowi.configManager.getConfig('crowi', 'slackbot:currentBotType'),
-        };
-        return res.apiv3({ customBotWithoutProxySettingParams });
+        const slackBotTypeParam = { slackBotType: crowi.configManager.getConfig('crowi', 'slackbot:currentBotType') };
+        return res.apiv3({ slackBotTypeParam });
+      }
+      catch (error) {
+        const msg = 'Error occured in updating Custom bot setting';
+        logger.error('Error', error);
+        return res.apiv3Err(new ErrorV3(msg, 'update-CustomBotSetting-failed'), 500);
+      }
+    });
+
+  /*
+    TODO: add swagger by GW-5930
+  */
+
+  router.delete('/bot-type',
+    accessTokenParser, loginRequiredStrictly, adminRequired, csrf, apiV3FormValidator, async(req, res) => {
+
+      await resetAllBotSettings();
+      const params = { 'slackbot:currentBotType': null };
+
+      try {
+        await updateSlackBotSettings(params);
+        crowi.slackBotService.publishUpdatedMessage();
+
+        // TODO Impl to delete AccessToken both of Proxy and GROWI when botType changes.
+        const slackBotTypeParam = { slackBotType: crowi.configManager.getConfig('crowi', 'slackbot:currentBotType') };
+        return res.apiv3({ slackBotTypeParam });
       }
       }
       catch (error) {
       catch (error) {
         const msg = 'Error occured in updating Custom bot setting';
         const msg = 'Error occured in updating Custom bot setting';