|
|
@@ -10,9 +10,13 @@ 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');
|
|
|
|
|
|
@@ -62,14 +66,23 @@ export class GrowiToSlackCtrl {
|
|
|
// check validation by verifyGrowiToSlackRequest
|
|
|
const { tokenGtoP } = req;
|
|
|
|
|
|
- // retrieve relation with Installation
|
|
|
- const relation = await this.relationRepository.createQueryBuilder('relation')
|
|
|
- .where('tokenGtoP = :token', { token: tokenGtoP })
|
|
|
- .leftJoinAndSelect('relation.installation', 'installation')
|
|
|
- .getOne();
|
|
|
+ let relation: Relation|undefined;
|
|
|
+ try {
|
|
|
+ // retrieve relation with Installation
|
|
|
+ relation = await this.relationRepository.createQueryBuilder('relation')
|
|
|
+ .where('tokenGtoP = :token', { token: tokenGtoP })
|
|
|
+ .leftJoinAndSelect('relation.installation', 'installation')
|
|
|
+ .getOne();
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ logger.error(error);
|
|
|
+ return res.status(500).send({ message: 'find relation is failure' });
|
|
|
+ }
|
|
|
|
|
|
// 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' });
|
|
|
@@ -79,46 +92,54 @@ export class GrowiToSlackCtrl {
|
|
|
return res.send({ relation });
|
|
|
}
|
|
|
catch (error) {
|
|
|
+ logger.error(error);
|
|
|
return res.status(500).send({ message: 'relation test is failure' });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ let order: Order|undefined;
|
|
|
+ try {
|
|
|
// 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();
|
|
|
-
|
|
|
- console.log(order);
|
|
|
-
|
|
|
+ order = await this.orderRepository.createQueryBuilder('order')
|
|
|
+ .orderBy('order.createdAt', 'DESC')
|
|
|
+ .where('growiAccessToken = :token', { token: tokenGtoP })
|
|
|
+ .leftJoinAndSelect('order.installation', 'installation')
|
|
|
+ .getOne();
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ logger.error(error);
|
|
|
+ return res.status(500).send({ message: 'find order is failure' });
|
|
|
+ }
|
|
|
|
|
|
- // if (order == null || order.isExpired()) {
|
|
|
- // return 'order has expired or does not exist.';
|
|
|
- // }
|
|
|
+ if (order == null || order.isExpired()) {
|
|
|
+ return res.status(400).send({ message: 'order has expired or does not exist.' });
|
|
|
+ }
|
|
|
|
|
|
- // logger.debug('order found', order);
|
|
|
+ logger.debug('order found', order);
|
|
|
|
|
|
- // const token = order.installation?.data?.bot?.token;
|
|
|
- // if (token == null) {
|
|
|
- // return 'token does not exist.';
|
|
|
- // }
|
|
|
+ const token = order.installation.data.bot?.token;
|
|
|
+ if (token == null) {
|
|
|
+ return res.status(400).send({ message: 'installation is invalid' });
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ await relationTestToSlack(token);
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ logger.error(error);
|
|
|
+ return res.status(500).send({ message: 'relation test is failure' });
|
|
|
+ }
|
|
|
|
|
|
- // const connectionStatuses = await getConnectionStatuses([token]);
|
|
|
- // if (connectionStatuses[token].workspaceName == null) {
|
|
|
- // return 'connection test failed.';
|
|
|
- // }
|
|
|
+ logger.debug('relation test is success', order);
|
|
|
|
|
|
- // logger.debug('retrieve WS name', connectionStatuses[token].workspaceName);
|
|
|
+ try {
|
|
|
+ // TODO GW-5864 issue relation
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ logger.error(error);
|
|
|
+ return res.status(500).send({ message: 'relation test is failure' });
|
|
|
+ }
|
|
|
|
|
|
- // TODO GW-5864 issue relation
|
|
|
- // try {
|
|
|
- // await relationTestToSlack(token);
|
|
|
- // }
|
|
|
- // catch (error) {
|
|
|
- // console.log(error);
|
|
|
- // }
|
|
|
- return res.send({ tokenGtoP });
|
|
|
+ return res.send({ order });
|
|
|
}
|
|
|
|
|
|
}
|