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

Changed mysql(typeorm) column type to timestamp

Taichi Masuyama 4 лет назад
Родитель
Сommit
4feb8de894

+ 2 - 2
packages/slackbot-proxy/src/controllers/growi-to-slack.ts

@@ -3,6 +3,7 @@ import {
 } from '@tsed/common';
 import axios from 'axios';
 import createError from 'http-errors';
+import { addHours } from 'date-fns';
 
 import { WebAPICallResult } from '@slack/web-api';
 
@@ -185,8 +186,7 @@ export class GrowiToSlackCtrl {
     logger.debug('relation test is success', order);
 
     // temporary cache for 48 hours
-    const nowDate = new Date();
-    const expiredAtCommands = nowDate.getTime() + 48 * 60 * 60 * 1000;
+    const expiredAtCommands = addHours(new Date(), 48);
 
     // Transaction is not considered because it is used infrequently,
     const response = await this.relationRepository.createQueryBuilder('relation')

+ 3 - 2
packages/slackbot-proxy/src/entities/relation-mock.ts

@@ -1,3 +1,4 @@
+import { differenceInMilliseconds } from 'date-fns';
 import {
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
 } from 'typeorm';
@@ -50,10 +51,10 @@ export class RelationMock {
   permittedChannelsForEachCommand : PermittedChannelsForEachCommand
 
   @Column({ type: 'timestamp' })
-  expiredAtCommands: number;
+  expiredAtCommands: Date;
 
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
-    return this.expiredAtCommands - baseDate.getTime();
+    return differenceInMilliseconds(this.expiredAtCommands, baseDate);
   }
 
 }

+ 2 - 2
packages/slackbot-proxy/src/entities/relation.ts

@@ -38,10 +38,10 @@ export class Relation {
   supportedCommandsForSingleUse: string[];
 
   @Column({ type: 'timestamp' })
-  expiredAtCommands: number;
+  expiredAtCommands: Date;
 
   getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
-    // differenceInMilliseconds uses Date.getTime() internally
+    // differenceInMilliseconds uses Date.prototype.getTime() internally
     return differenceInMilliseconds(this.expiredAtCommands, baseDate);
   }
 

+ 2 - 2
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -1,5 +1,6 @@
 import { Inject, Service } from '@tsed/di';
 import axios from 'axios';
+import { addHours } from 'date-fns';
 
 import { Relation } from '~/entities/relation';
 import { RelationRepository } from '~/repositories/relation';
@@ -29,8 +30,7 @@ export class RelationsService {
     const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
     relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
     relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
-    const nowDate = new Date();
-    relation.expiredAtCommands = nowDate.getTime() + 48 * 60 * 60 * 1000;
+    relation.expiredAtCommands = addHours(new Date(), 48);
 
     return this.relationRepository.save(relation);
   }