UNPKG

docparse-upload-process

Version:

process upload api request for the docparse server

83 lines (78 loc) 1.91 kB
var inspect = require('eyespect').inspector(); var mongoose = require('mongoose'); var Upload = require('docparse-upload'); var User = require('docparse-user'); var async = require('async'); var userInUpload = require('./userInUpload'); var rk = require('required-keys'); function keysExist(data, cb) { var keys = [ 'userID', 'hash' ] rk.nonNull(data, keys, function (err, reply) { if (err) { return cb(err); } cb(); }); } function findUpload(data, cb) { Upload.findOne({hash: data.hash}, function (err, reply) { if (err) { return cb(err); } if (!reply) { return cb('upload not found with hash: '+ data.hash); } data.upload = reply; cb(); }); } function findUser(data, cb) { User.findById(data.userID, function (err, reply) { if (err || !reply) { return cb('user not found with id: '+ data.userID); } data.user = reply; cb(); }); } /** * Remove user from the "users" field in the upload document, but leave the * actual upload document itself in the database */ function removeUpload(data, cb) { var upload = data.upload; var user = data.user; var userID = data.user._id upload.users.remove(userID) if (upload.user_details.hasOwnProperty(userID)) { delete upload.user_details[userID]; } upload.save(cb); // Upload.remove({hash: data.hash}, cb); } module.exports = function(data, callback) { var upload; var output; async.series([ function (cb) { keysExist(data, cb) }, function (cb) { findUpload(data, cb) }, function (cb) { findUser(data, cb) }, function(cb) { userInUpload(data, function (err, reply) { if (err) { return cb(err); } if (!reply) { return cb('you are not permitted to remove this upload'); } cb(); }); }, function(cb) { removeUpload(data, cb) } ], callback); }