Taichi Masuyama 4 years ago
parent
commit
5ba086068e

+ 53 - 4
packages/app/src/server/routes/apiv3/slack-integration-settings.js

@@ -415,6 +415,19 @@ module.exports = (crowi) => {
         supportedCommandsForBroadcastUse: defaultSupportedCommandsNameForBroadcastUse,
         supportedCommandsForSingleUse: defaultSupportedCommandsNameForSingleUse,
       });
+      // MOCK DATA DELETE THIS GW-6972 ---------------
+      /**
+       * this code represents the creation of new SlackAppIntegration model
+       */
+      const SlackAppIntegrationMock = mongoose.model('SlackAppIntegrationMock');
+      const MOCK = await SlackAppIntegrationMock.create({
+        tokenGtoP,
+        tokenPtoG,
+        supportedCommandsForBroadcastUse: defaultSupportedCommandsNameForBroadcastUse,
+        supportedCommandsForSingleUse: defaultSupportedCommandsNameForSingleUse,
+        permittedChannelsForEachCommand: {},
+      });
+      // MOCK DATA DELETE THIS GW-6972 ---------------
       return res.apiv3(slackAppTokens, 200);
     }
     catch (error) {
@@ -523,17 +536,52 @@ module.exports = (crowi) => {
         { new: true },
       );
 
+      // await requestToProxyServer(
+      //   slackAppIntegration.tokenGtoP,
+      //   'put',
+      //   '/g2s/supported-commands',
+      //   {
+      //     supportedCommandsForBroadcastUse: slackAppIntegration.supportedCommandsForBroadcastUse,
+      //     supportedCommandsForSingleUse: slackAppIntegration.supportedCommandsForSingleUse,
+      //   },
+      // );
+
+      // return res.apiv3({ slackAppIntegration });
+
+      // MOCK DATA DELETE THIS GW-6972 ---------------
+      /**
+       * this code represents the update operation using request from client (slackapp integration settings page)
+       * , then send request to proxy to update cache
+       * permittedChannelsForEachCommandFromClient represents the data sent from client
+       */
+      const SlackAppIntegrationMock = mongoose.model('SlackAppIntegrationMock');
+      // assume that these data were sent from client
+      const permittedChannelsForEachCommandFromClient = {
+        search: ['random'],
+      };
+      const slackAppIntegrationMock = await SlackAppIntegrationMock.findOneAndUpdate(
+        { tokenPtoG: slackAppIntegration.tokenPtoG },
+        {
+          supportedCommandsForBroadcastUse,
+          supportedCommandsForSingleUse,
+          permittedChannelsForEachCommand: permittedChannelsForEachCommandFromClient,
+        },
+        { new: true },
+      );
+
       await requestToProxyServer(
-        slackAppIntegration.tokenGtoP,
+        slackAppIntegrationMock.tokenGtoP,
         'put',
         '/g2s/supported-commands',
         {
-          supportedCommandsForBroadcastUse: slackAppIntegration.supportedCommandsForBroadcastUse,
-          supportedCommandsForSingleUse: slackAppIntegration.supportedCommandsForSingleUse,
+          supportedCommandsForBroadcastUse: slackAppIntegrationMock.supportedCommandsForBroadcastUse,
+          supportedCommandsForSingleUse: slackAppIntegrationMock.supportedCommandsForSingleUse,
+          permittedChannelsForEachCommand: slackAppIntegrationMock.permittedChannelsForEachCommand,
         },
       );
+      // MOCK DATA DELETE THIS GW-6972 ---------------
 
-      return res.apiv3({ slackAppIntegration });
+      return res.apiv3({ slackAppIntegrationMock });
     }
     catch (error) {
       const msg = 'Error occured in updating Custom bot setting';
@@ -578,6 +626,7 @@ module.exports = (crowi) => {
         return res.apiv3Err(new ErrorV3(msg, 'find-slackAppIntegration-failed'), 400);
       }
 
+      // USE MOCK DATA HERE FOR cache creation at /relation-test GW-7021
       const result = await requestToProxyServer(
         slackAppIntegration.tokenGtoP,
         'post',

+ 33 - 2
packages/slackbot-proxy/src/controllers/growi-to-slack.ts

@@ -16,6 +16,7 @@ import { WebclientRes, AddWebclientResponseToRes } from '~/middlewares/slack-to-
 import { GrowiReq } from '~/interfaces/growi-to-slack/growi-req';
 import { InstallationRepository } from '~/repositories/installation';
 import { RelationRepository } from '~/repositories/relation';
+import { RelationMockRepository } from '~/repositories/relation-mock';
 import { OrderRepository } from '~/repositories/order';
 
 import { InstallerService } from '~/services/InstallerService';
@@ -39,6 +40,9 @@ export class GrowiToSlackCtrl {
   @Inject()
   relationRepository: RelationRepository;
 
+  @Inject()
+  relationMockRepository: RelationMockRepository;
+
   @Inject()
   orderRepository: OrderRepository;
 
@@ -97,14 +101,15 @@ export class GrowiToSlackCtrl {
   async putSupportedCommands(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
     // asserted (tokenGtoPs.length > 0) by verifyGrowiToSlackRequest
     const { tokenGtoPs } = req;
-    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = req.body;
+    // const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = req.body;
+    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse, permittedChannelsForEachCommand } = req.body;
 
     if (tokenGtoPs.length !== 1) {
       throw createError(400, 'installation is invalid');
     }
 
     const tokenGtoP = tokenGtoPs[0];
-    const relation = await this.relationRepository.update({ tokenGtoP }, { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse });
+    const relation = await this.relationMockRepository.update({ tokenGtoP }, { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse });
 
     return res.send({ relation });
   }
@@ -207,6 +212,32 @@ export class GrowiToSlackCtrl {
       })
       .execute();
 
+    // MOCK DATA DELETE THIS GW-6972 7004 ---------------
+    /**
+     * this code represents the creation of cache (Relation schema) using request from GROWI
+     */
+    await this.relationMockRepository.createQueryBuilder('relation_mock')
+      .insert()
+      .values({
+        installation: order.installation,
+        tokenGtoP: order.tokenGtoP,
+        tokenPtoG: order.tokenPtoG,
+        growiUri: order.growiUrl,
+        supportedCommandsForBroadcastUse: req.body.supportedCommandsForBroadcastUse,
+        supportedCommandsForSingleUse: req.body.supportedCommandsForSingleUse,
+        permittedChannelsForEachCommand: {
+          search: ['random'],
+        },
+        expiredAtCommands,
+      })
+      // https://github.com/typeorm/typeorm/issues/1090#issuecomment-634391487
+      .orUpdate({
+        conflict_target: ['installation', 'growiUri'],
+        overwrite: ['tokenGtoP', 'tokenPtoG', 'supportedCommandsForBroadcastUse', 'supportedCommandsForSingleUse'],
+      })
+      .execute();
+    // MOCK DATA DELETE THIS GW-6972 7004 ---------------
+
     // Find the generated relation
     const generatedRelation = await this.relationRepository.findOne({ id: response.identifiers[0].id });
 

+ 10 - 0
packages/slackbot-proxy/src/repositories/relation-mock.ts

@@ -0,0 +1,10 @@
+import {
+  Repository, EntityRepository,
+} from 'typeorm';
+
+import { RelationMock } from '~/entities/relation-mock';
+
+@EntityRepository(RelationMock)
+export class RelationMockRepository extends Repository<RelationMock> {
+
+}

+ 52 - 11
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -5,6 +5,9 @@ import { addHours } from 'date-fns';
 import { Relation } from '~/entities/relation';
 import { RelationRepository } from '~/repositories/relation';
 
+import { RelationMock } from '~/entities/relation-mock';
+import { RelationMockRepository } from '~/repositories/relation-mock';
+
 import loggerFactory from '~/utils/logger';
 
 const logger = loggerFactory('slackbot-proxy:services:RelationsService');
@@ -15,6 +18,17 @@ export class RelationsService {
   @Inject()
   relationRepository: RelationRepository;
 
+  @Inject()
+  relationMockRepository: RelationMockRepository;
+
+  // DELETE THIS METHOD GW-6972
+  async getMockFromRelation(relation: Relation): Promise<RelationMock|null> {
+    const tokenGtoP = relation.tokenGtoP;
+    const relationMock = await this.relationMockRepository.findOne({ where: [{ tokenGtoP }] });
+
+    return relationMock || null;
+  }
+
   async getSupportedGrowiCommands(relation:Relation):Promise<any> {
     // generate API URL
     const url = new URL('/_api/v3/slack-integration/supported-commands', relation.growiUri);
@@ -25,22 +39,49 @@ export class RelationsService {
     });
   }
 
-  async syncSupportedGrowiCommands(relation:Relation): Promise<Relation> {
+  async syncSupportedGrowiCommands(relation:Relation): Promise<RelationMock> {
     const res = await this.getSupportedGrowiCommands(relation);
-    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
-    relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
-    relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
-    relation.expiredAtCommands = addHours(new Date(), 48);
+    // const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
+    // relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
+    // relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
+    // relation.expiredAtCommands = addHours(new Date(), 48);
 
-    return this.relationRepository.save(relation);
+    // return this.relationRepository.save(relation);
+
+    // MOCK DATA MODIFY THIS GW-6972 ---------------
+    /**
+     * this code represents the update of cache (Relation schema) using request from GROWI
+     */
+    const relationMock = await this.getMockFromRelation(relation);
+    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse, permittedChannelsForEachCommand } = res.data;
+    if (relationMock !== null) {
+      relationMock.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
+      relationMock.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
+      relationMock.permittedChannelsForEachCommand = permittedChannelsForEachCommand;
+      relationMock.expiredAtCommands = addHours(new Date(), 48);
+
+      // MODIFY THIS ORIGINAL OPERATION GW-6972
+      relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
+      relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
+      relation.expiredAtCommands = addHours(new Date(), 48);
+      this.relationRepository.save(relation);
+
+      return this.relationMockRepository.save(relationMock);
+    }
+    throw Error('No relation mock is null.');
+    // MOCK DATA MODIFY THIS GW-6972 ---------------
   }
 
-  async syncRelation(relation:Relation, baseDate:Date):Promise<Relation|null> {
-    const distanceMillisecondsToExpiredAt = relation.getDistanceInMillisecondsToExpiredAt(baseDate);
+  // MODIFY THIS METHOD USING ORIGINAL RELATION MODEL GW-6972
+  async syncRelation(relation:Relation, baseDate:Date):Promise<RelationMock|null> {
+    const relationMock = await this.getMockFromRelation(relation);
+    if (relationMock == null) return null;
+
+    const distanceMillisecondsToExpiredAt = relationMock.getDistanceInMillisecondsToExpiredAt(baseDate);
 
     if (distanceMillisecondsToExpiredAt < 0) {
       try {
-        return await this.syncSupportedGrowiCommands(relation);
+        return await this.syncSupportedGrowiCommands(relationMock);
       }
       catch (err) {
         logger.error(err);
@@ -51,14 +92,14 @@ export class RelationsService {
     // 24 hours
     if (distanceMillisecondsToExpiredAt < 1000 * 60 * 60 * 24) {
       try {
-        this.syncSupportedGrowiCommands(relation);
+        this.syncSupportedGrowiCommands(relationMock);
       }
       catch (err) {
         logger.error(err);
       }
     }
 
-    return relation;
+    return relationMock;
   }
 
   async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {