@@ -253,38 +253,42 @@ export async function sendOtpAndNotify(username): Promise<any> {
253253 const usuario = await AuthUsers . findOne ( { usuario : username } ) ;
254254
255255 // Se mantiene la validación para usuarios temporales con email
256- if ( usuario && usuario . tipo === 'temporal' && usuario . email ) {
257- // Genera un código OTP de 6 dígitos
258- const otpCode = crypto . randomInt ( 100000 , 999999 ) . toString ( ) ;
259-
260- usuario . otp = {
261- code : otpCode ,
262- expiresAt : new Date ( Date . now ( ) + 60 * 60 * 1000 ) , // 10 minutos en milisegundos
263- } ;
264-
265- usuario . audit ( userScheduler ) ;
266- await usuario . save ( ) ;
267-
268- const extras : any = {
269- titulo : 'Código de Verificación' ,
270- usuario,
271- otpCode,
272- } ;
273- const htmlToSend = await renderHTML ( 'emails/otp-code.html' , extras ) ;
274-
275- const options : MailOptions = {
276- from : enviarMail . auth . user ,
277- to : usuario . email . toString ( ) ,
278- subject : 'Tu código de verificación' ,
279- text : '' ,
280- html : htmlToSend ,
281- attachments : null ,
282- } ;
283- await sendMail ( options ) ;
256+ if ( usuario ) {
257+ if ( usuario . tipo === 'temporal' && usuario . email ) {
258+ // Genera un código OTP de 6 dígitos
259+ const otpCode = crypto . randomInt ( 100000 , 999999 ) . toString ( ) ;
260+
261+ usuario . otp = {
262+ code : otpCode ,
263+ expiresAt : new Date ( Date . now ( ) + 60 * 60 * 1000 ) , // 10 minutos en milisegundos
264+ } ;
265+
266+ usuario . audit ( userScheduler ) ;
267+ await usuario . save ( ) ;
268+
269+ const extras : any = {
270+ titulo : 'Código de Verificación' ,
271+ usuario,
272+ otpCode,
273+ } ;
274+ const htmlToSend = await renderHTML ( 'emails/otp-code.html' , extras ) ;
275+
276+ const options : MailOptions = {
277+ from : enviarMail . auth . user ,
278+ to : usuario . email . toString ( ) ,
279+ subject : 'Tu código de verificación' ,
280+ text : '' ,
281+ html : htmlToSend ,
282+ attachments : null ,
283+ } ;
284+ await sendMail ( options ) ;
285+ } else {
286+ return null ;
287+ }
284288 return usuario ;
285289 } else {
286290 // El usuario no existe o no es un usuario temporal con email
287- return null ;
291+ throw { tipo : 'cuentaInexistenteAndes' } ;
288292 }
289293 } catch ( error ) {
290294 throw error ;
0 commit comments