FIX: Corrige nome de arquivo em mídias enviadas e recebidas
All checks were successful
Deploy Dev — Backend / Deploy para VM Dev (push) Successful in 6s

Reviewed-on: #5
This commit is contained in:
Rafael Alves Lopes 2026-07-23 11:36:01 -03:00
commit 2d6e3da322
3 changed files with 20 additions and 6 deletions

View File

@ -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],
);
}

View File

@ -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' })

View File

@ -106,6 +106,7 @@ export class WhatsappService {
mimeType: string,
senderName?: string,
caption?: string,
filename?: string,
): Promise<any> {
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) {