@@ -231,38 +231,42 @@ export async function sendOtpAndNotify(username): Promise<any> {
231231 const usuario = await AuthUsers . findOne ( { usuario : username } ) ;
232232
233233 // Se mantiene la validación para usuarios temporales con email
234- if ( usuario && usuario . tipo === 'temporal' && usuario . email ) {
235- // Genera un código OTP de 6 dígitos
236- const otpCode = crypto . randomInt ( 100000 , 999999 ) . toString ( ) ;
237-
238- usuario . otp = {
239- code : otpCode ,
240- expiresAt : new Date ( Date . now ( ) + 60 * 60 * 1000 ) , // 10 minutos en milisegundos
241- } ;
242-
243- usuario . audit ( userScheduler ) ;
244- await usuario . save ( ) ;
245-
246- const extras : any = {
247- titulo : 'Código de Verificación' ,
248- usuario,
249- otpCode,
250- } ;
251- const htmlToSend = await renderHTML ( 'emails/otp-code.html' , extras ) ;
252-
253- const options : MailOptions = {
254- from : enviarMail . auth . user ,
255- to : usuario . email . toString ( ) ,
256- subject : 'Tu código de verificación' ,
257- text : '' ,
258- html : htmlToSend ,
259- attachments : null ,
260- } ;
261- await sendMail ( options ) ;
234+ if ( usuario ) {
235+ if ( usuario . tipo === 'temporal' && usuario . email ) {
236+ // Genera un código OTP de 6 dígitos
237+ const otpCode = crypto . randomInt ( 100000 , 999999 ) . toString ( ) ;
238+
239+ usuario . otp = {
240+ code : otpCode ,
241+ expiresAt : new Date ( Date . now ( ) + 60 * 60 * 1000 ) , // 10 minutos en milisegundos
242+ } ;
243+
244+ usuario . audit ( userScheduler ) ;
245+ await usuario . save ( ) ;
246+
247+ const extras : any = {
248+ titulo : 'Código de Verificación' ,
249+ usuario,
250+ otpCode,
251+ } ;
252+ const htmlToSend = await renderHTML ( 'emails/otp-code.html' , extras ) ;
253+
254+ const options : MailOptions = {
255+ from : enviarMail . auth . user ,
256+ to : usuario . email . toString ( ) ,
257+ subject : 'Tu código de verificación' ,
258+ text : '' ,
259+ html : htmlToSend ,
260+ attachments : null ,
261+ } ;
262+ await sendMail ( options ) ;
263+ } else {
264+ return null ;
265+ }
262266 return usuario ;
263267 } else {
264268 // El usuario no existe o no es un usuario temporal con email
265- return null ;
269+ throw { tipo : 'cuentaInexistenteAndes' } ;
266270 }
267271 } catch ( error ) {
268272 throw error ;
0 commit comments