mercredi 21 juin 2017

User Model as Observer of Event in ExpressJS with Mongodb

I have an express application where administrators should receive an instant notification when a new user registers into the database. Note that I am using Passport to log users in via the Google strategy:

  passport.use(new GoogleStrategy({
    clientID    : configAuth.googleAuth.clientID,
    clientSecret: configAuth.googleAuth.clientSecret,
    callbackURL : configAuth.googleAuth.callbackURL,
  },
  function(token, refreshToken, profile, done){

    process.nextTick(function(){
      // try to find a user based on their google id
      User.findOne({ 'google.id' : profile.id }, function(err, user){
        if(err)
          return done(err)

        if(user){
          // if a user is found, log them in
          return done(null, user)
        } else {

          // if the user isn't in our database, create a new user
          var newUser = new User({
            'google.id': profile.id,
            'google.token': token,
            'google.name': profile.displayName,
            'google.email': profile.emails[0].value,  //pull the first email
            'google.active': 0,
            'google.level': 0
          })

          // save the user
          newUser.save(function(err){
            if(err)
              throw err
            return done(null, newUser)
          })

          // Give the new user the role of 'inactive'
          acl.addUserRoles(newUser.id, 'inactive', function(err){})

          // Email a notice to the new user that they have to be accepted by an administrator.
          ...

          // Send an email to all "admin-level" users to activate this user.
          acl.roleUsers('admin', function(err, users){
            // Loop through all admin users
            users.forEach(function(value){
              // Find each individual admin user by _id
              User.findOne({ '_id' : value }, function(err, user){
                if(err)
                  return done(err)

                // Send this admin an internal mail (IMail)
                var iMail = new IMail()
                iMail.body = newUser.google.name + ' needs approval.',

                // Send each administrator a notification that a new user needs approval
                iMail.recipients.push(user.id)
                iMail.save(function(err){
                  if(err)
                    return err
                  console.log("IMail was sent to notify admin " + user.google.name + " to approve " + newUser.google.name + " as a system user.")
                })
              })
            })
          })
        }
      })
    })
  }))

Within the

iMail.save(function(err){
      ...
})

block, I know that some type of event needs to be fired which would somehow alert each admin who is currently logged in. Unfortunately, this is where my understanding breaks down at this point. How do I immediately notify an admin of this new notification? I'd like it to be like Gmail, for example, when the Inbox automatically displays (1) when a new email is sent.

As a side note, I've read up on the Publisher/Subscriber pattern (http://ift.tt/2sQtVAW) and think this might set me on the right track, I'm just not sure exactly how to implement it. A little push in the right direction from somebody would be appreciated.

Aucun commentaire:

Enregistrer un commentaire