@@ -107,7 +107,7 @@ export class WAMonitoringService {
107107 } ;
108108 }
109109
110- const findIntegration = await this . repository . integration . find ( key ) ;
110+ const findIntegration = await this . waInstances [ key ] . findIntegration ( ) ;
111111 const integration = {
112112 ...findIntegration ,
113113 webhook_wa_business : `${ urlServer } /webhook/whatsapp/${ encodeURIComponent ( key ) } ` ,
@@ -170,9 +170,21 @@ export class WAMonitoringService {
170170 return instances . find ( ( i ) => i . instance . instanceName === instanceName ) ?? instances ;
171171 }
172172
173- public async instanceInfoById ( instanceId ?: string ) {
173+ public async instanceInfoById ( instanceId ?: string , number ?: string ) {
174174 this . logger . verbose ( 'get instance info' ) ;
175- const instanceName = await this . repository . auth . findInstanceNameById ( instanceId ) ;
175+ let instanceName : string ;
176+ if ( instanceId ) {
177+ instanceName = await this . repository . auth . findInstanceNameById ( instanceId ) ;
178+ if ( ! instanceName ) {
179+ throw new NotFoundException ( `Instance "${ instanceId } " not found` ) ;
180+ }
181+ } else if ( number ) {
182+ instanceName = await this . repository . auth . findInstanceNameByNumber ( number ) ;
183+ if ( ! instanceName ) {
184+ throw new NotFoundException ( `Instance "${ number } " not found` ) ;
185+ }
186+ }
187+
176188 if ( ! instanceName ) {
177189 throw new NotFoundException ( `Instance "${ instanceId } " not found` ) ;
178190 }
@@ -181,85 +193,7 @@ export class WAMonitoringService {
181193 throw new NotFoundException ( `Instance "${ instanceName } " not found` ) ;
182194 }
183195
184- const instances : any [ ] = [ ] ;
185-
186- for await ( const [ key , value ] of Object . entries ( this . waInstances ) ) {
187- if ( value ) {
188- this . logger . verbose ( 'get instance info: ' + key ) ;
189- let chatwoot : any ;
190-
191- const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
192-
193- const findChatwoot = await this . waInstances [ key ] . findChatwoot ( ) ;
194-
195- if ( findChatwoot && findChatwoot . enabled ) {
196- chatwoot = {
197- ...findChatwoot ,
198- webhook_url : `${ urlServer } /chatwoot/webhook/${ encodeURIComponent ( key ) } ` ,
199- } ;
200- }
201-
202- const findIntegration = await this . repository . integration . find ( key ) ;
203- const integration = {
204- ...findIntegration ,
205- webhook_wa_business : `${ urlServer } /webhook/whatsapp/${ encodeURIComponent ( key ) } ` ,
206- } ;
207-
208- if ( value . connectionStatus . state === 'open' ) {
209- this . logger . verbose ( 'instance: ' + key + ' - connectionStatus: open' ) ;
210-
211- const instanceData = {
212- instance : {
213- instanceName : key ,
214- instanceId : ( await this . repository . auth . find ( key ) ) ?. instanceId ,
215- owner : value . wuid ,
216- profileName : ( await value . getProfileName ( ) ) || 'not loaded' ,
217- profilePictureUrl : value . profilePictureUrl ,
218- profileStatus : ( await value . getProfileStatus ( ) ) || '' ,
219- status : value . connectionStatus . state ,
220- } ,
221- } ;
222-
223- if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
224- instanceData . instance [ 'serverUrl' ] = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
225-
226- instanceData . instance [ 'apikey' ] = ( await this . repository . auth . find ( key ) ) ?. apikey ;
227-
228- instanceData . instance [ 'chatwoot' ] = chatwoot ;
229-
230- instanceData . instance [ 'integration' ] = integration ;
231- }
232-
233- instances . push ( instanceData ) ;
234- } else {
235- this . logger . verbose ( 'instance: ' + key + ' - connectionStatus: ' + value . connectionStatus . state ) ;
236-
237- const instanceData = {
238- instance : {
239- instanceName : key ,
240- instanceId : ( await this . repository . auth . find ( key ) ) ?. instanceId ,
241- status : value . connectionStatus . state ,
242- } ,
243- } ;
244-
245- if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
246- instanceData . instance [ 'serverUrl' ] = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
247-
248- instanceData . instance [ 'apikey' ] = ( await this . repository . auth . find ( key ) ) ?. apikey ;
249-
250- instanceData . instance [ 'chatwoot' ] = chatwoot ;
251-
252- instanceData . instance [ 'integration' ] = integration ;
253- }
254-
255- instances . push ( instanceData ) ;
256- }
257- }
258- }
259-
260- this . logger . verbose ( 'return instance info: ' + instances . length ) ;
261-
262- return instances . find ( ( i ) => i . instance . instanceName === instanceName ) ?? instances ;
196+ return this . instanceInfo ( instanceName ) ;
263197 }
264198
265199 private delInstanceFiles ( ) {
0 commit comments