refs #6427 perf: restore method

This commit is contained in:
Javier Segarra 2024-03-20 13:48:54 +01:00
parent 6a47493180
commit 01f23abec9
1 changed files with 11 additions and 9 deletions

View File

@ -1,4 +1,3 @@
module.exports = Self => {
Self.remoteMethod('recoverPassword', {
description: 'Send email to the user',
@ -22,17 +21,20 @@ module.exports = Self => {
});
Self.recoverPassword = async function(user, app) {
const usesPhone = new RegExp(/([+]\d{2})?\d{9}/, 'g').test(user);
const account = await Self.app.models.Application.rawSql(
`SELECT c.id, c.phone, u.email from account.user u, vn.client c
where c.id=u.id and( u.email = ? or u.name = ? or c.phone = ?)`,
[user, user, user]);
const models = Self.app.models;
if (!account || account.length > 1) return;
const {email, phone} = account[0];
const usesEmail = user.indexOf('@') !== -1;
if (!usesEmail) {
const account = await models.VnUser.findOne({
fields: ['email'],
where: {name: user}
});
if (!account) return;
user = account.email;
}
try {
await Self.resetPassword({email, phone, emailTemplate: 'recover-password', app, usesPhone});
await Self.resetPassword({email: user, emailTemplate: 'recover-password', app});
} catch (err) {
if (err.code === 'EMAIL_NOT_FOUND')
return;