From 812bedf363e041cc8171674da7c65b14f5dc7875 Mon Sep 17 00:00:00 2001 From: Rafael Lopes Date: Thu, 23 Jul 2026 11:26:28 -0300 Subject: [PATCH] =?UTF-8?q?FIX:=20Corrige=20nome=20de=20arquivo=20em=20m?= =?UTF-8?q?=C3=ADdias=20enviadas=20e=20recebidas=20(Bug=202=20e=203)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Passa file.originalname ao sendMediaMessage no controller - Adiciona filename ao payload da Meta API para documentos - Usa filename como bodyText quando não há legenda - Emite mediaFilename via gateway Socket.IO - Persiste media_filename no banco (migration 028) - Adiciona mediaFilename ao repositório de mensagens --- .../repositories/mensagens.repository.ts | 7 ++++--- src/modules/whatsapp/whatsapp.controller.ts | 2 +- src/modules/whatsapp/whatsapp.service.ts | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/modules/whatsapp/repositories/mensagens.repository.ts b/src/modules/whatsapp/repositories/mensagens.repository.ts index dfa5de7..b1d40f4 100644 --- a/src/modules/whatsapp/repositories/mensagens.repository.ts +++ b/src/modules/whatsapp/repositories/mensagens.repository.ts @@ -16,12 +16,13 @@ export class MensagensRepository { timestamp: number; mediaPath?: string; mediaMimeType?: string; + mediaFilename?: string; }) { await this.database.query( - `INSERT INTO mensagens (wamid, chat_id, from_me, type, body, sender_name, status, timestamp, media_path, media_mime_type) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + `INSERT INTO mensagens (wamid, chat_id, from_me, type, body, sender_name, status, timestamp, media_path, media_mime_type, media_filename) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT (wamid) DO NOTHING`, - [msg.wamid, msg.chatId, msg.fromMe, msg.type, msg.body ?? null, msg.senderName ?? null, msg.status ?? null, msg.timestamp, msg.mediaPath ?? null, msg.mediaMimeType ?? null], + [msg.wamid, msg.chatId, msg.fromMe, msg.type, msg.body ?? null, msg.senderName ?? null, msg.status ?? null, msg.timestamp, msg.mediaPath ?? null, msg.mediaMimeType ?? null, msg.mediaFilename ?? null], ); } diff --git a/src/modules/whatsapp/whatsapp.controller.ts b/src/modules/whatsapp/whatsapp.controller.ts index 2ff52c9..e008893 100644 --- a/src/modules/whatsapp/whatsapp.controller.ts +++ b/src/modules/whatsapp/whatsapp.controller.ts @@ -114,7 +114,7 @@ export class WhatsappController { throw new BadRequestException(`Arquivo muito grande. Limite para ${mediaType}: ${limitMB} MB.`); } - return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption); + return this.whatsappService.sendMediaMessage(to, file.buffer, file.mimetype, senderName, caption, file.originalname); } @ApiOperation({ summary: 'Inicia atendimento ativo' }) diff --git a/src/modules/whatsapp/whatsapp.service.ts b/src/modules/whatsapp/whatsapp.service.ts index 3882bb8..1a533ba 100644 --- a/src/modules/whatsapp/whatsapp.service.ts +++ b/src/modules/whatsapp/whatsapp.service.ts @@ -106,6 +106,7 @@ export class WhatsappService { mimeType: string, senderName?: string, caption?: string, + filename?: string, ): Promise { const { access_token: token, phone_number_id: phoneNumberId, api_version: apiVersion } = await this.configService.getConfig(); @@ -136,6 +137,7 @@ export class WhatsappService { const mediaPayload: any = { id: mediaId }; if (caption && mediaType !== 'audio') mediaPayload.caption = senderName ? `*Atendente: ${senderName}*\n\n${caption}` : caption; + if (mediaType === 'document' && filename) mediaPayload.filename = filename; const messageResponse = await fetch( `https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`, @@ -162,7 +164,9 @@ export class WhatsappService { this.logger.log(`[Meta] Mídia (${mediaType}) enviada para ${to}: ${wamid}`); const senderLabel = senderName ?? 'Sistema'; - const bodyText = caption ? (senderName ? `*Atendente: ${senderLabel}*\n\n${caption}` : caption) : `[${mediaType}]`; + const bodyText = caption + ? (senderName ? `*Atendente: ${senderLabel}*\n\n${caption}` : caption) + : (filename || `[${mediaType}]`); const timestamp = Math.floor(Date.now() / 1000); if (wamid) { @@ -177,6 +181,7 @@ export class WhatsappService { timestamp, mediaPath, mediaMimeType: mimeType, + mediaFilename: filename, }); } @@ -191,6 +196,7 @@ export class WhatsappService { hasMedia: true, mediaUrl: this.mediaStorageService.getUrl(mediaPath), mediaMimeType: mimeType, + mediaFilename: filename ?? null, }); return messageResult; @@ -437,6 +443,8 @@ export class WhatsappService { let mediaPath: string | undefined; let mediaMimeType: string | undefined; + let mediaFilename: string | undefined; + if ((MEDIA_TYPES as readonly string[]).includes(messageType)) { const mediaObj = message[messageType]; if (mediaObj?.id) { @@ -444,6 +452,7 @@ export class WhatsappService { const { buffer, mimeType } = await this.downloadMetaMedia(mediaObj.id, mediaObj.mime_type); mediaPath = await this.mediaStorageService.upload(buffer, mimeType); mediaMimeType = mimeType; + mediaFilename = mediaObj.filename || undefined; this.logger.log(`[Meta] Mídia salva: ${mediaPath}`); } catch (err) { this.logger.error(`[Meta] Falha ao baixar mídia ${mediaObj.id} de ${from}:`, err); @@ -451,16 +460,19 @@ export class WhatsappService { } } + const bodyToSave = messageBody || mediaFilename || undefined; + await this.mensagensRepository.save({ wamid: messageId, chatId: from, fromMe: false, type: messageType, - body: messageBody || undefined, + body: bodyToSave, senderName: contactName, timestamp, mediaPath, mediaMimeType, + mediaFilename, }); this.gateway.emitNewMessage({ @@ -474,6 +486,7 @@ export class WhatsappService { hasMedia: !!mediaPath, mediaUrl: mediaPath ? this.mediaStorageService.getUrl(mediaPath) : null, mediaMimeType: mediaMimeType ?? null, + mediaFilename: mediaFilename ?? null, }); if (messageBody || mediaPath) { -- 2.43.0