itizawa пре 4 година
родитељ
комит
d22bc80a4b
1 измењених фајлова са 62 додато и 0 уклоњено
  1. 62 0
      src/server/routes/apiv3/slack-integration-settings.js

+ 62 - 0
src/server/routes/apiv3/slack-integration-settings.js

@@ -59,6 +59,9 @@ module.exports = (crowi) => {
       body('channel').trim().not().isEmpty()
       body('channel').trim().not().isEmpty()
         .isString(),
         .isString(),
     ],
     ],
+    RelationTest: [
+      body('slackappintegrationsId').isMongoId(),
+    ],
   };
   };
 
 
   async function resetAllBotSettings() {
   async function resetAllBotSettings() {
@@ -91,6 +94,18 @@ module.exports = (crowi) => {
     return result.data;
     return result.data;
   }
   }
 
 
+  async function postRelationTest(token) {
+    const proxyUri = crowi.configManager.getConfig('crowi', 'slackbot:serverUri');
+
+    const result = await axios.get(urljoin(proxyUri, '/g2s/relations-test'), {
+      headers: {
+        'x-growi-gtop-tokens': token,
+      },
+    });
+
+    return result.data;
+  }
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *
@@ -383,5 +398,52 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
+  /**
+   * @swagger
+   *
+   *    /slack-integration-settings/with-proxy/relation-test:
+   *      post:
+   *        tags: [botType]
+   *        operationId: deleteBotType
+   *        summary: /slack-integration/bot-type
+   *        description: Delete botType setting.
+   *        requestBody:
+   *          content:
+   *            application/json:
+   *              schema:
+   *                description: slack app integration id for test
+   *                properties:
+   *                  slackappintegrationsId:
+   *                    type: string
+   *        responses:
+   *           200:
+   *             description: Succeeded to delete botType setting.
+   */
+  router.post('/with-proxy/relation-test', loginRequiredStrictly, adminRequired, csrf, validator.RelationTest, apiV3FormValidator, async(req, res) => {
+    const currentBotType = crowi.configManager.getConfig('crowi', 'slackbot:currentBotType');
+    if (currentBotType === 'customBotWithoutProxy') {
+      const msg = 'Not Proxy Type';
+      return res.apiv3Err(new ErrorV3(msg, 'not-proxy-type'), 400);
+    }
+
+    const { slackappintegrationsId } = req.body;
+
+    try {
+      const slackAppIntegration = await SlackAppIntegration.findOne({ _id: slackappintegrationsId });
+      if (slackAppIntegration == null) {
+        const msg = 'Could not find SlackAppIntegration by id';
+        return res.apiv3Err(new ErrorV3(msg, 'find-slackAppIntegration-failed'), 400);
+      }
+      const response = await postRelationTest(slackAppIntegration.tokenGtoP);
+
+      return res.apiv3({ connectionStatuses: response.connectionStatuses });
+    }
+    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);
+    }
+  });
+
   return router;
   return router;
 };
 };