<commit_before>var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
var constants = require('./constants.js');

var buildHeaders = function(options) {
  var headers = {
    Accept: options.accept || 'application/json',
    'Content-Type': options.contentType || 'application/json',
    'User-Agent': 'smartsheet-javascript-sdk'
  };
  if(options.accessToken) {
    headers.Authorization = 'Bearer ' + options.accessToken;
  }
  if (options.fileName) {
    headers['Content-Disposition'] = 'attachment; filename="'+options.fileName +'"';
    headers['Content-Length'] = options.fileSize;
  }
  return headers;
};

exports.handleResponse = function(response, body) {
  if (response.statusCode != 200) {
    var errorResponse = {
      statusCode: response.statusCode
    };
    if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
      var responseBody = JSON.parse(body);
      errorResponse.errorCode = responseBody.errorCode;
      errorResponse.message = responseBody.message;
    } else {
      errorResponse.message = body;
    }
    return new Promise.reject(errorResponse);
  } else if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
    return JSON.parse(body);
  } else {
    return body;
  }
};

var buildUrl = function(options) {
  var baseUrl = process.env.SMARTSHEET_API_HOST || 'https://api.smartsheet.com/';
  if (options.id) {
    return baseUrl + options.url + options.id;
  } else {
    return baseUrl + (options.url || '');
  }
};

exports.get = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters
  };

  return makeRequestWithRetries(requestOptions, request.getAsync, callback, options);
};

exports.post = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: JSON.stringify(options.body)
  };

  return makeRequestWithRetries(requestOptions, request.postAsync, callback, options);
};

exports.postFile = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: options.fileStream
  };

  return makeRequestWithRetries(requestOptions, request.postAsync, callback, options);
};

exports.delete = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters
  };

  return makeRequestWithRetries(requestOptions, request.delAsync, callback, options);
};

exports.put = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: JSON.stringify(options.body)
  };

  return makeRequestWithRetries(requestOptions, request.putAsync, callback, options);
};

function makeRequestWithRetries(requestOptions, requestMethod, callback, options) {
  var maxRetryTime = options.maxRetryTime || constants.maxRetryTime;
  var calcBackoff = options.calcRetryBackoff || defaultCalcBackoff;

  var endRetryTime = Date.now() + maxRetryTime;

  return retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, 0)
    .nodeify(callback);
}

function retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries) {
  return makeRequest(requestOptions, requestMethod)
    .catch(retryWithBackoffHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries));
}

function retryWithBackoffHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries) {
    return function (error) {
      if (!shouldRetry(error) || Date.now() + calcBackoff(numRetries) >= endRetryTime) {
        return new Promise.reject(error);
      }

      // var backoffInSeconds = (calcBackoff(numRetries) / 1000).toFixed(1);
      // console.log('HttpError status_code=' + error.statusCode + ': Retrying in ' + backoffInSeconds + ' seconds');

      return Promise.delay(calcBackoff(numRetries))
        .then(function () {
          return retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries + 1);
        });
    };
}

function shouldRetry(error) {
  return error.errorCode === 4001 ||
    error.errorCode === 4002 ||
    error.errorCode === 4003 ||
    error.errorCode === 4004;
}

function defaultCalcBackoff(numRetries) {
  return (Math.pow(2, numRetries) + Math.random()) * 1000;
}

function makeRequest (requestOptions, requestMethod) {
  return requestMethod(requestOptions)
  .catch(function(error) {
    return new Promise.reject(error);
  })
  .spread(function(response, body) {
    return exports.handleResponse(response, body);
  });
}

var internal = exports.internal = {};
internal.buildHeaders = buildHeaders;
internal.buildUrl = buildUrl;
<commit_msg>Add ref id to errors<commit_after>var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
var constants = require('./constants.js');

var buildHeaders = function(options) {
  var headers = {
    Accept: options.accept || 'application/json',
    'Content-Type': options.contentType || 'application/json',
    'User-Agent': 'smartsheet-javascript-sdk'
  };
  if(options.accessToken) {
    headers.Authorization = 'Bearer ' + options.accessToken;
  }
  if (options.fileName) {
    headers['Content-Disposition'] = 'attachment; filename="'+options.fileName +'"';
    headers['Content-Length'] = options.fileSize;
  }
  return headers;
};

exports.handleResponse = function(response, body) {
  if (response.statusCode != 200) {
    var errorResponse = {
      statusCode: response.statusCode
    };
    if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
      var responseBody = JSON.parse(body);
      errorResponse.errorCode = responseBody.errorCode;
      errorResponse.message = responseBody.message;
      errorResponse.refId = responseBody.refId;
    } else {
      errorResponse.message = body;
    }
    return new Promise.reject(errorResponse);
  } else if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
    return JSON.parse(body);
  } else {
    return body;
  }
};

var buildUrl = function(options) {
  var baseUrl = process.env.SMARTSHEET_API_HOST || 'https://api.smartsheet.com/';
  if (options.id) {
    return baseUrl + options.url + options.id;
  } else {
    return baseUrl + (options.url || '');
  }
};

exports.get = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters
  };

  return makeRequestWithRetries(requestOptions, request.getAsync, callback, options);
};

exports.post = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: JSON.stringify(options.body)
  };

  return makeRequestWithRetries(requestOptions, request.postAsync, callback, options);
};

exports.postFile = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: options.fileStream
  };

  return makeRequestWithRetries(requestOptions, request.postAsync, callback, options);
};

exports.delete = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters
  };

  return makeRequestWithRetries(requestOptions, request.delAsync, callback, options);
};

exports.put = function(options, callback) {
  var requestOptions = {
    url: buildUrl(options),
    headers: buildHeaders(options),
    qs: options.queryParameters,
    body: JSON.stringify(options.body)
  };

  return makeRequestWithRetries(requestOptions, request.putAsync, callback, options);
};

function makeRequestWithRetries(requestOptions, requestMethod, callback, options) {
  var maxRetryTime = options.maxRetryTime || constants.maxRetryTime;
  var calcBackoff = options.calcRetryBackoff || defaultCalcBackoff;

  var endRetryTime = Date.now() + maxRetryTime;

  return retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, 0)
    .nodeify(callback);
}

function retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries) {
  return makeRequest(requestOptions, requestMethod)
    .catch(retryWithBackoffHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries));
}

function retryWithBackoffHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries) {
    return function (error) {
      if (!shouldRetry(error) || Date.now() + calcBackoff(numRetries) >= endRetryTime) {
        return new Promise.reject(error);
      }

      // var backoffInSeconds = (calcBackoff(numRetries) / 1000).toFixed(1);
      // console.log('HttpError status_code=' + error.statusCode + ': Retrying in ' + backoffInSeconds + ' seconds');

      return Promise.delay(calcBackoff(numRetries))
        .then(function () {
          return retryHelper(requestOptions, requestMethod, endRetryTime, calcBackoff, numRetries + 1);
        });
    };
}

function shouldRetry(error) {
  return error.errorCode === 4001 ||
    error.errorCode === 4002 ||
    error.errorCode === 4003 ||
    error.errorCode === 4004;
}

function defaultCalcBackoff(numRetries) {
  return (Math.pow(2, numRetries) + Math.random()) * 1000;
}

function makeRequest (requestOptions, requestMethod) {
  return requestMethod(requestOptions)
  .catch(function(error) {
    return new Promise.reject(error);
  })
  .spread(function(response, body) {
    return exports.handleResponse(response, body);
  });
}

var internal = exports.internal = {};
internal.buildHeaders = buildHeaders;
internal.buildUrl = buildUrl;
<|endoftext|>


<commit_before>const RequestRepository = require('src/plex/requestRepository.js');

const requestRepository = new RequestRepository();

/**
 * Controller: POST a media id to be donwloaded
 * @param {Request} req http request variable
 * @param {Response} res
 * @returns {Callback}
 */

function submitRequestController(req, res) {
   // This is the id that is the param of the url
   const id = req.params.mediaId;
   const type = req.query.type;
   const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
   const user_agent = req.headers['user-agent'];
   const user = req.headers.loggedInUser;

   requestRepository.sendRequest(id, type, ip, user_agent, user)
      .then(() => {
         res.send({ success: true, message: 'Media item sucessfully requested!' });
      })
      .catch((error) => {
         res.status(500).send({ success: false, error: error.message });
      });
}

module.exports = submitRequestController;
<commit_msg>Get the user variable from req not req.headers<commit_after>const RequestRepository = require('src/plex/requestRepository.js');

const requestRepository = new RequestRepository();

/**
 * Controller: POST a media id to be donwloaded
 * @param {Request} req http request variable
 * @param {Response} res
 * @returns {Callback}
 */

function submitRequestController(req, res) {
   // This is the id that is the param of the url
   const id = req.params.mediaId;
   const type = req.query.type;
   const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
   const user_agent = req.headers['user-agent'];
   const user = req.loggedInUser;

   requestRepository.sendRequest(id, type, ip, user_agent, user)
      .then(() => {
         res.send({ success: true, message: 'Media item sucessfully requested!' });
      })
      .catch((error) => {
         res.status(500).send({ success: false, error: error.message });
      });
}

module.exports = submitRequestController;
<|endoftext|>


<commit_before>exports = module.exports = lifecycle
exports.cmd = cmd
exports.makeEnv = makeEnv

var log = require('npmlog')
var spawn = require('./spawn')
var npm = require('../npm.js')
var path = require('path')
var fs = require('graceful-fs')
var chain = require('slide').chain
var Stream = require('stream').Stream
var PATH = 'PATH'
var uidNumber = require('uid-number')
var umask = require('./umask')

// windows calls it's path 'Path' usually, but this is not guaranteed.
if (process.platform === 'win32') {
  PATH = 'Path'
  Object.keys(process.env).forEach(function (e) {
    if (e.match(/^PATH$/i)) {
      PATH = e
    }
  })
}

function lifecycle (pkg, stage, wd, unsafe, failOk, cb) {
  if (typeof cb !== 'function') {
    cb = failOk
    failOk = false
  }
  if (typeof cb !== 'function') {
    cb = unsafe
    unsafe = false
  }
  if (typeof cb !== 'function') {
    cb = wd
    wd = null
  }

  while (pkg && pkg._data) pkg = pkg._data
  if (!pkg) return cb(new Error('Invalid package data'))

  log.info(stage, pkg._id)
  if (!pkg.scripts || npm.config.get('ignore-scripts')) pkg.scripts = {}

  validWd(wd || path.resolve(npm.dir, pkg.name), function (er, wd) {
    if (er) return cb(er)

    unsafe = unsafe || npm.config.get('unsafe-perm')

    if ((wd.indexOf(npm.dir) !== 0 ||
          wd.indexOf(pkg.name) !== wd.length - pkg.name.length) &&
        !unsafe && pkg.scripts[stage]) {
      log.warn(
        'cannot run in wd',
        '%s %s (wd=%s)', pkg._id, pkg.scripts[stage], wd
      )
      return cb()
    }

    // set the env variables, then run scripts as a child process.
    var env = makeEnv(pkg)
    env.npm_lifecycle_event = stage
    env.npm_node_execpath = env.NODE = env.NODE || process.execPath
    env.npm_execpath = require.main.filename

    // 'nobody' typically doesn't have permission to write to /tmp
    // even if it's never used, sh freaks out.
    if (!npm.config.get('unsafe-perm')) env.TMPDIR = wd

    lifecycle_(pkg, stage, wd, env, unsafe, failOk, cb)
  })
}

function lifecycle_ (pkg, stage, wd, env, unsafe, failOk, cb) {
  var pathArr = []
  var p = wd.split('node_modules')
  var acc = path.resolve(p.shift())

  p.forEach(function (pp) {
    pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
    acc = path.join(acc, 'node_modules', pp)
  })
  pathArr.unshift(path.join(acc, 'node_modules', '.bin'))

  // we also unshift the bundled node-gyp-bin folder so that
  // the bundled one will be used for installing things.
  pathArr.unshift(path.join(__dirname, '..', '..', 'bin', 'node-gyp-bin'))

  if (env[PATH]) pathArr.push(env[PATH])
  env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':')

  var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)

  if (packageLifecycle) {
    // define this here so it's available to all scripts.
    env.npm_lifecycle_script = pkg.scripts[stage]
  }

  function done (er) {
    if (er) {
      if (npm.config.get('force')) {
        log.info('forced, continuing', er)
        er = null
      } else if (failOk) {
        log.warn('continuing anyway', er.message)
        er = null
      }
    }
    cb(er)
  }

  chain(
    [
      packageLifecycle && [runPackageLifecycle, pkg, env, wd, unsafe],
      [runHookLifecycle, pkg, env, wd, unsafe]
    ],
    done
  )
}

function validWd (d, cb) {
  fs.stat(d, function (er, st) {
    if (er || !st.isDirectory()) {
      var p = path.dirname(d)
      if (p === d) {
        return cb(new Error('Could not find suitable wd'))
      }
      return validWd(p, cb)
    }
    return cb(null, d)
  })
}

function runPackageLifecycle (pkg, env, wd, unsafe, cb) {
  // run package lifecycle scripts in the package root, or the nearest parent.
  var stage = env.npm_lifecycle_event
  var cmd = env.npm_lifecycle_script

  var note = '\n> ' + pkg._id + ' ' + stage + ' ' + wd +
             '\n> ' + cmd + '\n'
  runCmd(note, cmd, pkg, env, stage, wd, unsafe, cb)
}

var running = false
var queue = []
function dequeue () {
  running = false
  if (queue.length) {
    var r = queue.shift()
    runCmd.apply(null, r)
  }
}

function runCmd (note, cmd, pkg, env, stage, wd, unsafe, cb) {
  if (running) {
    queue.push([note, cmd, pkg, env, stage, wd, unsafe, cb])
    return
  }

  running = true
  log.pause()
  var user = unsafe ? null : npm.config.get('user')
  var group = unsafe ? null : npm.config.get('group')

  if (log.level !== 'silent') {
    log.clearProgress()
    console.log(note)
    log.showProgress()
  }
  log.verbose('unsafe-perm in lifecycle', unsafe)

  if (process.platform === 'win32') {
    unsafe = true
  }

  if (unsafe) {
    runCmd_(cmd, pkg, env, wd, stage, unsafe, 0, 0, cb)
  } else {
    uidNumber(user, group, function (er, uid, gid) {
      runCmd_(cmd, pkg, env, wd, stage, unsafe, uid, gid, cb)
    })
  }
}

function runCmd_ (cmd, pkg, env, wd, stage, unsafe, uid, gid, cb_) {

  function cb (er) {
    cb_.apply(null, arguments)
    log.resume()
    process.nextTick(dequeue)
  }

  var conf = {
    cwd: wd,
    env: env,
    stdio: [ 0, 1, 2 ]
  }

  if (!unsafe) {
    conf.uid = uid ^ 0
    conf.gid = gid ^ 0
  }

  var sh = 'sh'
  var shFlag = '-c'

  if (process.platform === 'win32') {
    sh = process.env.comspec || 'cmd'
    shFlag = '/c'
    conf.windowsVerbatimArguments = true
  }

  var progressEnabled = log.progressEnabled
  if (progressEnabled) log.disableProgress()
  var proc = spawn(sh, [shFlag, cmd], conf)

  proc.on('error', procError)
  proc.on('close', function (code, signal) {
    if (signal) {
      process.kill(process.pid, signal)
    } else if (code) {
      var er = new Error('Exit status ' + code)
    }
    procError(er)
  })

  function procError (er) {
    if (progressEnabled) log.enableProgress()
    if (er && !npm.ROLLBACK) {
      log.info(pkg._id, 'Failed to exec ' + stage + ' script')
      er.message = pkg._id + ' ' + stage + ': `' + cmd + '`\n' +
                   er.message
      if (er.code !== 'EPERM') {
        er.code = 'ELIFECYCLE'
      }
      er.pkgid = pkg._id
      er.stage = stage
      er.script = cmd
      er.pkgname = pkg.name
      return cb(er)
    } else if (er) {
      log.error(pkg._id + '.' + stage, er)
      log.error(pkg._id + '.' + stage, 'continuing anyway')
      return cb()
    }
    cb(er)
  }
}

function runHookLifecycle (pkg, env, wd, unsafe, cb) {
  // check for a hook script, run if present.
  var stage = env.npm_lifecycle_event
  var hook = path.join(npm.dir, '.hooks', stage)
  var cmd = hook

  fs.stat(hook, function (er) {
    if (er) return cb()
    var note = '\n> ' + pkg._id + ' ' + stage + ' ' + wd +
               '\n> ' + cmd
    runCmd(note, hook, pkg, env, stage, wd, unsafe, cb)
  })
}

function makeEnv (data, prefix, env) {
  prefix = prefix || 'npm_package_'
  if (!env) {
    env = {}
    for (var i in process.env) if (!i.match(/^npm_/)) {
      env[i] = process.env[i]
    }

    // npat asks for tap output
    if (npm.config.get('npat')) env.TAP = 1

    // express and others respect the NODE_ENV value.
    if (npm.config.get('production')) env.NODE_ENV = 'production'

  } else if (!data.hasOwnProperty('_lifecycleEnv')) {
    Object.defineProperty(data, '_lifecycleEnv',
      {
        value: env,
        enumerable: false
      }
    )
  }

  for (i in data) if (i.charAt(0) !== '_') {
    var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
    if (i === 'readme') {
      continue
    }
    if (data[i] && typeof data[i] === 'object') {
      try {
        // quick and dirty detection for cyclical structures
        JSON.stringify(data[i])
        makeEnv(data[i], envKey + '_', env)
      } catch (ex) {
        // usually these are package objects.
        // just get the path and basic details.
        var d = data[i]
        makeEnv(
          { name: d.name, version: d.version, path: d.path },
          envKey + '_',
          env
        )
      }
    } else {
      env[envKey] = String(data[i])
      env[envKey] = env[envKey].indexOf('\n') !== -1 ?
                      JSON.stringify(env[envKey]) :
                      env[envKey]
    }

  }

  if (prefix !== 'npm_package_') return env

  prefix = 'npm_config_'
  var pkgConfig = {}
  var keys = npm.config.keys
  var pkgVerConfig = {}
  var namePref = data.name + ':'
  var verPref = data.name + '@' + data.version + ':'

  keys.forEach(function (i) {
    // in some rare cases (e.g. working with nerf darts), there are segmented
    // "private" (underscore-prefixed) config names -- don't export
    if (i.charAt(0) === '_' && i.indexOf('_' + namePref) !== 0 || i.match(/:_/)) {
      return
    }
    var value = npm.config.get(i)
    if (value instanceof Stream || Array.isArray(value)) return
    if (i.match(/umask/)) value = umask.toString(value)
    if (!value) value = ''
    else if (typeof value === 'number') value = '' + value
    else if (typeof value !== 'string') value = JSON.stringify(value)

    value = value.indexOf('\n') !== -1
          ? JSON.stringify(value)
          : value
    i = i.replace(/^_+/, '')
    var k
    if (i.indexOf(namePref) === 0) {
      k = i.substr(namePref.length).replace(/[^a-zA-Z0-9_]/g, '_')
      pkgConfig[k] = value
    } else if (i.indexOf(verPref) === 0) {
      k = i.substr(verPref.length).replace(/[^a-zA-Z0-9_]/g, '_')
      pkgVerConfig[k] = value
    }
    var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
    env[envKey] = value
  })

  prefix = 'npm_package_config_'
  ;[pkgConfig, pkgVerConfig].forEach(function (conf) {
    for (var i in conf) {
      var envKey = (prefix + i)
      env[envKey] = conf[i]
    }
  })

  return env
}

function cmd (stage) {
  function CMD (args, cb) {
    npm.commands['run-script']([stage].concat(args), cb)
  }
  CMD.usage = 'npm ' + stage + ' [-- <args>]'
  var installedShallow = require('./completion/installed-shallow.js')
  CMD.completion = function (opts, cb) {
    installedShallow(opts, function (d) {
      return d.scripts && d.scripts[stage]
    }, cb)
  }
  return CMD
}
<commit_msg>lifecycle: add /d and /s to cmd.exe<commit_after>exports = module.exports = lifecycle
exports.cmd = cmd
exports.makeEnv = makeEnv

var log = require('npmlog')
var spawn = require('./spawn')
var npm = require('../npm.js')
var path = require('path')
var fs = require('graceful-fs')
var chain = require('slide').chain
var Stream = require('stream').Stream
var PATH = 'PATH'
var uidNumber = require('uid-number')
var umask = require('./umask')

// windows calls it's path 'Path' usually, but this is not guaranteed.
if (process.platform === 'win32') {
  PATH = 'Path'
  Object.keys(process.env).forEach(function (e) {
    if (e.match(/^PATH$/i)) {
      PATH = e
    }
  })
}

function lifecycle (pkg, stage, wd, unsafe, failOk, cb) {
  if (typeof cb !== 'function') {
    cb = failOk
    failOk = false
  }
  if (typeof cb !== 'function') {
    cb = unsafe
    unsafe = false
  }
  if (typeof cb !== 'function') {
    cb = wd
    wd = null
  }

  while (pkg && pkg._data) pkg = pkg._data
  if (!pkg) return cb(new Error('Invalid package data'))

  log.info(stage, pkg._id)
  if (!pkg.scripts || npm.config.get('ignore-scripts')) pkg.scripts = {}

  validWd(wd || path.resolve(npm.dir, pkg.name), function (er, wd) {
    if (er) return cb(er)

    unsafe = unsafe || npm.config.get('unsafe-perm')

    if ((wd.indexOf(npm.dir) !== 0 ||
          wd.indexOf(pkg.name) !== wd.length - pkg.name.length) &&
        !unsafe && pkg.scripts[stage]) {
      log.warn(
        'cannot run in wd',
        '%s %s (wd=%s)', pkg._id, pkg.scripts[stage], wd
      )
      return cb()
    }

    // set the env variables, then run scripts as a child process.
    var env = makeEnv(pkg)
    env.npm_lifecycle_event = stage
    env.npm_node_execpath = env.NODE = env.NODE || process.execPath
    env.npm_execpath = require.main.filename

    // 'nobody' typically doesn't have permission to write to /tmp
    // even if it's never used, sh freaks out.
    if (!npm.config.get('unsafe-perm')) env.TMPDIR = wd

    lifecycle_(pkg, stage, wd, env, unsafe, failOk, cb)
  })
}

function lifecycle_ (pkg, stage, wd, env, unsafe, failOk, cb) {
  var pathArr = []
  var p = wd.split('node_modules')
  var acc = path.resolve(p.shift())

  p.forEach(function (pp) {
    pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
    acc = path.join(acc, 'node_modules', pp)
  })
  pathArr.unshift(path.join(acc, 'node_modules', '.bin'))

  // we also unshift the bundled node-gyp-bin folder so that
  // the bundled one will be used for installing things.
  pathArr.unshift(path.join(__dirname, '..', '..', 'bin', 'node-gyp-bin'))

  if (env[PATH]) pathArr.push(env[PATH])
  env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':')

  var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)

  if (packageLifecycle) {
    // define this here so it's available to all scripts.
    env.npm_lifecycle_script = pkg.scripts[stage]
  }

  function done (er) {
    if (er) {
      if (npm.config.get('force')) {
        log.info('forced, continuing', er)
        er = null
      } else if (failOk) {
        log.warn('continuing anyway', er.message)
        er = null
      }
    }
    cb(er)
  }

  chain(
    [
      packageLifecycle && [runPackageLifecycle, pkg, env, wd, unsafe],
      [runHookLifecycle, pkg, env, wd, unsafe]
    ],
    done
  )
}

function validWd (d, cb) {
  fs.stat(d, function (er, st) {
    if (er || !st.isDirectory()) {
      var p = path.dirname(d)
      if (p === d) {
        return cb(new Error('Could not find suitable wd'))
      }
      return validWd(p, cb)
    }
    return cb(null, d)
  })
}

function runPackageLifecycle (pkg, env, wd, unsafe, cb) {
  // run package lifecycle scripts in the package root, or the nearest parent.
  var stage = env.npm_lifecycle_event
  var cmd = env.npm_lifecycle_script

  var note = '\n> ' + pkg._id + ' ' + stage + ' ' + wd +
             '\n> ' + cmd + '\n'
  runCmd(note, cmd, pkg, env, stage, wd, unsafe, cb)
}

var running = false
var queue = []
function dequeue () {
  running = false
  if (queue.length) {
    var r = queue.shift()
    runCmd.apply(null, r)
  }
}

function runCmd (note, cmd, pkg, env, stage, wd, unsafe, cb) {
  if (running) {
    queue.push([note, cmd, pkg, env, stage, wd, unsafe, cb])
    return
  }

  running = true
  log.pause()
  var user = unsafe ? null : npm.config.get('user')
  var group = unsafe ? null : npm.config.get('group')

  if (log.level !== 'silent') {
    log.clearProgress()
    console.log(note)
    log.showProgress()
  }
  log.verbose('unsafe-perm in lifecycle', unsafe)

  if (process.platform === 'win32') {
    unsafe = true
  }

  if (unsafe) {
    runCmd_(cmd, pkg, env, wd, stage, unsafe, 0, 0, cb)
  } else {
    uidNumber(user, group, function (er, uid, gid) {
      runCmd_(cmd, pkg, env, wd, stage, unsafe, uid, gid, cb)
    })
  }
}

function runCmd_ (cmd, pkg, env, wd, stage, unsafe, uid, gid, cb_) {

  function cb (er) {
    cb_.apply(null, arguments)
    log.resume()
    process.nextTick(dequeue)
  }

  var conf = {
    cwd: wd,
    env: env,
    stdio: [ 0, 1, 2 ]
  }

  if (!unsafe) {
    conf.uid = uid ^ 0
    conf.gid = gid ^ 0
  }

  var sh = 'sh'
  var shFlag = '-c'

  if (process.platform === 'win32') {
    sh = process.env.comspec || 'cmd'
    shFlag = '/d /s /c'
    conf.windowsVerbatimArguments = true
  }

  var progressEnabled = log.progressEnabled
  if (progressEnabled) log.disableProgress()
  var proc = spawn(sh, [shFlag, cmd], conf)

  proc.on('error', procError)
  proc.on('close', function (code, signal) {
    if (signal) {
      process.kill(process.pid, signal)
    } else if (code) {
      var er = new Error('Exit status ' + code)
    }
    procError(er)
  })

  function procError (er) {
    if (progressEnabled) log.enableProgress()
    if (er && !npm.ROLLBACK) {
      log.info(pkg._id, 'Failed to exec ' + stage + ' script')
      er.message = pkg._id + ' ' + stage + ': `' + cmd + '`\n' +
                   er.message
      if (er.code !== 'EPERM') {
        er.code = 'ELIFECYCLE'
      }
      er.pkgid = pkg._id
      er.stage = stage
      er.script = cmd
      er.pkgname = pkg.name
      return cb(er)
    } else if (er) {
      log.error(pkg._id + '.' + stage, er)
      log.error(pkg._id + '.' + stage, 'continuing anyway')
      return cb()
    }
    cb(er)
  }
}

function runHookLifecycle (pkg, env, wd, unsafe, cb) {
  // check for a hook script, run if present.
  var stage = env.npm_lifecycle_event
  var hook = path.join(npm.dir, '.hooks', stage)
  var cmd = hook

  fs.stat(hook, function (er) {
    if (er) return cb()
    var note = '\n> ' + pkg._id + ' ' + stage + ' ' + wd +
               '\n> ' + cmd
    runCmd(note, hook, pkg, env, stage, wd, unsafe, cb)
  })
}

function makeEnv (data, prefix, env) {
  prefix = prefix || 'npm_package_'
  if (!env) {
    env = {}
    for (var i in process.env) if (!i.match(/^npm_/)) {
      env[i] = process.env[i]
    }

    // npat asks for tap output
    if (npm.config.get('npat')) env.TAP = 1

    // express and others respect the NODE_ENV value.
    if (npm.config.get('production')) env.NODE_ENV = 'production'

  } else if (!data.hasOwnProperty('_lifecycleEnv')) {
    Object.defineProperty(data, '_lifecycleEnv',
      {
        value: env,
        enumerable: false
      }
    )
  }

  for (i in data) if (i.charAt(0) !== '_') {
    var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
    if (i === 'readme') {
      continue
    }
    if (data[i] && typeof data[i] === 'object') {
      try {
        // quick and dirty detection for cyclical structures
        JSON.stringify(data[i])
        makeEnv(data[i], envKey + '_', env)
      } catch (ex) {
        // usually these are package objects.
        // just get the path and basic details.
        var d = data[i]
        makeEnv(
          { name: d.name, version: d.version, path: d.path },
          envKey + '_',
          env
        )
      }
    } else {
      env[envKey] = String(data[i])
      env[envKey] = env[envKey].indexOf('\n') !== -1 ?
                      JSON.stringify(env[envKey]) :
                      env[envKey]
    }

  }

  if (prefix !== 'npm_package_') return env

  prefix = 'npm_config_'
  var pkgConfig = {}
  var keys = npm.config.keys
  var pkgVerConfig = {}
  var namePref = data.name + ':'
  var verPref = data.name + '@' + data.version + ':'

  keys.forEach(function (i) {
    // in some rare cases (e.g. working with nerf darts), there are segmented
    // "private" (underscore-prefixed) config names -- don't export
    if (i.charAt(0) === '_' && i.indexOf('_' + namePref) !== 0 || i.match(/:_/)) {
      return
    }
    var value = npm.config.get(i)
    if (value instanceof Stream || Array.isArray(value)) return
    if (i.match(/umask/)) value = umask.toString(value)
    if (!value) value = ''
    else if (typeof value === 'number') value = '' + value
    else if (typeof value !== 'string') value = JSON.stringify(value)

    value = value.indexOf('\n') !== -1
          ? JSON.stringify(value)
          : value
    i = i.replace(/^_+/, '')
    var k
    if (i.indexOf(namePref) === 0) {
      k = i.substr(namePref.length).replace(/[^a-zA-Z0-9_]/g, '_')
      pkgConfig[k] = value
    } else if (i.indexOf(verPref) === 0) {
      k = i.substr(verPref.length).replace(/[^a-zA-Z0-9_]/g, '_')
      pkgVerConfig[k] = value
    }
    var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
    env[envKey] = value
  })

  prefix = 'npm_package_config_'
  ;[pkgConfig, pkgVerConfig].forEach(function (conf) {
    for (var i in conf) {
      var envKey = (prefix + i)
      env[envKey] = conf[i]
    }
  })

  return env
}

function cmd (stage) {
  function CMD (args, cb) {
    npm.commands['run-script']([stage].concat(args), cb)
  }
  CMD.usage = 'npm ' + stage + ' [-- <args>]'
  var installedShallow = require('./completion/installed-shallow.js')
  CMD.completion = function (opts, cb) {
    installedShallow(opts, function (d) {
      return d.scripts && d.scripts[stage]
    }, cb)
  }
  return CMD
}
<|endoftext|>


<commit_before>// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Onload
//-------------------------------
cur_frm.cscript.onload = function(doc,dt,dn){
	if(!doc.return_date) set_multiple(dt,dn,{return_date:get_today()});
	doc.delivery_note_no = '';
	doc.purchase_receipt_no = '';
	doc.sales_invoice_no = '';
	doc.return_type ='';
	refresh_many(['delivery_note_no', 'sales_invoice_no', 'purchase_receipt_no', 'return_type']);
}

// Link field query
//--------------------------------
cur_frm.fields_dict.delivery_note_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabDelivery Note`.name FROM `tabDelivery Note` WHERE `tabDelivery Note`.docstatus = 1 AND `tabDelivery Note`.%(key)s LIKE "%s" ORDER BY `tabDelivery Note`.name desc LIMIT 50';
}

cur_frm.fields_dict.sales_invoice_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabSales Invoice`.name FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 AND `tabSales Invoice`.%(key)s LIKE "%s" ORDER BY `tabSales Invoice`.name desc LIMIT 50';
}

cur_frm.fields_dict.purchase_receipt_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabPurchase Receipt`.name FROM `tabPurchase Receipt` WHERE `tabPurchase Receipt`.docstatus = 1 AND `tabPurchase Receipt`.%(key)s LIKE "%s" ORDER BY `tabPurchase Receipt`.name desc LIMIT 50';
}

// Hide/unhide based on return type
//----------------------------------
cur_frm.cscript.return_type = function(doc, cdt, cdn) {
	var cp = wn.control_panel;
	hide_field(['purchase_receipt_no', 'delivery_note_no', 'sales_invoice_no',
		'return_details', 'Get Items', 'Make Excise Invoice', 'Make Stock Entry',
		'Make Debit Note', 'Make Credit Note']);

	if(doc.return_type == 'Sales Return') {
		unhide_field(['delivery_note_no', 'sales_invoice_no', 'Get Items',
			'return_details', 'Make Credit Note', 'Make Stock Entry']);

		if(cp.country == 'India') {	unhide_field(['Make Excise Invoice']); }

	} else if(doc.return_type == 'Purchase Return') {
		unhide_field(['purchase_receipt_no', 'Get Items', 'return_details',
			'Make Debit Note', 'Make Stock Entry']);

		if(cp.country == 'India') {	unhide_field(['Make Excise Invoice']); }
	}

	cur_frm.cscript.clear_fields(doc);
}

// Create item table
//-------------------------------
cur_frm.cscript.get_items = function(doc, cdt, cdn) {
	flag = 0
	if(doc.return_type == 'Sales Return') {
		if (doc.delivery_note_no && doc.sales_invoice_no) {
			msgprint("You can not enter both Delivery Note No and Sales Invoice No. Please enter any one.");
			flag = 1;
		} else if (!doc.delivery_note_no && !doc.sales_invoice_no) {
			msgprint("Please enter Delivery Note No or Sales Invoice No to proceed");
			flag = 1;
		}
	} else if (doc.return_type == 'Purchase Return' && !doc.purchase_receipt_no) {
		msgprint("Please enter Purchase Receipt No to proceed");
		flag = 1;
	}
	if (!flag)
		$c_obj(make_doclist(doc.doctype, doc.name),'pull_item_details','', function(r, rt) {
			refresh_many(['return_details', 'cust_supp', 'cust_supp_name', 'cust_supp_address']);
		});
}

// Clear fields
//-------------------------------
cur_frm.cscript.clear_fields = function(doc) {
	doc.purchase_receipt_no, doc.delivery_note_no, doc.sales_invoice_no = '', '', '';
	var cl = getchildren('Sales and Purchase Return Item', doc.name, 'return_details')
	if(cl.length) $c_obj(make_doclist(doc.doctype, doc.name),'clear_return_table','', function(r, rt) {refresh_field('return_details')});
	refresh_many(['delivery_note_no', 'sales_invoice_no', 'purchase_receipt_no', 'return_details']);
}

// Make Stock Entry
//-------------------------------
cur_frm.cscript.make_stock_entry = function(doc, cdt, cdn) {
	var cl = getchildren('Sales and Purchase Return Item', doc.name, 'return_details');
	if (!cl.length)
		msgprint("Item table can not be blank. Please click on 'Get Items'.");
	else if (!cur_frm.cscript.validate_returned_qty(cl)) {
		se = cur_frm.cscript.map_parent_fields(doc,cdt,cdn);
		cur_frm.cscript.map_child_fields(cl, se);
		loaddoc('Stock Entry', se.name);
	}
}

// Validate returned qty
//---------------------------
cur_frm.cscript.validate_returned_qty = function(cl) {
	flag = 0
	for(var i = 0; i<cl.length; i++){
		if(cl[i].returned_qty > cl[i].qty) {
			msgprint("Returned Qty can not be greater than qty. Please check for item: " + cl[i].item_code);
			flag = 1
		}
	}
	return flag
}

// map parent fields of stock entry
//----------------------------------
cur_frm.cscript.map_parent_fields = function(doc, cdt, cdn) {
	var se = LocalDB.create('Stock Entry');
	se = locals['Stock Entry'][se];
	se.posting_date = dateutil.obj_to_str(new Date());
	se.transfer_date = dateutil.obj_to_str(new Date());
	se.fiscal_year = sys_defaults.fiscal_year;
	se.purpose = doc.return_type;
	se.remarks = doc.return_type + ' of ' + (doc.delivery_note_no || doc.sales_invoice_no || doc.purchase_receipt_no);
	if(doc.return_type == 'Sales Return'){
		se.delivery_note_no = doc.delivery_note_no;
		se.sales_invoice_no = doc.sales_invoice_no;
		se.customer = doc.cust_supp_name;
		se.customer_name = doc.cust_supp_name;
		se.customer_address = doc.cust_supp_address;
	}
	else if(doc.return_type == 'Purchase Return'){
		se.purchase_receipt_no = doc.purchase_receipt_no;
		se.supplier = doc.cust_supp_name;
		se.supplier_name = doc.cust_supp_name;
		se.supplier_address = doc.cust_supp_address;
	}
	return se
}

// map child fields of stock entry
//---------------------------------
cur_frm.cscript.map_child_fields = function(cl, se) {
	for(var i = 0; i<cl.length; i++){
		if (cl[i].returned_qty) {
			var d1 = LocalDB.add_child(se, 'Stock Entry Detail', 'mtn_details');
			d1.detail_name = cl[i].detail_name;
			d1.item_code = cl[i].item_code;
			d1.description = cl[i].description;
			d1.transfer_qty = cl[i].returned_qty;
			d1.qty = cl[i].returned_qty;
			d1.stock_uom = cl[i].uom;
			d1.uom = cl[i].uom;
			d1.conversion_factor = 1;
			d1.incoming_rate = cl[i].rate;
			d1.serial_no = cl[i].serial_no;
			d1.batch_no = cl[i].batch_no;
		}
	}
}

// Make excise voucher
//-------------------------------
cur_frm.cscript.make_excise_invoice = function(doc) {
	var excise = LocalDB.create('Journal Voucher');
	excise = locals['Journal Voucher'][excise];
	excise.voucher_type = 'Excise Voucher';
	loaddoc('Journal Voucher',excise.name);
}
// Make debit note
//------------------------------
cur_frm.cscript.make_debit_note = function(doc) {
	var doclist = make_doclist(doc.doctype, doc.name);
	$c('accounts.get_new_jv_details', {
			doclist: JSON.stringify(doclist),
			fiscal_year: sys_defaults.fiscal_year
		}, function(r, rt) {
		if(!r.exc) {
			cur_frm.cscript.make_jv(doc, 'Debit Note', r.message);
		}
	});
}
// Make credit note
//------------------------------
cur_frm.cscript.make_credit_note = function(doc) {
	var doclist = make_doclist(doc.doctype, doc.name);
	$c('accounts.get_new_jv_details', {
			doclist: JSON.stringify(doclist),
			fiscal_year: sys_defaults.fiscal_year
		}, function(r, rt) {
		if(!r.exc) {
			cur_frm.cscript.make_jv(doc, 'Credit Note', r.message);
		}
	});
}

// Make JV
//--------------------------------
cur_frm.cscript.make_jv = function(doc, dr_or_cr, children) {
	var jv = LocalDB.create('Journal Voucher');
	jv = locals['Journal Voucher'][jv];

	jv.voucher_type = dr_or_cr;
	jv.company = sys_defaults.company;
	jv.fiscal_year = sys_defaults.fiscal_year;
	jv.is_opening = 'No';
	jv.posting_date = dateutil.obj_to_str(new Date());
	jv.voucher_date = dateutil.obj_to_str(new Date());

	// Add children
	if(children) {
		for(var i=0; i<children.length; i++) {
			var ch = LocalDB.add_child(jv, 'Journal Voucher Detail', 'entries');
			$.extend(ch, children[i]);
		}
	}

	loaddoc('Journal Voucher', jv.name);
}
<commit_msg>field changes in sp return tool<commit_after>// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Onload
//-------------------------------
cur_frm.cscript.onload = function(doc,dt,dn){
	if(!doc.return_date) set_multiple(dt,dn,{return_date:get_today()});
	doc.delivery_note_no = '';
	doc.purchase_receipt_no = '';
	doc.sales_invoice_no = '';
	doc.return_type ='';
	refresh_many(['delivery_note_no', 'sales_invoice_no', 'purchase_receipt_no', 'return_type']);
}

// Link field query
//--------------------------------
cur_frm.fields_dict.delivery_note_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabDelivery Note`.name FROM `tabDelivery Note` WHERE `tabDelivery Note`.docstatus = 1 AND `tabDelivery Note`.%(key)s LIKE "%s" ORDER BY `tabDelivery Note`.name desc LIMIT 50';
}

cur_frm.fields_dict.sales_invoice_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabSales Invoice`.name FROM `tabSales Invoice` WHERE `tabSales Invoice`.docstatus = 1 AND `tabSales Invoice`.%(key)s LIKE "%s" ORDER BY `tabSales Invoice`.name desc LIMIT 50';
}

cur_frm.fields_dict.purchase_receipt_no.get_query = function(doc) {
	return 'SELECT DISTINCT `tabPurchase Receipt`.name FROM `tabPurchase Receipt` WHERE `tabPurchase Receipt`.docstatus = 1 AND `tabPurchase Receipt`.%(key)s LIKE "%s" ORDER BY `tabPurchase Receipt`.name desc LIMIT 50';
}

// Hide/unhide based on return type
//----------------------------------
cur_frm.cscript.return_type = function(doc, cdt, cdn) {
	var cp = wn.control_panel;
	hide_field(['purchase_receipt_no', 'delivery_note_no', 'sales_invoice_no',
		'return_details', 'get_items', 'Make Excise Invoice', 'Make Stock Entry',
		'Make Debit Note', 'Make Credit Note']);

	if(doc.return_type == 'Sales Return') {
		unhide_field(['delivery_note_no', 'sales_invoice_no', 'get_items',
			'return_details', 'Make Credit Note', 'Make Stock Entry']);

		if(cp.country == 'India') {	unhide_field(['Make Excise Invoice']); }

	} else if(doc.return_type == 'Purchase Return') {
		unhide_field(['purchase_receipt_no', 'get_items', 'return_details',
			'Make Debit Note', 'Make Stock Entry']);

		if(cp.country == 'India') {	unhide_field(['Make Excise Invoice']); }
	}

	cur_frm.cscript.clear_fields(doc);
}

// Create item table
//-------------------------------
cur_frm.cscript.get_items = function(doc, cdt, cdn) {
	flag = 0
	if(doc.return_type == 'Sales Return') {
		if (doc.delivery_note_no && doc.sales_invoice_no) {
			msgprint("You can not enter both Delivery Note No and Sales Invoice No. Please enter any one.");
			flag = 1;
		} else if (!doc.delivery_note_no && !doc.sales_invoice_no) {
			msgprint("Please enter Delivery Note No or Sales Invoice No to proceed");
			flag = 1;
		}
	} else if (doc.return_type == 'Purchase Return' && !doc.purchase_receipt_no) {
		msgprint("Please enter Purchase Receipt No to proceed");
		flag = 1;
	}
	if (!flag)
		$c_obj(make_doclist(doc.doctype, doc.name),'pull_item_details','', function(r, rt) {
			refresh_many(['return_details', 'cust_supp', 'cust_supp_name', 'cust_supp_address']);
		});
}

// Clear fields
//-------------------------------
cur_frm.cscript.clear_fields = function(doc) {
	doc.purchase_receipt_no, doc.delivery_note_no, doc.sales_invoice_no = '', '', '';
	var cl = getchildren('Sales and Purchase Return Item', doc.name, 'return_details')
	if(cl.length) $c_obj(make_doclist(doc.doctype, doc.name),'clear_return_table','', function(r, rt) {refresh_field('return_details')});
	refresh_many(['delivery_note_no', 'sales_invoice_no', 'purchase_receipt_no', 'return_details']);
}

// Make Stock Entry
//-------------------------------
cur_frm.cscript.make_stock_entry = function(doc, cdt, cdn) {
	var cl = getchildren('Sales and Purchase Return Item', doc.name, 'return_details');
	if (!cl.length)
		msgprint("Item table can not be blank. Please click on 'Get Items'.");
	else if (!cur_frm.cscript.validate_returned_qty(cl)) {
		se = cur_frm.cscript.map_parent_fields(doc,cdt,cdn);
		cur_frm.cscript.map_child_fields(cl, se);
		loaddoc('Stock Entry', se.name);
	}
}

// Validate returned qty
//---------------------------
cur_frm.cscript.validate_returned_qty = function(cl) {
	flag = 0
	for(var i = 0; i<cl.length; i++){
		if(cl[i].returned_qty > cl[i].qty) {
			msgprint("Returned Qty can not be greater than qty. Please check for item: " + cl[i].item_code);
			flag = 1
		}
	}
	return flag
}

// map parent fields of stock entry
//----------------------------------
cur_frm.cscript.map_parent_fields = function(doc, cdt, cdn) {
	var se = LocalDB.create('Stock Entry');
	se = locals['Stock Entry'][se];
	se.posting_date = dateutil.obj_to_str(new Date());
	se.transfer_date = dateutil.obj_to_str(new Date());
	se.fiscal_year = sys_defaults.fiscal_year;
	se.purpose = doc.return_type;
	se.remarks = doc.return_type + ' of ' + (doc.delivery_note_no || doc.sales_invoice_no || doc.purchase_receipt_no);
	if(doc.return_type == 'Sales Return'){
		se.delivery_note_no = doc.delivery_note_no;
		se.sales_invoice_no = doc.sales_invoice_no;
		se.customer = doc.cust_supp_name;
		se.customer_name = doc.cust_supp_name;
		se.customer_address = doc.cust_supp_address;
	}
	else if(doc.return_type == 'Purchase Return'){
		se.purchase_receipt_no = doc.purchase_receipt_no;
		se.supplier = doc.cust_supp_name;
		se.supplier_name = doc.cust_supp_name;
		se.supplier_address = doc.cust_supp_address;
	}
	return se
}

// map child fields of stock entry
//---------------------------------
cur_frm.cscript.map_child_fields = function(cl, se) {
	for(var i = 0; i<cl.length; i++){
		if (cl[i].returned_qty) {
			var d1 = LocalDB.add_child(se, 'Stock Entry Detail', 'mtn_details');
			d1.detail_name = cl[i].detail_name;
			d1.item_code = cl[i].item_code;
			d1.description = cl[i].description;
			d1.transfer_qty = cl[i].returned_qty;
			d1.qty = cl[i].returned_qty;
			d1.stock_uom = cl[i].uom;
			d1.uom = cl[i].uom;
			d1.conversion_factor = 1;
			d1.incoming_rate = cl[i].rate;
			d1.serial_no = cl[i].serial_no;
			d1.batch_no = cl[i].batch_no;
		}
	}
}

// Make excise voucher
//-------------------------------
cur_frm.cscript.make_excise_invoice = function(doc) {
	var excise = LocalDB.create('Journal Voucher');
	excise = locals['Journal Voucher'][excise];
	excise.voucher_type = 'Excise Voucher';
	loaddoc('Journal Voucher',excise.name);
}
// Make debit note
//------------------------------
cur_frm.cscript.make_debit_note = function(doc) {
	var doclist = make_doclist(doc.doctype, doc.name);
	$c('accounts.get_new_jv_details', {
			doclist: JSON.stringify(doclist),
			fiscal_year: sys_defaults.fiscal_year
		}, function(r, rt) {
		if(!r.exc) {
			cur_frm.cscript.make_jv(doc, 'Debit Note', r.message);
		}
	});
}
// Make credit note
//------------------------------
cur_frm.cscript.make_credit_note = function(doc) {
	var doclist = make_doclist(doc.doctype, doc.name);
	$c('accounts.get_new_jv_details', {
			doclist: JSON.stringify(doclist),
			fiscal_year: sys_defaults.fiscal_year
		}, function(r, rt) {
		if(!r.exc) {
			cur_frm.cscript.make_jv(doc, 'Credit Note', r.message);
		}
	});
}

// Make JV
//--------------------------------
cur_frm.cscript.make_jv = function(doc, dr_or_cr, children) {
	var jv = LocalDB.create('Journal Voucher');
	jv = locals['Journal Voucher'][jv];

	jv.voucher_type = dr_or_cr;
	jv.company = sys_defaults.company;
	jv.fiscal_year = sys_defaults.fiscal_year;
	jv.is_opening = 'No';
	jv.posting_date = dateutil.obj_to_str(new Date());
	jv.voucher_date = dateutil.obj_to_str(new Date());

	// Add children
	if(children) {
		for(var i=0; i<children.length; i++) {
			var ch = LocalDB.add_child(jv, 'Journal Voucher Detail', 'entries');
			$.extend(ch, children[i]);
		}
	}

	loaddoc('Journal Voucher', jv.name);
}
<|endoftext|>


<commit_before><commit_msg>Remove validator folder<commit_after><|endoftext|>


<commit_before>/** @jsx etch.dom */
/* eslint react/no-unknown-property: "off" */

import etch from 'etch';
import {autobind} from 'core-decorators';

export default class HunkView {
  constructor(props) {
    this.lastMousemoveLine = null;
    this.props = props;
    if (props.registerView != null) { props.registerView(props.hunk, this); } // only for tests
    this.lineElements = new WeakMap();
    this.registerLineElement = this.lineElements.set.bind(this.lineElements);
    etch.initialize(this);
  }

  destroy() {
    return etch.destroy(this);
  }

  @autobind
  mousedownOnLine(event, line) {
    this.props.mousedownOnLine(event, this.props.hunk, line);
  }

  @autobind
  mousemoveOnLine(event, line) {
    if (line !== this.lastMousemoveLine) {
      this.lastMousemoveLine = line;
      this.props.mousemoveOnLine(event, this.props.hunk, line);
    }
  }

  update(props) {
    this.props = props;
    if (props.registerView != null) { props.registerView(props.hunk, this); } // only for tests
    return etch.update(this);
  }

  render() {
    const hunkSelectedClass = this.props.isSelected ? 'is-selected' : '';
    const hunkModeClass = this.props.hunkSelectionMode ? 'is-hunkMode' : '';
    return (
      <div className={`github-HunkView ${hunkModeClass} ${hunkSelectedClass}`}>

 this.props.mousedownOnHeader(e)}>

            {this.props.hunk.getHeader().trim()} {this.props.hunk.getSectionHeading().trim()}

          <button ref="stageButton"
            className="github-HunkView-stageButton"
            onclick={this.props.didClickStageButton}
            onmousedown={event => event.stopPropagation()}>
            {this.props.stageButtonLabel}
          </button>

        {this.props.hunk.getLines().map((line, idx) =>
          <LineView
            key={idx}
            line={line}
            isSelected={this.props.selectedLines.has(line)}
            registerLineElement={this.registerLineElement}
            mousedown={this.mousedownOnLine}
            mousemove={this.mousemoveOnLine}
            contextMenuOnItem={(event, line) => this.props.contextMenuOnItem(event, this.props.hunk, line)}
          />,
        )}

    );
  }

  registerLineElement(line, element) {
    this.lineElements.set(line, element);
  }

  writeAfterUpdate() {
    if (this.props.headHunk === this.props.hunk) {
      this.element.scrollIntoViewIfNeeded();
    } else if (this.props.headLine && this.lineElements.has(this.props.headLine)) {
      this.lineElements.get(this.props.headLine).scrollIntoViewIfNeeded();
    }
  }
}

class LineView {
  constructor(props) {
    this.props = props;
    etch.initialize(this);
    this.props.registerLineElement(props.line, this.element);
  }

  update(props) {
    this.props = props;
    return etch.update(this);
  }

  render() {
    const line = this.props.line;
    const oldLineNumber = line.getOldLineNumber() === -1 ? ' ' : line.getOldLineNumber();
    const newLineNumber = line.getNewLineNumber() === -1 ? ' ' : line.getNewLineNumber();
    const lineSelectedClass = this.props.isSelected ? 'is-selected' : '';
    return (
      <div className={`github-HunkView-line ${lineSelectedClass} is-${line.getStatus()}`}
        onmousedown={event => this.props.mousedown(event, line)}
        onmousemove={event => this.props.mousemove(event, line)}
        oncontextmenu={event => this.props.contextMenuOnItem(event, line)}>

{oldLineNumber}

{newLineNumber}

          {line.getOrigin()}
          {line.getText()}

    );
  }
}
<commit_msg>:shirt:<commit_after>/** @jsx etch.dom */
/* eslint react/no-unknown-property: "off" */

import etch from 'etch';
import {autobind} from 'core-decorators';

export default class HunkView {
  constructor(props) {
    this.lastMousemoveLine = null;
    this.props = props;
    if (props.registerView != null) { props.registerView(props.hunk, this); } // only for tests
    this.lineElements = new WeakMap();
    this.registerLineElement = this.lineElements.set.bind(this.lineElements);
    etch.initialize(this);
  }

  destroy() {
    return etch.destroy(this);
  }

  @autobind
  mousedownOnLine(event, line) {
    this.props.mousedownOnLine(event, this.props.hunk, line);
  }

  @autobind
  mousemoveOnLine(event, line) {
    if (line !== this.lastMousemoveLine) {
      this.lastMousemoveLine = line;
      this.props.mousemoveOnLine(event, this.props.hunk, line);
    }
  }

  update(props) {
    this.props = props;
    if (props.registerView != null) { props.registerView(props.hunk, this); } // only for tests
    return etch.update(this);
  }

  render() {
    const hunkSelectedClass = this.props.isSelected ? 'is-selected' : '';
    const hunkModeClass = this.props.hunkSelectionMode ? 'is-hunkMode' : '';
    return (
      <div className={`github-HunkView ${hunkModeClass} ${hunkSelectedClass}`}>

 this.props.mousedownOnHeader(e)}>

            {this.props.hunk.getHeader().trim()} {this.props.hunk.getSectionHeading().trim()}

          <button ref="stageButton"
            className="github-HunkView-stageButton"
            onclick={this.props.didClickStageButton}
            onmousedown={event => event.stopPropagation()}>
            {this.props.stageButtonLabel}
          </button>

        {this.props.hunk.getLines().map((line, idx) =>
          <LineView
            key={idx}
            line={line}
            isSelected={this.props.selectedLines.has(line)}
            registerLineElement={this.registerLineElement}
            mousedown={this.mousedownOnLine}
            mousemove={this.mousemoveOnLine}
            contextMenuOnItem={(e, clickedLine) => this.props.contextMenuOnItem(e, this.props.hunk, clickedLine)}
          />,
        )}

    );
  }

  registerLineElement(line, element) {
    this.lineElements.set(line, element);
  }

  writeAfterUpdate() {
    if (this.props.headHunk === this.props.hunk) {
      this.element.scrollIntoViewIfNeeded();
    } else if (this.props.headLine && this.lineElements.has(this.props.headLine)) {
      this.lineElements.get(this.props.headLine).scrollIntoViewIfNeeded();
    }
  }
}

class LineView {
  constructor(props) {
    this.props = props;
    etch.initialize(this);
    this.props.registerLineElement(props.line, this.element);
  }

  update(props) {
    this.props = props;
    return etch.update(this);
  }

  render() {
    const line = this.props.line;
    const oldLineNumber = line.getOldLineNumber() === -1 ? ' ' : line.getOldLineNumber();
    const newLineNumber = line.getNewLineNumber() === -1 ? ' ' : line.getNewLineNumber();
    const lineSelectedClass = this.props.isSelected ? 'is-selected' : '';
    return (
      <div className={`github-HunkView-line ${lineSelectedClass} is-${line.getStatus()}`}
        onmousedown={event => this.props.mousedown(event, line)}
        onmousemove={event => this.props.mousemove(event, line)}
        oncontextmenu={event => this.props.contextMenuOnItem(event, line)}>

{oldLineNumber}

{newLineNumber}

          {line.getOrigin()}
          {line.getText()}

    );
  }
}
<|endoftext|>


<commit_before>var axios = require('axios');
var url = require('url');
var yaml = require('yamljs');
var pg = require('pg');
var fs = require('fs');

//get modules
var USGS_CONSTANT = require("./lib/usgs_api/usgs_constants.js");
var USGS_FUNCTION = require("./lib/usgs_api/usgs_functions.js");
var USGS_HELPER = require("./lib/usgs_api/usgs_helpers.js");
var PG_HANDLER = require('./lib/postgres/postgres_handlers.js')

//setup shared helpers
var apphelpers = require('./lib/helpers/app_helpers.js')
var APP_HELPERS = apphelpers();

//setup failure email
var emailer = require('./lib/email/send_error_email.js');
var error_email = emailer()

//call delete old files
APP_HELPERS.delete_old_files('order_landsat_data', 'logs/', '.log');
APP_HELPERS.delete_old_files('order_failed', '', '.txt');
APP_HELPERS.delete_old_files('download_failed', '', '.txt');
APP_HELPERS.delete_old_files('downloaded', '', '.txt');
APP_HELPERS.delete_old_files('ordered', '', '.txt');
APP_HELPERS.set_logfile('order_landsat_data')

const LOG_LEVEL_ERR = 'error';
const LOG_LEVEL_INFO = 'info';

//config data
const CONFIG_YAML = yaml.load("./lib/usgs_api/config.yaml");

var scene_downloads = [];
var orders = [];

APP_HELPERS.set_logger_level('debug');

APP_HELPERS.write_message(LOG_LEVEL_INFO, 'ordering data start', '');

//set base URL for axios
axios.defaults.baseURL = USGS_CONSTANT.USGS_URL;

//get config data
const PG_CONNECT = yaml.load("./lib/postgres/config.yaml");

//get pg_client so we can query the LSF database
const pg_client = PG_HANDLER.pg_connect(PG_CONNECT)

//get fields for sql query - that gets scences that need ordering,
const scenes_fields = ' scene_id, sensor, acquisition_date, browse_url, path, row, cc_full, cc_quad_ul, cc_quad_ur, cc_quad_ll, cc_quad_lr, data_type_l1 ';

//set the SQL query to retreive scenes that need to be ordered
const scenes_for_dowloading_SQL = "SELECT " + scenes_fields + " FROM landsat_metadata WHERE needs_ordering = 'YES'"
//LT50190341989361XXX02,LT50190331989361XXX02,LT50150361989365XXX01
// WHERE sensor = 'LANDSAT_TM' LIMIT 250" // WHERE needs_ordering = 'YES'"

//captures lastpromise first one is resolved
var lastPromise = Promise.resolve();

//Qiery the LSF database for scences that need to ordered
const query = pg_client.query(scenes_for_dowloading_SQL);

//login and get promise for api key
var api_key_main = USGS_HELPER.get_api_key();

// query to check for duplicate scenes
query.on('row', function(row, result) {

  // process rows here
  api_key_main
  .then( (apiKey) => {

    //get constant for node "EE"
    var node = USGS_CONSTANT.NODE_EE;
    var entityIds = [];
    var products =  ["STANDARD"];
    var scene_id = row.scene_id;
    var acquisition_date = row.acquisition_date;

    //derieve dataset name from the scene_id and acquisition_date
    const datasetName = USGS_HELPER.get_datasetName(scene_id, acquisition_date);

    // make call to USGS api.  Make sure last promise is resolved first
    //  becuase USGS api is throttled for one request at a time
    //  wrap this in a resolve promoise so the there all requests are in promise and each one has
    //  to be resolved befire the next promise is started.  This is due to only limitations of the USGS API- only allows one
    //  api call at at time,
    return lastPromise = lastPromise.then( () => {

      var entityIds = [scene_id]

      var request_body = {apiKey, node, datasetName, entityIds};

      console.log('');
      console.log('getorderproducts');
      console.log(LOG_LEVEL_INFO, request_body)

      const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('getorderproducts');

      //actual request after the last promise has been resolved
      return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
      .then( getorderproducts_response => {

        // ensure there was a response
        if(getorderproducts_response[0]){

          //filter possible products to downloadable level 1 datasets, with no cost.
          const orderobj = getorderproducts_response[0].availableProducts.filter( res => {
            return res.price === 0 && res.productCode.substring(0,1) != 'W' && res.outputMedias[0] === "DWNLD"

          })

          //only order the product if it is level 1 and downloadable.
          //  we need to get the product code and orderid
          if (orderobj.length > 0){

            //make request json for updating an order
            const orderingId = getorderproducts_response[0].orderingId
            const productCode = orderobj[0].productCode
            const option = 'None'
            const outputMedia = 'DWNLD'

            const request_body = USGS_FUNCTION.usgsapi_updateorderscene(apiKey, node, datasetName, productCode, outputMedia, option, orderingId);

            console.log('')
            console.log('updateorderscene')
            cconsole.log(LOG_LEVEL_INFO, apiKey, node, datasetName, productCode, outputMedia, option, orderingId)

            //send request to USGS api to add the scene as an order
            const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('updateorderscene');

              return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
                  .then( order_response => {

                    //make request json for submitting the order
                    const ordered_scene = entityIds[0]
                    const request_body = USGS_FUNCTION.usgsapi_submitorder(apiKey, node)

                    //send request to USGS api to submit the order
                    //  unfourtunately there is no way to check the status (complete or in process) via the api
                    const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('submitorder');

                    console.log('')
                    console.log('submitorder');
                    console.log(LOG_LEVEL_INFO, apiKey, node)

                    return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
                      .then ( order => {

                        const msg_header = 'order submitted for';
                        const msg = ordered_scene;
                        console.log(LOG_LEVEL_INFO, msg_header, msg)

                   })
              })
          } else {
            console.log('nothing to order for scene ' + scene_id)
            console.log('');
          }

        }

      })
      .catch( (error) => {

        msg_header = 'get order products: ';
        msg = error.message;
        console.error(LOG_LEVEL_INFO,msg_header + msg)
      });

    })

  })

})

query.on('error', function(err) {
  msg_header = 'query error';
  msg = err.message;
  console.error(LOG_LEVEL_ERR, msg_header, msg);
});

query.on('end', function(result) {
  // DownloadScenes.set_total(result.rowCount);
  msg_header = 'query completed';
  const message = result.rowCount === 1 ? ' scene that needs to ordered' : ' scenes that needs to ordered'
  msg = result.rowCount + message;
  console.log(LOG_LEVEL_INFO, msg_header, msg);
});

  //  .catch( (error) => {
  //   msg_header = 'last promise error';
  //   msg = error.message;
  //   console.error(msg_header + ': ' + msg)
  //
  //   // APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
  //
  // });
      // const list = {apiKey, scene_id, node, datasetName, acquisition_date}
// write_file('test-orders', list, false)

  // .catch( (error) => {
  //   msg_header = 'api';
  //   msg = error.message;
  //   console.error(msg_header + ': ' + msg)

    // APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);

    // });

      //   //add scene_id to entityIds array only one here,  api requires the scene_id(s) as an array
      //   entityIds.push(scene_id);
      //
      //   //see if scene needs to be downloaded or ordered by finding out about availablelty
      //   const request_body = USGS_FUNCTION.usgsapi_downloadoptions(apiKey, node, datasetName, entityIds);
      //
      //
      //   const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('downloadoptions');
      //
      //   //make call to USGS api.  Make sure last promise is resolved first
      //   //  becuase USGS api is throttled for one request at a time
      //   //  wrap this in a resolve promoise so the there all requests are in promise and each one has
      //   //  to be resolved befire the next promise is started.  This is due to only limitations of the USGS API- only allows one
      //   //  api call at at time,
      //   return lastPromise = lastPromise.then( () => {
      //     //yes USGS throttles downloads so lets wait a few seconds before next request;
      //
      //       //actual request after the last promise has been resolved
      //       return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
      //         .then( downloads => {
      //
      //           //console.log('checking ordering status of scene: ' + scene_id)
      //
      //           // nothing to return so write out failed????
      //           if(!downloads[0]){
      //             DownloadScenes.add_failed('not able to download scene', scene_id);
      //           }
      //
      //           //get the orders option for the standard
      //           const standard_option_order = downloads[0].downloadOptions.filter( options => {
      //             return options.downloadCode === "STANDARD" && !options.available
      //           })
      //
      //           const entityId = downloads[0].entityId;
      //           const entityIds = [entityId]
      //
      //           //create array to hold the request json for all orders
      //           if(standard_option_order.length > 0){
      //             const orders_obj = {apiKey,node,datasetName,entityIds};
      //
      //             msg_header = 'YOU NEED TO ORDER '
      //             msg =  JSON.stringify(orders_obj)
      //             APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //             orders.push(orders_obj);
      //
      //           }
      //
      //
      //
      //       }).catch( (error) => {
      //
      //         msg_header = 'could not determine for scene';
      //         msg = DownloadScenes.iscomplete();
      //
      //         DownloadScenes.add_failed(msg_header, scene_id);
      //         APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //
      //       });
      //
      //   }).catch( (error) => {
      //     msg_header = 'last promise error';
      //     msg = error.message;
      //     APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //   });
      //
      // }).catch( (error) => {
      //   msg_header = 'api';
      //   msg = error.message;
      //   APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //   });

  // });

  //
  // const entityId = downloads[0].entityId;
  // const entityIds = [entityId]
  //
  // //create array to hold the request json for all orders
  // if(standard_option_order.length > 0){
  //   const orders_obj = {apiKey,node,datasetName,entityIds};
  //
  //   msg_header = 'YOU NEED TO ORDER '
  //   msg =  JSON.stringify(orders_obj)
  //   APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
  //
  //   orders.push(orders_obj);
  //
  // }

// })
// .catch( (error) => {
//
// msg_header = 'could not determine for scene: ' + scene_id;
// msg = error.message;
// console.error(msg_header + msg + ': ' + scene_id)
// msg = DownloadScenes.iscomplete();
//
// DownloadScenes.add_failed(msg_header, scene_id);
// APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
// });

  //   console.log('')
  //   console.log('updateorderscene');
  //   console.log(apiKey, node, datasetName, productCode, outputMedia, option, orderingId)
  // //
  //   return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
  //       .then( order_response => {
  //
  //         //make request json for submitting the order
  //         const ordered_scene = entityIds[0]
  //         const request_body = USGS_FUNCTION.usgsapi_submitorder(apiKey, node)
  //
  //         //send request to USGS api to submit the order
  //         //  unfourtunately there is no way to check the status (complete or in process) via the api
  //         const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('submitorder');
  //
  //         console.log('')
  //         console.log('submitorder');
  //         console.log(apiKey, node)
  //
  //         // return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
  //         //   .then ( order => {
  //         //
  //         //     const msg_header = 'order submitted for';
  //         //     const msg = ordered_scene;
  //         //     APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg)
  //         //
  //         //
  //         //     //create the download request json of course the the order will need to be completed before it can be downloaded
  //         //     //  since their is no way via api to check and verify the status of the order we have to hope it is completed
  //         //     var products;
  //         //     const entityIds = [ordered_scene]
  //         //     const download_body = USGS_FUNCTION.usgsapi_download(apiKey, node, datasetName, products, entityIds)
  //         //
  //         //
  //         //
  //         //   })
  //           // //catch errors for the submitorder
  //           // .catch( (error) => {
  //           //   const msg_header = 'submitorder api';
  //           //   const msg = error.message;
  //           //   console.error(msg_header + msg + ': ' + scene_id)
  //           //
  //           // });
  //       })
  //       //catch errors for adding the order
  //       .catch( (error) => {
  //         const msg_header = 'updateorderscene api';
  //         const msg = error.message;
  //         // APP_HELPERS.write_message(LOG_LEVEL_ERR, msg_header, msg)
  //         console.error(msg_header + msg + ': ' + scene_id)
  //
  //       });
  //
  //
<commit_msg>just to commit - trying to capture possible errors and ensure order is sent<commit_after>var axios = require('axios');
var url = require('url');
var yaml = require('yamljs');
var pg = require('pg');
var fs = require('fs');

//get modules
var USGS_CONSTANT = require("./lib/usgs_api/usgs_constants.js");
var USGS_FUNCTION = require("./lib/usgs_api/usgs_functions.js");
var USGS_HELPER = require("./lib/usgs_api/usgs_helpers.js");
var PG_HANDLER = require('./lib/postgres/postgres_handlers.js')

//setup shared helpers
var apphelpers = require('./lib/helpers/app_helpers.js')
var APP_HELPERS = apphelpers();

//setup failure email
var emailer = require('./lib/email/send_error_email.js');
var error_email = emailer()

//call delete old files
APP_HELPERS.delete_old_files('order_landsat_data', 'logs/', '.log');
APP_HELPERS.delete_old_files('order_failed', '', '.txt');
APP_HELPERS.delete_old_files('download_failed', '', '.txt');
APP_HELPERS.delete_old_files('downloaded', '', '.txt');
APP_HELPERS.delete_old_files('ordered', '', '.txt');
APP_HELPERS.set_logfile('order_landsat_data')

const LOG_LEVEL_ERR = 'error';
const LOG_LEVEL_INFO = 'info';

//config data
const CONFIG_YAML = yaml.load("./lib/usgs_api/config.yaml");

var scene_downloads = [];
var orders = [];

APP_HELPERS.set_logger_level('debug');

APP_HELPERS.write_message(LOG_LEVEL_INFO, 'ordering data start', '');

//set base URL for axios
axios.defaults.baseURL = USGS_CONSTANT.USGS_URL;

//get config data
const PG_CONNECT = yaml.load("./lib/postgres/config.yaml");

//get pg_client so we can query the LSF database
const pg_client = PG_HANDLER.pg_connect(PG_CONNECT)

//get fields for sql query - that gets scences that need ordering,
const scenes_fields = ' scene_id, sensor, acquisition_date, browse_url, path, row, cc_full, cc_quad_ul, cc_quad_ur, cc_quad_ll, cc_quad_lr, data_type_l1 ';

//set the SQL query to retreive scenes that need to be ordered
const scenes_for_dowloading_SQL = "SELECT " + scenes_fields + " FROM landsat_metadata WHERE needs_ordering = 'YES'"
//LT50190341989361XXX02,LT50190331989361XXX02,LT50150361989365XXX01
// WHERE sensor = 'LANDSAT_TM' LIMIT 250" // WHERE needs_ordering = 'YES'"

//captures lastpromise first one is resolved
var lastPromise = Promise.resolve();

//Qiery the LSF database for scences that need to ordered
const query = pg_client.query(scenes_for_dowloading_SQL);

//login and get promise for api key
var api_key_main = USGS_HELPER.get_api_key();

// query to check for duplicate scenes
query.on('row', function(row, result) {

  // process rows here
  api_key_main
  .then( (apiKey) => {

    //get constant for node "EE"
    var node = USGS_CONSTANT.NODE_EE;
    var entityIds = [];
    var products =  ["STANDARD"];
    var scene_id = row.scene_id;
    var acquisition_date = row.acquisition_date;

    //derieve dataset name from the scene_id and acquisition_date
    const datasetName = USGS_HELPER.get_datasetName(scene_id, acquisition_date);

    // make call to USGS api.  Make sure last promise is resolved first
    //  becuase USGS api is throttled for one request at a time
    //  wrap this in a resolve promoise so the there all requests are in promise and each one has
    //  to be resolved befire the next promise is started.  This is due to only limitations of the USGS API- only allows one
    //  api call at at time,
    return lastPromise = lastPromise.then( () => {

      var entityIds = [scene_id]

      var request_body = {apiKey, node, datasetName, entityIds};

      console.log('');
      console.log('getorderproducts');
      console.log(LOG_LEVEL_INFO, request_body)

      const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('getorderproducts');

      //actual request after the last promise has been resolved
      return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
      .then( getorderproducts_response => {

        // ensure there was a response
        if(getorderproducts_response[0] || getorderproducts_response){

          //filter possible products to downloadable level 1 datasets, with no cost.
          const orderobj = getorderproducts_response[0].availableProducts.filter( res => {
            return res.price === 0 && res.productCode.substring(0,1) != 'W' && res.outputMedias[0] === "DWNLD"

          })

          //only order the product if it is level 1 and downloadable.
          //  we need to get the product code and orderid
          if (orderobj.length > 0){

            //make request json for updating an order
            const orderingId = getorderproducts_response[0].orderingId
            const productCode = orderobj[0].productCode
            const option = 'None'
            const outputMedia = 'DWNLD'

            const request_body = USGS_FUNCTION.usgsapi_updateorderscene(apiKey, node, datasetName, productCode, outputMedia, option, orderingId);

            console.log('')
            console.log('updateorderscene')
            console.log(LOG_LEVEL_INFO, apiKey, node, datasetName, productCode, outputMedia, option, orderingId)

            //send request to USGS api to add the scene as an order
            const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('updateorderscene');

              return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
                  .then( order_response => {

                    //make request json for submitting the order
                    const ordered_scene = entityIds[0]
                    const request_body = USGS_FUNCTION.usgsapi_submitorder(apiKey, node)

                    //send request to USGS api to submit the order
                    //  unfourtunately there is no way to check the status (complete or in process) via the api
                    const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('submitorder');

                    console.log('')
                    console.log('submitorder');
                    console.log(LOG_LEVEL_INFO, apiKey, node)

                    return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
                      .then ( order => {

                        const msg_header = 'order submitted for';
                        const msg = ordered_scene;
                        console.log(LOG_LEVEL_INFO, msg_header, msg)

                   })
              })
          } else {
            console.log('nothing to order for scene ' + scene_id)
            console.log('');
          }

        }

      })
      .catch( (error) => {

        msg_header = 'get order products: ';
        msg = error.message;
        console.error(LOG_LEVEL_INFO,msg_header + msg)
      });

    })

  })

})

query.on('error', function(err) {
  msg_header = 'query error';
  msg = err.message;
  console.error(LOG_LEVEL_ERR, msg_header, msg);
});

query.on('end', function(result) {
  // DownloadScenes.set_total(result.rowCount);
  msg_header = 'query completed';
  const message = result.rowCount === 1 ? ' scene that need to ordered' : ' scenes that need to ordered'
  msg = result.rowCount + message;
  console.log(LOG_LEVEL_INFO, msg_header, msg);
});

  //  .catch( (error) => {
  //   msg_header = 'last promise error';
  //   msg = error.message;
  //   console.error(msg_header + ': ' + msg)
  //
  //   // APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
  //
  // });
      // const list = {apiKey, scene_id, node, datasetName, acquisition_date}
// write_file('test-orders', list, false)

  // .catch( (error) => {
  //   msg_header = 'api';
  //   msg = error.message;
  //   console.error(msg_header + ': ' + msg)

    // APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);

    // });

      //   //add scene_id to entityIds array only one here,  api requires the scene_id(s) as an array
      //   entityIds.push(scene_id);
      //
      //   //see if scene needs to be downloaded or ordered by finding out about availablelty
      //   const request_body = USGS_FUNCTION.usgsapi_downloadoptions(apiKey, node, datasetName, entityIds);
      //
      //
      //   const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('downloadoptions');
      //
      //   //make call to USGS api.  Make sure last promise is resolved first
      //   //  becuase USGS api is throttled for one request at a time
      //   //  wrap this in a resolve promoise so the there all requests are in promise and each one has
      //   //  to be resolved befire the next promise is started.  This is due to only limitations of the USGS API- only allows one
      //   //  api call at at time,
      //   return lastPromise = lastPromise.then( () => {
      //     //yes USGS throttles downloads so lets wait a few seconds before next request;
      //
      //       //actual request after the last promise has been resolved
      //       return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
      //         .then( downloads => {
      //
      //           //console.log('checking ordering status of scene: ' + scene_id)
      //
      //           // nothing to return so write out failed????
      //           if(!downloads[0]){
      //             DownloadScenes.add_failed('not able to download scene', scene_id);
      //           }
      //
      //           //get the orders option for the standard
      //           const standard_option_order = downloads[0].downloadOptions.filter( options => {
      //             return options.downloadCode === "STANDARD" && !options.available
      //           })
      //
      //           const entityId = downloads[0].entityId;
      //           const entityIds = [entityId]
      //
      //           //create array to hold the request json for all orders
      //           if(standard_option_order.length > 0){
      //             const orders_obj = {apiKey,node,datasetName,entityIds};
      //
      //             msg_header = 'YOU NEED TO ORDER '
      //             msg =  JSON.stringify(orders_obj)
      //             APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //             orders.push(orders_obj);
      //
      //           }
      //
      //
      //
      //       }).catch( (error) => {
      //
      //         msg_header = 'could not determine for scene';
      //         msg = DownloadScenes.iscomplete();
      //
      //         DownloadScenes.add_failed(msg_header, scene_id);
      //         APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //
      //       });
      //
      //   }).catch( (error) => {
      //     msg_header = 'last promise error';
      //     msg = error.message;
      //     APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //   });
      //
      // }).catch( (error) => {
      //   msg_header = 'api';
      //   msg = error.message;
      //   APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
      //
      //   });

  // });

  //
  // const entityId = downloads[0].entityId;
  // const entityIds = [entityId]
  //
  // //create array to hold the request json for all orders
  // if(standard_option_order.length > 0){
  //   const orders_obj = {apiKey,node,datasetName,entityIds};
  //
  //   msg_header = 'YOU NEED TO ORDER '
  //   msg =  JSON.stringify(orders_obj)
  //   APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
  //
  //   orders.push(orders_obj);
  //
  // }

// })
// .catch( (error) => {
//
// msg_header = 'could not determine for scene: ' + scene_id;
// msg = error.message;
// console.error(msg_header + msg + ': ' + scene_id)
// msg = DownloadScenes.iscomplete();
//
// DownloadScenes.add_failed(msg_header, scene_id);
// APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg);
// });

  //   console.log('')
  //   console.log('updateorderscene');
  //   console.log(apiKey, node, datasetName, productCode, outputMedia, option, orderingId)
  // //
  //   return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
  //       .then( order_response => {
  //
  //         //make request json for submitting the order
  //         const ordered_scene = entityIds[0]
  //         const request_body = USGS_FUNCTION.usgsapi_submitorder(apiKey, node)
  //
  //         //send request to USGS api to submit the order
  //         //  unfourtunately there is no way to check the status (complete or in process) via the api
  //         const USGS_REQUEST_CODE = USGS_HELPER.get_usgs_response_code('submitorder');
  //
  //         console.log('')
  //         console.log('submitorder');
  //         console.log(apiKey, node)
  //
  //         // return USGS_HELPER.get_usgsapi_response(USGS_REQUEST_CODE, request_body)
  //         //   .then ( order => {
  //         //
  //         //     const msg_header = 'order submitted for';
  //         //     const msg = ordered_scene;
  //         //     APP_HELPERS.write_message(LOG_LEVEL_INFO, msg_header, msg)
  //         //
  //         //
  //         //     //create the download request json of course the the order will need to be completed before it can be downloaded
  //         //     //  since their is no way via api to check and verify the status of the order we have to hope it is completed
  //         //     var products;
  //         //     const entityIds = [ordered_scene]
  //         //     const download_body = USGS_FUNCTION.usgsapi_download(apiKey, node, datasetName, products, entityIds)
  //         //
  //         //
  //         //
  //         //   })
  //           // //catch errors for the submitorder
  //           // .catch( (error) => {
  //           //   const msg_header = 'submitorder api';
  //           //   const msg = error.message;
  //           //   console.error(msg_header + msg + ': ' + scene_id)
  //           //
  //           // });
  //       })
  //       //catch errors for adding the order
  //       .catch( (error) => {
  //         const msg_header = 'updateorderscene api';
  //         const msg = error.message;
  //         // APP_HELPERS.write_message(LOG_LEVEL_ERR, msg_header, msg)
  //         console.error(msg_header + msg + ': ' + scene_id)
  //
  //       });
  //
  //
<|endoftext|>


<commit_before>/*
 * winston-rsyslog.js: Transport for logging to remote syslog
 *
 * (C) 2013 Fabio Grande
 * MIT LICENCE
 *
 */

var events = require('events'),
  dgram = require('dgram'),
  net = require('net'),
  os = require('os'),
  util = require('util'),
  winston = require('winston'),
  Transport = winston.Transport;

/**
 * Constructor function for the rsyslog transport object responsible
 * for sending messages to syslog daemon
 * @param {Object} [options] Options for this instance
 * @constructor
 */
var Rsyslog = exports.Rsyslog = function (options) {
  options = options || {};
  Transport.call(this, options);

  this.name = 'rsyslog';
  this.host = options.host || 'localhost';
  this.port = options.port || 514;
  this.facility = options.facility || 0;
  this.protocol = options.protocol || 'U';
  this.hostname = options.hostname || os.hostname();
  this.tag = options.tag || 'winston';
  this.timeout = options.timeout || 2000;
  this.levelMapping = options.levelMapping || winston.config.syslog.levelMapping;
  this.dateProvider = options.dateProvider || dateProviderDefault;
  this.messageProvider = options.messageProvider || messageProviderDefault;

  if (this.facility > 23 || this.facility < 0) {
    throw new Error('Facility index is out of range! (valid range is 0..23)');
  }

  if (this.protocol != 'U' && this.protocol != 'T') {
    throw new Error('Undefined Protocol! (valid options are U or T)');
  }
};

//
// Inherit from `winston.Transport`.
//
util.inherits(Rsyslog, winston.Transport);

//
// Add a new property to expose the new transport....
//
winston.transports.Rsyslog = Rsyslog;

//
// Expose the name of this Transport on the prototype
//
Rsyslog.prototype.name = 'rsyslog';

/**
 * Core logging method exposed to Winston. Metadata is optional.
 * @param {string} level Level at which to log the message
 * @param {string} msg Message to log
 * @param {Object} [meta] Additional metadata to attach
 * @callback callback Called on completion
 */
Rsyslog.prototype.log = function (level, msg, meta, callback) {
  if (this.silent) {
    return callback(null, true);
  }

  var self = this;

  // If the specified level is not included in the syslog list, convert it to 'debug'
  var _severity = 7;
  if (this.levelMapping[level] !== undefined) {
    _severity = this.levelMapping[level];
  }

  var _pri = (this.facility << 3) + _severity;
  var _date = this.dateProvider();
  var _message = this.messageProvider(level, msg, meta);
  var _buffer = new Buffer('<' + _pri + '>' + _date + ' ' + this.hostname + ' ' + this.tag + ' ' + _message);

  if (this.protocol === 'U') {
    sendUdp(self, _buffer, callback);
  } else if (this.protocol === 'T') {
    sendTcp(self, _buffer, callback);
  }
};

function sendUdp(self, buffer, callback) {
  var client = dgram.createSocket('udp4');
  client.send(buffer, 0, buffer.length, self.port, self.host, function (err) {
    if (err) {
      if (callback) {
        return callback(err);
      }
      throw err;
    }

    self.emit('logged');

    if (callback) {
      callback(null, true);
    }
    callback = null;

    client.close();
  });
}

function sendTcp(self, buffer, callback) {
  var socket = net.connect(self.port, self.host, function () {
    socket.end(buffer + '\n');

    self.emit('logged');

    if (callback) {
      callback(null, true);
    }
    callback = null;
  });

  socket.setTimeout(self.timeout);

  socket.on('error', function (err) {
    socket.destroy();
    if (callback) {
      return callback(err);
    }

    throw err;
  });

  socket.on('timeout', function (err) {
    socket.destroy();
    if (callback) {
      return callback(err);
    }

    throw err;
  });
  return callback;
}

var dateProviderDefault = function () {
  return new Date().toISOString();
};

var messageProviderDefault = function (level, msg, meta) {
  var message = process.pid + ' - ' + level + ' - ' + msg;
  if (meta && (typeof meta !== 'object' || Object.keys(meta).length)) {
    message += ' ' + util.inspect(meta);
  }
  return message;
};
<commit_msg>Correct default levelMapping so tests pass.<commit_after>/*
 * winston-rsyslog.js: Transport for logging to remote syslog
 *
 * (C) 2013 Fabio Grande
 * MIT LICENCE
 *
 */

var events = require('events'),
  dgram = require('dgram'),
  net = require('net'),
  os = require('os'),
  util = require('util'),
  winston = require('winston'),
  Transport = winston.Transport;

/**
 * Constructor function for the rsyslog transport object responsible
 * for sending messages to syslog daemon
 * @param {Object} [options] Options for this instance
 * @constructor
 */
var Rsyslog = exports.Rsyslog = function (options) {
  options = options || {};
  Transport.call(this, options);

  this.name = 'rsyslog';
  this.host = options.host || 'localhost';
  this.port = options.port || 514;
  this.facility = options.facility || 0;
  this.protocol = options.protocol || 'U';
  this.hostname = options.hostname || os.hostname();
  this.tag = options.tag || 'winston';
  this.timeout = options.timeout || 2000;
  this.levelMapping = options.levelMapping || {};
  this.dateProvider = options.dateProvider || dateProviderDefault;
  this.messageProvider = options.messageProvider || messageProviderDefault;

  if (this.facility > 23 || this.facility < 0) {
    throw new Error('Facility index is out of range! (valid range is 0..23)');
  }

  if (this.protocol != 'U' && this.protocol != 'T') {
    throw new Error('Undefined Protocol! (valid options are U or T)');
  }
};

//
// Inherit from `winston.Transport`.
//
util.inherits(Rsyslog, winston.Transport);

//
// Add a new property to expose the new transport....
//
winston.transports.Rsyslog = Rsyslog;

//
// Expose the name of this Transport on the prototype
//
Rsyslog.prototype.name = 'rsyslog';

/**
 * Core logging method exposed to Winston. Metadata is optional.
 * @param {string} level Level at which to log the message
 * @param {string} msg Message to log
 * @param {Object} [meta] Additional metadata to attach
 * @callback callback Called on completion
 */
Rsyslog.prototype.log = function (level, msg, meta, callback) {
  if (this.silent) {
    return callback(null, true);
  }

  var self = this;

  // If the specified level is not included in the syslog list, convert it to 'debug'
  var _severity = 7;
  if (this.levelMapping[level] !== undefined) {
    _severity = this.levelMapping[level];
  }

  var _pri = (this.facility << 3) + _severity;
  var _date = this.dateProvider();
  var _message = this.messageProvider(level, msg, meta);
  var _buffer = new Buffer('<' + _pri + '>' + _date + ' ' + this.hostname + ' ' + this.tag + ' ' + _message);

  if (this.protocol === 'U') {
    sendUdp(self, _buffer, callback);
  } else if (this.protocol === 'T') {
    sendTcp(self, _buffer, callback);
  }
};

function sendUdp(self, buffer, callback) {
  var client = dgram.createSocket('udp4');
  client.send(buffer, 0, buffer.length, self.port, self.host, function (err) {
    if (err) {
      if (callback) {
        return callback(err);
      }
      throw err;
    }

    self.emit('logged');

    if (callback) {
      callback(null, true);
    }
    callback = null;

    client.close();
  });
}

function sendTcp(self, buffer, callback) {
  var socket = net.connect(self.port, self.host, function () {
    socket.end(buffer + '\n');

    self.emit('logged');

    if (callback) {
      callback(null, true);
    }
    callback = null;
  });

  socket.setTimeout(self.timeout);

  socket.on('error', function (err) {
    socket.destroy();
    if (callback) {
      return callback(err);
    }

    throw err;
  });

  socket.on('timeout', function (err) {
    socket.destroy();
    if (callback) {
      return callback(err);
    }

    throw err;
  });
  return callback;
}

var dateProviderDefault = function () {
  return new Date().toISOString();
};

var messageProviderDefault = function (level, msg, meta) {
  var message = process.pid + ' - ' + level + ' - ' + msg;
  if (meta && (typeof meta !== 'object' || Object.keys(meta).length)) {
    message += ' ' + util.inspect(meta);
  }
  return message;
};
<|endoftext|>