|
| 1 | +import { ConnectionTypeTestEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js'; |
1 | 2 | import { nanoid } from 'nanoid'; |
2 | 3 | import { ConnectionEntity } from '../../connection/connection.entity.js'; |
3 | 4 | import { AgentEntity } from '../agent.entity.js'; |
4 | | -import { ConnectionTypeTestEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js'; |
5 | 5 |
|
6 | 6 | export const customAgentRepositoryExtension = { |
7 | | - async saveNewAgent(agent: AgentEntity): Promise<AgentEntity> { |
8 | | - return await this.save(agent); |
9 | | - }, |
| 7 | + async saveNewAgent(agent: AgentEntity): Promise<AgentEntity> { |
| 8 | + return await this.save(agent); |
| 9 | + }, |
10 | 10 |
|
11 | | - async createNewAgentForConnectionAndReturnToken(connection: ConnectionEntity): Promise<string> { |
12 | | - const newAgent = await this.createNewAgentForConnection(connection); |
13 | | - return newAgent.token; |
14 | | - }, |
| 11 | + async createNewAgentForConnectionAndReturnToken(connection: ConnectionEntity): Promise<string> { |
| 12 | + const newAgent = await this.createNewAgentForConnection(connection); |
| 13 | + return newAgent.token; |
| 14 | + }, |
15 | 15 |
|
16 | | - async createNewAgentForConnection(connection: ConnectionEntity): Promise<AgentEntity> { |
17 | | - const agent = new AgentEntity(); |
18 | | - let token = nanoid(64); |
19 | | - if (process.env.NODE_ENV !== 'test') { |
20 | | - agent.token = token; |
21 | | - } else { |
22 | | - token = this.getTestAgentToken(connection.type); |
23 | | - agent.token = token; |
24 | | - } |
25 | | - agent.connection = connection; |
26 | | - const savedAgent = await this.save(agent); |
27 | | - savedAgent.token = token; |
28 | | - return savedAgent; |
29 | | - }, |
| 16 | + async createNewAgentForConnection(connection: ConnectionEntity): Promise<AgentEntity> { |
| 17 | + const agent = new AgentEntity(); |
| 18 | + let token = nanoid(64); |
| 19 | + if (process.env.NODE_ENV !== 'test') { |
| 20 | + agent.token = token; |
| 21 | + } else { |
| 22 | + token = this.getTestAgentToken(connection.type); |
| 23 | + agent.token = token; |
| 24 | + } |
| 25 | + agent.connection = connection; |
| 26 | + const savedAgent = await this.save(agent); |
| 27 | + savedAgent.token = token; |
| 28 | + return savedAgent; |
| 29 | + }, |
30 | 30 |
|
31 | | - async renewOrCreateConnectionToken(connectionId: string): Promise<string> { |
32 | | - const agentQb = this.createQueryBuilder('agent') |
33 | | - .leftJoinAndSelect('agent.connection', 'connection') |
34 | | - .andWhere('connection.id = :connectionId', { connectionId: connectionId }); |
35 | | - const foundAgent = await agentQb.getOne(); |
36 | | - if (!foundAgent) { |
37 | | - const connectionQb = this.manager |
38 | | - .getRepository(ConnectionEntity) |
39 | | - .createQueryBuilder('connection') |
40 | | - .andWhere('connection.id = :connectionId', { connectionId: connectionId }); |
41 | | - const foundConnection = await connectionQb.getOne(); |
42 | | - return await this.createNewAgentForConnectionAndReturnToken(foundConnection); |
43 | | - } else { |
44 | | - const newToken = nanoid(64); |
45 | | - foundAgent.token = newToken; |
46 | | - await this.save(foundAgent); |
47 | | - return newToken; |
48 | | - } |
49 | | - }, |
| 31 | + async renewOrCreateConnectionToken(connectionId: string): Promise<string> { |
| 32 | + const agentQb = this.createQueryBuilder('agent') |
| 33 | + .leftJoinAndSelect('agent.connection', 'connection') |
| 34 | + .andWhere('connection.id = :connectionId', { connectionId: connectionId }); |
| 35 | + const foundAgent = await agentQb.getOne(); |
| 36 | + if (!foundAgent) { |
| 37 | + const connectionQb = this.manager |
| 38 | + .getRepository(ConnectionEntity) |
| 39 | + .createQueryBuilder('connection') |
| 40 | + .andWhere('connection.id = :connectionId', { connectionId: connectionId }); |
| 41 | + const foundConnection = await connectionQb.getOne(); |
| 42 | + return await this.createNewAgentForConnectionAndReturnToken(foundConnection); |
| 43 | + } else { |
| 44 | + const newToken = nanoid(64); |
| 45 | + foundAgent.token = newToken; |
| 46 | + await this.save(foundAgent); |
| 47 | + return newToken; |
| 48 | + } |
| 49 | + }, |
50 | 50 |
|
51 | | - getTestAgentToken(connectionType: ConnectionTypeTestEnum): string { |
52 | | - if (process.env.NODE_ENV !== 'test') throw new Error('Test agent token can only be used in test environment'); |
53 | | - switch (connectionType) { |
54 | | - case ConnectionTypeTestEnum.agent_oracledb: |
55 | | - return 'ORACLE-TEST-AGENT-TOKEN'; |
56 | | - case ConnectionTypeTestEnum.agent_mssql: |
57 | | - return 'MSSQL-TEST-AGENT-TOKEN'; |
58 | | - case ConnectionTypeTestEnum.agent_mysql: |
59 | | - return 'MYSQL-TEST-AGENT-TOKEN'; |
60 | | - case ConnectionTypeTestEnum.agent_postgres: |
61 | | - return 'POSTGRES-TEST-AGENT-TOKEN'; |
62 | | - case ConnectionTypeTestEnum.agent_ibmdb2: |
63 | | - return 'IBMDB2-TEST-AGENT-TOKEN'; |
64 | | - case ConnectionTypeTestEnum.agent_mongodb: |
65 | | - return 'MONGODB-TEST-AGENT-TOKEN'; |
66 | | - case ConnectionTypeTestEnum.agent_redis: |
67 | | - return 'REDIS-TEST-AGENT-TOKEN'; |
68 | | - case ConnectionTypeTestEnum.agent_cassandra: |
69 | | - return 'CASSANDRA-TEST-AGENT-TOKEN'; |
70 | | - case ConnectionTypeTestEnum.agent_clickhouse: |
71 | | - return 'CLICKHOUSE-TEST-AGENT-TOKEN'; |
72 | | - default: |
73 | | - throw new Error(`Unsupported connection type for test agent token: ${connectionType}`); |
74 | | - } |
75 | | - }, |
| 51 | + getTestAgentToken(connectionType: ConnectionTypeTestEnum): string { |
| 52 | + if (process.env.NODE_ENV !== 'test') throw new Error('Test agent token can only be used in test environment'); |
| 53 | + switch (connectionType) { |
| 54 | + case ConnectionTypeTestEnum.agent_oracledb: |
| 55 | + return 'ORACLE-TEST-AGENT-TOKEN'; |
| 56 | + case ConnectionTypeTestEnum.agent_mssql: |
| 57 | + return 'MSSQL-TEST-AGENT-TOKEN'; |
| 58 | + case ConnectionTypeTestEnum.agent_mysql: |
| 59 | + return 'MYSQL-TEST-AGENT-TOKEN'; |
| 60 | + case ConnectionTypeTestEnum.agent_postgres: |
| 61 | + return 'POSTGRES-TEST-AGENT-TOKEN'; |
| 62 | + case ConnectionTypeTestEnum.agent_ibmdb2: |
| 63 | + return 'IBMDB2-TEST-AGENT-TOKEN'; |
| 64 | + case ConnectionTypeTestEnum.agent_mongodb: |
| 65 | + return 'MONGODB-TEST-AGENT-TOKEN'; |
| 66 | + case ConnectionTypeTestEnum.agent_redis: |
| 67 | + return 'REDIS-TEST-AGENT-TOKEN'; |
| 68 | + case ConnectionTypeTestEnum.agent_cassandra: |
| 69 | + return 'CASSANDRA-TEST-AGENT-TOKEN'; |
| 70 | + case ConnectionTypeTestEnum.agent_clickhouse: |
| 71 | + return 'CLICKHOUSE-TEST-AGENT-TOKEN'; |
| 72 | + default: |
| 73 | + throw new Error(`Unsupported connection type for test agent token: ${connectionType}`); |
| 74 | + } |
| 75 | + }, |
76 | 76 | }; |
0 commit comments