|
@@ -5,6 +5,9 @@ import { addHours } from 'date-fns';
|
|
|
import { Relation } from '~/entities/relation';
|
|
import { Relation } from '~/entities/relation';
|
|
|
import { RelationRepository } from '~/repositories/relation';
|
|
import { RelationRepository } from '~/repositories/relation';
|
|
|
|
|
|
|
|
|
|
+import { RelationMock } from '~/entities/relation-mock';
|
|
|
|
|
+import { RelationMockRepository } from '~/repositories/relation-mock';
|
|
|
|
|
+
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
const logger = loggerFactory('slackbot-proxy:services:RelationsService');
|
|
const logger = loggerFactory('slackbot-proxy:services:RelationsService');
|
|
@@ -15,6 +18,17 @@ export class RelationsService {
|
|
|
@Inject()
|
|
@Inject()
|
|
|
relationRepository: RelationRepository;
|
|
relationRepository: RelationRepository;
|
|
|
|
|
|
|
|
|
|
+ @Inject()
|
|
|
|
|
+ relationMockRepository: RelationMockRepository;
|
|
|
|
|
+
|
|
|
|
|
+ // MOCK DATA DELETE THIS METHOD GW-6972
|
|
|
|
|
+ async getMockFromRelation(relation: Relation): Promise<RelationMock|null> {
|
|
|
|
|
+ const tokenGtoP = relation.tokenGtoP;
|
|
|
|
|
+ const relationMock = await this.relationMockRepository.findOne({ where: [{ tokenGtoP }] });
|
|
|
|
|
+
|
|
|
|
|
+ return relationMock || null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async getSupportedGrowiCommands(relation:Relation):Promise<any> {
|
|
async getSupportedGrowiCommands(relation:Relation):Promise<any> {
|
|
|
// generate API URL
|
|
// generate API URL
|
|
|
const url = new URL('/_api/v3/slack-integration/supported-commands', relation.growiUri);
|
|
const url = new URL('/_api/v3/slack-integration/supported-commands', relation.growiUri);
|
|
@@ -25,22 +39,49 @@ export class RelationsService {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async syncSupportedGrowiCommands(relation:Relation): Promise<Relation> {
|
|
|
|
|
|
|
+ async syncSupportedGrowiCommands(relation:Relation): Promise<RelationMock> {
|
|
|
const res = await this.getSupportedGrowiCommands(relation);
|
|
const res = await this.getSupportedGrowiCommands(relation);
|
|
|
- const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
|
|
|
|
|
- relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
|
|
|
|
|
- relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
|
|
|
|
|
- relation.expiredAtCommands = addHours(new Date(), 48);
|
|
|
|
|
|
|
+ // const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
|
|
|
|
|
+ // relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
|
|
|
|
|
+ // relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
|
|
|
|
|
+ // relation.expiredAtCommands = addHours(new Date(), 48);
|
|
|
|
|
|
|
|
- return this.relationRepository.save(relation);
|
|
|
|
|
|
|
+ // return this.relationRepository.save(relation);
|
|
|
|
|
+
|
|
|
|
|
+ // MOCK DATA MODIFY THIS GW-6972 ---------------
|
|
|
|
|
+ /**
|
|
|
|
|
+ * this code represents the update of cache (Relation schema) using request from GROWI
|
|
|
|
|
+ */
|
|
|
|
|
+ const relationMock = await this.getMockFromRelation(relation);
|
|
|
|
|
+ const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse, permittedChannelsForEachCommand } = res.data;
|
|
|
|
|
+ if (relationMock !== null) {
|
|
|
|
|
+ relationMock.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
|
|
|
|
|
+ relationMock.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
|
|
|
|
|
+ relationMock.permittedChannelsForEachCommand = permittedChannelsForEachCommand;
|
|
|
|
|
+ relationMock.expiredAtCommands = addHours(new Date(), 48);
|
|
|
|
|
+
|
|
|
|
|
+ // NOT MOCK DATA MODIFY THIS ORIGINAL OPERATION GW-6972
|
|
|
|
|
+ relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
|
|
|
|
|
+ relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
|
|
|
|
|
+ relation.expiredAtCommands = addHours(new Date(), 48);
|
|
|
|
|
+ this.relationRepository.save(relation);
|
|
|
|
|
+
|
|
|
|
|
+ return this.relationMockRepository.save(relationMock);
|
|
|
|
|
+ }
|
|
|
|
|
+ throw Error('No relation mock exists.');
|
|
|
|
|
+ // MOCK DATA MODIFY THIS GW-6972 ---------------
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async syncRelation(relation:Relation, baseDate:Date):Promise<Relation|null> {
|
|
|
|
|
- const distanceMillisecondsToExpiredAt = relation.getDistanceInMillisecondsToExpiredAt(baseDate);
|
|
|
|
|
|
|
+ // MODIFY THIS METHOD USING ORIGINAL RELATION MODEL GW-6972
|
|
|
|
|
+ async syncRelation(relation:Relation, baseDate:Date):Promise<RelationMock|null> {
|
|
|
|
|
+ const relationMock = await this.getMockFromRelation(relation);
|
|
|
|
|
+ if (relationMock == null) return null;
|
|
|
|
|
+
|
|
|
|
|
+ const distanceMillisecondsToExpiredAt = relationMock.getDistanceInMillisecondsToExpiredAt(baseDate);
|
|
|
|
|
|
|
|
if (distanceMillisecondsToExpiredAt < 0) {
|
|
if (distanceMillisecondsToExpiredAt < 0) {
|
|
|
try {
|
|
try {
|
|
|
- return await this.syncSupportedGrowiCommands(relation);
|
|
|
|
|
|
|
+ return await this.syncSupportedGrowiCommands(relationMock);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|
|
@@ -51,14 +92,14 @@ export class RelationsService {
|
|
|
// 24 hours
|
|
// 24 hours
|
|
|
if (distanceMillisecondsToExpiredAt < 1000 * 60 * 60 * 24) {
|
|
if (distanceMillisecondsToExpiredAt < 1000 * 60 * 60 * 24) {
|
|
|
try {
|
|
try {
|
|
|
- this.syncSupportedGrowiCommands(relation);
|
|
|
|
|
|
|
+ this.syncSupportedGrowiCommands(relationMock);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return relation;
|
|
|
|
|
|
|
+ return relationMock;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
|
|
async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
|