diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index e04262999..104a390a7 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1542,10 +1542,11 @@ export class BaileysStartupService extends ChannelStartupService { } } + let msg: Message | null = null; if (this.configService.get('DATABASE').SAVE_DATA.NEW_MESSAGE) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { pollUpdates, ...messageData } = messageRaw as any; - const msg = await this.prismaRepository.message.create({ data: messageData }); + msg = await this.prismaRepository.message.create({ data: messageData }); const { remoteJid } = received.key; const timestamp = msg.messageTimestamp; @@ -1573,16 +1574,14 @@ export class BaileysStartupService extends ChannelStartupService { } else { this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`); } + } - if (isMedia) { - if (this.configService.get('S3').ENABLE) { - try { - if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { - this.logger.warn('Video upload is disabled. Skipping video upload.'); - // Skip video upload by returning early from this block - return; - } - + if (isMedia) { + if (this.configService.get('S3').ENABLE) { + try { + if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { + this.logger.warn('Video upload is disabled. Skipping video upload.'); + } else { const message: any = received; // Verificação adicional para garantir que há conteúdo de mídia real @@ -1595,38 +1594,41 @@ export class BaileysStartupService extends ChannelStartupService { if (!media) { this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO'); - return; - } + } else { + const { buffer, mediaType, fileName, size } = media; + const mimetype = mimeTypes.lookup(fileName).toString(); + const fullName = join( + `${this.instance.id}`, + received.key.remoteJid, + mediaType, + `${Date.now()}_${fileName}`, + ); + await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); + + if (msg?.id) { + await this.prismaRepository.media.create({ + data: { + messageId: msg.id, + instanceId: this.instanceId, + type: mediaType, + fileName: fullName, + mimetype, + }, + }); + } + + const mediaUrl = await s3Service.getObjectUrl(fullName); + + (messageRaw.message as any).mediaUrl = mediaUrl; - const { buffer, mediaType, fileName, size } = media; - const mimetype = mimeTypes.lookup(fileName).toString(); - const fullName = join( - `${this.instance.id}`, - received.key.remoteJid, - mediaType, - `${Date.now()}_${fileName}`, - ); - await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype }); - - await this.prismaRepository.media.create({ - data: { - messageId: msg.id, - instanceId: this.instanceId, - type: mediaType, - fileName: fullName, - mimetype, - }, - }); - - const mediaUrl = await s3Service.getObjectUrl(fullName); - - (messageRaw.message as any).mediaUrl = mediaUrl; - - await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw }); + if (msg?.id) { + await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw }); + } + } } - } catch (error) { - this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } + } catch (error) { + this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } } } @@ -1674,7 +1676,6 @@ export class BaileysStartupService extends ChannelStartupService { messageRaw.key.addressingMode = 'pn'; } - console.log(messageRaw); this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);