|
| 1 | +import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; |
| 2 | +import { In } from 'typeorm'; |
| 3 | +import AbstractUseCase from '../../../common/abstract-use.case.js'; |
| 4 | +import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; |
| 5 | +import { BaseType } from '../../../common/data-injection.tokens.js'; |
| 6 | +import { ConnectionEntity } from '../../../entities/connection/connection.entity.js'; |
| 7 | +import { Messages } from '../../../exceptions/text/messages.js'; |
| 8 | +import { FoundConnectionInfoRO } from '../data-structures/found-connection-info.ro.js'; |
| 9 | +import { GetConnectionsInfoByIdsDS } from '../data-structures/get-connections-info-by-ids.ds.js'; |
| 10 | +import { IGetConnectionsInfoByIds } from './saas-use-cases.interface.js'; |
| 11 | + |
| 12 | +@Injectable() |
| 13 | +export class GetConnectionsInfoByIdsUseCase |
| 14 | + extends AbstractUseCase<GetConnectionsInfoByIdsDS, Array<FoundConnectionInfoRO>> |
| 15 | + implements IGetConnectionsInfoByIds |
| 16 | +{ |
| 17 | + constructor( |
| 18 | + @Inject(BaseType.GLOBAL_DB_CONTEXT) |
| 19 | + protected _dbContext: IGlobalDatabaseContext, |
| 20 | + ) { |
| 21 | + super(); |
| 22 | + } |
| 23 | + |
| 24 | + protected async implementation(inputData: GetConnectionsInfoByIdsDS): Promise<Array<FoundConnectionInfoRO>> { |
| 25 | + const { connectionIds } = inputData; |
| 26 | + if (!connectionIds || connectionIds.length === 0) { |
| 27 | + throw new HttpException( |
| 28 | + { |
| 29 | + message: Messages.CONNECTION_ID_MISSING, |
| 30 | + }, |
| 31 | + HttpStatus.BAD_REQUEST, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + const connections = await this._dbContext.connectionRepository.findBy({ |
| 36 | + id: In(connectionIds), |
| 37 | + }); |
| 38 | + |
| 39 | + return connections.map((connection) => this.buildConnectionInfoRO(connection)); |
| 40 | + } |
| 41 | + |
| 42 | + private buildConnectionInfoRO(connection: ConnectionEntity): FoundConnectionInfoRO { |
| 43 | + return { |
| 44 | + id: connection.id, |
| 45 | + title: connection.title, |
| 46 | + type: connection.type, |
| 47 | + host: connection.host, |
| 48 | + port: connection.port, |
| 49 | + database: connection.database, |
| 50 | + schema: connection.schema, |
| 51 | + sid: connection.sid, |
| 52 | + ssh: connection.ssh, |
| 53 | + ssl: connection.ssl, |
| 54 | + createdAt: connection.createdAt, |
| 55 | + updatedAt: connection.updatedAt, |
| 56 | + isTestConnection: connection.isTestConnection, |
| 57 | + is_frozen: connection.is_frozen, |
| 58 | + masterEncryption: connection.masterEncryption, |
| 59 | + azure_encryption: connection.azure_encryption, |
| 60 | + authSource: connection.authSource, |
| 61 | + dataCenter: connection.dataCenter, |
| 62 | + }; |
| 63 | + } |
| 64 | +} |
0 commit comments