@@ -159,6 +159,84 @@ export class WAMonitoringService {
159159 return instances . find ( ( i ) => i . instance . instanceName === instanceName ) ?? instances ;
160160 }
161161
162+ public async instanceInfoById ( instanceId ?: string ) {
163+ this . logger . verbose ( 'get instance info' ) ;
164+ const instanceName = await this . repository . auth . findInstanceNameById ( instanceId ) ;
165+ if ( instanceName && ! this . waInstances [ instanceName ] ) {
166+ throw new NotFoundException ( `Instance "${ instanceName } " not found` ) ;
167+ }
168+
169+ const instances : any [ ] = [ ] ;
170+
171+ for await ( const [ key , value ] of Object . entries ( this . waInstances ) ) {
172+ if ( value ) {
173+ this . logger . verbose ( 'get instance info: ' + key ) ;
174+ let chatwoot : any ;
175+
176+ const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
177+
178+ const findChatwoot = await this . waInstances [ key ] . findChatwoot ( ) ;
179+
180+ if ( findChatwoot && findChatwoot . enabled ) {
181+ chatwoot = {
182+ ...findChatwoot ,
183+ webhook_url : `${ urlServer } /chatwoot/webhook/${ encodeURIComponent ( key ) } ` ,
184+ } ;
185+ }
186+
187+ if ( value . connectionStatus . state === 'open' ) {
188+ this . logger . verbose ( 'instance: ' + key + ' - connectionStatus: open' ) ;
189+
190+ const instanceData = {
191+ instance : {
192+ instanceName : key ,
193+ instanceId : ( await this . repository . auth . find ( key ) ) ?. instanceId ,
194+ owner : value . wuid ,
195+ profileName : ( await value . getProfileName ( ) ) || 'not loaded' ,
196+ profilePictureUrl : value . profilePictureUrl ,
197+ profileStatus : ( await value . getProfileStatus ( ) ) || '' ,
198+ status : value . connectionStatus . state ,
199+ } ,
200+ } ;
201+
202+ if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
203+ instanceData . instance [ 'serverUrl' ] = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
204+
205+ instanceData . instance [ 'apikey' ] = ( await this . repository . auth . find ( key ) ) ?. apikey ;
206+
207+ instanceData . instance [ 'chatwoot' ] = chatwoot ;
208+ }
209+
210+ instances . push ( instanceData ) ;
211+ } else {
212+ this . logger . verbose ( 'instance: ' + key + ' - connectionStatus: ' + value . connectionStatus . state ) ;
213+
214+ const instanceData = {
215+ instance : {
216+ instanceName : key ,
217+ instanceId : ( await this . repository . auth . find ( key ) ) ?. instanceId ,
218+ status : value . connectionStatus . state ,
219+ } ,
220+ } ;
221+
222+ if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
223+ instanceData . instance [ 'serverUrl' ] = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
224+
225+ instanceData . instance [ 'apikey' ] = ( await this . repository . auth . find ( key ) ) ?. apikey ;
226+
227+ instanceData . instance [ 'chatwoot' ] = chatwoot ;
228+ }
229+
230+ instances . push ( instanceData ) ;
231+ }
232+ }
233+ }
234+
235+ this . logger . verbose ( 'return instance info: ' + instances . length ) ;
236+
237+ return instances . find ( ( i ) => i . instance . instanceName === instanceName ) ?? instances ;
238+ }
239+
162240 private delInstanceFiles ( ) {
163241 this . logger . verbose ( 'cron to delete instance files started' ) ;
164242 setInterval ( async ( ) => {
0 commit comments