<commit_before>/**
 * User Model
 * ---------------
 * This file acts as a controller, co-ordinating between two
 * database backends: MongoDB (through Mongoose) and MySQL
 * (through Sequelize).
 *
 * All non-create calls will run through MySQL first, and if
 * a record cannot be found, data will be checked against the
 * contents of the MongoDB.
 *
 * Anytime a record for a user is accessed in MongoDB, it will
 * be immediately copied into MySQL, which will return the
 * data to the client.
 *
 * TODOs:
 *  1) Add logging to all calls on success/error
 *     - https://bugzilla.mozilla.org/show_bug.cgi?id=882970
 *  2) Healthcheck
 *     - https://bugzilla.mozilla.org/show_bug.cgi?id=867328
 */

module.exports = function ( env ) {
  var mongoose = require( "../../../lib/mongoose" )( env ),
      sqlHandle = require( "./sqlController" )( env ),
      mongoHandle = require( "./mongoController" )( mongoose.conn ),
      emailer = require( "../../../lib/emailer" );

  /**
   * Model Access methods
   */
  return {
    /**
     * getUser( id, callback )
     * -
     * id: username, email or sql id
     * callback: function( err, user )
     */
    getUser: function( id, callback ) {
      // First, check the MySQL db
      sqlHandle.getUser( id, function( err, user ){
        if ( err ) {
          return callback( err, null );
        }

        if ( user ) {
          return callback( null, user.getValues() );
        }

        // No user, check mongo
        mongoHandle.getUser( id, function( err, user ){
          if ( err ) {
            return callback( err, null );
          }

          // No user at all?
          if ( !user ) {
            return callback();
          }

          sqlHandle.createUser( user, function( err, thisUser ){
            if ( err ) {
              return callback( err );
            }

            return callback( null, thisUser.getValues() );
          });
        });
      });
    },
    /**
     * createUser( data, callback )
     * -
     * data: JSON object containing user fields
     * callback: function( err, thisUser )
     */
    createUser: function( data, callback ) {
      sqlHandle.createUser( data, function( sqlErr, user ) {
        if ( sqlErr ) {
          return callback( sqlErr );
        }

        emailer.sendWelcomeEmail({
          to: user.email,
          fullName: user.fullName
        }, function( emailEerr, msg ) {
          if ( emailEerr ) {
            // non-fatal error
            console.error( emailErr );
          }
          if ( msg ) {
            console.log( "Sent welcome email with id %s", msg.MessageId) ;
          }

          callback( null, user );
        });
      });
    },

    /**
     * updateUser( id, data, callback )
     * -
     * id: username, email or _id<commit_msg>No bug - Fix emailErr variable name<commit_after>/**
 * User Model
 * ---------------
 * This file acts as a controller, co-ordinating between two
 * database backends: MongoDB (through Mongoose) and MySQL
 * (through Sequelize).
 *
 * All non-create calls will run through MySQL first, and if
 * a record cannot be found, data will be checked against the
 * contents of the MongoDB.
 *
 * Anytime a record for a user is accessed in MongoDB, it will
 * be immediately copied into MySQL, which will return the
 * data to the client.
 *
 * TODOs:
 *  1) Add logging to all calls on success/error
 *     - https://bugzilla.mozilla.org/show_bug.cgi?id=882970
 *  2) Healthcheck
 *     - https://bugzilla.mozilla.org/show_bug.cgi?id=867328
 */

module.exports = function ( env ) {
  var mongoose = require( "../../../lib/mongoose" )( env ),
      sqlHandle = require( "./sqlController" )( env ),
      mongoHandle = require( "./mongoController" )( mongoose.conn ),
      emailer = require( "../../../lib/emailer" );

  /**
   * Model Access methods
   */
  return {
    /**
     * getUser( id, callback )
     * -
     * id: username, email or sql id
     * callback: function( err, user )
     */
    getUser: function( id, callback ) {
      // First, check the MySQL db
      sqlHandle.getUser( id, function( err, user ){
        if ( err ) {
          return callback( err, null );
        }

        if ( user ) {
          return callback( null, user.getValues() );
        }

        // No user, check mongo
        mongoHandle.getUser( id, function( err, user ){
          if ( err ) {
            return callback( err, null );
          }

          // No user at all?
          if ( !user ) {
            return callback();
          }

          sqlHandle.createUser( user, function( err, thisUser ){
            if ( err ) {
              return callback( err );
            }

            return callback( null, thisUser.getValues() );
          });
        });
      });
    },
    /**
     * createUser( data, callback )
     * -
     * data: JSON object containing user fields
     * callback: function( err, thisUser )
     */
    createUser: function( data, callback ) {
      sqlHandle.createUser( data, function( sqlErr, user ) {
        if ( sqlErr ) {
          return callback( sqlErr );
        }

        emailer.sendWelcomeEmail({
          to: user.email,
          fullName: user.fullName
        }, function( emailErr, msg ) {
          if ( emailErr ) {
            // non-fatal error
            console.error( emailErr );
          }
          if ( msg ) {
            console.log( "Sent welcome email with id %s", msg.MessageId) ;
          }

          callback( null, user );
        });
      });
    },

    /**
     * updateUser( id, data, callback )
     * -
     * id: username, email or _id
{'fraction_non_alphanumeric': 0.12872869318181818, 'fraction_numerical': 0.004971590909090909, 'mean_word_length': 1.7029750479846448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '737531', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1597, 'n_words': 498}