module.exports = Self => { Self.remoteMethod('recoverPassword', { description: 'Send email to the user', accepts: [ { arg: 'user', type: 'string', description: 'The user name or email', required: true }, { arg: 'app', type: 'string', description: 'The directory for mail' } ], http: { path: `/recoverPassword`, verb: 'POST' } }); 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]); if (!account || account.length > 1) return; const {email, phone} = account[0]; try { await Self.resetPassword({email, phone, emailTemplate: 'recover-password', app, usesPhone}); } catch (err) { if (err.code === 'EMAIL_NOT_FOUND') return; else throw err; } }; };