<commit_before>
/**
 * Module dependencies
 */

var async = require('async');
var ldap  = require('./ldap.js');
var model = require('./model.js');
var user  = model.User;

/**
 * find user for device
 * @param device
 */

module.exports.findUserForDevice = function(device_id, fn) {
  async.waterfall([
      /**
       * check if ldap user exists for usb or nfc id
       * @param next
       */
        function findLdapUser(next) {
        ldap.findUserForDevice(id, function(err, ldapUser) {
          if (err) {
            return next(err, null);
          }
          next(null, ldapUser);
        });
      },
      /**
       * if no ldap user exists, check local db
       *
       * @param res
       * @param next
       */
        function findCascadeUser(user, next) {
        if (user !== null) {
          return next(null, user.uid); //ldap user exists, skip local db
        }

        User.find({
          where: ['nfcID=? or usbSerial=?', id, id],
          attributes: ['username']
        })
          .error(function(err) {
            throw err;
          })
          .success(function(user) {
            var username = user ? user.username : null;
            next(false, username);
          });
      },
    ],
    /**
     * done
     *
     * @param err
     * @param user
     */
    function done(err, user) {
      if (err) {
        log.error('find user for device failed');
      }
      fn(err, user)
    }
  );
};<commit_msg>refactor<commit_after>
/**
 * Module dependencies
 */

var async = require('async');
var ldap  = require('./ldap.js');
var model = require('./model.js');
var User  = model.User;

// ------------------------------------------------------------------------------------
// Find cascade user
// ------------------------------------------------------------------------------------

/**
 * find cascade user for given device
 *
 * @param device_id device
 * @param fn callback
 */

module.exports.findUserForDevice = function(device_id, fn) {
  async.waterfall(
    [
      findLdapUser(device_id, next),
      findCascadeUser(device_id, ldapUser, done)
    ],
    function done(err, cascadeUser) {
      if (err) {
        log.error('find user for device failed');
      }
      fn(err, cascadeUser)
    }
  );
};

// ------------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------------

/**
 * find an ldap user for a given device
 *
 * @param device id
 * @param fn callback
 */

function findLdapUser(device, next) {
  ldap.findUserForDevice(device, function(err, ldapUser) {
    if (err) {
      /**
       * device, error, no ldap user
       */
      return next(device, err, undefined);
    }
    /**
     * device, no error, ldap user
     */
    return next(device, undefined, ldapUser);
  });
}

/**
 * find cascade user for given device or ldap user
 *
 * @param device id
 * @param ldapUser user
 * @param fn callback
 */

function findCascadeUser(device, ldapUser, done) {
  User.find({
    where: ['uid = ?, nfcID=? or usbSerial=?',ldapUser.uid, device, device],
    attributes: ['username', 'balance']
  })
  .error(function(err) {
    throw err;
  })
  .success(function(user) {
    return done(undefined, user);
  });
}
{'fraction_non_alphanumeric': 0.23141993957703927, 'fraction_numerical': 0.0, 'mean_word_length': 1.5159574468085106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '444818', 'n_tokens_mistral': 959, 'n_tokens_neox': 950, 'n_words': 245}