Optimize forgot-password whatsapp number matching and automatic DDI 55 prefix injection
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 1m4s Details

This commit is contained in:
Sidney Gomes 2026-07-23 22:48:51 -03:00
parent b6a57c6f00
commit a4f42a31ec
1 changed files with 26 additions and 4 deletions

View File

@ -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';