Просмотр исходного кода

Merge pull request #3595 from weseek/feat/5544-5554-implement-order-model

Feat/5544 5554 implement order model
itizawa 5 лет назад
Родитель
Сommit
0f84979758

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

@@ -3,7 +3,10 @@ import {
 } from '@tsed/common';
 
 import { Installation } from '~/entities/installation';
+import { Order } from '~/entities/order';
+
 import { InstallationRepository } from '~/repositories/installation';
+import { OrderRepository } from '~/repositories/order';
 import { InstallerService } from '~/services/InstallerService';
 import { ReceiveService } from '~/services/RecieveService';
 
@@ -17,6 +20,9 @@ export class SlackCtrl {
   @Inject()
   installationRepository: InstallationRepository;
 
+  @Inject()
+  orderRepository: OrderRepository;
+
   @Inject()
   receiveService: ReceiveService;
 
@@ -60,7 +66,7 @@ export class SlackCtrl {
   }
 
   @Post('/events')
-  handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): string {
+  async handleEvent(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<string> {
     // Send response immediately to avoid opelation_timeout error
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
 
@@ -68,7 +74,26 @@ export class SlackCtrl {
     console.log('Controller/events', slackInput);
     res.send();
 
+    const installation = await this.installationRepository.findByID('1');
+    if (installation == null) {
+      throw new Error('installation is reqiured');
+    }
+
+    // Find the latest order by installationId
+    let order = await this.orderRepository.findOne({
+      installation: installation.id,
+    }, {
+      order: {
+        createdAt: 'DESC',
+      },
+    });
+
+    if (order == null || order.isExpired()) {
+      order = await this.orderRepository.save({ installation: installation.id });
+    }
+
     console.log('body', body);
+    console.log('order', order);
 
     return 'This action will be handled by bolt service.';
   }

+ 2 - 1
packages/slackbot-proxy/src/entities/installation.ts

@@ -2,10 +2,11 @@ import {
   Required,
 } from '@tsed/schema';
 import {
-  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn,
+  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, OneToMany,
 } from 'typeorm';
 
 import { Installation as SlackInstallation } from '@slack/oauth';
+import { Order } from './order';
 
 @Entity()
 export class Installation {

+ 38 - 0
packages/slackbot-proxy/src/entities/order.ts

@@ -0,0 +1,38 @@
+import {
+  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne,
+} from 'typeorm';
+import { Installation } from './installation';
+
+@Entity()
+export class Order {
+
+  @PrimaryGeneratedColumn()
+  readonly id: number;
+
+  @CreateDateColumn()
+  readonly createdAt: Date;
+
+  @UpdateDateColumn()
+  readonly updatedAt: Date;
+
+  @ManyToOne(() => Installation)
+  readonly installation: number;
+
+  @Column({ nullable: true, default: false })
+  isCompleted?: boolean;
+
+  @Column({ nullable: true })
+  growiUrl?: string;
+
+  @Column({ nullable: true })
+  growiAccessToken?: string;
+
+  @Column({ nullable: true })
+  proxyAccessToken?: string;
+
+  isExpired():boolean {
+    // TODO GW-5555 implement this
+    return false;
+  }
+
+}

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

@@ -0,0 +1,10 @@
+import {
+  Repository, EntityRepository,
+} from 'typeorm';
+
+import { Order } from '~/entities/order';
+
+@EntityRepository(Order)
+export class OrderRepository extends Repository<Order> {
+
+}