diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 9ec45f58dd..a14631c224 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -304,6 +304,42 @@ module.exports = Self => { const assignmentChanged = workerIdBefore != workerIdAfter; if (assignmentChanged) await Self.notifyAssignment(instance, workerIdBefore, workerIdAfter); + + const emails = instance.email ? instance.email.split(',') : null; + + const findParams = []; + if (emails.length) { + for (let email of emails) + findParams.push({email: email}); + } + + if (instance.phone) + findParams.push({phone: instance.phone}); + + if (instance.mobile) + findParams.push({mobile: instance.mobile}); + + const filterObj = { + where: { + and: [ + {or: findParams}, + {id: {neq: instance.id}} + ] + } + }; + + const clientSameData = await Self.findOne(filterObj); + + if (clientSameData) { + await Self.app.models.Mail.create({ + receiver: 'direccioncomercial@verdnatura.es', + subject: `Cliente con email/teléfono/móvil duplicados`, + body: 'El cliente ' + instance.id + ' comparte alguno de estos datos con el cliente ' + clientSameData.id + + '\n- Email: ' + instance.email + + '\n- Teléfono: ' + instance.phone + + '\n- Móvil: ' + instance.mobile + }); + } }); // Send notification on client worker assignment