UNPKG

docparse-upload-process

Version:

process upload api request for the docparse server

67 lines (65 loc) 1.99 kB
var inspect = require('eyespect').inspector(); var path = require('path'); var should = require('should'); var Upload = require('docparse-supplier'); var pathhash = require('pathhash'); var createUser = require('./createUser'); var createSupplier = require('./createSupplier'); var create = require('../lib/create'); function getData(data, cb) { if (data) { return cb(null, data); } data = {}; createUser(function (err, user) { should.not.exist(err, 'error creating test user: ' + err); should.exist(user, 'failed to creat test user'); createSupplier(function (err, supplier) { should.not.exist(err, 'error creating test supplier: ' + err); should.exist(user, 'failed to creat test supplier'); var configFilePath = path.join(__dirname, 'local_config.json'); var config = require('docparse-config')(configFilePath); var filePath = path.join(__dirname, 'multipage_raw.pdf'); data = { config: config, supplier_code: 'FGS', files: { upload: { path: filePath } }, user_id: user._id, user: user, supplier_id: supplier._id, filePath: filePath } cb(null, data); }); }); } module.exports = function(data, cb) { var filePath; if (!cb) { cb = data; data = null; } getData(data, function (err, data) { pathhash(data.filePath, function (err, hash) { should.not.exist(err); should.exist(hash,'failed to get hash of test pdf file'); Upload.findOne({hash: hash}, function (err, reply) { if (err) { return cb(err); } if (reply) { return cb(null, reply); } create(data, function (err, reply) { if (err) { should.not.exist(err); return cb(err); } should.exist(reply); reply.hash.should.eql(hash, 'create reply.hash is incorrect'); cb(null, reply); }); }); }); }); }