2
0
Эх сурвалжийг харах

Merge branch 'feat/growi-bot-proxy' into feat/create-modal-after-typing-growi-register

zahmis 5 жил өмнө
parent
commit
f78718d920

+ 5 - 6
packages/slackbot-proxy/src/Server.ts

@@ -13,13 +13,15 @@ import { ConnectionOptions } from 'typeorm';
 export const rootDir = __dirname;
 
 const connectionOptions: ConnectionOptions = {
+  // The 'name' property must be set. Otherwise, the 'name' will be '0' and won't work well. -- 2021.04.05 Yuki Takei
+  // see: https://github.com/TypedProject/tsed/blob/7630cda20a1f6fa3a692ecc3e6cd51d37bc3c45f/packages/typeorm/src/utils/createConnection.ts#L10
+  name: 'default',
   type: process.env.TYPEORM_CONNECTION,
   host: process.env.TYPEORM_HOST,
   database: process.env.TYPEORM_DATABASE,
   username: process.env.TYPEORM_USERNAME,
   password: process.env.TYPEORM_PASSWORD,
   synchronize: true,
-  logging: true,
 } as ConnectionOptions;
 
 
@@ -84,11 +86,8 @@ export class Server {
   }
 
   async $onReady(): Promise<void> {
-    const typeormService = this.injector.get<TypeORMService>(TypeORMService);
-    console.log(typeormService);
-
-    const connection = typeormService?.connectionManager.get('0');
-    console.log(connection);
+    // for synchromizing when boot
+    this.injector.get<TypeORMService>(TypeORMService);
   }
 
 }

+ 6 - 5
packages/slackbot-proxy/src/controllers/slack.ts

@@ -4,19 +4,18 @@ import {
 import { parse } from '@growi/slack/src/utils/slash-command-parser';
 import { Installation } from '~/entities/installation';
 import { InstallationRepository } from '~/repositories/installation';
-
 import { InstallerService } from '~/services/InstallerService';
 import { RegisterService } from '~/services/RegisterService';
 
+
 @Controller('/slack')
 export class SlackCtrl {
 
   @Inject()
-  installationRepository: InstallationRepository;
+  installerService: InstallerService;
 
-  // eslint-disable-next-line no-useless-constructor
-  constructor(private readonly installerService: InstallerService) {
-  }
+  @Inject()
+  installationRepository: InstallationRepository;
 
   @Inject()
   registerService: RegisterService
@@ -35,6 +34,8 @@ export class SlackCtrl {
       },
     };
 
+    // const installationRepository = getRepository(Installation);
+
     this.installationRepository.save(installation);
   }
 

+ 26 - 4
packages/slackbot-proxy/src/entities/installation.ts

@@ -1,8 +1,8 @@
 import {
-  Property, Required,
+  Required,
 } from '@tsed/schema';
 import {
-  Column, Entity, PrimaryGeneratedColumn,
+  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn,
 } from 'typeorm';
 
 import { Installation as SlackInstallation } from '@slack/oauth';
@@ -11,11 +11,33 @@ import { Installation as SlackInstallation } from '@slack/oauth';
 export class Installation {
 
   @PrimaryGeneratedColumn()
-  @Property()
-  id: number;
+  readonly id: number;
 
   @Column({ type: 'json' })
   @Required()
   data: SlackInstallation;
 
+  @CreateDateColumn()
+  readonly createdAt: Date;
+
+  @UpdateDateColumn()
+  readonly updatedAt: Date;
+
+  @Column({ nullable: true })
+  isEnterpriseInstall?: boolean;
+
+  @Column({ nullable: true, unique: true })
+  teamId?: string;
+
+  @Column({ nullable: true, unique: true })
+  enterpriseId?: string;
+
+  setData(slackInstallation: SlackInstallation): void {
+    this.data = slackInstallation;
+
+    this.isEnterpriseInstall = slackInstallation.isEnterpriseInstall;
+    this.teamId = slackInstallation.team?.id;
+    this.enterpriseId = slackInstallation.enterprise?.id;
+  }
+
 }

+ 4 - 10
packages/slackbot-proxy/src/services/InstallerService.ts

@@ -1,7 +1,7 @@
 import {
   Installation as SlackInstallation, InstallationQuery, InstallProvider,
 } from '@slack/oauth';
-import { Service } from '@tsed/di';
+import { Inject, Service } from '@tsed/di';
 
 import { Installation } from '~/entities/installation';
 import { InstallationRepository } from '~/repositories/installation';
@@ -11,12 +11,8 @@ export class InstallerService {
 
   installer: InstallProvider;
 
-  repository: InstallationRepository;
-
-  // eslint-disable-next-line no-useless-constructor
-  constructor(repository: InstallationRepository) {
-    this.repository = repository;
-  }
+  @Inject()
+  private readonly repository: InstallationRepository;
 
   $onInit(): Promise<any> | void {
     const clientId = process.env.SLACK_CLIENT_ID;
@@ -38,10 +34,8 @@ export class InstallerService {
       stateSecret,
       installationStore: {
         storeInstallation: async(slackInstallation: SlackInstallation<'v1' | 'v2', boolean>) => {
-          console.log({ slackInstallation });
-
           const installation = new Installation();
-          installation.data = slackInstallation;
+          installation.setData(slackInstallation);
 
           await repository.save(installation);