1- import {
2- BadRequestException ,
3- Inject ,
4- Injectable ,
5- InternalServerErrorException ,
6- Scope ,
7- } from "@nestjs/common" ;
8- import { getDataAccessObject } from "@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js" ;
9- import AbstractUseCase from "../../../common/abstract-use.case.js" ;
10- import { IGlobalDatabaseContext } from "../../../common/application/global-database-context.interface.js" ;
11- import { BaseType } from "../../../common/data-injection.tokens.js" ;
12- import { Messages } from "../../../exceptions/text/messages.js" ;
13- import { Encryptor } from "../../../helpers/encryption/encryptor.js" ;
14- import {
15- isConnectionTypeAgent ,
16- slackPostMessage ,
17- } from "../../../helpers/index.js" ;
18- import { SharedJobsService } from "../../shared-jobs/shared-jobs.service.js" ;
19- import { UserRoleEnum } from "../../user/enums/user-role.enum.js" ;
20- import { UserEntity } from "../../user/user.entity.js" ;
21- import { CreateConnectionDs } from "../application/data-structures/create-connection.ds.js" ;
22- import { CreatedConnectionDTO } from "../application/dto/created-connection.dto.js" ;
23- import { ConnectionEntity } from "../connection.entity.js" ;
24- import { buildConnectionEntity } from "../utils/build-connection-entity.js" ;
25- import { buildCreatedConnectionDs } from "../utils/build-created-connection.ds.js" ;
26- import { processAWSConnection } from "../utils/process-aws-connection.util.js" ;
27- import { validateCreateConnectionData } from "../utils/validate-create-connection-data.js" ;
28- import { ICreateConnection } from "./use-cases.interfaces.js" ;
1+ import { BadRequestException , 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 * as Sentry from '@sentry/node' ;
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 { Encryptor } from '../../../helpers/encryption/encryptor.js' ;
9+ import { isConnectionTypeAgent , slackPostMessage } from '../../../helpers/index.js' ;
10+ import { SharedJobsService } from '../../shared-jobs/shared-jobs.service.js' ;
11+ import { UserRoleEnum } from '../../user/enums/user-role.enum.js' ;
12+ import { UserEntity } from '../../user/user.entity.js' ;
13+ import { CreateConnectionDs } from '../application/data-structures/create-connection.ds.js' ;
14+ import { CreatedConnectionDTO } from '../application/dto/created-connection.dto.js' ;
15+ import { ConnectionEntity } from '../connection.entity.js' ;
16+ import { buildConnectionEntity } from '../utils/build-connection-entity.js' ;
17+ import { buildCreatedConnectionDs } from '../utils/build-created-connection.ds.js' ;
18+ import { processAWSConnection } from '../utils/process-aws-connection.util.js' ;
19+ import { validateCreateConnectionData } from '../utils/validate-create-connection-data.js' ;
20+ import { ICreateConnection } from './use-cases.interfaces.js' ;
2921
3022@Injectable ( { scope : Scope . REQUEST } )
3123export class CreateConnectionUseCase
@@ -39,41 +31,28 @@ export class CreateConnectionUseCase
3931 ) {
4032 super ( ) ;
4133 }
42- protected async implementation (
43- createConnectionData : CreateConnectionDs ,
44- ) : Promise < CreatedConnectionDTO > {
34+ protected async implementation ( createConnectionData : CreateConnectionDs ) : Promise < CreatedConnectionDTO > {
4535 const {
4636 creation_info : { authorId, masterPwd } ,
4737 } = createConnectionData ;
48- const connectionAuthor : UserEntity =
49- await this . _dbContext . userRepository . findOneUserById ( authorId ) ;
38+ const connectionAuthor : UserEntity = await this . _dbContext . userRepository . findOneUserById ( authorId ) ;
5039
5140 if ( ! connectionAuthor ) {
5241 throw new InternalServerErrorException ( Messages . USER_NOT_FOUND ) ;
5342 }
5443
55- if (
56- connectionAuthor . role !== UserRoleEnum . ADMIN &&
57- connectionAuthor . role !== UserRoleEnum . DB_ADMIN
58- ) {
59- throw new BadRequestException (
60- Messages . CANT_CREATE_CONNECTION_USER_NON_COMPANY_ADMIN ,
61- ) ;
44+ if ( connectionAuthor . role !== UserRoleEnum . ADMIN && connectionAuthor . role !== UserRoleEnum . DB_ADMIN ) {
45+ throw new BadRequestException ( Messages . CANT_CREATE_CONNECTION_USER_NON_COMPANY_ADMIN ) ;
6246 }
6347
6448 await slackPostMessage (
65- Messages . USER_TRY_CREATE_CONNECTION (
66- connectionAuthor . email ,
67- createConnectionData . connection_parameters . type ,
68- ) ,
49+ Messages . USER_TRY_CREATE_CONNECTION ( connectionAuthor . email , createConnectionData . connection_parameters . type ) ,
6950 ) ;
7051 await validateCreateConnectionData ( createConnectionData ) ;
7152
7253 createConnectionData = await processAWSConnection ( createConnectionData ) ;
7354 let isConnectionTestedSuccessfully : boolean = false ;
74- if (
75- ! isConnectionTypeAgent ( createConnectionData . connection_parameters . type )
76- ) {
55+ if ( ! isConnectionTypeAgent ( createConnectionData . connection_parameters . type ) ) {
7756 const connectionParamsCopy = {
7857 ...createConnectionData . connection_parameters ,
7958 } ;
@@ -84,10 +63,7 @@ export class CreateConnectionUseCase
8463 } catch ( e ) {
8564 const text : string = e . message . toLowerCase ( ) ;
8665 isConnectionTestedSuccessfully = false ;
87- if (
88- text . includes ( "ssl required" ) ||
89- text . includes ( "ssl connection required" )
90- ) {
66+ if ( text . includes ( 'ssl required' ) || text . includes ( 'ssl connection required' ) ) {
9167 createConnectionData . connection_parameters . ssl = true ;
9268 connectionParamsCopy . ssl = true ;
9369 try {
@@ -104,79 +80,49 @@ export class CreateConnectionUseCase
10480 }
10581 let connectionCopy : ConnectionEntity = null ;
10682 try {
107- const createdConnection : ConnectionEntity = await buildConnectionEntity (
108- createConnectionData ,
109- connectionAuthor ,
110- ) ;
83+ const createdConnection : ConnectionEntity = await buildConnectionEntity ( createConnectionData , connectionAuthor ) ;
11184 const savedConnection : ConnectionEntity =
112- await this . _dbContext . connectionRepository . saveNewConnection (
113- createdConnection ,
114- ) ;
85+ await this . _dbContext . connectionRepository . saveNewConnection ( createdConnection ) ;
11586
11687 connectionCopy = { ...savedConnection } as ConnectionEntity ;
117- if (
118- savedConnection . masterEncryption &&
119- masterPwd &&
120- ! isConnectionTypeAgent ( savedConnection . type )
121- ) {
122- connectionCopy = Encryptor . decryptConnectionCredentials (
123- connectionCopy ,
124- masterPwd ,
125- ) ;
88+ if ( savedConnection . masterEncryption && masterPwd && ! isConnectionTypeAgent ( savedConnection . type ) ) {
89+ connectionCopy = Encryptor . decryptConnectionCredentials ( connectionCopy , masterPwd ) ;
12690 }
12791
12892 let token : string ;
12993 if ( isConnectionTypeAgent ( savedConnection . type ) ) {
130- token =
131- await this . _dbContext . agentRepository . createNewAgentForConnectionAndReturnToken (
132- savedConnection ,
133- ) ;
94+ token = await this . _dbContext . agentRepository . createNewAgentForConnectionAndReturnToken ( savedConnection ) ;
13495 }
135- const createdAdminGroup =
136- await this . _dbContext . groupRepository . createdAdminGroupInConnection (
137- savedConnection ,
138- connectionAuthor ,
139- ) ;
140- await this . _dbContext . permissionRepository . createdDefaultAdminPermissionsInGroup (
141- createdAdminGroup ,
96+ const createdAdminGroup = await this . _dbContext . groupRepository . createdAdminGroupInConnection (
97+ savedConnection ,
98+ connectionAuthor ,
14299 ) ;
100+ await this . _dbContext . permissionRepository . createdDefaultAdminPermissionsInGroup ( createdAdminGroup ) ;
143101 delete createdAdminGroup . connection ;
144102 await this . _dbContext . userRepository . saveUserEntity ( connectionAuthor ) ;
145103 createdConnection . groups = [ createdAdminGroup ] ;
146- const foundUserCompany =
147- await this . _dbContext . companyInfoRepository . findOneCompanyInfoByUserIdWithConnections (
148- connectionAuthor . id ,
149- ) ;
104+ const foundUserCompany = await this . _dbContext . companyInfoRepository . findOneCompanyInfoByUserIdWithConnections (
105+ connectionAuthor . id ,
106+ ) ;
150107 if ( foundUserCompany ) {
151108 const connection = await this . _dbContext . connectionRepository . findOne ( {
152109 where : { id : savedConnection . id } ,
153110 } ) ;
154111 connection . company = foundUserCompany ;
155- await this . _dbContext . connectionRepository . saveUpdatedConnection (
156- connection ,
157- ) ;
112+ await this . _dbContext . connectionRepository . saveUpdatedConnection ( connection ) ;
158113 }
159114 await slackPostMessage (
160- Messages . USER_CREATED_CONNECTION (
161- connectionAuthor . email ,
162- createConnectionData . connection_parameters . type ,
163- ) ,
164- ) ;
165- const connectionRO = buildCreatedConnectionDs (
166- savedConnection ,
167- token ,
168- masterPwd ,
115+ Messages . USER_CREATED_CONNECTION ( connectionAuthor . email , createConnectionData . connection_parameters . type ) ,
169116 ) ;
117+ const connectionRO = buildCreatedConnectionDs ( savedConnection , token , masterPwd ) ;
170118 return connectionRO ;
171119 } finally {
172- if (
173- isConnectionTestedSuccessfully &&
174- ! isConnectionTypeAgent ( connectionCopy . type )
175- ) {
176- // await this.sharedJobsService.scanDatabaseAndCreateWidgets(connectionCopy);
177- await this . sharedJobsService . scanDatabaseAndCreateSettingsAndWidgetsWithAI (
178- connectionCopy ,
179- ) ;
120+ if ( isConnectionTestedSuccessfully && ! isConnectionTypeAgent ( connectionCopy . type ) ) {
121+ // Fire-and-forget: run AI scan in background without blocking response
122+ this . sharedJobsService . scanDatabaseAndCreateSettingsAndWidgetsWithAI ( connectionCopy ) . catch ( ( error ) => {
123+ console . error ( 'Background AI scan failed:' , error ) ;
124+ Sentry . captureException ( error ) ;
125+ } ) ;
180126 }
181127 }
182128 }
0 commit comments