|
|
@@ -4,14 +4,19 @@ import {
|
|
|
|
|
|
import { WebAPICallResult } from '@slack/web-api';
|
|
|
|
|
|
-import { verifyGrowiToSlackRequest, getConnectionStatuses } from '@growi/slack';
|
|
|
+import { verifyGrowiToSlackRequest, getConnectionStatuses, relationTestToSlack } from '@growi/slack';
|
|
|
|
|
|
import { GrowiReq } from '~/interfaces/growi-to-slack/growi-req';
|
|
|
import { InstallationRepository } from '~/repositories/installation';
|
|
|
import { RelationRepository } from '~/repositories/relation';
|
|
|
+import { OrderRepository } from '~/repositories/order';
|
|
|
+
|
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
+import { Relation } from '~/entities/relation';
|
|
|
+import { Order } from '~/entities/order';
|
|
|
+
|
|
|
|
|
|
const logger = loggerFactory('slackbot-proxy:controllers:growi-to-slack');
|
|
|
|
|
|
@@ -28,6 +33,9 @@ export class GrowiToSlackCtrl {
|
|
|
@Inject()
|
|
|
relationRepository: RelationRepository;
|
|
|
|
|
|
+ @Inject()
|
|
|
+ orderRepository: OrderRepository;
|
|
|
+
|
|
|
@Get('/connection-status')
|
|
|
@UseBefore(verifyGrowiToSlackRequest)
|
|
|
async getConnectionStatuses(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
|
|
|
@@ -52,4 +60,63 @@ export class GrowiToSlackCtrl {
|
|
|
return res.send({ connectionStatuses });
|
|
|
}
|
|
|
|
|
|
+ @Get('/relation-test')
|
|
|
+ @UseBefore(verifyGrowiToSlackRequest)
|
|
|
+ async postRelation(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
|
|
|
+ const { tokenGtoPs } = req;
|
|
|
+
|
|
|
+ if (tokenGtoPs.length !== 1) {
|
|
|
+ return res.status(400).send({ message: 'installation is invalid' });
|
|
|
+ }
|
|
|
+
|
|
|
+ const tokenGtoP = tokenGtoPs[0];
|
|
|
+
|
|
|
+ // retrieve relation with Installation
|
|
|
+ const relation = await this.relationRepository.createQueryBuilder('relation')
|
|
|
+ .where('tokenGtoP = :token', { token: tokenGtoP })
|
|
|
+ .leftJoinAndSelect('relation.installation', 'installation')
|
|
|
+ .getOne();
|
|
|
+
|
|
|
+ // Returns the result of the test if it already exists
|
|
|
+ if (relation != null) {
|
|
|
+ logger.debug('relation found', relation);
|
|
|
+
|
|
|
+ const token = relation.installation.data.bot?.token;
|
|
|
+ if (token == null) {
|
|
|
+ return res.status(400).send({ message: 'installation is invalid' });
|
|
|
+ }
|
|
|
+
|
|
|
+ await relationTestToSlack(token);
|
|
|
+ return res.send({ relation });
|
|
|
+ }
|
|
|
+
|
|
|
+ // retrieve latest Order with Installation
|
|
|
+ const order = await this.orderRepository.createQueryBuilder('order')
|
|
|
+ .orderBy('order.createdAt', 'DESC')
|
|
|
+ .where('growiAccessToken = :token', { token: tokenGtoP })
|
|
|
+ .leftJoinAndSelect('order.installation', 'installation')
|
|
|
+ .getOne();
|
|
|
+
|
|
|
+ if (order == null || order.isExpired()) {
|
|
|
+ return res.status(400).send({ message: 'order has expired or does not exist.' });
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.debug('order found', order);
|
|
|
+
|
|
|
+ const token = order.installation.data.bot?.token;
|
|
|
+ if (token == null) {
|
|
|
+ return res.status(400).send({ message: 'installation is invalid' });
|
|
|
+ }
|
|
|
+
|
|
|
+ await relationTestToSlack(token);
|
|
|
+
|
|
|
+ logger.debug('relation test is success', order);
|
|
|
+
|
|
|
+ // TODO GW-5864 issue relation
|
|
|
+
|
|
|
+ // return order temporary
|
|
|
+ // TODO return new relation
|
|
|
+ return res.send({ order });
|
|
|
+ }
|
|
|
+
|
|
|
}
|