Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"express-rate-limit": "8.0.1",
"fetch-blob": "^4.0.0",
"helmet": "8.1.0",
"i18n-iso-countries": "^7.14.0",
"ip-range-check": "0.2.0",
"json2csv": "^5.0.7",
"jsonwebtoken": "^9.0.2",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { AppLoggerMiddleware } from './middlewares/logging-middleware/app-logger
import { DatabaseModule } from './shared/database/database.module.js';
import { GetHelloUseCase } from './use-cases-app/get-hello.use.case.js';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { SharedJobsModule } from './entities/shared-jobs/shared-jobs.module.js';

@Module({
imports: [
Expand Down Expand Up @@ -77,6 +78,7 @@ import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
TableFiltersModule,
DemoDataModule,
LoggingModule,
SharedJobsModule,
],
controllers: [AppController],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { buildCreatedConnectionDs } from '../utils/build-created-connection.ds.j
import { processAWSConnection } from '../utils/process-aws-connection.util.js';
import { validateCreateConnectionData } from '../utils/validate-create-connection-data.js';
import { ICreateConnection } from './use-cases.interfaces.js';
import { SharedJobsService } from '../../shared-jobs/shared-jobs.service.js';
import { Encryptor } from '../../../helpers/encryption/encryptor.js';

@Injectable({ scope: Scope.REQUEST })
export class CreateConnectionUseCase
Expand All @@ -24,7 +26,7 @@ export class CreateConnectionUseCase
constructor(
@Inject(BaseType.GLOBAL_DB_CONTEXT)
protected _dbContext: IGlobalDatabaseContext,
// private readonly saasCompanyGatewayService: SaasCompanyGatewayService,
private readonly sharedJobsService: SharedJobsService,
) {
super();
}
Expand All @@ -34,18 +36,6 @@ export class CreateConnectionUseCase
} = createConnectionData;
const connectionAuthor: UserEntity = await this._dbContext.userRepository.findOneUserById(authorId);

// if (isSaaS()) {
// const userCompany = await this._dbContext.companyInfoRepository.finOneCompanyInfoByUserId(authorId);
// const companyInfoFromSaas = await this.saasCompanyGatewayService.getCompanyInfo(userCompany.id);
// if (companyInfoFromSaas.subscriptionLevel === SubscriptionLevelEnum.FREE_PLAN) {
// if (Constants.NON_FREE_PLAN_CONNECTION_TYPES.includes(createConnectionData.connection_parameters.type)) {
// throw new NonAvailableInFreePlanException(
// Messages.CANNOT_CREATE_CONNECTION_THIS_TYPE_IN_FREE_PLAN(createConnectionData.connection_parameters.type),
// );
// }
// }
// }

if (!connectionAuthor) {
throw new InternalServerErrorException(Messages.USER_NOT_FOUND);
}
Expand All @@ -60,55 +50,73 @@ export class CreateConnectionUseCase
await validateCreateConnectionData(createConnectionData);

createConnectionData = await processAWSConnection(createConnectionData);

let isConnectionTestedSuccessfully: boolean = false;
if (!isConnectionTypeAgent(createConnectionData.connection_parameters.type)) {
const connectionParamsCopy = { ...createConnectionData.connection_parameters };
const dao = getDataAccessObject(connectionParamsCopy);
try {
await dao.testConnect();
const testResult = await dao.testConnect();
isConnectionTestedSuccessfully = testResult.result;
} catch (e) {
const text: string = e.message.toLowerCase();
isConnectionTestedSuccessfully = false;
if (text.includes('ssl required') || text.includes('ssl connection required')) {
createConnectionData.connection_parameters.ssl = true;
connectionParamsCopy.ssl = true;
try {
const updatedDao = getDataAccessObject(connectionParamsCopy);
await updatedDao.testConnect();
const sslTestResult = await updatedDao.testConnect();
isConnectionTestedSuccessfully = sslTestResult.result;
} catch (_e) {
isConnectionTestedSuccessfully = false;
createConnectionData.connection_parameters.ssl = false;
connectionParamsCopy.ssl = false;
}
}
}
}
let connectionCopy: ConnectionEntity = null;
try {
const createdConnection: ConnectionEntity = await buildConnectionEntity(createConnectionData, connectionAuthor);
const savedConnection: ConnectionEntity =
await this._dbContext.connectionRepository.saveNewConnection(createdConnection);

const createdConnection: ConnectionEntity = await buildConnectionEntity(createConnectionData, connectionAuthor);
connectionCopy = { ...savedConnection } as ConnectionEntity;
if (savedConnection.masterEncryption && masterPwd && !isConnectionTypeAgent(savedConnection.type)) {
connectionCopy = Encryptor.decryptConnectionCredentials(connectionCopy, masterPwd);
}

const savedConnection: ConnectionEntity =
await this._dbContext.connectionRepository.saveNewConnection(createdConnection);
let token: string;
if (isConnectionTypeAgent(savedConnection.type)) {
token = await this._dbContext.agentRepository.createNewAgentForConnectionAndReturnToken(savedConnection);
}
const createdAdminGroup = await this._dbContext.groupRepository.createdAdminGroupInConnection(
savedConnection,
connectionAuthor,
);
await this._dbContext.permissionRepository.createdDefaultAdminPermissionsInGroup(createdAdminGroup);
delete createdAdminGroup.connection;
await this._dbContext.userRepository.saveUserEntity(connectionAuthor);
createdConnection.groups = [createdAdminGroup];
const foundUserCompany = await this._dbContext.companyInfoRepository.findOneCompanyInfoByUserIdWithConnections(
connectionAuthor.id,
);
if (foundUserCompany) {
const connection = await this._dbContext.connectionRepository.findOne({ where: { id: savedConnection.id } });
connection.company = foundUserCompany;
await this._dbContext.connectionRepository.saveUpdatedConnection(connection);
let token: string;
if (isConnectionTypeAgent(savedConnection.type)) {
token = await this._dbContext.agentRepository.createNewAgentForConnectionAndReturnToken(savedConnection);
}
const createdAdminGroup = await this._dbContext.groupRepository.createdAdminGroupInConnection(
savedConnection,
connectionAuthor,
);
await this._dbContext.permissionRepository.createdDefaultAdminPermissionsInGroup(createdAdminGroup);
delete createdAdminGroup.connection;
await this._dbContext.userRepository.saveUserEntity(connectionAuthor);
createdConnection.groups = [createdAdminGroup];
const foundUserCompany = await this._dbContext.companyInfoRepository.findOneCompanyInfoByUserIdWithConnections(
connectionAuthor.id,
);
if (foundUserCompany) {
const connection = await this._dbContext.connectionRepository.findOne({ where: { id: savedConnection.id } });
connection.company = foundUserCompany;
await this._dbContext.connectionRepository.saveUpdatedConnection(connection);
}
await slackPostMessage(
Messages.USER_CREATED_CONNECTION(connectionAuthor.email, createConnectionData.connection_parameters.type),
);
const connectionRO = buildCreatedConnectionDs(savedConnection, token, masterPwd);
return connectionRO;
} catch (e) {
throw e;
} finally {
if (isConnectionTestedSuccessfully && !isConnectionTypeAgent(connectionCopy.type)) {
await this.sharedJobsService.scanDatabaseAndCreateWidgets(connectionCopy);
}
}
await slackPostMessage(
Messages.USER_CREATED_CONNECTION(connectionAuthor.email, createConnectionData.connection_parameters.type),
);
return buildCreatedConnectionDs(savedConnection, token, masterPwd);
}
}
22 changes: 22 additions & 0 deletions backend/src/entities/shared-jobs/shared-jobs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Global, MiddlewareConsumer, Module } from '@nestjs/common';
import { BaseType } from '../../common/data-injection.tokens.js';
import { GlobalDatabaseContext } from '../../common/application/global-database-context.js';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SharedJobsService } from './shared-jobs.service.js';

@Global()
@Module({
imports: [TypeOrmModule.forFeature([])],
providers: [
{
provide: BaseType.GLOBAL_DB_CONTEXT,
useClass: GlobalDatabaseContext,
},
SharedJobsService,
],
controllers: [],
exports: [SharedJobsService],
})
export class SharedJobsModule {
public configure(_consumer: MiddlewareConsumer): any {}
}
Loading
Loading