@@ -239,26 +239,37 @@ export class WAMonitoringService {
239239 }
240240
241241 public async saveInstance ( data : any ) {
242+ const truncate = ( str : string | null | undefined , maxLength : number ) : string | null => {
243+ if ( ! str ) return null ;
244+ return str . length > maxLength ? str . substring ( 0 , maxLength ) : str ;
245+ } ;
246+
242247 try {
243- const clientName = await this . configService . get < Database > ( 'DATABASE' ) . CONNECTION . CLIENT_NAME ;
248+ const instanceName = truncate ( data . instanceName , 255 ) ;
249+ if ( ! instanceName || instanceName . trim ( ) . length === 0 ) {
250+ throw new Error ( 'instanceName is required and cannot be empty' ) ;
251+ }
252+
253+ const clientName = this . configService . get < Database > ( 'DATABASE' ) . CONNECTION . CLIENT_NAME ;
244254 await this . prismaRepository . instance . create ( {
245255 data : {
246256 id : data . instanceId ,
247- name : data . instanceName ,
248- ownerJid : data . ownerJid ,
249- profileName : data . profileName ,
250- profilePicUrl : data . profilePicUrl ,
257+ name : instanceName ,
258+ ownerJid : truncate ( data . ownerJid , 100 ) ,
259+ profileName : truncate ( data . profileName , 100 ) ,
260+ profilePicUrl : truncate ( data . profilePicUrl , 500 ) ,
251261 connectionStatus :
252262 data . integration && data . integration === Integration . WHATSAPP_BAILEYS ? 'close' : ( data . status ?? 'open' ) ,
253- number : data . number ,
254- integration : data . integration || Integration . WHATSAPP_BAILEYS ,
255- token : data . hash ,
256- clientName : clientName ,
257- businessId : data . businessId ,
263+ number : truncate ( data . number , 100 ) ,
264+ integration : truncate ( data . integration || Integration . WHATSAPP_BAILEYS , 100 ) ,
265+ token : truncate ( data . hash , 255 ) ,
266+ clientName : truncate ( clientName , 100 ) ,
267+ businessId : truncate ( data . businessId , 100 ) ,
258268 } ,
259269 } ) ;
260270 } catch ( error ) {
261271 this . logger . error ( error ) ;
272+ throw error ;
262273 }
263274 }
264275
@@ -283,15 +294,31 @@ export class WAMonitoringService {
283294
284295 if ( ! instance ) return ;
285296
286- instance . setInstance ( {
287- instanceId : instanceData . instanceId ,
288- instanceName : instanceData . instanceName ,
289- integration : instanceData . integration ,
290- token : instanceData . token ,
291- number : instanceData . number ,
292- businessId : instanceData . businessId ,
293- ownerJid : instanceData . ownerJid ,
294- } ) ;
297+ // Para WhatsApp Business, setInstance é async e precisa ser aguardado
298+ if ( instanceData . integration === Integration . WHATSAPP_BUSINESS ) {
299+ const setInstanceResult = ( instance as any ) . setInstance ( {
300+ instanceId : instanceData . instanceId ,
301+ instanceName : instanceData . instanceName ,
302+ integration : instanceData . integration ,
303+ token : instanceData . token ,
304+ number : instanceData . number ,
305+ businessId : instanceData . businessId ,
306+ ownerJid : instanceData . ownerJid ,
307+ } ) ;
308+ if ( setInstanceResult instanceof Promise ) {
309+ await setInstanceResult ;
310+ }
311+ } else {
312+ instance . setInstance ( {
313+ instanceId : instanceData . instanceId ,
314+ instanceName : instanceData . instanceName ,
315+ integration : instanceData . integration ,
316+ token : instanceData . token ,
317+ number : instanceData . number ,
318+ businessId : instanceData . businessId ,
319+ ownerJid : instanceData . ownerJid ,
320+ } ) ;
321+ }
295322
296323 if ( instanceData . connectionStatus === 'open' || instanceData . connectionStatus === 'connecting' ) {
297324 this . logger . info (
@@ -321,11 +348,21 @@ export class WAMonitoringService {
321348 return ;
322349 }
323350
351+ // Para WhatsApp Business, tenta carregar token completo do cache
352+ let token = instanceData . token ;
353+ if ( instanceData . integration === Integration . WHATSAPP_BUSINESS ) {
354+ const cacheKey = `instance:${ instanceData . name } :fullToken` ;
355+ const fullToken = await this . cache . get ( cacheKey ) ;
356+ if ( fullToken ) {
357+ token = fullToken ;
358+ }
359+ }
360+
324361 const instance = {
325362 instanceId : k . split ( ':' ) [ 1 ] ,
326363 instanceName : k . split ( ':' ) [ 2 ] ,
327364 integration : instanceData . integration ,
328- token : instanceData . token ,
365+ token : token ,
329366 number : instanceData . number ,
330367 businessId : instanceData . businessId ,
331368 connectionStatus : instanceData . connectionStatus as any , // Pass connection status
@@ -350,11 +387,21 @@ export class WAMonitoringService {
350387
351388 await Promise . all (
352389 instances . map ( async ( instance ) => {
390+ // Para WhatsApp Business, tenta carregar token completo do cache
391+ let token = instance . token ;
392+ if ( instance . integration === Integration . WHATSAPP_BUSINESS ) {
393+ const cacheKey = `instance:${ instance . name } :fullToken` ;
394+ const fullToken = await this . cache . get ( cacheKey ) ;
395+ if ( fullToken ) {
396+ token = fullToken ;
397+ }
398+ }
399+
353400 this . setInstance ( {
354401 instanceId : instance . id ,
355402 instanceName : instance . name ,
356403 integration : instance . integration ,
357- token : instance . token ,
404+ token : token ,
358405 number : instance . number ,
359406 businessId : instance . businessId ,
360407 ownerJid : instance . ownerJid ,
@@ -377,11 +424,21 @@ export class WAMonitoringService {
377424 where : { id : instanceId } ,
378425 } ) ;
379426
427+ // Para WhatsApp Business, tenta carregar token completo do cache
428+ let token = instance . token ;
429+ if ( instance . integration === Integration . WHATSAPP_BUSINESS ) {
430+ const cacheKey = `instance:${ instance . name } :fullToken` ;
431+ const fullToken = await this . cache . get ( cacheKey ) ;
432+ if ( fullToken ) {
433+ token = fullToken ;
434+ }
435+ }
436+
380437 this . setInstance ( {
381438 instanceId : instance . id ,
382439 instanceName : instance . name ,
383440 integration : instance . integration ,
384- token : instance . token ,
441+ token : token ,
385442 businessId : instance . businessId ,
386443 connectionStatus : instance . connectionStatus as any , // Pass connection status
387444 } ) ;
0 commit comments