Browse Source

WIP: save installation

Yuki Takei 5 years ago
parent
commit
d0c44d5e38

+ 23 - 4
packages/slackbot-proxy/src/Server.ts

@@ -1,4 +1,4 @@
-import { Configuration, Inject } from '@tsed/di';
+import { Configuration, Inject, InjectorService } from '@tsed/di';
 import { PlatformApplication } from '@tsed/common';
 import '@tsed/platform-express'; // /!\ keep this import
 import bodyParser from 'body-parser';
@@ -6,15 +6,23 @@ import compress from 'compression';
 import cookieParser from 'cookie-parser';
 import methodOverride from 'method-override';
 import '@tsed/swagger';
-import '@tsed/typeorm';
+import { TypeORMService } from '@tsed/typeorm';
 import { ConnectionOptions } from 'typeorm';
 
+
 export const rootDir = __dirname;
 
 const connectionOptions: ConnectionOptions = {
   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;
+
+
 @Configuration({
   rootDir,
   acceptMimes: ['application/json'],
@@ -33,13 +41,13 @@ const connectionOptions: ConnectionOptions = {
     {
       ...connectionOptions,
       entities: [
-        `${rootDir}/entity/*{.ts,.js}`,
+        `${rootDir}/entities/*{.ts,.js}`,
       ],
       migrations: [
         `${rootDir}/migrations/*{.ts,.js}`,
       ],
       subscribers: [
-        `${rootDir}/subscriber/*{.ts,.js}`,
+        `${rootDir}/subscribers/*{.ts,.js}`,
       ],
     } as ConnectionOptions,
   ],
@@ -61,6 +69,9 @@ export class Server {
   @Configuration()
   settings: Configuration;
 
+  @Inject()
+  injector: InjectorService;
+
   $beforeRoutesInit(): void {
     this.app
       .use(cookieParser())
@@ -72,4 +83,12 @@ 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);
+  }
+
 }

+ 23 - 1
packages/slackbot-proxy/src/controllers/slack.ts

@@ -1,16 +1,38 @@
 import {
-  BodyParams, Controller, Get, Post, Req, Res,
+  BodyParams, Controller, Get, Inject, Post, Req, Res,
 } from '@tsed/common';
+import { Installation } from '~/entities/installation';
+import { InstallationRepository } from '~/repositories/installation';
 
 import { InstallerService } from '~/services/InstallerService';
 
 @Controller('/slack')
 export class SlackCtrl {
 
+  @Inject()
+  installationRepository: InstallationRepository;
+
   // eslint-disable-next-line no-useless-constructor
   constructor(private readonly installerService: InstallerService) {
   }
 
+  @Get('/testsave')
+  testsave(): void {
+    const installation = new Installation();
+    installation.data = {
+      team: undefined,
+      enterprise: undefined,
+      user: {
+        id: '',
+        token: undefined,
+        scopes: undefined,
+      },
+    };
+
+    this.installationRepository.save(installation);
+  }
+
+
   @Get('/install')
   async install(): Promise<string> {
     const url = await this.installerService.installer.generateInstallUrl({

+ 36 - 20
packages/slackbot-proxy/src/services/InstallerService.ts

@@ -1,32 +1,23 @@
 import {
-  Installation, InstallationQuery, InstallationStore, InstallProvider,
+  Installation as SlackInstallation, InstallationQuery, InstallProvider,
 } from '@slack/oauth';
 import { Service } from '@tsed/di';
 
-
-const installationStore: InstallationStore = {
-  storeInstallation: async(installation: Installation<'v1' | 'v2', boolean>) => {
-    console.log({ installation });
-  },
-  fetchInstallation: async(installQuery: InstallationQuery<boolean>) => {
-    const installation: Installation<'v1' | 'v2', boolean> = {
-      team: undefined,
-      enterprise: undefined,
-      user: {
-        id: '',
-        token: undefined,
-        scopes: undefined,
-      },
-    };
-    return installation;
-  },
-};
+import { Installation } from '~/entities/installation';
+import { InstallationRepository } from '~/repositories/installation';
 
 @Service()
 export class InstallerService {
 
   installer: InstallProvider;
 
+  repository: InstallationRepository;
+
+  // eslint-disable-next-line no-useless-constructor
+  constructor(repository: InstallationRepository) {
+    this.repository = repository;
+  }
+
   $onInit(): Promise<any> | void {
     const clientId = process.env.SLACK_CLIENT_ID;
     const clientSecret = process.env.SLACK_CLIENT_SECRET;
@@ -39,11 +30,36 @@ export class InstallerService {
       throw new Error('The environment variable \'SLACK_CLIENT_SECRET\' must be defined.');
     }
 
+    const { repository } = this;
+
     this.installer = new InstallProvider({
       clientId,
       clientSecret,
       stateSecret,
-      installationStore,
+      installationStore: {
+        storeInstallation: async(slackInstallation: SlackInstallation<'v1' | 'v2', boolean>) => {
+          console.log({ slackInstallation });
+
+          const installation = new Installation();
+          installation.data = slackInstallation;
+
+          await repository.save(installation);
+
+          return;
+        },
+        fetchInstallation: async(installQuery: InstallationQuery<boolean>) => {
+          const installation: SlackInstallation<'v1' | 'v2', boolean> = {
+            team: undefined,
+            enterprise: undefined,
+            user: {
+              id: '',
+              token: undefined,
+              scopes: undefined,
+            },
+          };
+          return installation;
+        },
+      },
     });
   }