@@ -11,6 +11,8 @@ const stripBomStream = require('strip-bom-stream');
1111var randomstring = require ( "randomstring" ) ;
1212var upload = multer ( { dest : 'uploads/' } ) ;
1313const { Op } = require ( "sequelize" ) ;
14+ const EmailUtil = require ( '../utils/emailUtil' )
15+ const { v4 : uuidv4 } = require ( 'uuid' ) ;
1416
1517const router = express . Router ( ) ;
1618
@@ -361,6 +363,9 @@ router.post('/user/:id', (req, res) => {
361363 */
362364
363365router . post ( '/upload/:id' , upload . single ( "file" ) , function ( req , res ) {
366+ var addedToClass = [ ]
367+ var accountCreated = [ ]
368+
364369 Class . findByPk ( req . params . id , {
365370 include :
366371 [
@@ -384,22 +389,29 @@ router.post('/upload/:id', upload.single("file"), function (req, res) {
384389 let section = student_entry [ 'Section' ]
385390 let email = student_entry [ 'Email' ]
386391 if ( email ) {
392+ var reset_password_id = uuidv4 ( ) ;
393+
394+
387395 User . create ( {
388396 username : email ,
389397 first_name : student_entry [ "First" ] ,
390398 last_name : student_entry [ "Last" ] ,
391399 email : email . toLowerCase ( ) ,
392400 password : randomstring . generate ( ) ,
401+ reset_password_id : reset_password_id ,
393402 } )
394- . then ( ( user ) => {
395- resolve ( ) // resolve so that we can send a signal back to the frontend
403+ . then ( ( user ) => {
396404 utils . addStudentToSection ( nb_class , user , section )
405+ const link = req . headers . origin + "/reset?id=" + user . reset_password_id ;
406+ accountCreated . push ( { user, link} )
407+ resolve ( )
397408 } ) . catch ( ( err ) => {
398- resolve ( )
399409 User . findOne ( { where : { email : { [ Op . iLike ] : email } } } )
400- . then ( function ( user ) {
410+ . then ( function ( user ) {
401411 if ( user ) {
402412 utils . addStudentToSection ( nb_class , user , section )
413+ addedToClass . push ( { user} )
414+ resolve ( )
403415 }
404416 } )
405417 } )
@@ -409,7 +421,17 @@ router.post('/upload/:id', upload.single("file"), function (req, res) {
409421 } ) ;
410422 } )
411423 Promise . all ( requests )
412- . then ( ( ) => { res . status ( 200 ) . json ( null ) ; } )
424+ . then ( async ( ) => {
425+ for ( const r of accountCreated ) {
426+ await sendEmailAccountCreatedForClass ( nb_class , r . user , r . link )
427+ }
428+
429+ for ( const r of addedToClass ) {
430+ await sendEmailAddedToClass ( nb_class , r . user )
431+ }
432+
433+ res . status ( 200 ) . json ( null ) ;
434+ } )
413435 . catch ( ( err ) => {
414436 res . status ( 200 ) . json ( null ) ;
415437 } ) ;
@@ -418,6 +440,26 @@ router.post('/upload/:id', upload.single("file"), function (req, res) {
418440 } ) ;
419441} ) ;
420442
443+ async function sendEmailAddedToClass ( nb_class , user ) {
444+ const body = `Hi,<br><br>Your instructor added to ${ nb_class . class_name } .<br><br><a href="https://nb.mit.edu">https://nb.mit.edu</a>`
445+ const email = new EmailUtil ( ) . to ( user . email ) . subject ( `[NB] You've been added to ${ nb_class . class_name } ` ) . userId ( user . id ) . emailType ( 'SYSTEM' ) . html ( body )
446+ try {
447+ await email . send ( )
448+ } catch ( error ) {
449+ console . error ( error . message ) ;
450+ }
451+ }
452+
453+ async function sendEmailAccountCreatedForClass ( nb_class , user , resetLink ) {
454+ const body = `Hi,<br><br>Welcome to NB, your instructor created your account for ${ nb_class . class_name } . Please use the following link to set your password: <a href="${ resetLink } ">${ resetLink } </a>`
455+ const email = new EmailUtil ( ) . to ( user . email ) . subject ( `[NB] Your account created by instructor for ${ nb_class . class_name } ` ) . userId ( user . id ) . emailType ( 'SYSTEM' ) . html ( body )
456+ try {
457+ await email . send ( )
458+ } catch ( error ) {
459+ console . error ( error . message ) ;
460+ }
461+ }
462+
421463/**
422464 * Remove a student from a given class
423465 * @name DELETE/api/classes/student/:courseid/:userid
0 commit comments