Revert "FIX: IDs no formato @c.us para compatibilidade com o frontend"
Some checks failed
Deploy Dev / deploy (push) Has been cancelled
Some checks failed
Deploy Dev / deploy (push) Has been cancelled
This reverts commit 224594918d.
This commit is contained in:
parent
224594918d
commit
5e9f328932
@ -55,23 +55,14 @@ export class WhatsappService {
|
|||||||
return 'CONNECTED';
|
return 'CONNECTED';
|
||||||
}
|
}
|
||||||
|
|
||||||
private cleanPhone(id: string): string {
|
|
||||||
return id.replace(/@c\.us$/, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
private toWaId(phone: string): string {
|
|
||||||
return phone.includes('@') ? phone : `${phone}@c.us`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getChats() {
|
async getChats() {
|
||||||
const rows = await this.mensagensRepository.findChats();
|
const rows = await this.mensagensRepository.findChats();
|
||||||
return Promise.all(rows.map(async (row: any) => {
|
return Promise.all(rows.map(async (row: any) => {
|
||||||
const chatId = row.chat_id;
|
const chatId = row.chat_id;
|
||||||
const waId = this.toWaId(chatId);
|
|
||||||
const assignment = await this.assignmentService.getAssignment(chatId);
|
const assignment = await this.assignmentService.getAssignment(chatId);
|
||||||
const contactProfile = await this.getCustomerContact(chatId);
|
const contactProfile = await this.getCustomerContact(chatId);
|
||||||
return {
|
return {
|
||||||
id: { _serialized: waId, user: chatId, server: 'c.us' },
|
id: { _serialized: chatId, user: chatId, server: 'c.us' },
|
||||||
name: contactProfile?.name || row.sender_name || chatId,
|
name: contactProfile?.name || row.sender_name || chatId,
|
||||||
preview: row.preview || '',
|
preview: row.preview || '',
|
||||||
timestamp: Number(row.timestamp),
|
timestamp: Number(row.timestamp),
|
||||||
@ -84,8 +75,7 @@ export class WhatsappService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getChatMessages(chatId: string) {
|
async getChatMessages(chatId: string) {
|
||||||
const cleanId = this.cleanPhone(chatId);
|
const rows = await this.mensagensRepository.findByChatId(chatId);
|
||||||
const rows = await this.mensagensRepository.findByChatId(cleanId);
|
|
||||||
return rows.map((row: any) => ({
|
return rows.map((row: any) => ({
|
||||||
id: { _serialized: row.wamid, user: row.wamid, server: 'c.us' },
|
id: { _serialized: row.wamid, user: row.wamid, server: 'c.us' },
|
||||||
body: row.body ?? '',
|
body: row.body ?? '',
|
||||||
@ -93,7 +83,7 @@ export class WhatsappService {
|
|||||||
timestamp: Number(row.timestamp),
|
timestamp: Number(row.timestamp),
|
||||||
hasMedia: row.type !== 'text',
|
hasMedia: row.type !== 'text',
|
||||||
media: null,
|
media: null,
|
||||||
chatId: this.toWaId(row.chat_id),
|
chatId: row.chat_id,
|
||||||
status: row.status,
|
status: row.status,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@ -122,7 +112,6 @@ export class WhatsappService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(to: string, message: string, _media?: { data: string; mimetype: string; filename?: string }, senderName?: string) {
|
async sendMessage(to: string, message: string, _media?: { data: string; mimetype: string; filename?: string }, senderName?: string) {
|
||||||
const cleanTo = this.cleanPhone(to);
|
|
||||||
const token = process.env.META_ACCESS_TOKEN;
|
const token = process.env.META_ACCESS_TOKEN;
|
||||||
const phoneNumberId = process.env.META_PHONE_NUMBER_ID;
|
const phoneNumberId = process.env.META_PHONE_NUMBER_ID;
|
||||||
const apiVersion = process.env.META_API_VERSION || 'v25.0';
|
const apiVersion = process.env.META_API_VERSION || 'v25.0';
|
||||||
@ -144,7 +133,7 @@ export class WhatsappService {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
messaging_product: 'whatsapp',
|
messaging_product: 'whatsapp',
|
||||||
recipient_type: 'individual',
|
recipient_type: 'individual',
|
||||||
to: cleanTo,
|
to,
|
||||||
type: 'text',
|
type: 'text',
|
||||||
text: { preview_url: false, body },
|
text: { preview_url: false, body },
|
||||||
}),
|
}),
|
||||||
@ -153,18 +142,18 @@ export class WhatsappService {
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json().catch(() => ({}));
|
const error = await response.json().catch(() => ({}));
|
||||||
this.logger.error(`[Meta] Falha ao enviar mensagem para ${cleanTo}:`, error);
|
this.logger.error(`[Meta] Falha ao enviar mensagem para ${to}:`, error);
|
||||||
throw new Error(`Meta API erro ${response.status}: ${JSON.stringify(error)}`);
|
throw new Error(`Meta API erro ${response.status}: ${JSON.stringify(error)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json() as any;
|
const result = await response.json() as any;
|
||||||
const wamid = result?.messages?.[0]?.id;
|
const wamid = result?.messages?.[0]?.id;
|
||||||
this.logger.log(`[Meta] Mensagem enviada para ${cleanTo}: ${wamid}`);
|
this.logger.log(`[Meta] Mensagem enviada para ${to}: ${wamid}`);
|
||||||
|
|
||||||
if (wamid) {
|
if (wamid) {
|
||||||
await this.mensagensRepository.save({
|
await this.mensagensRepository.save({
|
||||||
wamid,
|
wamid,
|
||||||
chatId: cleanTo,
|
chatId: to,
|
||||||
fromMe: true,
|
fromMe: true,
|
||||||
type: 'text',
|
type: 'text',
|
||||||
body: body,
|
body: body,
|
||||||
@ -174,7 +163,7 @@ export class WhatsappService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.assignmentService.clearTransferNote(cleanTo);
|
await this.assignmentService.clearTransferNote(to);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,7 +257,7 @@ export class WhatsappService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.gateway.emitNewMessage({
|
this.gateway.emitNewMessage({
|
||||||
from: this.toWaId(from),
|
from,
|
||||||
body: messageBody,
|
body: messageBody,
|
||||||
timestamp,
|
timestamp,
|
||||||
isGroupMsg: false,
|
isGroupMsg: false,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user