refs #6427 feat: use query for multiple models

This commit is contained in:
Javier Segarra 2024-02-14 08:08:47 +01:00
parent 19bbb4a4bf
commit e9645610d2
1 changed files with 27 additions and 7 deletions

View File

@ -1,3 +1,5 @@
const server = require('vn-loopback/server/server');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('recoverPassword', { Self.remoteMethod('recoverPassword', {
description: 'Send email to the user', description: 'Send email to the user',
@ -21,20 +23,38 @@ module.exports = Self => {
}); });
Self.recoverPassword = async function(user, app) { Self.recoverPassword = async function(user, app) {
const models = Self.app.models; // const models = Self.app.models;
const usesEmail = user.indexOf('@') !== -1; // const usesEmail = user.indexOf('@') !== -1;
const usesPhone = new RegExp(/([+]\d{2})?\d{9}/, 'g').test(user);
/* if (usesPhone) {
const worker = await models.Client.findOne({
fields: ['email', 'phone'],
where: {or: [{name: user, phone: user}]}
});
if (!worker) return;
user = worker.email;
}
if (!usesEmail) { if (!usesEmail) {
const account = await models.VnUser.findOne({ const account = await models.VnUser.findOne({
fields: ['email'], fields: ['email', 'phone'],
where: {name: user} where: {or: [{name: user, phone: user}]}
}); });
if (!account) return; if (!account) return;
user = account.email; user = account.email;
} }*/
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]);
if (!account || account.length > 1) return;
const {email, phone} = account[0];
try { try {
await Self.resetPassword({email: user, emailTemplate: 'recover-password', app}); // if (usesPhone) {
// await models.Sms.send(ctx, user, Self.resetPassword({email: user, usesPhone}));
// } else {
await Self.resetPassword({email, phone, emailTemplate: 'recover-password', app, usesPhone});
// }
} catch (err) { } catch (err) {
if (err.code === 'EMAIL_NOT_FOUND') if (err.code === 'EMAIL_NOT_FOUND')
return; return;