|
| 1 | +import { Inject, Injectable, InternalServerErrorException, Scope } from '@nestjs/common'; |
| 2 | +import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js'; |
| 3 | +import { ConnectionTypesEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js'; |
| 4 | +import AbstractUseCase from '../../../common/abstract-use.case.js'; |
| 5 | +import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; |
| 6 | +import { BaseType } from '../../../common/data-injection.tokens.js'; |
| 7 | +import { Messages } from '../../../exceptions/text/messages.js'; |
| 8 | +import { slackPostMessage } from '../../../helpers/index.js'; |
| 9 | +import { AccessLevelEnum } from '../../../enums/index.js'; |
| 10 | +import { generateCedarPolicyForGroup } from '../../../entities/cedar-authorization/cedar-policy-generator.js'; |
| 11 | +import { CreatedConnectionDTO } from '../../../entities/connection/application/dto/created-connection.dto.js'; |
| 12 | +import { ConnectionEntity } from '../../../entities/connection/connection.entity.js'; |
| 13 | +import { readSslCertificate } from '../../../entities/connection/ssl-certificate/read-certificate.js'; |
| 14 | +import { buildCreatedConnectionDs } from '../../../entities/connection/utils/build-created-connection.ds.js'; |
| 15 | +import { CreateConnectionForHostedDbDto } from '../data-structures/create-connecttion-for-selfhosted-db.dto.js'; |
| 16 | +import { ICreateConnectionForHostedDb } from './saas-use-cases.interface.js'; |
| 17 | + |
| 18 | +@Injectable({ scope: Scope.REQUEST }) |
| 19 | +export class CreateConnectionForHostedDbUseCase |
| 20 | + extends AbstractUseCase<CreateConnectionForHostedDbDto, CreatedConnectionDTO> |
| 21 | + implements ICreateConnectionForHostedDb |
| 22 | +{ |
| 23 | + constructor( |
| 24 | + @Inject(BaseType.GLOBAL_DB_CONTEXT) |
| 25 | + protected _dbContext: IGlobalDatabaseContext, |
| 26 | + ) { |
| 27 | + super(); |
| 28 | + } |
| 29 | + |
| 30 | + protected async implementation(inputData: CreateConnectionForHostedDbDto): Promise<CreatedConnectionDTO> { |
| 31 | + const { companyId, userId, databaseName, hostname, port, username, password } = inputData; |
| 32 | + |
| 33 | + const connectionAuthor = await this._dbContext.userRepository.findOneUserById(userId); |
| 34 | + if (!connectionAuthor) { |
| 35 | + throw new InternalServerErrorException(Messages.USER_NOT_FOUND); |
| 36 | + } |
| 37 | + |
| 38 | + await slackPostMessage( |
| 39 | + Messages.USER_TRY_CREATE_CONNECTION(connectionAuthor.email, ConnectionTypesEnum.postgres), |
| 40 | + ); |
| 41 | + |
| 42 | + const cert = await readSslCertificate(); |
| 43 | + |
| 44 | + const connectionParams = { |
| 45 | + type: ConnectionTypesEnum.postgres, |
| 46 | + host: hostname, |
| 47 | + port: port, |
| 48 | + username: username, |
| 49 | + password: password, |
| 50 | + database: databaseName, |
| 51 | + schema: null, |
| 52 | + sid: null, |
| 53 | + ssh: false, |
| 54 | + privateSSHKey: null, |
| 55 | + sshHost: null, |
| 56 | + sshPort: null, |
| 57 | + sshUsername: null, |
| 58 | + ssl: true, |
| 59 | + cert: cert, |
| 60 | + azure_encryption: false, |
| 61 | + authSource: null, |
| 62 | + dataCenter: null, |
| 63 | + }; |
| 64 | + |
| 65 | + const dao = getDataAccessObject(connectionParams); |
| 66 | + await dao.testConnect(); |
| 67 | + |
| 68 | + const connection = new ConnectionEntity(); |
| 69 | + connection.type = ConnectionTypesEnum.postgres; |
| 70 | + connection.host = hostname; |
| 71 | + connection.port = port; |
| 72 | + connection.username = username; |
| 73 | + connection.password = password; |
| 74 | + connection.database = databaseName; |
| 75 | + connection.ssl = true; |
| 76 | + connection.cert = cert; |
| 77 | + connection.ssh = false; |
| 78 | + connection.azure_encryption = false; |
| 79 | + connection.masterEncryption = false; |
| 80 | + connection.author = connectionAuthor; |
| 81 | + |
| 82 | + const savedConnection = await this._dbContext.connectionRepository.saveNewConnection(connection); |
| 83 | + |
| 84 | + const createdAdminGroup = await this._dbContext.groupRepository.createdAdminGroupInConnection( |
| 85 | + savedConnection, |
| 86 | + connectionAuthor, |
| 87 | + ); |
| 88 | + await this._dbContext.permissionRepository.createdDefaultAdminPermissionsInGroup(createdAdminGroup); |
| 89 | + createdAdminGroup.cedarPolicy = generateCedarPolicyForGroup( |
| 90 | + savedConnection.id, |
| 91 | + true, |
| 92 | + { |
| 93 | + connection: { connectionId: savedConnection.id, accessLevel: AccessLevelEnum.edit }, |
| 94 | + group: { groupId: createdAdminGroup.id, accessLevel: AccessLevelEnum.edit }, |
| 95 | + tables: [], |
| 96 | + }, |
| 97 | + ); |
| 98 | + await this._dbContext.groupRepository.saveNewOrUpdatedGroup(createdAdminGroup); |
| 99 | + delete createdAdminGroup.connection; |
| 100 | + await this._dbContext.userRepository.saveUserEntity(connectionAuthor); |
| 101 | + savedConnection.groups = [createdAdminGroup]; |
| 102 | + |
| 103 | + const foundCompany = await this._dbContext.companyInfoRepository.findCompanyInfoByCompanyIdWithoutConnections(companyId); |
| 104 | + if (foundCompany) { |
| 105 | + const connectionToUpdate = await this._dbContext.connectionRepository.findOne({ |
| 106 | + where: { id: savedConnection.id }, |
| 107 | + }); |
| 108 | + connectionToUpdate.company = foundCompany; |
| 109 | + await this._dbContext.connectionRepository.saveUpdatedConnection(connectionToUpdate); |
| 110 | + } |
| 111 | + |
| 112 | + await slackPostMessage( |
| 113 | + Messages.USER_CREATED_CONNECTION(connectionAuthor.email, ConnectionTypesEnum.postgres), |
| 114 | + ); |
| 115 | + |
| 116 | + const connectionRO = buildCreatedConnectionDs(savedConnection, null, null); |
| 117 | + return connectionRO; |
| 118 | + } |
| 119 | +} |
0 commit comments