From a4f42a31ec031ec5415eb474d200f0030a86406f Mon Sep 17 00:00:00 2001 From: Sidney Gomes Date: Thu, 23 Jul 2026 22:48:51 -0300 Subject: [PATCH] Optimize forgot-password whatsapp number matching and automatic DDI 55 prefix injection --- server.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/server.ts b/server.ts index 6914eca..639035a 100644 --- a/server.ts +++ b/server.ts @@ -248,9 +248,26 @@ app.post('/api/auth/forgot-password', async (req: Request, res: Response) => { } try { - const user = await prisma.user.findFirst({ - where: channel === 'whatsapp' ? { whatsapp: identifier } : { email: identifier.toLowerCase() } - }); + let user; + if (channel === 'whatsapp') { + const cleanIdentifier = identifier.replace(/\D/g, ''); + const numberWithoutDDI = cleanIdentifier.replace(/^55/, ''); + const numberWithDDI = '55' + numberWithoutDDI; + + user = await prisma.user.findFirst({ + where: { + OR: [ + { whatsapp: cleanIdentifier }, + { whatsapp: numberWithoutDDI }, + { whatsapp: numberWithDDI } + ] + } + }); + } else { + user = await prisma.user.findFirst({ + where: { email: identifier.toLowerCase() } + }); + } if (!user) { res.json({ success: true, message: 'Se o usuário existir, o link foi enviado.' }); @@ -1342,9 +1359,14 @@ export async function sendWhatsappNotification(phone: string, text: string) { } // Clean phone number (leave only digits) - const cleanPhone = phone.replace(/\D/g, ''); + let cleanPhone = phone.replace(/\D/g, ''); if (!cleanPhone) return false; + // Add DDI 55 for Brazil if not present + if (cleanPhone.length === 10 || cleanPhone.length === 11) { + cleanPhone = '55' + cleanPhone; + } + const apiUrl = settings.evolutionApiUrl.replace(/\/$/, ''); const instance = settings.evolutionInstance || 'microtecflix';