2020-07-13 14:35:11 +00:00
const { Job } = require ( 'flitter-jobs' )
class PasswordResetAlertJob extends Job {
static get services ( ) {
return [ ... super . services , 'models' , 'jobs' , 'output' , 'configs' ]
}
async execute ( job ) {
const { data } = job
const { user _id , ip } = data
try {
const User = this . models . get ( 'auth:User' )
const user = await User . findById ( user _id )
if ( ! user ) throw new Error ( 'Unable to find user with ID: ' + user _id )
this . output . info ( 'Sending password reset alert to user.' )
await this . jobs . queue ( 'mailer' ) . add ( 'EMail' , {
to : user . email ,
subject : ` Security Alert | ${ this . configs . get ( 'app.name' ) } ` ,
email _params : {
header _text : 'Your Password Was Reset' ,
body _paragraphs : [
` The password to your ${ this . configs . get ( 'app.name' ) } account ( ${ user . uid } ) was recently reset from the IP ${ ip } . ` ,
'If this was you, please disregard this email. Otherwise, please contact your administrator for assistance recovering your account.' ,
] ,
} ,
} )
2020-09-02 13:27:09 +00:00
if ( user . notify _config && user . notify _config . active ) {
await user . notify _config . log ( {
title : ` ${ this . configs . get ( 'app.name' ) } : Password Reset ` ,
message : ` The password to your account ( ${ user . uid } ) was reset from the IP address ${ ip } . If this was not you, please contact your system administrator. ` ,
priority : 8 ,
} )
}
2020-07-13 14:35:11 +00:00
} catch ( e ) {
this . output . error ( e )
}
}
}
module . exports = exports = PasswordResetAlertJob