// --------- Dependencies ---------
var mongoose = require('mongoose');

module.exports = function(UserSchema, messages) {
  ['Facebook', 'YouTube'].forEach(function(serviceName) {
    /**
     * Populate service identifiers and tokens.
     * @param  {ObjectId} id          The current user's id in MongoDB
     * @param  {Object}   service     User-specific details for the service
     * @param  {Function} done        The callback function to execute upon
     *                                completion
     */
    UserSchema.statics['add' + serviceName] = function(id, service, done) {
      mongoose.models.User.findById(id, function(err, user) {
        // Database Error
        if (err) {
          return done(new Error(messages.ERROR.GENERAL));
        }

        // Unexpected Error: User not found
        if (!user) {
          return done(new Error(messages.ERROR.GENERAL));
        }

        if (service.reauth) {
          return done(messages.STATUS[serviceName.toUpperCase()]
            .MISSING_PERMISSIONS);
        } else if (service.refreshAccessToken) {
          delete service.refreshAccessToken;
          user[serviceName.toLowerCase()] = service;
          user.save(function(err) {
            // Database Error
            if (err) {
              return done(new Error(messages.ERROR.GENERAL));
            }

            // Success: Refreshed access token for service
            return done(messages.STATUS[serviceName.toUpperCase()].RENEWED);
          });
        } else if (user['has' + serviceName]) {
          // Defined Error: Service already exists
          return done(new Error(messages.STATUS[serviceName.toUpperCase()]
            .ALREADY_CONNECTED));
        } else {
          // Save service information (excluding other states) to account
          delete service.reauth;
          delete service.refreshAccessToken;
          user[serviceName.toLowerCase()] = service;
          user.save(function(err) {
            // Database Error
            if (err) {
              return done(new Error(messages.ERROR.GENERAL));
            }

            // Success: Added service
            return done(null, user);
          });
        }
      });
    };

    /**
     * Check if the user is connected to the service.
     * @return {Boolean} A status of whether the user has added this service
     */
    UserSchema.virtual('has' + serviceName).get(function() {
      return Boolean(this[serviceName.toLowerCase()].profileId);
    });

    /**
     * Enable or disable updates for a service.
     * @param {Function} done The callback function to execute upon completion
     */
    UserSchema.methods['toggle' + serviceName] = function(done) {
      mongoose.models.User.findById(this._id, function(err, user) {
        // Database Error
        if (err) {
          return done(new Error(messages.ERROR.GENERAL));
        }

        var message = messages.STATUS[serviceName.toUpperCase()].NOT_CONFIGURED;
        if (user['has' + serviceName]) {
          message = user[serviceName.toLowerCase()].acceptUpdates ?
            messages.STATUS[serviceName.toUpperCase()].UPDATES_DISABLED :
            messages.STATUS[serviceName.toUpperCase()].UPDATES_ENABLED;
          user[serviceName.toLowerCase()].acceptUpdates =
            !user[serviceName.toLowerCase()].acceptUpdates;
        }

        user.save(function(err) {
          // An error occurred
          if (err) {
            return done(new Error(messages.ERROR.GENERAL));
          }

          // Saved update preference
          return done(null, message);
        });
      });
    };
  });
};

@@ -66,16 +66,664 @@
 ose');%0A%0A
+/**%0A * Saves the update for the user into the database or returns a general error%0A * on failure.%0A * @param  %7BObject%7D   user    The user object%0A * @param  %7BObject%7D   obj     The object to return to the user%0A * @param  %7Bstring%7D   message The message to display on an error%0A * @param  %7BFunction%7D done    The callback function to execute upon completion%0A * @return %7BFunction%7D         Execute the callback function%0A */%0Afunction saveUpdate(user, obj, message, done) %7B%0A  return user.save(function(err) %7B%0A    // An error occurred%0A    if (err) %7B%0A      return done(new Error(message));%0A    %7D%0A%0A    // Saved object to user%0A    return done(null, obj);%0A  %7D);%0A%7D%0A%0A
 module.e
@@ -2581,250 +2581,66 @@
     
-user.save(function(err) %7B%0A            // Database Error%0A            if (err) %7B%0A              return done(new Error(messages.ERROR.GENERAL));%0A            %7D%0A%0A            // Success: Added service%0A            return done(null, user);%0A          %7D
+return saveUpdate(user, user, messages.ERROR.GENERAL, done
 );%0A 
@@ -3816,243 +3816,69 @@
     
-user.save(function(err) %7B%0A          // An error occurred%0A          if (err) %7B%0A            return done(new Error(messages.ERROR.GENERAL));%0A          %7D%0A%0A          // Saved update preference%0A          return done(null, message);%0A        %7D
+return saveUpdate(user, message, messages.ERROR.GENERAL, done
 );%0A 

Resolve similar code