From: Motion Team Date: Mon, 11 Jan 2016 15:38:39 +0000 (+0100) Subject: Built motion from commit 22bc4a4.|0.0.18 X-Git-Tag: 0.0.18 X-Git-Url: http://repos.xcallymotion.com/base//%22http:/google.github.io/material-design-icons?a=commitdiff_plain;h=48afb17a3a72e4cc7229c27456ccad5c2a03bbb6;p=motion.git Built motion from commit 22bc4a4.|0.0.18 --- diff --git a/package.json b/package.json index 24541d6..497d188 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "motion", - "version": "0.0.18", + "version": "0.0.19", "main": "server/app.js", "dependencies": { "accept-language-parser": "^1.0.2", diff --git a/release-notes/changelog_0.0.17.txt b/release-notes/changelog_0.0.18.txt similarity index 100% rename from release-notes/changelog_0.0.17.txt rename to release-notes/changelog_0.0.18.txt diff --git a/server/app.js b/server/app.js index 384cf15..57d32fb 100644 --- a/server/app.js +++ b/server/app.js @@ -1,175 +1,12 @@ -/** - * Main application file - */ - -'use strict'; - -// Set default node environment to development -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; -process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; - -process.on('uncaughtException', function(err) { - if (typeof err === 'object') { - if (err.message) { - console.log('\nMessage: ' + err.message) - } - if (err.stack) { - console.log('\nStacktrace:') - console.log('====================') - console.log(err.stack); - } - } else { - console.log('dumpError :: argument is not an object'); - } -}); - -var os = require('os'); -var http = require('http'); -var util = require('util'); -var crypto = require('crypto'); - -var express = require('express'); -var models = require('./models'); -var config = require('./config/environment'); - -var Setting = models.Setting; - -// Setup server -var app = express(); -var server = require('http').createServer(app); -var io = require('socket.io')(server, { - serveClient: (config.env === 'production') ? false : true, - path: '/socket.io-client' -}); - -var ami = require('asterisk-manager')(config.asterisk.port, config.asterisk.host, - config.asterisk.username, config.asterisk.password, true); -ami.keepConnected(); - -//Initialization Kue -var kue = require('kue'); -kue.app.listen(3001); - -var cronjob = require('cron').CronJob; - -require('./config/express')(app); -// require('./routes')(app); - -// Connect to database -models - .sequelize - .sync() - .then(function() { - - require('./routes')(app); - - // Populate DB with sample data - if (config.seedDB) { - require('./config/seed'); - } - // - // Start Cally Square Projects - require('./config/socketio')(io); - require('./config/kue.mail')(kue); - require('./config/kue.chat')(kue); - require('./config/kue.fax')(io, kue); - require('./config/automations/mail')(); - require('./config/automations/fax')(); - require('./config/triggers')(); - require('./config/imap').init(); - require('./config/smtp').init(); - require('./config/ami')(ami); - require('./config/agi')(); - require('./config/autodialer')(ami); - // require('./config/integration')(io, ami); - - // Start server - server.listen(config.port, config.ip, function() { - console.log('Express server listening on %d, in %s mode', config.port, app.get('env')); - - // var netw0= os.networkInterfaces(); - // var myConf = null; - // - // for (var networkInterface in networkInterfaces) { - // if (networkInterfaces.hasOwnProperty(networkInterface)) { - // - // var addresses = networkInterfaces[networkInterface]; - // - // for (var address in addresses) { - // if (addresses.hasOwnProperty(address)) { - // if (config.ip === addresses[address].address) { - // myConf = addresses[address]; - // break; - // } - // } - // } - // } - // } - // - // var postData = querystring.stringify(myConf); - // - // var options = { - // hostname: '192.168.2.133', - // port: 9000, - // path: '/api/servers', - // method: 'POST', - // headers: { - // 'Content-Type': 'application/x-www-form-urlencoded', - // 'Content-Length': postData.length - // } - // }; - // - // var req = http.request(options, function(res) { - // console.log('STATUS: ' + res.statusCode); - // console.log('HEADERS: ' + JSON.stringify(res.headers)); - // res.setEncoding('utf8'); - // res.on('data', function(chunk) { - // console.log('BODY: ' + chunk); - // - // Setting - // .upsert({ - // id: 1, - // license: JSON.parse(chunk).license, - // address: myConf.address, - // netmask: myConf.netmask, - // mac: myConf.mac - // }) - // .then(function(setting) { - // console.log(setting); - // }) - // .catch(function(err) { - // console.error(err); - // }); - // - // }); - // res.on('end', function() { - // console.log('No more data in response.') - // }) - // }); - // - // req.on('error', function(e) { - // console.log('problem with request: ' + e.message); - // }); - // - // // write data to request body - // req.write(postData); - // req.end(); - }); - }) - .catch(function(err) { - console.error(err); - }); - -// Connect to database history -models - .sequelize_history - .sync() - .then(function() { - require('./config/history')(cronjob); - }) - .catch(function(err) { - console.error(err); - }); - -// Expose app -exports = module.exports = app; +"use strict";function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("agent:save",b)}function onRemove(a,b,c){a.emit("agent:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("ami:save",b)}function onRemove(a,b,c){a.emit("ami:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("automation:save",b)}function onRemove(a,b,c){a.emit("automation:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("business_action:save",b)}function onRemove(a,b,c){a.emit("business_action:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("business_condition:save",b)}function onRemove(a,b,c){a.emit("business_condition:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("chat_application:save",b)}function onRemove(a,b,c){a.emit("chat_application:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("enquiry:save",b)}function onRemove(a,b,c){a.emit("enquiry:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){return b.getChatRoom().then(function(a){return[a.getUsers({attributes:["id","name","fullname","email","role","internal"],raw:!0}),a.getChatVisitor({attributes:["id","fullname","email"],raw:!0})]}).spread(function(c,d){var e=_.find(c,{id:b.UserId});c.forEach(function(c){a.userId==c.id&&(e||d)&&a.emit("chat_message:save",_.merge(b.dataValues,{User:e,ChatVisitor:d}))})})["catch"](function(a){console.error(a)})}function onRemove(a,b,c){a.emit("chat_message:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("chat_queue:save",b)}function onRemove(a,b,c){a.emit("chat_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("chat_room:save",b)}function onRemove(a,b,c){a.emit("chat_room:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("chat_visitor:save",b)}function onRemove(a,b,c){a.emit("chat_visitor:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("chat_website:save",b)}function onRemove(a,b,c){a.emit("chat_website:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("company:save",b)}function onRemove(a,b,c){a.emit("company:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("contact:save",b)}function onRemove(a,b,c){a.emit("contact:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("contact_manager:save",b)}function onRemove(a,b,c){a.emit("contact_manager:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("custom_field:save",b)}function onRemove(a,b,c){a.emit("custom_field:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("custom_report:save",b)}function onRemove(a,b,c){a.emit("custom_report:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("dashboard:save",b)}function onRemove(a,b,c){a.emit("dashboard:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("default_report:save",b)}function onRemove(a,b,c){a.emit("default_report:remove",b)}function stripTrailingSlash(a){return"/"===a.substr(-1)?a.substr(0,a.length-1):a}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("desk_account:save",b)}function onRemove(a,b,c){a.emit("desk_account:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("desk_configuration:save",b)}function onRemove(a,b,c){a.emit("desk_configuration:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("desk_field:save",b)}function onRemove(a,b,c){a.emit("desk_field:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit(("event:"+b.channel+":save").toLowerCase(),b)}function onRemove(a,b,c){a.emit("event:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("fax_account:save",b)}function onRemove(a,b,c){a.emit("fax_account:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("fax_application:save",b)}function onRemove(a,b,c){a.emit("fax_application:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("fax_business_automation:save",b)}function onRemove(a,b,c){a.emit("fax_business_automation:remove",b)}function onSave(a,b,c){if("NOT SENT"===b.status&&!b.actionid){var d=JSON.parse(b.body);console.log("faxBody",d),Trunk.findById(d.trunk.id).then(function(c){var e={Action:"originate",CallerID:d.from,Channel:util.format("%s/%s@%s",d.technology,d.to,c.name),Context:"outbound-fax",Exten:"s",Priority:1,Async:"true",Variable:{MAXRATE:d.maxrate,MINRATE:d.minrate,ECM:d.ecm?"yes":"no",LOCALID:d.fax_localid,FAXFILE:d.fax_file,FAXHEADER:d.fax_header,FAXUUID:d.uuid}};a.action(e,function(a,c){a?b.updateAttributes({status:"FAILED"}):c.response&&c.actionid&&b.updateAttributes({actionid:c.actionid})})})["catch"](function(a){console.error(a)})}}function convertPdfAndSendFax(a,b,c,d){var e=path.join(config.root,"server","files","fax","outbound","original",c+".pdf"),f=path.join(config.root,"server","files","fax","outbound","converted",c+".tif"),g="gs -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sPAPERSIZE=letter -sOutputFile="+f+" "+e,h=uuidLib.v4();FaxAccount.findById(d.accountId).then(function(a){if(a){var i={technology:"SIP",to:d.toNum,from:a.phone,fax_file:f,fax_header:a.faxheader,fax_localid:a.localid,maxrate:a.maxrate,minrate:a.minrate,ecm:a.ecm,uuid:h,trunk:{id:a.TrunkId}};fs.exists(e,function(a){a?exec(g,function(a){a?(console.error(a),handleError(b,a)):(console.log("Fax: PDF converted to TIFF"),fs.chmod(f,511,function(a){d.roomId?FaxRoom.findOrCreate({where:{id:d.roomId},defaults:{}}).spread(function(a,d){FaxMessage.create({status:"NOT SENT",read:!0,filenamePDF:c+".pdf",filename:c+".tif",body:JSON.stringify(i),uuid:h,FaxRoomId:a.id}).then(function(a){return Contact.findOrCreate({where:{phone:i.from},defaults:{phone:i.from,fullname:i.from}}).spread(function(b,c){a.setFrom(b)}),Contact.findOrCreate({where:{phone:i.to},defaults:{phone:i.to,fullname:i.to}}).spread(function(b,c){a.setTo(b)}),b.status(200).send(a)})["catch"](function(a){handleError(b,a)})}):FaxRoom.create({status:"OPEN",from:d.toNum,FaxAccountId:d.accountId,UserId:d.userId}).then(function(a){FaxMessage.create({status:"NOT SENT",read:!0,filenamePDF:c+".pdf",filename:c+".tif",body:JSON.stringify(i),uuid:h,FaxRoomId:a.id}).then(function(a){return Contact.findOrCreate({where:{phone:i.from},defaults:{phone:i.from,fullname:i.from}}).spread(function(b,c){a.setFrom(b)}),Contact.findOrCreate({where:{phone:i.to},defaults:{phone:i.to,fullname:i.to}}).spread(function(b,c){a.setTo(b)}),b.status(200).send(a)})["catch"](function(a){handleError(b,a)})})["catch"](function(a){handleError(b,a)})}))}):handleError(b,new Error("File not exists"))})}})}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("fax_message:save",b)}function onUpdate(a,b,c){a.emit("fax_message:update",b)}function onRemove(a,b,c){a.emit("fax_message:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("fax_queue:save",b)}function onRemove(a,b,c){a.emit("fax_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){"NEW"===b.status&&FaxAccount.findById(b.FaxAccountId).then(function(c){c.getFaxApplications({include:[{model:User,attributes:["id"]},{model:FaxQueue,attributes:["id","strategy","timeout"],include:{model:User,attributes:["id"]}}],order:[["priority"]]}).then(function(c){var d={roomId:b.id,accountId:b.FaxAccountId,channel:"FAX",msg:{from:b.from},applications:c};a.create("fax",d).save()})["catch"](function(a){console.error(a)})})["catch"](function(a){console.error(a)})}function onSave(a,b,c){a.emit("fax_room:save",b)}function onRemove(a,b,c){a.emit("fax_room:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("freshdesk_account:save",b)}function onRemove(a,b,c){a.emit("freshdesk_account:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("freshdesk_configuration:save",b)}function onRemove(a,b,c){a.emit("freshdesk_configuration:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("freshdesk_field:save",b)}function onRemove(a,b,c){a.emit("freshdesk_field:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("integration:save",b)}function onRemove(a,b,c){a.emit("integration:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("interval:save",b)}function onRemove(a,b,c){a.emit("interval:remove",b)}function onUpdate(a,b,c){a.emit("interval:update",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_account:save",b)}function onRemove(a,b,c){a.emit("mail_account:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_application:save",b)}function onRemove(a,b,c){a.emit("mail_application:remove",b)}function onSave(a,b,c){a.emit("mail_attachment:save",b)}function onRemove(a,b,c){a.emit("mail_attachment:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_business_automation:save",b)}function onRemove(a,b,c){a.emit("mail_business_automation:remove",b)}function handleError(a,b,c){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_message:save",b)}function onRemove(a,b,c){a.emit("mail_message:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_queue:save",b)}function onRemove(a,b,c){a.emit("mail_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_room:save",b)}function onRemove(a,b,c){a.emit("mail_room:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_server_in:save",b)}function onRemove(a,b,c){a.emit("mail_server_in:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_server_out:save",b)}function onRemove(a,b,c){a.emit("mail_server_out:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("mail_template:save",b)}function onRemove(a,b,c){a.emit("mail_template:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("metric:save",b)}function onRemove(a,b,c){a.emit("metric:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("module:save",b)}function onRemove(a,b,c){a.emit("module:remove",b)}function handleError(a,b){return a.status(500).send(b)}function decrypt(a,b,c){var d=crypto.createDecipher(a,b),e=d.update(c,"hex","utf8");return e+=d["final"]("utf8")}function onSave(a,b,c){a.emit("setting:save",b)}function onRemove(a,b,c){a.emit("setting:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.userId==b.UserId&&a.emit("motionbar:save",b)}function onRemove(a,b,c){a.emit("motionbar:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){(a.name===b.membername||"admin"===a.role)&&a.emit("report_agent:save",b)}function onRemove(a,b,c){a.emit("report_agent:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){(a.name===b.membername||"admin"===a.role)&&a.emit("report_chat:save",b)}function onUpdate(a,b,c){(a.name===b.membername||"admin"===a.role)&&(b.changed("agentid")?a.emit("report_chat:assignment",b):a.emit("report_chat:update",b))}function onRemove(a,b,c){a.emit("report_chat:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("report_chat_session:save",b)}function onRemove(a,b,c){a.emit("report_chat_session:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.name===b.membername&&a.emit(util.format("report_integration:%s",b.integration),b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){(a.name===b.membername||"admin"===a.role)&&a.emit("report_mail:save",b)}function onUpdate(a,b,c){(a.name===b.membername||"admin"===a.role)&&a.emit("report_mail:update",b)}function onRemove(a,b,c){a.emit("report_mail:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("report_mail_session:save",b)}function onRemove(a,b,c){a.emit("report_mail_session:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){"admin"===a.role&&a.emit("report_queue:save",b)}function onRemove(a,b,c){a.emit("report_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("report_tree:save",b)}function onRemove(a,b,c){a.emit("report_tree:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("salesforce_account:save",b)}function onRemove(a,b,c){a.emit("salesforce_account:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("salesforce_configuration:save",b)}function onRemove(a,b,c){a.emit("salesforce_configuration:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("salesforce_field:save",b)}function onRemove(a,b,c){a.emit("salesforce_field:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("setting:save",b)}function onRemove(a,b,c){a.emit("setting:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("square_odbc:save",b)}function onRemove(a,b,c){a.emit("square_odbc:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("square_project:save",b)}function onRemove(a,b,c){a.emit("square_project:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function stripTrailingSlash(a){return"/"===a.substr(-1)?a.substr(0,a.length-1):a}function onSave(a,b,c){a.emit("sugarcrm_account:save",b)}function onRemove(a,b,c){a.emit("sugarcrm_account:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("sugarcrm_configuration:save",b)}function onRemove(a,b,c){a.emit("sugarcrm_configuration:remove",b)}function handleError(a,b){return b=b||500,function(c){a.status(b).send(c)}}function responseWithResult(a,b){return b=b||200,function(c){c&&a.status(b).json(c)}}function handleEntityNotFound(a){return function(b){return b?b:(a.status(404).end(),null)}}function saveUpdates(a){return function(b){return b.updateAttributes(a).then(function(a){return a})}}function removeEntity(a){return function(b){return b?b.destroy().then(function(){a.status(204).end()}):void 0}}function onSave(a,b,c){a.emit("sugarcrm_field:save",b)}function onRemove(a,b,c){a.emit("sugarcrm_field:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("tag:save",b)}function onRemove(a,b,c){a.emit("tag:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("team:save",b)}function onRemove(a,b,c){a.emit("team:remove",b)}function onSave(a,b,c){a.emit("thing:save",b)}function onRemove(a,b,c){a.emit("thing:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("trigger:save",b)}function onRemove(a,b,c){a.emit("trigger:remove",b)}function trunksRewrite(a,b,c){var d="";if(!c){var e=_.clone(a.dataValues);delete e.id,delete e.createdAt,delete e.updatedAt,d+=util.format("[%s]\n",e.name),e.otherFields&&(d+=util.format("%s\n",e.otherFields),delete e.otherFields);for(var f in e)e[f]&&(d+=util.format("%s=%s\n",f,e[f]))}Trunk.findAll({where:{id:{$ne:a.id}}}).then(function(a){var c=_.pluck(a,"dataValues");c.forEach(function(a){delete a.id,delete a.createdAt,delete a.updatedAt,d+=util.format("[%s]\n",a.name),a.otherFields&&(d+=util.format("%s\n",a.otherFields),delete a.otherFields);for(var b in a)a[b]&&(d+=util.format("%s=%s\n",b,a[b]))}),fs.writeFile(path.join(config.root,"server/files/asterisk/sip_xcally_trunks.conf"),d,{flags:"w",mode:parseInt("0777",8)},function(a){if(a)throw a;console.log("Trunks file saved!"),b.action({Action:"Reload",Module:"chan_sip"},function(a,b){a?console.error(a):console.log(b)})})})["catch"](function(a){console.log(a)})}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("trunk:save",b)}function onRemove(a,b,c){a.emit("trunk:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("update:save",b)}function onRemove(a,b,c){a.emit("update:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("upload:save",b)}function onRemove(a,b,c){a.emit("upload:remove",b)}function createCallerID(a){a.name&&a.internal&&(a.callerid=util.format('"%s" <%s>',a.name,a.internal),a.save())}function updateExtension(a){a.changed("name")&&a.getUserExtensions().then(function(b){b.forEach(function(b){b.exten=b.exten===a._previousDataValues.internal?a.internal:a.name,b.appdata="SIP/"+a.name,b.save()})})["catch"](function(a){console.log(a)})}function createExtension(a){if(a.internal){var b=[{context:"from-sip",exten:a.internal,priority:1,app:"Dial",appdata:"SIP/"+a.name,UserId:a.id,type:"internal"},{context:"from-sip",exten:a.name,priority:1,app:"Dial",appdata:"SIP/"+a.name,UserId:a.id,type:"internal"}];return VoiceExtension.bulkCreate(b)["catch"](function(a){console.log(a)})}}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){"admin"===b.role&&a.emit("user:save",b)}function onRemove(a,b,c){"admin"===b.role&&a.emit("user:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("user_has_chat_queue:save",b)}function onRemove(a,b,c){a.emit("user_has_chat_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b){var c;return ChatRoom.findById(b.ChatRoomId,{include:[{model:ChatVisitor,attributes:["id","email","fullname"]},{model:User,attributes:["id","email","name","fullname"]}]}).then(function(b){c=b,c.dataValues.ChatMessages=[],console.log("_chatRoom",c);var d=_.pluck(c.Users,"id");d.forEach(function(b){a.userId==b&&a.emit("user_has_chat_room:save",c.dataValues)})})["catch"](function(a){console.error(a)})}function onRemove(a,b){a.emit("user_has_chat_room:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("user_has_fax_queue:save",b)}function onRemove(a,b,c){a.emit("user_has_fax_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("user_has_mail_queue:save",b)}function onRemove(a,b,c){a.emit("user_has_mail_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("user_has_team:save",b)}function onRemove(a,b,c){a.emit("user_has_team:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("user_has_voice_queue:save",{logged:b.logged,loggedAt:b.loggedAt,queue:b.queue,membername:b.membername,"interface":b["interface"],callstaken:b.callstaken,lastcall:b.lastcall,status:b.status,statusdesc:b.statusdesc,statusAt:b.statusAt,paused:b.paused,pausedAt:b.pausedAt,reason:b.reason})}function onRemove(a,b,c){a.emit("user_has_voice_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("variable:save",b)}function onRemove(a,b,c){a.emit("variable:remove",b)}function rewriteContexts(a,b,c){var d="";c||(d=util.format("[%s]\nswitch => Realtime\n",a.name)),VoiceContext.findAll({where:{id:{$ne:a.id}}}).then(function(a){a.forEach(function(a){d+=util.format("[%s]\nswitch => Realtime\n",a.name)}),fs.writeFile(path.join(config.root,"server/files/asterisk/sip_xcally_contexts.conf"),d,{flags:"w",mode:parseInt("0777",8)},function(a){if(a)throw a;console.log("Contexts file saved!"),b.action({Action:"Reload"},function(a,b){a?console.error(a):console.log(b)})})})["catch"](function(a){console.log(a)})}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("voice_context:save",b)}function onRemove(a,b,c){a.emit("voice_context:remove",b)}function appCreate(a,b,c,d,e){var f=d.length;switch(b.customApp&&(b.app="custom"),b.app){case"Dial":b.callerID||_.remove(a,{app:"Set"}),a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Dial":"outbound"===b.type?(a.IntervalId=b.IntervalId,a.interval=b.interval,a.trunk=b.trunk,a.TrunkId=b.TrunkId,a.cutdigits=b.cutdigits||0,a.callerID=b.callerID||null):(a.IntervalId=b.IntervalId,a.interval=b.interval,a["interface"]=b["interface"],a.app_options=b.app_options||"",a.timeout=b.timeout||"",a.url=b.url||"",a.appdata=util.format(a.appdata,b["interface"],a.timeout,a.app_options?a.app_options.replace(",",""):"",a.url));break;case"Set":a.appdata=util.format(a.appdata,b.callerID);break;case"NoOp":"outbound"===b.type&&(a.appdata="Outbound Dial Application End")}});break;case"Queue":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Queue":a.IntervalId=b.IntervalId,a.app_options=b.app_options||"",a.audiofile=b.audiofile||"",a.queue=b.queue,a.interval=b.interval,a["interface"]=b["interface"],a.app_options=b.app_options,a.timeout=b.timeout||"",a.url=b.url||"",a.appdata=util.format(a.appdata,a.queue,a.app_options?a.app_options.replace(",",""):"",a.url,a.audiofile,a.timeout)}});break;case"Playback":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Playback":a.IntervalId=b.IntervalId,a.interval=b.interval,a.audiofile=b.audiofile||"",a.app_options=b.app_options||"",a.appdata=util.format(a.appdata,a.audiofile,a.app_options?a.app_options.replace(",",""):"")}});break;case"AGI":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"AGI":a.IntervalId=b.IntervalId,a.interval=b.interval,a.project=b.project,a.appdata=util.format(a.appdata,a.project)}});break;case"Goto":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Goto":a.IntervalId=b.IntervalId,a.interval=b.interval,a.alt_priority=b.alt_priority,a.alt_extension=b.alt_extension,a.alt_context=b.alt_context,a.appdata=util.format(a.appdata,a.alt_context,a.alt_extension,a.alt_priority)}});break;case"Hangup":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Hangup":a.IntervalId=b.IntervalId,a.interval=b.interval,a.causecode=b.causecode,a.appdata=util.format(a.appdata,a.causecode)}});break;case"Set":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Set":a.IntervalId=b.IntervalId,a.interval=b.interval,a.variable=b.variable,a.value=b.value,a.appdata=util.format(a.appdata,a.variable,a.value)}});break;case"custom":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"custom":a.IntervalId=b.IntervalId,a.interval=b.interval,a.app=a.customApp=b.customApp,a.appdata=util.format(a.appdata,b.appdata)}});break;case"Voicemail":a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"Voicemail":a.IntervalId=b.IntervalId,a.interval=b.interval,a.mailbox=b.mailbox,a.appdata=util.format(a.appdata,a.mailbox)}});break;default:a.forEach(function(a,d){switch(a.exten=b.exten,a.type=b.type,a.context=b.context,a.VoiceExtensionId=b.VoiceExtensionId,a.priority=e?e++:b.priority++,a.appGroup=c,a.app){case"GotoIfTime":a.appdata=util.format(a.appdata,b.IntervalId?"*,*,*,*":b.interval,a.context,a.priority+1,a.context,a.priority+2);break;case"GotoIfTimeFromArray":a.app="GotoIfTime",a.appdata=util.format(a.appdata,a.context,a.priority+f-d,a.context,f>d+1?a.priority+1:a.priority+2);break;case"custom":a.IntervalId=b.IntervalId,a.interval=b.interval,a.app=a.customApp=b.customApp,a.appdata=util.format(a.appdata,b.appdata)}})}return e?e:void 0}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("voice_extension:save",b); +}function onRemove(a,b,c){a.emit("voice_extension:remove",b)}function onUpdate(a,b,c){a.emit("voice_extension:update",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("voice_musiconhold:save",b)}function onRemove(a,b,c){a.emit("voice_musiconhold:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("voice_queue:save",b)}function onRemove(a,b,c){a.emit("voice_queue:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("voice_voicemail:save",b)}function onRemove(a,b,c){a.emit("voice_voicemail:remove",b)}function onMessageSave(a,b,c){a.emit("voice_voicemail_messages:save",b)}function onMessageRemove(a,b,c){a.emit("voice_voicemail_messages:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("xchatty:save",b)}function onRemove(a,b,c){a.emit("xchatty:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("zendesk_account:save",b)}function onRemove(a,b,c){a.emit("zendesk_account:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("zendesk_configuration:save",b)}function onRemove(a,b,c){a.emit("zendesk_configuration:remove",b)}function handleError(a,b){return a.status(500).send(b)}function onSave(a,b,c){a.emit("zendesk_field:save",b)}function onRemove(a,b,c){a.emit("zendesk_field:remove",b)}process.env.NODE_ENV=process.env.NODE_ENV||"development",process.env.NODE_TLS_REJECT_UNAUTHORIZED=0,process.on("uncaughtException",function(a){"object"==typeof a?(a.message&&console.log("\nMessage: "+a.message),a.stack&&(console.log("\nStacktrace:"),console.log("===================="),console.log(a.stack))):console.log("dumpError :: argument is not an object")});var os=require("os"),http=require("http"),util=require("util"),crypto=require("crypto"),express=require("express"),models=require("./models"),config=require("./config/environment"),Setting=models.Setting,app=express(),server=require("http").createServer(app),io=require("socket.io")(server,{serveClient:"production"===config.env?!1:!0,path:"/socket.io-client"}),ami=require("asterisk-manager")(config.asterisk.port,config.asterisk.host,config.asterisk.username,config.asterisk.password,!0);ami.keepConnected();var kue=require("kue");kue.app.listen(3001);var cronjob=require("cron").CronJob;require("./config/express")(app),models.sequelize.sync().then(function(){require("./routes")(app),config.seedDB&&require("./config/seed"),require("./config/socketio")(io),require("./config/kue.mail")(kue),require("./config/kue.chat")(kue),require("./config/kue.fax")(io,kue),require("./config/automations/mail")(),require("./config/automations/fax")(),require("./config/triggers")(),require("./config/imap").init(),require("./config/smtp").init(),require("./config/ami")(ami),require("./config/agi")(),require("./config/autodialer")(ami),server.listen(config.port,config.ip,function(){console.log("Express server listening on %d, in %s mode",config.port,app.get("env"))})})["catch"](function(a){console.error(a)}),models.sequelize_history.sync().then(function(){require("./config/history")(cronjob)})["catch"](function(a){console.error(a)}),exports=module.exports=app;var _=require("lodash"),util=require("util"),Agent=require("../../models").User,Team=require("../../models").Team;exports.index=function(a,b,c){var d=["fullname","name","email"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{role:"agent"},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),Agent.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Agent.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.internalValidation=function(a,b){Agent.findAll({where:{internal:a.body.internal}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Agent.create(a.body).then(function(a){Team.findOne({where:{defaultEntry:!0}}).then(function(c){c.addUser(a.id).then(function(){return b.status(201).send(a)})["catch"](function(a){return console.log(a),handleError(b,a)})})["catch"](function(a){return console.log(a),handleError(b,a)})})["catch"](function(a){return console.log(a),handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Agent.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Agent.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Agent.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.changePassword=function(a,b,c){console.log(a.body);var d=a.user.id,e=String(a.body.oldPassword),f=String(a.body.newPassword);Agent.findById(d).then(function(a){a.authenticate(e)?(a.password=f,a.save().then(function(){b.status(200).send(a)})["catch"](function(a){return c(a)})):b.sendStatus(403)})},exports.resetPassword=function(a,b,c){var d=String(a.body.newPassword);Agent.findById(a.params.id).then(function(a){a.password=d,a.save().then(function(){b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})})},exports.agentValidation=function(a,b){var c={};c[a.params.field]=a.body.value,Agent.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})};var agent=require("../../models").User;exports.register=function(a){agent.afterCreate(function(b){"agent"===b.role&&onSave(a,b)}),agent.afterUpdate(function(b){"agent"===b.role&&onSave(a,b)}),agent.afterDestroy(function(b){"agent"===b.role&&onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/agents",function(){it("should respond with JSON array",function(a){request(app).get("/api/agents").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./agent.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/validate/:field",auth.isAuthenticated(),controller.agentValidation),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.internalValidation),router.put("/password",auth.isAuthenticated(),controller.changePassword),router.put("/password/:id/reset",auth.isAuthenticated(),auth.hasRole("admin"),controller.resetPassword),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var Ami=require("../../models").Ami,async=require("async"),_=require("lodash"),ami=null;exports.register=function(a){this.ami=a},exports.action=function(a,b){this.ami&&this.ami.isConnected()?this.ami.action(a,function(c,d){c&&b(c);var e=_.merge(d,a);Ami.create(e).then(function(a){b(null,a)})["catch"](function(a){return b(a)})}):b(new Error("Asterisk Manager disconnected.."))};var _=require("lodash"),Ami=require("../../models").Ami;exports.index=function(a,b){Ami.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Ami.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){a.body.data=JSON.stringify(a.body),a.body.UserId=a.user.id,require("./ami.ami").action(a.body,function(a,c){return a?void handleError(b,a):b.status(201).send(c)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Ami.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Ami.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Ami=require("../../models").Ami;exports.register=function(a){Ami.afterCreate(function(b){onSave(a,b)}),Ami.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/ami",function(){it("should respond with JSON array",function(a){request(app).get("/api/ami").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./ami.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.post("/",auth.isAuthenticated(),controller.create),module.exports=router;var _=require("lodash"),util=require("util"),Automation=require("../../models").Automation;exports.index=function(a,b,c){var d=["fullname","name","email"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),Automation.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Automation.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){Automation.create(a.body).then(function(){return b.sendStatus(201)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b,c){a.body.id&&delete a.body.id,Automation.findById(a.params.id).then(function(d){if(!d)return b.sendStatus(404);var e=_.merge(d,a.body);e.save().then(function(){return b.status(200).send(d)})["catch"](function(a){return c(a)})})["catch"](function(a){return c(a)})},exports.destroy=function(a,b){Automation.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Automation.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Automation=require("../../models").Automation;exports.register=function(a){Automation.afterCreate(function(b){onSave(a,b)}),Automation.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/automations",function(){it("should respond with JSON array",function(a){request(app).get("/api/automations").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./automation.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),module.exports=router;var _=require("lodash"),BusinessAction=require("../../models").BusinessAction;exports.index=function(a,b){BusinessAction.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){BusinessAction.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){BusinessAction.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,BusinessAction.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){BusinessAction.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var BusinessAction=require("../../models").BusinessAction;exports.register=function(a){BusinessAction.afterCreate(function(b){onSave(a,b)}),BusinessAction.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/business/actions",function(){it("should respond with JSON array",function(a){request(app).get("/api/business/actions").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./business_action.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),BusinessCondition=require("../../models").BusinessCondition;exports.index=function(a,b){BusinessCondition.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){BusinessCondition.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){BusinessCondition.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,BusinessCondition.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){BusinessCondition.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var BusinessCondition=require("../../models").BusinessCondition;exports.register=function(a){BusinessCondition.afterCreate(function(b){onSave(a,b)}),BusinessCondition.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/business/conditions",function(){it("should respond with JSON array",function(a){request(app).get("/api/business/conditions").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./business_condition.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ChatApplication=require("../../models").ChatApplication;exports.index=function(a,b){ChatApplication.findAll({where:a.query,order:"priority",include:[{all:!0}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ChatApplication.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){ChatApplication.max("priority",{where:{ChatWebsiteId:a.body.ChatWebsiteId}}).then(function(d){a.body.priority=d?++d:1,ChatApplication.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return c(a)})})["catch"](function(a){c(a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ChatApplication.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatApplication.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ChatApplication=require("../../models").ChatApplication;exports.register=function(a){ChatApplication.afterCreate(function(b){onSave(a,b)}),ChatApplication.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/applications",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/applications").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./chat_application.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ChatEnquiry=require("../../models").ChatEnquiry,ChatVisitor=require("../../models").ChatVisitor;exports.index=function(a,b){ChatEnquiry.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ChatEnquiry.findOne({where:{id:a.params.id},include:[ChatVisitor]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ChatEnquiry.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ChatEnquiry.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatEnquiry.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var enquiry=require("../../models").ChatEnquiry;exports.register=function(a){enquiry.afterCreate(function(b){onSave(a,b)}),enquiry.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./enquiry.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),md5=require("md5"),User=require("../../models").User,ChatRoom=require("../../models").ChatRoom,ChatMessage=require("../../models").ChatMessage,ChatVisitor=require("../../models").ChatVisitor;exports.index=function(a,b){ChatMessage.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ChatMessage.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){ChatMessage.create(_.merge(a.body,{UserId:a.user.id})).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){return a.body.id&&delete a.body.id,ChatMessage.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);return d.save()}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatMessage.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var chat_message=require("../../models").ChatMessage,_=require("lodash");exports.register=function(a){chat_message.afterCreate(function(b){onSave(a,b)}),chat_message.afterUpdate(function(b){onSave(a,b)}),chat_message.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/messages",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/messages").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./chat_message.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),ChatQueue=require("../../models").ChatQueue,UserHasChatQueue=require("../../models").UserHasChatQueue;exports.index=function(a,b){var c=["description","name","timeout","strategy"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d,include:[{all:!0}]};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),ChatQueue.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.me=function(a,b,c){UserHasChatQueue.findAll({where:{UserId:a.user.id}}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ChatQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ChatQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ChatQueue.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.addAgents=function(a,b,c){return ChatQueue.findById(a.params.id).then(function(b){if(b)return b.addUsers(a.body.agents,{individualHooks:!0});throw new Error("No mail queue found")}).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)})},exports.removeAgents=function(a,b,c){return ChatQueue.findById(a.params.id).then(function(b){if(b)return b.removeUsers(a.body.agents,{individualHooks:!0});throw new Error("no mail queue found")}).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)})},exports.destroy=function(a,b){ChatQueue.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){ChatQueue.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var chatQueue=require("../../models").ChatQueue;exports.register=function(a){chatQueue.afterCreate(function(b){onSave(a,b)}),chatQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./chat_queue.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",controller.index),router.get("/me",auth.isAuthenticated(),controller.me),router.get("/:id",controller.show),router.post("/",controller.create),router.post("/:id/agents",controller.addAgents),router.put("/:id",controller.update),router.put("/:id/agents",controller.removeAgents),router.patch("/:id",controller.update),router["delete"]("/",controller.bulkDestroy),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),md5=require("md5"),moment=require("moment"),User=require("../../models").User,ChatRoom=require("../../models").ChatRoom,ChatMessage=require("../../models").ChatMessage,ChatVisitor=require("../../models").ChatVisitor,UserHasChatRoom=require("../../models").UserHasChatRoom;exports.index=function(a,b){return ChatRoom.findAll({include:[User,ChatMessage,ChatVisitor]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.me=function(a,b){return User.findById(a.user.id).then(function(a){return a.getChatRooms({include:[{model:ChatVisitor,attributes:["id","fullname","email"]},{model:User,attributes:["id","name","fullname","email","role","internal"]}]})}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){return ChatRoom.findById(a.params.id,{include:[{model:ChatMessage,include:[{model:ChatVisitor,attributes:["id","email","fullname"]},{model:User,attributes:["id","email","name","fullname"]}]},{model:User,attributes:["id","email","name","fullname"]}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.getRoomByUsers=function(a,b,c){var d;return ChatRoom.findOrCreate({where:{token:md5(JSON.stringify(a.query.users.sort())).toString("base64")},defaults:{type:"internal",token:md5(JSON.stringify(a.query.users.sort())).toString("base64"),status:"open"},include:[{model:ChatMessage,include:[{model:ChatVisitor,attributes:["id","email","fullname"]},{model:User,attributes:["id","name","fullname","email","role","internal"]}]},{model:User,attributes:["id","name","fullname","email","role","internal"]}]}).spread(function(b,c){return d=b,c?d.setUsers(a.query.users):void 0}).then(function(){return d.getUsers()}).then(function(a){return b.status(200).send(_.merge(d.dataValues,{Users:a}))})["catch"](function(a){return handleError(b,a)})},exports.getRoomByType=function(a,b){User.findById(a.user.id).then(function(c){c.getChatRooms({where:{type:a.params.type},include:[{model:ChatVisitor,attributes:["id","fullname","email"]}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.getRoomGroups=function(a,b){User.findById(a.user.id).then(function(a){a.getChatRooms({include:[{model:ChatVisitor,attributes:["id","fullname","email"]},{model:User,attributes:["id","name","fullname","email","role","internal"]}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){},exports.update=function(a,b){a.body.id&&delete a.body.id,ChatRoom.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.updateUsers=function(a,b){var c;return a.body.id&&delete a.body.id,ChatRoom.findById(a.params.id).then(function(c){return c&&a.body.users?c:b.sendStatus(404)}).then(function(b){return b.update({token:md5(JSON.stringify(a.body.users.sort())).toString("base64")})}).then(function(b){return c=b,b.setUsers(a.body.users.sort(),{individualHooks:!0})}).then(function(){return c.getUsers()}).then(function(a){return b.status(200).send(_.merge(c.dataValues,{Users:a,ChatMessages:[]}))})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatRoom.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var User=require("../../models").User,ChatRoom=require("../../models").ChatRoom,ChatMessage=require("../../models").ChatMessage,ChatVisitor=require("../../models").ChatVisitor;exports.register=function(a){ +ChatRoom.afterCreate(function(b){onSave(a,b)}),ChatRoom.afterUpdate(function(b){onSave(a,b)}),ChatRoom.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/rooms",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/rooms").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./chat_room.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",controller.index),router.get("/me",auth.isAuthenticated(),controller.me),router.get("/users",auth.isAuthenticated(),controller.getRoomByUsers),router.get("/groups",auth.isAuthenticated(),controller.getRoomGroups),router.get("/type/:type",auth.isAuthenticated(),controller.getRoomByType),router.get("/:id",controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",controller.update),router.put("/:id/users",controller.updateUsers),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ChatVisitor=require("../../models").ChatVisitor;exports.index=function(a,b){ChatVisitor.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ChatVisitor.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ChatVisitor.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ChatVisitor.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatVisitor.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var chat_visitor=require("../../models").ChatVisitor;exports.register=function(a){chat_visitor.afterCreate(function(b){onSave(a,b)}),chat_visitor.afterUpdate(function(b){onSave(a,b)}),chat_visitor.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/visitors",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/visitors").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./chat_visitor.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),async=require("async"),Mustache=require("mustache"),sequelize=require("../../models").sequelize,ChatWebsite=require("../../models").ChatWebsite,ChatApplication=require("../../models").ChatApplication,snippet=require("../../components/xchatty").snippet;exports.index=function(a,b,c){var d=["name","address","description","remote"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),ChatWebsite.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){ChatWebsite.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.getSnippetCode=function(a,b){ChatWebsite.findById(a.params.id).then(function(c){var d={remote:c.remote,websiteId:a.params.id},e=Mustache.render(snippet,d);return b.send(e)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ChatWebsite.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){return a.body.id&&delete a.body.id,ChatWebsite.findById(a.params.id).then(function(c){return c?c.update(a.body).then(function(a){return b.status(200).send(a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ChatWebsite.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.sortApplications=function(a,b,c){ChatApplication.findAll({where:{id:a.body.applications}}).then(function(c){var d=c;async.waterfall([function(b){ChatApplication.destroy({where:{id:a.body.applications}}).then(function(){b()})["catch"](function(a){b(a)})},function(b){for(var c=[],e=0;ef.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.companyValidation=function(a,b){console.log(a.body),Company.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Company.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Company.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){Company.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_COMPANY"}):(a.body.id&&delete a.body.id,void Company.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Company.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Company.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Company=require("../../models").Company;exports.register=function(a){Company.afterCreate(function(b){onSave(a,b)}),Company.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/contactmanager/companies",function(){it("should respond with JSON array",function(a){request(app).get("/api/contactmanager/companies").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./company.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.companyValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Contact=require("../../models").Contact;exports.index=function(a,b){Contact.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Contact.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Contact.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Contact.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Contact.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},module["export"]=function(a,b){var c=a.define("Contact",{name:String,info:String,active:Boolean});return c};var contact=require("../../models").Contact;exports.register=function(a){contact.afterCreate(function(b){onSave(a,b)}),contact.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/contacts",function(){it("should respond with JSON array",function(a){request(app).get("/api/contacts").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./contact.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),Contact=require("../../models").Contact,ContactPhone=require("../../models").ContactPhone,ContactEmail=require("../../models").ContactEmail,CustomField=require("../../models").CustomField,ReportCall=require("../../models").ReportCall,ReportCallHistory=require("../../models").history.ReportCallHistory,ReportMailSession=require("../../models").ReportMailSession,ReportMailSessionHistory=require("../../models").history.ReportMailSessionHistory,Tag=require("../../models").Tag,stream=require("stream"),sequelize=require("../../models").sequelize,util=require("util");exports.index=function(a,b){var c=["name","surname","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Contact.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Contact.findById(a.params.id,{include:[{model:ContactPhone,as:"Phones",include:[{model:ReportCall,as:"Inbounds",include:[{all:!0}]},{model:ReportCall,as:"Outbounds",include:[{all:!0}]},{model:ReportCallHistory,as:"HistoryInbounds",include:[{all:!0}]},{model:ReportCallHistory,as:"HistoryOutbounds",include:[{all:!0}]}]},{model:ContactEmail,as:"Emails",include:[{model:ReportMailSession,as:"InboundMessages",include:[{all:!0}]},{model:ReportMailSession,as:"OutboundMessages",include:[{all:!0}]},{model:ReportMailSessionHistory,as:"HistoryInboundMessages",include:[{all:!0}]},{model:ReportMailSessionHistory,as:"HistoryOutboundMessages",include:[{all:!0}]}]},{model:CustomField,as:"CustomFields"}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return console.log(a),handleError(b,a)})},exports.getHistory=function(a,b){},exports.contactValidation=function(a,b){Contact.findAll({where:{$or:{"$Phones.phone$":a.body.phones,"$Emails.email$":a.body.emails}},include:[{all:!0}]}).then(function(a){return console.log(a),b.status(200).send(a)})["catch"](function(a){return console.log(a),handleError(b,a)})},exports.create=function(a,b,c){var d=[],e=[],f={};Tag.findAll().then(function(b){var c=[];_.pluck(_.pluck(b,"dataValues"),"name");if(a.body.tags){var d=a.body.tags.split(",");d.forEach(function(a){_.includes(b,a)||c.push({name:a})}),c.length&&Tag.bulkCreate(c,{individualHooks:!0})}a.body.UserId=a.user.id;var e=_.uniq(_.pluck(a.body.Phones,"phone")),f=[];return e.forEach(function(a){f.push(ContactPhone.findOrCreate({where:{phone:a},defaults:{phone:a}}))}),f}).all().then(function(b){d=_.map(b,function(a){return a[0]});var c=[],e=_.uniq(_.pluck(a.body.Emails,"email"));return e.forEach(function(a){c.push(ContactEmail.findOrCreate({where:{email:a},defaults:{email:a}}))}),c}).all().then(function(b){return e=_.map(b,function(a){return a[0]}),delete a.body.Phones,delete a.body.Emails,f=_.clone(a.body.customFields),delete a.body.customFields,Contact.create(a.body)}).then(function(a){return[a,a.setPhones(d)]}).spread(function(a){return[a,a.setEmails(e)]}).spread(function(a){var b=_.keys(f);return[a,CustomField.findAll({where:{id:b}})]}).spread(function(a,b){return b.forEach(function(a){a.ContactHasCustomField={value:f[String(a.id)]}}),[a,a.setCustomFields(b)]}).spread(function(a){return b.status(201).send(a)})["catch"](function(a){return console.log(a),c(a)})},exports.update=function(a,b,c){var d=[],e=[],f={};Tag.findAll().then(function(b){var c=[];_.pluck(_.pluck(b,"dataValues"),"name");if(a.body.tags){var d=a.body.tags.split(",");d.forEach(function(a){_.includes(b,a)||c.push({name:a})}),c.length&&Tag.bulkCreate(c,{individualHooks:!0})}var e=[],f=_.uniq(_.pluck(a.body.Phones,"phone"));return f.forEach(function(a){e.push(ContactPhone.findOrCreate({where:{phone:a},defaults:{phone:a}}))}),e}).all().then(function(b){d=_.map(b,function(a){return a[0]});var c=[],e=_.uniq(_.pluck(a.body.Emails,"email"));return e.forEach(function(a){c.push(ContactEmail.findOrCreate({where:{email:a},defaults:{email:a}}))}),c}).all().then(function(b){e=_.map(b,function(a){return a[0]}),delete a.body.Phones,delete a.body.Emails,f=_.clone(a.body.customFields),delete a.body.customFields,delete a.body.CustomFields;var c=a.body.id;return delete a.body.id,Contact.findById(c)}).then(function(a){return[a,a.setPhones(d)]}).spread(function(a){return[a,a.setEmails(e)]}).spread(function(a){var b=_.keys(f);return[a,CustomField.findAll({where:{id:b}})]}).spread(function(a,b){return b.forEach(function(a){a.ContactHasCustomField={value:f[String(a.id)]}}),[a,a.setCustomFields(b)]}).spread(function(b){return b.updateAttributes(a.body)}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return console.log(a),handleError(b,a)})},exports.destroy=function(a,b){Contact.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Contact.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Contact=require("../../models").Contact;exports.register=function(a){Contact.afterCreate(function(b){onSave(a,b)}),Contact.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/contactmanager",function(){it("should respond with JSON array",function(a){request(app).get("/api/contactmanager").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./contact_manager.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/history",auth.isAuthenticated(),controller.getHistory),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.contactValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),CustomField=require("../../models").CustomField,util=require("util");exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{active:!0},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),CustomField.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.getAll=function(a,b){CustomField.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.customFieldValidation=function(a,b){console.log(a.body),CustomField.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){CustomField.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){CustomField.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){CustomField.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_CUSTOM_FIELD"}):(a.body.id&&delete a.body.id,void CustomField.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.deactivate=function(a,b){CustomField.findById(a.params.id).then(function(a){return a?void a.update({active:!1}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDeactivate=function(a,b){CustomField.update({active:!1},{where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var CustomField=require("../../models").CustomField;exports.register=function(a){CustomField.afterCreate(function(b){onSave(a,b)}),CustomField.afterUpdate(function(b){b.active||onRemove(a,b)}),CustomField.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/contactmanager/customfields",function(){it("should respond with JSON array",function(a){request(app).get("/api/contactmanager/customfields").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./custom_field.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/all",auth.isAuthenticated(),controller.getAll),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.customFieldValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDeactivate),router["delete"]("/:id",auth.isAuthenticated(),controller.deactivate),module.exports=router;var _=require("lodash"),util=require("util"),CustomReport=require("../../models").CustomReport,ReportTree=require("../../models").ReportTree;exports.index=function(a,b,c){var d=["name","description","parent"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;case"role":g.where.role={$or:b.split(/[\s,]+/)};break;case"clientQuery":var e=JSON.parse(b);_.forOwn(e,function(a,b){g.where[b]=a});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),CustomReport.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){CustomReport.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){CustomReport.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,CustomReport.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){CustomReport.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var CustomReport=require("../../models").CustomReport;exports.register=function(a){CustomReport.afterCreate(function(b){onSave(a,b)}),CustomReport.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/analytics/reports/custom",function(){it("should respond with JSON array",function(a){request(app).get("/api/analytic/reports/custom").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./custom_report.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),Dashboard=require("../../models").Dashboard;exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Dashboard.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.all=function(a,b){Dashboard.findAll().then(function(a){return a?b.status(200).send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Dashboard.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Dashboard.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Dashboard.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Dashboard.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Dashboard.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Dashboard=require("../../models").Dashboard;exports.register=function(a){Dashboard.afterCreate(function(b){onSave(a,b)}),Dashboard.afterUpdate(function(b){onSave(a,b)}),Dashboard.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/dashboards",function(){it("should respond with JSON array",function(a){request(app).get("/api/dashboards").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./dashboard.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/all",auth.isAuthenticated(),controller.all),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),DefaultReport=require("../../models").DefaultReport;exports.index=function(a,b,c){var d=["name","description","parent"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;case"role": +g.where.role={$or:b.split(/[\s,]+/)};break;case"clientQuery":var e=JSON.parse(b);_.forOwn(e,function(a,b){g.where[b]=a});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),DefaultReport.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){DefaultReport.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){DefaultReport.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,DefaultReport.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){DefaultReport.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var DefaultReport=require("../../models").DefaultReport;exports.register=function(a){DefaultReport.afterCreate(function(b){onSave(a,b)}),DefaultReport.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/analytics/reports/default",function(){it("should respond with JSON array",function(a){request(app).get("/api/analytic/reports/default").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./default_report.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),desk=require("desk.js"),DeskAccount=require("../../models").DeskAccount;exports.index=function(a,b){var c=["description","name","username","remoteUri"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),DeskAccount.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){DeskAccount.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){DeskAccount.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,DeskAccount.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){DeskAccount.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.checkAccount=function(a,b){DeskAccount.findById(a.params.id).then(function(a){var c={endpoint:stripTrailingSlash(a.remoteUri),retry:!1};"basic"===a.authType?(c.username=a.username,c.password=a.password):"oauth"===a.authType&&(c.consumerKey=a.consumerKey,c.consumerSecret=a.consumerSecret,c.token=a.token,c.tokenSecret=a.tokenSecret);var d=desk.createClient(c);d.get("/api/v2/users/me",function(a,c,d){return a?handleError(b,a):b.sendStatus(200)})})["catch"](function(a){return handleError(b,a)})},exports.deskValidation=function(a,b){var c={};c[a.params.field]=a.body.value,DeskAccount.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.getFields=function(a,b){DeskAccount.findById(a.params.id).then(function(a){var c={endpoint:stripTrailingSlash(a.remoteUri),retry:!1};"basic"===a.authType?(c.username=a.username,c.password=a.password):"oauth"===a.authType&&(c.consumerKey=a.consumerKey,c.consumerSecret=a.consumerSecret,c.token=a.token,c.tokenSecret=a.tokenSecret);var d=desk.createClient(c);d.get("/api/v2/custom_fields",function(a,c,d){return a?handleError(b,a):b.status(200).json(c._embedded.entries)})})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){DeskAccount.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var DeskAccount=require("../../models").DeskAccount;exports.register=function(a){DeskAccount.afterCreate(function(b){onSave(a,b)}),DeskAccount.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/desk/accounts",function(){it("should respond with JSON array",function(a){request(app).get("/api/desk/accounts").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./desk_account.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/check",auth.isAuthenticated(),controller.checkAccount),router.get("/:id/fields",auth.isAuthenticated(),controller.getFields),router.post("/validate/:field",auth.isAuthenticated(),controller.deskValidation),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),DeskConfiguration=require("../../models").DeskConfiguration,DeskField=require("../../models").DeskField;exports.index=function(a,b){DeskConfiguration.findAll({where:a.query}).then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){DeskConfiguration.find({where:{id:a.params.id},include:[{model:DeskField,as:"Subject"},{model:DeskField,as:"Description"},{model:DeskField,as:"Field"}]}).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){DeskConfiguration.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,DeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){DeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.addConfigurationField=function(a,b,c){var d;DeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(function(b){return d=b,DeskField.create(a.body)}).then(function(b){switch(a.params.type){case"subject":return[d.addSubject(b),b];case"description":return[d.addDescription(b),b];case"field":return[d.addField(b),b]}}).spread(function(a,c){return b.status(201).json(c)})["catch"](handleError(b))};var DeskConfiguration=require("../../models").DeskConfiguration;exports.register=function(a){DeskConfiguration.afterCreate(function(b){onSave(a,b)}),DeskConfiguration.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./desk_configuration.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/:id/:type",auth.isAuthenticated(),controller.addConfigurationField),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),deskConfigurationCtrlStub={index:"deskConfigurationCtrl.index",show:"deskConfigurationCtrl.show",create:"deskConfigurationCtrl.create",update:"deskConfigurationCtrl.update",destroy:"deskConfigurationCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},deskConfigurationIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./desk_configuration.controller":deskConfigurationCtrlStub});describe("DeskConfiguration API Router:",function(){it("should return an express router instance",function(){expect(deskConfigurationIndex).to.equal(routerStub)}),describe("GET /api/desk/configurations",function(){it("should route to deskConfiguration.controller.index",function(){expect(routerStub.get.withArgs("/","deskConfigurationCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/desk/configurations/:id",function(){it("should route to deskConfiguration.controller.show",function(){expect(routerStub.get.withArgs("/:id","deskConfigurationCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/desk/configurations",function(){it("should route to deskConfiguration.controller.create",function(){expect(routerStub.post.withArgs("/","deskConfigurationCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/desk/configurations/:id",function(){it("should route to deskConfiguration.controller.update",function(){expect(routerStub.put.withArgs("/:id","deskConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/desk/configurations/:id",function(){it("should route to deskConfiguration.controller.update",function(){expect(routerStub.patch.withArgs("/:id","deskConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/desk/configurations/:id",function(){it("should route to deskConfiguration.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","deskConfigurationCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),DeskField=require("../../models").DeskField;exports.index=function(a,b){DeskField.findAll().then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){DeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){DeskField.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,DeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){DeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))};var DeskField=require("../../models").DeskField;exports.register=function(a){DeskField.afterCreate(function(b){onSave(a,b)}),DeskField.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./desk_field.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),deskFieldCtrlStub={index:"deskFieldCtrl.index",show:"deskFieldCtrl.show",create:"deskFieldCtrl.create",update:"deskFieldCtrl.update",destroy:"deskFieldCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},deskFieldIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./desk_field.controller":deskFieldCtrlStub});describe("DeskField API Router:",function(){it("should return an express router instance",function(){expect(deskFieldIndex).to.equal(routerStub)}),describe("GET /api/desk/fields",function(){it("should route to deskField.controller.index",function(){expect(routerStub.get.withArgs("/","deskFieldCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/desk/fields/:id",function(){it("should route to deskField.controller.show",function(){expect(routerStub.get.withArgs("/:id","deskFieldCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/desk/fields",function(){it("should route to deskField.controller.create",function(){expect(routerStub.post.withArgs("/","deskFieldCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/desk/fields/:id",function(){it("should route to deskField.controller.update",function(){expect(routerStub.put.withArgs("/:id","deskFieldCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/desk/fields/:id",function(){it("should route to deskField.controller.update",function(){expect(routerStub.patch.withArgs("/:id","deskFieldCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/desk/fields/:id",function(){it("should route to deskField.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","deskFieldCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),Event=require("../../models").Event;exports.index=function(a,b){Event.findAll({include:[{all:!0}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.showByChannel=function(a,b){Event.findAll({where:{channel:a.params.channel.toUpperCase()},include:[{all:!0}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Event.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Event.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Event.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Event.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Event=require("../../models").Event;exports.register=function(a){Event.afterCreate(function(b){Event.findOne({where:{id:b.id},include:[{all:!0}]}).then(function(b){onSave(a,b)})}),Event.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/events",function(){it("should respond with JSON array",function(a){request(app).get("/api/events").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./event.controller"),router=express.Router();router.get("/",controller.index),router.get("/channel/:channel",controller.showByChannel),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),async=require("async"),Trunk=require("../../models").Trunk,FaxAccount=require("../../models").FaxAccount,FaxApplication=require("../../models").FaxApplication,VoiceExtension=require("../../models").VoiceExtension,sequelize=require("../../models").sequelize;exports.index=function(a,b,c){var d=["description","name","phone"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),FaxAccount.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b,c){FaxAccount.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){return sequelize.transaction().then(function(c){async.waterfall([function(b){return FaxAccount.create(a.body,{transaction:c}).then(function(a){b(null,a)})["catch"](function(a){b(a)})},function(b,d){return Trunk.findById(a.body.TrunkId,{transaction:c}).then(function(a){d(null,b,a)})["catch"](function(a){d(a)})},function(b,d,e){return VoiceExtension.create({context:d.context,exten:a.body.phone,priority:1,app:"Goto",appdata:"inbound-fax,s,1",type:"inbound-fax",TrunkId:d.id},{transaction:c}).then(function(a){e(null,b)})["catch"](function(a){e(a)})}],function(a,d){return a?(console.log(a),c.rollback(),b.status(404).send(a)):(console.log("ok"),c.commit(),b.status(201).send(d))})})},exports.update=function(a,b,c){return a.body.id&&delete a.body.id,sequelize.transaction().then(function(c){async.waterfall([function(b){return FaxAccount.findOne({where:{id:a.params.id},include:[{model:Trunk}]},{transaction:c}).then(function(a){b(null,a)})["catch"](function(a){b(a)})},function(b,d){return Trunk.findById(a.body.TrunkId,{transaction:c}).then(function(a){return b.updateAttributes({TrunkId:a.id},{transaction:c}).then(function(c){d(null,b,a)})["catch"](function(a){d(a)})})["catch"](function(a){d(a)})},function(b,d,e){return VoiceExtension.findOne({where:{priority:1,exten:b.phone,context:b.Trunk.context}},{transaction:c}).then(function(f){return f||e(null,b),f.updateAttributes({exten:a.body.phone,context:d.context,TrunkId:a.body.UserId},{transaction:c}).then(function(){console.log("voiceExtension edited"),e(null,b)})["catch"](function(a){e(a)})})["catch"](function(a){e(a)})},function(b,d){var e=_.merge(b,a.body);return e.save({transaction:c}).then(function(){console.log("faxAccount saved"),d(null,b)})["catch"](function(a){d(a)})}],function(a,d){return a?(console.log(a),c.rollback(),b.status(404).send(a)):(console.log("ok"),c.commit(),b.status(201).send(d))})})},exports.destroy=function(a,b,c){FaxAccount.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){c(a)}):b.sendStatus(404)})["catch"](function(a){c(a)})},exports.addApplications=function(a,b,c){FaxApplication.findAll({where:{id:a.body.applications}}).then(function(c){var d=c;return sequelize.transaction(function(b){return FaxApplication.destroy({where:{id:a.body.applications}},{transaction:b}).then(function(){for(var c=[],e=0;eg.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){FaxBusinessAutomation.findById(a.params.id,{include:[{all:!0}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){return sequelize.transaction(function(b){return FaxBusinessAutomation.create(a.body,{transaction:b}).then(function(c){var d=[];return a.body.or&&(a.body.or.forEach(function(a){a.FaxBusinessAutomationId=c.id}),d=d.concat(a.body.or)),a.body.and&&(a.body.and.forEach(function(a){a.FaxBusinessAutomationId=c.id}),d=d.concat(a.body.and)),BusinessCondition.bulkCreate(d,{transaction:b}).then(function(d){return a.body.actions&&a.body.actions.forEach(function(a){a.FaxBusinessAutomationId=c.id}),BusinessAction.bulkCreate(a.body.actions,{transaction:b})})})}).then(function(){return b.sendStatus(201)})["catch"](function(a){return c(a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,FaxBusinessAutomation.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){FaxBusinessAutomation.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var FaxBusinessAutomation=require("../../models").FaxBusinessAutomation;exports.register=function(a){FaxBusinessAutomation.afterCreate(function(b){onSave(a,b)}),FaxBusinessAutomation.afterUpdate(function(b,c){onSave(a,b)}),FaxBusinessAutomation.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/fax/business/automations",function(){it("should respond with JSON array",function(a){request(app).get("/api/fax/business/automations").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./fax_business_automation.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),FaxMessage=require("../../models").FaxMessage,Trunk=require("../../models").User;exports.register=function(a){FaxMessage.afterCreate(function(b){console.log("FaxMessage afterCreate"),onSave(a,b)})};var _=require("lodash"),fs=require("fs"),path=require("path"),uploadFile=require("upload-file"),uuidLib=require("node-uuid"),pdf=require("html-pdf"),spindrift=require("spindrift"),formidable=require("formidable"),exec=require("child_process").exec,config=require("../../config/environment"),FaxMessage=require("../../models").FaxMessage,FaxRoom=require("../../models").FaxRoom,FaxAccount=require("../../models").FaxAccount,Contact=require("../../models").Contact;exports.index=function(a,b){FaxMessage.findAll({where:a.query,order:"createdAt DESC",include:[{model:Contact,as:"From"},{model:Contact,as:"To"},{model:FaxRoom,include:[{model:FaxAccount}]}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){FaxMessage.findOne({where:{id:a.params.id},include:[{model:Contact,as:"From"},{model:Contact,as:"To"}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){var d=new formidable.IncomingForm;if(d.uploadDir=path.join(config.root,"server/files/fax/outbound/original"),d.keepExtensions=!0,d.multiples=!1,a.body.onlyText){if(a.body.html){var e=uuidLib.v4(),f=path.join(config.root,"server/files/fax/outbound/original",e+".pdf");pdf.create(a.body.html,{timeout:3e4}).toFile(f,function(c,d){console.log("Fax: HTML Converted"),convertPdfAndSendFax(a,b,e,a.body)})}}else d.parse(a,function(c,d,e){if(c)return handleError(b,c);if(d.html){var f=uuidLib.v4(),g=path.join(config.root,"server/files/fax/outbound/original",f+".pdf");pdf.create(d.html,{timeout:3e4}).toFile(g,function(c,f){console.log("Fax: HTML Converted");var g=spindrift(f.filename),h=spindrift(e.file.path),i=uuidLib.v4();spindrift.join(g,h).pdfStream().pipe(fs.createWriteStream(path.join(config.root,"server/files/fax/outbound/original",i+".pdf"))),console.log("Fax: HTML joined with PDF uploaded"),convertPdfAndSendFax(a,b,i,d)})}else{var f=uuidLib.v4(),g=path.join(config.root,"server/files/fax/outbound/original",f+".pdf");fs.rename(e.file.path,g,function(c){return c?(console.error(c),handleError(b,a)):(console.log("Fax: PDF Rename"),void convertPdfAndSendFax(a,b,f,d))})}})},exports.update=function(a,b){a.body.id&&delete a.body.id,FaxMessage.findOne({where:{id:a.params.id},include:[{model:Contact,as:"From"},{model:Contact,as:"To"}]}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){FaxMessage.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.getContentFromMessage=function(a,b){FaxMessage.findById(a.params.id).then(function(a){if(!a)return b.sendStatus(404);if(a.filenamePDF){if("RECEIVED"===a.status){var c=path.join(config.root,"server","files","fax","inbound",a.filenamePDF);fs.exists(c,function(a){return a?b.sendFile(c):b.sendStatus(404)})}else if("NOT SENT"===a.status||"SENT"===a.directory||"FAILED"===a.status){var c=path.join(config.root,"server","files","fax","outbound","original",a.filenamePDF); +fs.exists(c,function(a){return a?b.sendFile(c):b.sendStatus(404)})}}else b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var FaxMessage=require("../../models").FaxMessage,Contact=require("../../models").Contact,FaxRoom=require("../../models").FaxRoom,FaxAccount=require("../../models").FaxAccount;exports.register=function(a){FaxMessage.afterCreate(function(b){FaxMessage.findOne({where:{id:b.id},include:[{model:Contact,as:"From"},{model:Contact,as:"To"},{model:FaxRoom,include:[{model:FaxAccount}]}]}).then(function(b){onSave(a,b)})["catch"](function(a){console.error(a)})}),FaxMessage.afterUpdate(function(b){FaxMessage.findOne({where:{id:b.id},include:[{model:Contact,as:"From"},{model:Contact,as:"To"},{model:FaxRoom,include:[{model:FaxAccount}]}]}).then(function(b){onSave(a,b),onUpdate(a,b)})["catch"](function(a){console.error(a)})}),FaxMessage.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/fax/messages",function(){it("should respond with JSON array",function(a){request(app).get("/api/fax/messages").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./fax_message.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id/content",controller.getContentFromMessage),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),FaxQueue=require("../../models").FaxQueue;exports.index=function(a,b){var c=["description","name","timeout","strategy"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d,include:[{all:!0}]};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),FaxQueue.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){FaxQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){FaxQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,FaxQueue.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){FaxQueue.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.addAgents=function(a,b,c){FaxQueue.findById(a.params.id).then(function(d){return d?void d.addUsers(a.body.agents).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)}):c(new Error("no fax queue found"))})["catch"](function(a){return c(a)})},exports.removeAgents=function(a,b,c){FaxQueue.findById(a.params.id).then(function(d){return d?void d.removeUsers(a.body.agents).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)}):c(new Error("no fax queue found"))})["catch"](function(a){return c(a)})},exports.bulkDestroy=function(a,b){FaxQueue.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var fax_queue=require("../../models").FaxQueue;exports.register=function(a){fax_queue.afterCreate(function(b){onSave(a,b)}),fax_queue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/fax/queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/fax/queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./fax_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.post("/:id/agents",controller.addAgents),router.put("/:id",controller.update),router.put("/:id/agents",controller.removeAgents),router.patch("/:id",controller.update),router["delete"]("/",controller.bulkDestroy),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),FaxRoom=require("../../models").FaxRoom,FaxAccount=require("../../models").FaxAccount,FaxMessage=require("../../models").FaxMessage,Contact=require("../../models").Contact,User=require("../../models").User;exports.index=function(a,b){FaxRoom.findAll({include:[{model:FaxMessage,attributes:["id"]},{model:FaxAccount},{model:User}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.agentIndex=function(a,b,c){FaxRoom.findAll({where:{UserId:a.params.id},include:[{model:FaxAccount},{model:User}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return c(a)})},exports.show=function(a,b){FaxRoom.findOne({where:{id:a.params.id},include:[{model:FaxMessage,include:[{model:FaxRoom,include:[{model:FaxAccount}]},{model:Contact,as:"From"},{model:Contact,as:"To"}]},{model:FaxAccount}],order:[[FaxMessage,"createdAt","DESC"]]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){FaxRoom.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){FaxRoom.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){FaxRoom.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var User=require("../../models").User,FaxRoom=require("../../models").FaxRoom,FaxQueue=require("../../models").FaxQueue,FaxAccount=require("../../models").FaxAccount,FaxApplication=require("../../models").FaxApplication;exports.register=function(a){FaxRoom.afterCreate(function(b){onSave(a,b)})};var fax_room=require("../../models").FaxRoom;exports.register=function(a){fax_room.afterCreate(function(b){onSave(a,b)}),fax_room.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/fax/rooms",function(){it("should respond with JSON array",function(a){request(app).get("/api/fax/rooms").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./fax_room.controller"),router=express.Router();router.get("/",controller.index),router.get("/agent/:id",controller.agentIndex),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),_fd=require("freshdesk"),FreshdeskAccount=require("../../models").FreshdeskAccount;exports.index=function(a,b){var c=["description","name","username","remoteUri"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),FreshdeskAccount.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){FreshdeskAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){FreshdeskAccount.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,FreshdeskAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){FreshdeskAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.bulkDestroy=function(a,b){FreshdeskAccount.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.checkAccount=function(a,b){FreshdeskAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){var c=new _fd(a.remoteUri,a.apiKey);c.listTickets(function(a,c,d){return a?b.sendStatus(400):(d=JSON.parse(d),d.require_login?b.sendStatus(400):b.sendStatus(200))})})["catch"](handleError(b))},exports.accountValidation=function(a,b){var c={};c[a.params.field]=a.body.value,FreshdeskAccount.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.getFields=function(a,b,c){FreshdeskAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){var d=new _fd(a.remoteUri,a.apiKey);d.get("/ticket_fields.json",function(a,d,e){return a?c(a):(e=JSON.parse(e),b.status(200).send(e))})})["catch"](handleError(b))};var FreshdeskAccount=require("../../models").FreshdeskAccount;exports.register=function(a){FreshdeskAccount.afterCreate(function(b){onSave(a,b)}),FreshdeskAccount.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./freshdesk_account.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/fields",auth.isAuthenticated(),controller.getFields),router.get("/:id/check",auth.isAuthenticated(),controller.checkAccount),router.post("/validate/:field",auth.isAuthenticated(),controller.accountValidation),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),freshdeskAccountCtrlStub={index:"freshdeskAccountCtrl.index",show:"freshdeskAccountCtrl.show",create:"freshdeskAccountCtrl.create",update:"freshdeskAccountCtrl.update",destroy:"freshdeskAccountCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},freshdeskAccountIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./freshdesk_account.controller":freshdeskAccountCtrlStub});describe("FreshdeskAccount API Router:",function(){it("should return an express router instance",function(){expect(freshdeskAccountIndex).to.equal(routerStub)}),describe("GET /api/freshdesk/accounts",function(){it("should route to freshdeskAccount.controller.index",function(){expect(routerStub.get.withArgs("/","freshdeskAccountCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/freshdesk/accounts/:id",function(){it("should route to freshdeskAccount.controller.show",function(){expect(routerStub.get.withArgs("/:id","freshdeskAccountCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/freshdesk/accounts",function(){it("should route to freshdeskAccount.controller.create",function(){expect(routerStub.post.withArgs("/","freshdeskAccountCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/freshdesk/accounts/:id",function(){it("should route to freshdeskAccount.controller.update",function(){expect(routerStub.put.withArgs("/:id","freshdeskAccountCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/freshdesk/accounts/:id",function(){it("should route to freshdeskAccount.controller.update",function(){expect(routerStub.patch.withArgs("/:id","freshdeskAccountCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/freshdesk/accounts/:id",function(){it("should route to freshdeskAccount.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","freshdeskAccountCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),FreshdeskConfiguration=require("../../models").FreshdeskConfiguration,FreshdeskField=require("../../models").FreshdeskField;exports.index=function(a,b){FreshdeskConfiguration.findAll({where:a.query}).then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){FreshdeskConfiguration.find({where:{id:a.params.id},include:[{model:FreshdeskField,as:"Subject"},{model:FreshdeskField,as:"Description"},{model:FreshdeskField,as:"Field"}]}).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){FreshdeskConfiguration.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,FreshdeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){FreshdeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.addConfigurationField=function(a,b,c){var d;FreshdeskConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(function(b){return d=b,FreshdeskField.create(a.body)}).then(function(b){switch(a.params.type){case"subject":return[d.addSubject(b),b];case"description":return[d.addDescription(b),b];case"field":return[d.addField(b),b]}}).spread(function(a,c){return b.status(201).json(c)})["catch"](handleError(b))};var FreshdeskConfiguration=require("../../models").FreshdeskConfiguration;exports.register=function(a){FreshdeskConfiguration.afterCreate(function(b){onSave(a,b)}),FreshdeskConfiguration.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./freshdesk_configuration.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/:id/:type",auth.isAuthenticated(),controller.addConfigurationField),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),freshdeskConfigurationCtrlStub={index:"freshdeskConfigurationCtrl.index",show:"freshdeskConfigurationCtrl.show",create:"freshdeskConfigurationCtrl.create",update:"freshdeskConfigurationCtrl.update",destroy:"freshdeskConfigurationCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},freshdeskConfigurationIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./freshdesk_configuration.controller":freshdeskConfigurationCtrlStub});describe("FreshdeskConfiguration API Router:",function(){it("should return an express router instance",function(){expect(freshdeskConfigurationIndex).to.equal(routerStub)}),describe("GET /api/freshdesk/configurations",function(){it("should route to freshdeskConfiguration.controller.index",function(){expect(routerStub.get.withArgs("/","freshdeskConfigurationCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/freshdesk/configurations/:id",function(){it("should route to freshdeskConfiguration.controller.show",function(){expect(routerStub.get.withArgs("/:id","freshdeskConfigurationCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/freshdesk/configurations",function(){it("should route to freshdeskConfiguration.controller.create",function(){expect(routerStub.post.withArgs("/","freshdeskConfigurationCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/freshdesk/configurations/:id",function(){it("should route to freshdeskConfiguration.controller.update",function(){expect(routerStub.put.withArgs("/:id","freshdeskConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/freshdesk/configurations/:id",function(){it("should route to freshdeskConfiguration.controller.update",function(){expect(routerStub.patch.withArgs("/:id","freshdeskConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/freshdesk/configurations/:id",function(){it("should route to freshdeskConfiguration.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","freshdeskConfigurationCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),FreshdeskField=require("../../models").FreshdeskField;exports.index=function(a,b){FreshdeskField.findAll().then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){FreshdeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){FreshdeskField.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,FreshdeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){FreshdeskField.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))};var FreshdeskField=require("../../models").FreshdeskField;exports.register=function(a){FreshdeskField.afterCreate(function(b){onSave(a,b)}),FreshdeskField.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./freshdesk_field.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),freshdeskFieldCtrlStub={index:"freshdeskFieldCtrl.index",show:"freshdeskFieldCtrl.show",create:"freshdeskFieldCtrl.create",update:"freshdeskFieldCtrl.update",destroy:"freshdeskFieldCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},freshdeskFieldIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./freshdesk_field.controller":freshdeskFieldCtrlStub});describe("FreshdeskField API Router:",function(){it("should return an express router instance",function(){expect(freshdeskFieldIndex).to.equal(routerStub)}),describe("GET /api/freshdesk/fields",function(){it("should route to freshdeskField.controller.index",function(){expect(routerStub.get.withArgs("/","freshdeskFieldCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/freshdesk/fields/:id",function(){it("should route to freshdeskField.controller.show",function(){expect(routerStub.get.withArgs("/:id","freshdeskFieldCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/freshdesk/fields",function(){it("should route to freshdeskField.controller.create",function(){expect(routerStub.post.withArgs("/","freshdeskFieldCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/freshdesk/fields/:id",function(){it("should route to freshdeskField.controller.update",function(){expect(routerStub.put.withArgs("/:id","freshdeskFieldCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/freshdesk/fields/:id",function(){it("should route to freshdeskField.controller.update",function(){expect(routerStub.patch.withArgs("/:id","freshdeskFieldCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/freshdesk/fields/:id",function(){it("should route to freshdeskField.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","freshdeskFieldCtrl.destroy")).to.have.been.calledOnce})})});var express=require("express"),controller=require("./integration.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),uploadFile=require("upload-file"),config=require("../../config/environment"),path=require("path"),sr=require("simple-random"),admZip=require("adm-zip"),str2json=require("string-to-json"),fs=require("fs"),Integration=require("../../models").Integration;exports.index=function(a,b){Integration.findAll({where:a.query}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Integration.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){var c=new uploadFile({dest:path.join(config.root,"server/files/integrations"),maxFileSize:5e6,maxNumberOfFiles:1,minNumberOfFiles:1,acceptFileTypes:/(\.|\/)(zip)$/i,messages:{maxNumberOfFiles:"Max number of files upload exceeded",acceptFileTypes:"Only zip format accepted",maxFileSize:"The maximum file size is 5 Mb",invalidRequest:"Invalid request"}});c.on("end",function(a,c){var d=new admZip(c.file.path),e=!1,f=d.getEntries();if(f.forEach(function(a){if("manifest.json"===a.name){var f=a.getData().toString("utf8");console.log("uploadZip",f);try{e=JSON.parse(f),console.log("uploadZip",e),Integration.create(_.merge(c.file,e)).then(function(a){return d.extractAllTo(path.join(config.root,"server/files/integrations"),!0),b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})}catch(g){console.log(g);var h={errors:[{type:"json",message:g}]};return handleError(b,h)}}}),fs.unlinkSync(c.file.path),!e){var g={errors:[{type:"manifest",message:"manifest.json not found"}]};return handleError(b,g)}}),c.on("error",function(a){var c={errors:[{type:"upload",message:a}]};return handleError(b,c)}),c.parse(a)},exports.update=function(a,b){a.body.id&&delete a.body.id,Integration.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Integration.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Integration=require("../../models").Integration;exports.register=function(a){Integration.afterCreate(function(b){onSave(a,b)}),Integration.afterUpdate(function(b){onSave(a,b)}),Integration.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/integrations",function(){it("should respond with JSON array",function(a){request(app).get("/api/integrations").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./interval.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/all",auth.isAuthenticated(),controller.getAll),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.intervalValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Interval=require("../../models").Interval,VoiceExtension=require("../../models").VoiceExtension,sequelize=require("../../models").sequelize,util=require("util");exports.index=function(a,b){var c=["name"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{IntervalId:null},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Interval.findAndCountAll(f).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.getAll=function(a,b){Interval.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Interval.findById(a.params.id,{include:[{all:!0}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.intervalValidation=function(a,b){var c={name:a.body.name,IntervalId:a.body.IntervalId?a.body.IntervalId:null};a.body.id&&(c.id={$ne:a.body.id}),console.log(a.body),Interval.findAll({where:c}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Interval.findAll({where:{name:a.body.name,IntervalId:a.body.IntervalId?a.body.IntervalId:null}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:a.body.IntervalId?"MESSAGE_EXIST_SUBINTERVAL":"MESSAGE_EXIST_INTERVAL"}):void Interval.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){Interval.findAll({where:{name:a.body.name,IntervalId:a.body.IntervalId?a.body.IntervalId:null,id:{$ne:a.body.id}}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:a.body.IntervalId?"MESSAGE_EXIST_SUBINTERVAL":"MESSAGE_EXIST_INTERVAL"}):(a.body.id&&delete a.body.id,void Interval.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);return sequelize.transaction(function(a){return d.save({transaction:a}).then(function(b){VoiceExtension.update({interval:b.name},{where:{IntervalId:b.id}},{transaction:a})})}).then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Interval.findById(a.params.id).then(function(a){return a?void a.getVoiceExtensions({where:{isApp:!0}}).then(function(c){return console.log(c.length),c.length>0?b.status(500).send({message:"MESSAGE_INTERVAL_ROUTE_ASSOCIATED"}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){var c=!1;Interval.findAll({where:{id:a.query.id},include:[{all:!0}]}).then(function(d){return d?(d.forEach(function(a){a.VoiceExtensions.length>0&&(c=!0)}),c?b.status(500).send({message:"MESSAGE_INTERVAL_ROUTE_ASSOCIATED"}):void Interval.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Interval=require("../../models").Interval;exports.register=function(a){Interval.afterCreate(function(b){onSave(a,b)}),Interval.afterDestroy(function(b){onRemove(a,b)}),Interval.afterUpdate(function(b){onUpdate(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/intervals",function(){it("should respond with JSON array",function(a){request(app).get("/api/intervals").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./mail_account.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.post("/:id/applications",controller.sortApplications),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/",controller.bulkDestroy),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),async=require("async"),sequelize=require("../../models").sequelize,MailAccount=require("../../models").MailAccount,MailApplication=require("../../models").MailApplication,MailServerIn=require("../../models").MailServerIn,MailServerOut=require("../../models").MailServerOut;exports.index=function(a,b){var c=["description","name","address"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d,include:[{all:!0}]};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),MailAccount.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null; +b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b,c){MailAccount.findById(a.params.id,{include:[{all:!0}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){return sequelize.transaction(function(b){return MailAccount.create({name:a.body.name,address:a.body.address,description:a.body.description},{transaction:b}).then(function(c){return a.body.incoming.MailAccountId=c.id,MailServerIn.create(a.body.incoming,{transaction:b}).then(function(d){return a.body.outgoing.MailAccountId=c.id,MailServerOut.create(a.body.outgoing,{transaction:b})})})}).then(function(a){return b.status(201).send(a)})["catch"](function(a){return c(a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,MailAccount.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.sortApplications=function(a,b,c){MailApplication.findAll({where:{id:a.body.applications}}).then(function(c){var d=c;async.waterfall([function(b){MailApplication.destroy({where:{id:a.body.applications}}).then(function(){console.log("destroy"),b()})["catch"](function(a){b(a)})},function(b){for(var c=[],e=0;eg.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,mailAutomations:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){MailBusinessAutomation.findById(a.params.id,{include:[{all:!0}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){return sequelize.transaction(function(b){return MailBusinessAutomation.create(a.body,{transaction:b}).then(function(c){var d=[];return a.body.or&&(a.body.or.forEach(function(a){a.MailBusinessAutomationId=c.id}),d=d.concat(a.body.or)),a.body.and&&(a.body.and.forEach(function(a){a.MailBusinessAutomationId=c.id}),d=d.concat(a.body.and)),BusinessCondition.bulkCreate(d,{transaction:b}).then(function(d){return a.body.actions&&a.body.actions.forEach(function(a){a.MailBusinessAutomationId=c.id}),BusinessAction.bulkCreate(a.body.actions,{transaction:b})})})}).then(function(){return b.sendStatus(201)})["catch"](function(a){return c(a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,MailBusinessAutomation.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){MailBusinessAutomation.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){MailBusinessAutomation.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var MailBusinessAutomation=require("../../models").MailBusinessAutomation;exports.register=function(a){MailBusinessAutomation.afterCreate(function(b,c){onSave(a,b)}),MailBusinessAutomation.afterUpdate(function(b,c){onSave(a,b)}),MailBusinessAutomation.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/mail/business/automations",function(){it("should respond with JSON array",function(a){request(app).get("/api/mail/business/automations").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./mail_message.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/",auth.isAuthenticated(),controller.update),router.put("/delete/bulk",controller.bulkDelete),router.put("/delete/:id",controller.update),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),async=require("async"),htmlToText=require("html-to-text"),formidable=require("formidable"),path=require("path"),config=require("../../config/environment"),sequelize=require("../../models").sequelize,MailMessage=require("../../models").MailMessage,MailAttachment=require("../../models").MailAttachment,MailRoom=require("../../models").MailRoom,MailAccount=require("../../models").MailAccount,Contact=require("../../models").Contact;exports.index=function(a,b,c){var d={order:"createdAt DESC",include:[{all:!0,include:[{all:!0}]}]};switch(a.user.role){case"admin":d.where=a.query;break;case"agent":}MailMessage.findAll(d).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a,c)})},exports.show=function(a,b,c){MailMessage.findById(a.params.id,{include:[{all:!0,include:[{all:!0}]}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a,c)})},exports.create=function(a,b,c){var d=new formidable.IncomingForm;return d.uploadDir=path.join(config.root,"server","files","attachments"),d.keepExtensions=!0,d.multiples=!0,_.isEmpty(a.body)?void d.parse(a,function(a,c,d){if(a)return b.status(400).send(a);var e=d.file;return c.MailAttachments=[{path:e.path,fileName:e.name,length:e.size,contentType:e.type}],sequelize.transaction().then(function(a){return MailMessage.create(c,{include:[{all:!0}],transaction:a}).then(function(c){return a.commit(),b.status(201).send(c)})["catch"](function(c){return a.rollback(),handleError(b,c)})})}):(console.log("NO ATTACHMENT"),a.body.status="SENDING",sequelize.transaction().then(function(c){return MailMessage.create(a.body,{transaction:c}).then(function(a){return c.commit(),b.status(201).send(a)})["catch"](function(a){return c.rollback(),handleError(b,a)})}))},exports.update=function(a,b,c){MailMessage.findById(a.params.id,{include:[{all:!0}]}).then(function(d){if(!d)return b.sendStatus(404);var e=_.merge(d,a.body);e.save().then(function(){return b.status(200).send(e)})["catch"](function(a){return handleError(b,a,c)})})["catch"](function(a){return handleError(b,a,c)})},exports.destroy=function(a,b,c){MailMessage.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a,c)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a,c)})},exports.bulkDelete=function(a,b,c){MailMessage.bulkUpdate({where:{id:{$in:a.body.id}},attributes:{directory:"TRASH"}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a,c)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a,c)})};var MailMessage=require("../../models").MailMessage;exports.register=function(a){MailMessage.afterCreate(function(b){onSave(a,b)}),MailMessage.afterUpdate(function(b){onSave(a,b)}),MailMessage.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./mail_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.post("/:id/agents",controller.addAgents),router.put("/:id",controller.update),router.put("/:id/agents",controller.removeAgents),router.patch("/:id",controller.update),router["delete"]("/",controller.bulkDestroy),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),MailQueue=require("../../models").MailQueue;exports.index=function(a,b){var c=["description","name","timeout","strategy"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d,include:[{all:!0}]};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),MailQueue.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){MailQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){MailQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,MailQueue.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.addAgents=function(a,b,c){return MailQueue.findById(a.params.id).then(function(b){if(b)return b.addUsers(a.body.agents,{individualHooks:!0});throw new Error("MailQueue not found")}).then(function(){return b.sendStatus(200)})["catch"](function(a){return handleError(b,a)})},exports.removeAgents=function(a,b){return MailQueue.findById(a.params.id).then(function(b){if(b)return b.removeUsers(a.body.agents,{individualHooks:!0});throw new Error("MailQueue not found")}).then(function(){return b.sendStatus(200)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){MailQueue.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){MailQueue.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var MailQueue=require("../../models").MailQueue;exports.register=function(a){MailQueue.afterCreate(function(b){onSave(a,b)}),MailQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/mail/queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/mail/queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./mail_room.controller"),router=express.Router();router.get("/",controller.index),router.get("/agent/:id",controller.agentIndex),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/",controller.bulkDestroy),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),User=require("../../models").User,Contact=require("../../models").Contact,MailRoom=require("../../models").MailRoom,MailMessage=require("../../models").MailMessage,MailAccount=require("../../models").MailAccount,MailAttachment=require("../../models").MailAttachment;exports.index=function(a,b,c){var d=["subject","from","status"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{status:{$or:["NEW","OPEN"]}},limit:e,offset:f*e,include:[{all:!0}]};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),MailRoom.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,mailRooms:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){return c(a)})},exports.agentIndex=function(a,b,c){MailRoom.findAll({where:{UserId:a.params.id},include:[{all:!0}]}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return c(a)})},exports.show=function(a,b,c){return MailRoom.findById(a.params.id,{include:[{model:MailMessage,include:[{model:MailRoom,include:[{model:MailAccount}]},{model:MailAttachment}]}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){return MailRoom.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b,c){return MailRoom.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);if(c.jobId)throw new Error("Mail already assigned");var d=_.merge(c,a.body);return d.save()}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b,c){return MailRoom.findById(a.params.id).then(function(a){return a?a.destroy():b.sendStatus(404)}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){return MailRoom.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(200)})["catch"](function(a){return handleError(b,a)})};var MailMessage=require("../../models").MailMessage,MailRoom=require("../../models").MailRoom,User=require("../../models").User;exports.register=function(a){MailRoom.afterCreate(function(b){onSave(a,b)}),MailRoom.afterDestroy(function(b){onRemove(a,b)}),MailRoom.afterUpdate(function(b){MailRoom.findById(b.id,{include:[{all:!0}]}).then(function(b){onSave(a,b)})["catch"](function(a){console.error(a)})}),MailRoom.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./mail_server_in.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),MailServerIn=require("../../models").MailServerIn;exports.index=function(a,b){MailServerIn.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){MailServerIn.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){MailServerIn.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){return a.body.id&&delete a.body.id,MailServerIn.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return console.error(a),handleError(b,a)})})["catch"](function(a){return console.error(a),handleError(b,a)})},exports.destroy=function(a,b){MailServerIn.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var MailServerIn=require("../../models").MailServerIn;exports.register=function(a){MailServerIn.afterCreate(function(b){onSave(a,b)}),MailServerIn.afterUpdate(function(b){onSave(a,b)}),MailServerIn.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./mail_server_out.controller"),router=express.Router();router.get("/",controller.index),router.get("/service",controller.indexService),router.get("/:id",controller.show),router.get("/:id/test",controller.test),router.post("/",controller.create),router.post("/test",controller.test),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),MailServerOut=require("../../models").MailServerOut,MailMessage=require("../../models").MailMessage,sequelize=require("../../models").sequelize;exports.index=function(a,b){MailServerOut.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.indexService=function(a,b){MailServerOut.findAll({where:{service:!0}}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){MailServerOut.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){MailServerOut.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,MailServerOut.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){MailServerOut.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.test=function(a,b){return sequelize.transaction().then(function(c){var d,e;return MailServerOut.findById(a.params.id).then(function(a){return a?a:(c.rollback(),b.sendStatus(404))}).then(function(a){return d=a,MailMessage.create({subject:"MailAccount "+a.username+" test..",text:"MailAccount "+a.username+" test..",from:a.username,to:a.username,status:"SENDING"},{transaction:c})}).then(function(a){return e=a,d.update({state:"CONNECTED"})}).then(function(){return c.commit(),b.status(201).send(e)})["catch"](function(a){d.update({state:"ERROR"}).then(function(){return c.rollback(),handleError(b,a)})})})};var MailServerOut=require("../../models").MailServerOut;exports.register=function(a){MailServerOut.afterCreate(function(b){console.log("MailServerOut afterCreate"),onSave(a,b)}),MailServerOut.afterDestroy(function(b){console.log("MailServerOut afterDestroy"),onRemove(a,b)})};var express=require("express"),controller=require("./mail_template.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),MailTemplate=require("../../models").MailTemplate;exports.index=function(a,b){var c=["description","name","timeout","strategy"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d,include:[{all:!0}]};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),MailTemplate.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){MailTemplate.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){MailTemplate.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,MailTemplate.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){MailTemplate.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var MailTemplate=require("../../models").MailTemplate;exports.register=function(a){MailTemplate.afterCreate(function(b){onSave(a,b)}),MailTemplate.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./metric.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.metricValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Metric=require("../../models").Metric,util=require("util");exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Metric.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.metricValidation=function(a,b){console.log(a.body),Metric.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Metric.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Metric.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){Metric.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_METRIC"}):(a.body.id&&delete a.body.id,void Metric.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Metric.findById(a.params.id).then(function(a){return a?void a.getZendeskTexts().then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_METRIC_CONFIGURATION_ASSOCIATED" +}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Metric.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Metric=require("../../models").Metric;exports.register=function(a){Metric.afterCreate(function(b){onSave(a,b)}),Metric.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/analytics/metrics",function(){it("should respond with JSON array",function(a){request(app).get("/api/analytics/metrics").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./module.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),Module=require("../../models").Module;exports.index=function(a,b){Module.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){var c=a.params.id;Module.findById(c).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Module.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){var c=a.body.id;a.body.id&&delete a.body.id,Module.findById(c).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){var c=a.params.id;Module.findById(c).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var module=require("../../models").Module;exports.register=function(a){module.afterCreate(function(b){onSave(a,b)}),module.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/modules",function(){it("should respond with JSON array",function(a){request(app).get("/api/modules").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./setting.controller"),router=express.Router();router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),crypto=require("crypto"),Setting=require("../../models").Setting;exports.index=function(a,b){Setting.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Setting.findById(a.params.id).then(function(a){return a?(a.license&&(a.license=decrypt("aes-256-ctr",util.format("%s:%s:%s",a.address,a.netmask,a.mac),a.license),a.license=JSON.parse(a.license)),b.send(a)):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Setting.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Setting.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Setting.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Setting=require("../../models").Setting;exports.register=function(a){Setting.afterCreate(function(b){onSave(a,b)}),Setting.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/settings",function(){it("should respond with JSON array",function(a){request(app).get("/api/settings").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./motionbar.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/:action/:state",auth.isAuthenticated(),controller.action),module.exports=router;var _=require("lodash"),Motionbar=require("../../models").Motionbar;exports.action=function(a,b){return a.query.userId?(a.body.UserId=parseInt(a.query.userId,10),a.body.action=a.params.action,a.body.state=a.params.state,a.body.AdminId=a.user.id,void Motionbar.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})):handleError(b,{message:"Use userId"})};var Motionbar=require("../../models").Motionbar;exports.register=function(a){Motionbar.afterCreate(function(b){onSave(a,b)}),Motionbar.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/motionbar",function(){it("should respond with JSON array",function(a){request(app).get("/api/motionbar").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./report_agent.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportAgent=require("../../models").ReportAgent;exports.index=function(a,b){ReportAgent.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportAgent.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportAgent.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportAgent.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportAgent.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportAgent=require("../../models").ReportAgent;exports.register=function(a){ReportAgent.afterCreate(function(b){onSave(a,b)}),ReportAgent.afterUpdate(function(b){onSave(a,b)}),ReportAgent.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./report_chat.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportChat=require("../../models").ReportChat,sequelize=require("../../models").sequelize;exports.index=function(a,b){ReportChat.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportChat.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportChat.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b,c){return a.body.id&&delete a.body.id,ReportChat.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);return d.save().then(function(){return b.status(200).send(c)})})["catch"](sequelize.ValidationError,function(a){return handleError(b,a)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportChat.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportChat=require("../../models").ReportChat;exports.register=function(a){ReportChat.afterCreate(function(b){onSave(a,b)}),ReportChat.afterUpdate(function(b){onUpdate(a,b)}),ReportChat.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/report/chats",function(){it("should respond with JSON array",function(a){request(app).get("/api/report/chats").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./report_chat_session.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportChatSession=require("../../models").ReportChatSession;exports.index=function(a,b){ReportChatSession.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportChatSession.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportChatSession.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportChatSession.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportChatSession.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportChatSession=require("../../models").ReportChatSession;exports.register=function(a){ReportChatSession.afterCreate(function(b){onSave(a,b)}),ReportChatSession.afterUpdate(function(b){onSave(a,b)}),ReportChatSession.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/report/chat/sessions",function(){it("should respond with JSON array",function(a){request(app).get("/api/report/chat/sessions").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./report_integration.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportIntegration=require("../../models").ReportIntegration;exports.index=function(a,b){ReportIntegration.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportIntegration.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportIntegration.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportIntegration.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportIntegration.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var util=require("util"),ReportIntegration=require("../../models").ReportIntegration;exports.register=function(a){ReportIntegration.afterCreate(function(b){onSave(a,b)})};var express=require("express"),controller=require("./report_mail.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportMail=require("../../models").ReportMail;exports.index=function(a,b){ReportMail.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportMail.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportMail.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportMail.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportMail.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportMail=require("../../models").ReportMail;exports.register=function(a){ReportMail.afterCreate(function(b){onSave(a,b)}),ReportMail.afterUpdate(function(b){onUpdate(a,b)}),ReportMail.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/report/mails",function(){it("should respond with JSON array",function(a){request(app).get("/api/report/mails").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./report_mail_session.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportMailSession=require("../../models").ReportMailSession;exports.index=function(a,b){ReportMailSession.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportMailSession.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportMailSession.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportMailSession.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportMailSession.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportMailSession=require("../../models").ReportMailSession;exports.register=function(a){ReportMailSession.afterCreate(function(b){onSave(a,b)}),ReportMailSession.afterUpdate(function(b){onSave(a,b)}),ReportMailSession.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/report/mail/sessions",function(){it("should respond with JSON array",function(a){request(app).get("/api/report/mail/sessions").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./report_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ReportQueue=require("../../models").ReportQueue;exports.index=function(a,b){ReportQueue.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ReportQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ReportQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ReportQueue.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ReportQueue.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ReportQueue=require("../../models").ReportQueue;exports.register=function(a){ReportQueue.afterCreate(function(b){onSave(a,b)}),ReportQueue.afterUpdate(function(b){onSave(a,b)}),ReportQueue.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./report_tree.controller"),router=express.Router();router.get("/",controller.index),router.put("/:id",controller.update),router.patch("/:id",controller.update),module.exports=router;var _=require("lodash"),util=require("util"),ReportTree=require("../../models").ReportTree;exports.index=function(a,b){ReportTree.findAll().then(function(a){return a?b.status(200).send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){ReportTree.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})};var ReportTree=require("../../models").ReportTree;exports.register=function(a){ReportTree.afterCreate(function(b){onSave(a,b)}),ReportTree.afterUpdate(function(b){onSave(a,b)}),ReportTree.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/analytics/reports/trees",function(){it("should respond with JSON array",function(a){request(app).get("/api/analytic/reports/trees").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./salesforce_account.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/check",auth.isAuthenticated(),controller.checkAccount),router.get("/:id/fields",auth.isAuthenticated(),controller.getFields),router.post("/validate/:field",auth.isAuthenticated(),controller.accountValidation),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),salesforceAccountCtrlStub={index:"salesforceAccountCtrl.index",show:"salesforceAccountCtrl.show",create:"salesforceAccountCtrl.create",update:"salesforceAccountCtrl.update",destroy:"salesforceAccountCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},salesforceAccountIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./salesforce_account.controller":salesforceAccountCtrlStub});describe("SalesforceAccount API Router:",function(){it("should return an express router instance",function(){expect(salesforceAccountIndex).to.equal(routerStub)}),describe("GET /api/salesforce/accounts",function(){it("should route to salesforceAccount.controller.index",function(){expect(routerStub.get.withArgs("/","salesforceAccountCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/salesforce/accounts/:id",function(){it("should route to salesforceAccount.controller.show",function(){expect(routerStub.get.withArgs("/:id","salesforceAccountCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/salesforce/accounts",function(){it("should route to salesforceAccount.controller.create",function(){expect(routerStub.post.withArgs("/","salesforceAccountCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/salesforce/accounts/:id",function(){it("should route to salesforceAccount.controller.update",function(){expect(routerStub.put.withArgs("/:id","salesforceAccountCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/salesforce/accounts/:id",function(){it("should route to salesforceAccount.controller.update",function(){expect(routerStub.patch.withArgs("/:id","salesforceAccountCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/salesforce/accounts/:id",function(){it("should route to salesforceAccount.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","salesforceAccountCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),util=require("util"),jsforce=require("jsforce"),SalesforceAccount=require("../../models").SalesforceAccount;exports.index=function(a,b){var c=["description","name","username","remoteUri"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),SalesforceAccount.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){SalesforceAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SalesforceAccount.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SalesforceAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SalesforceAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.checkAccount=function(a,b){SalesforceAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){var c=new jsforce.Connection({oauth2:{loginUrl:a.remoteUri,clientId:a.clientId,clientSecret:a.clientSecret}}),d=a.password.concat(a.securityToken);c.login(a.username,d,function(a,c){return a?b.status(400).send(a):b.sendStatus(200)})})["catch"](handleError(b))},exports.accountValidation=function(a,b){var c={};c[a.params.field]=a.body.value,SalesforceAccount.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.getFields=function(a,b){SalesforceAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){var c=new jsforce.Connection({oauth2:{loginUrl:a.remoteUri,clientId:a.clientId,clientSecret:a.clientSecret}}),d=a.password.concat(a.securityToken);c.login(a.username,d,function(a,d){return a?b.status(400).send(a):void c.sobject("Task").describe(function(a,c){return a?b.status(400).send(a):b.status(200).send(c.fields)})})})["catch"](handleError(b))},exports.bulkDestroy=function(a,b){SalesforceAccount.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var SalesforceAccount=require("../../models").SalesforceAccount;exports.register=function(a){SalesforceAccount.afterCreate(function(b){onSave(a,b)}),SalesforceAccount.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./salesforce_configuration.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/:id/:type",auth.isAuthenticated(),controller.addConfigurationField),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),salesforceConfigurationCtrlStub={index:"salesforceConfigurationCtrl.index",show:"salesforceConfigurationCtrl.show",create:"salesforceConfigurationCtrl.create",update:"salesforceConfigurationCtrl.update",destroy:"salesforceConfigurationCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},salesforceConfigurationIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./salesforce_configuration.controller":salesforceConfigurationCtrlStub});describe("SalesforceConfiguration API Router:",function(){it("should return an express router instance",function(){expect(salesforceConfigurationIndex).to.equal(routerStub)}),describe("GET /api/salesforce/configurations",function(){it("should route to salesforceConfiguration.controller.index",function(){expect(routerStub.get.withArgs("/","salesforceConfigurationCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/salesforce/configurations/:id",function(){it("should route to salesforceConfiguration.controller.show",function(){expect(routerStub.get.withArgs("/:id","salesforceConfigurationCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/salesforce/configurations",function(){it("should route to salesforceConfiguration.controller.create",function(){expect(routerStub.post.withArgs("/","salesforceConfigurationCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/salesforce/configurations/:id",function(){it("should route to salesforceConfiguration.controller.update",function(){expect(routerStub.put.withArgs("/:id","salesforceConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/salesforce/configurations/:id",function(){it("should route to salesforceConfiguration.controller.update",function(){expect(routerStub.patch.withArgs("/:id","salesforceConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/salesforce/configurations/:id",function(){it("should route to salesforceConfiguration.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","salesforceConfigurationCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),SalesforceConfiguration=require("../../models").SalesforceConfiguration,SalesforceField=require("../../models").SalesforceField;exports.index=function(a,b){SalesforceConfiguration.findAll({where:a.query}).then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){SalesforceConfiguration.find({where:{id:a.params.id},include:[{model:SalesforceField,as:"Subject"},{model:SalesforceField,as:"Description"},{model:SalesforceField,as:"Field"}]}).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SalesforceConfiguration.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SalesforceConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SalesforceConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.addConfigurationField=function(a,b,c){var d;SalesforceConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(function(b){return d=b,SalesforceField.create(a.body)}).then(function(b){switch(a.params.type){case"subject":return[d.addSubject(b),b];case"description":return[d.addDescription(b),b];case"field":return[d.addField(b),b]}}).spread(function(a,c){return b.status(201).json(c)})["catch"](handleError(b))};var SalesforceConfiguration=require("../../models").SalesforceConfiguration;exports.register=function(a){SalesforceConfiguration.afterCreate(function(b){onSave(a,b)}),SalesforceConfiguration.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./salesforce_field.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create), +router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),salesforceFieldCtrlStub={index:"salesforceFieldCtrl.index",show:"salesforceFieldCtrl.show",create:"salesforceFieldCtrl.create",update:"salesforceFieldCtrl.update",destroy:"salesforceFieldCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},salesforceFieldIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./salesforce_field.controller":salesforceFieldCtrlStub});describe("SalesforceField API Router:",function(){it("should return an express router instance",function(){expect(salesforceFieldIndex).to.equal(routerStub)}),describe("GET /api/salesforce/fields",function(){it("should route to salesforceField.controller.index",function(){expect(routerStub.get.withArgs("/","salesforceFieldCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/salesforce/fields/:id",function(){it("should route to salesforceField.controller.show",function(){expect(routerStub.get.withArgs("/:id","salesforceFieldCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/salesforce/fields",function(){it("should route to salesforceField.controller.create",function(){expect(routerStub.post.withArgs("/","salesforceFieldCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/salesforce/fields/:id",function(){it("should route to salesforceField.controller.update",function(){expect(routerStub.put.withArgs("/:id","salesforceFieldCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/salesforce/fields/:id",function(){it("should route to salesforceField.controller.update",function(){expect(routerStub.patch.withArgs("/:id","salesforceFieldCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/salesforce/fields/:id",function(){it("should route to salesforceField.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","salesforceFieldCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),SalesforceField=require("../../models").SalesforceField;exports.index=function(a,b){SalesforceField.findAll().then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){SalesforceField.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SalesforceField.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SalesforceField.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SalesforceField.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))};var SalesforceField=require("../../models").SalesforceField;exports.register=function(a){SalesforceField.afterCreate(function(b){onSave(a,b)}),SalesforceField.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./setting.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/internal",auth.isAuthenticated(),controller.getInternal),router.get("/info",controller.getInfo),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Settings=require("../../models").Settings,Agent=require("../../models").User;exports.index=function(a,b){Settings.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.getInternal=function(a,b){Settings.findOne().then(function(a){Agent.findAll({where:{internal:{$gte:a.min_internal}}}).then(function(c){var d=_.pluck(_.pluck(c,"dataValues"),"internal"),e=a.min_internal,f={value:null};do-1===d.indexOf(e)?f.value=e:e++;while(!f.value);return b.status(200).send(f)})["catch"](function(a){return console.log(a),handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.getInfo=function(a,b){var c=require("../../../package.json");return b.status(200).send({name:c.name,version:c.version,engines:c.engines})},exports.show=function(a,b){Settings.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Settings.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Settings.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Settings.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Settings=require("../../models").Settings;exports.register=function(a){Settings.afterCreate(function(b){onSave(a,b)}),Settings.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/settings",function(){it("should respond with JSON array",function(a){request(app).get("/api/settings").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./square_odbc.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.odbcValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),SquareOdbc=require("../../models").SquareOdbc,util=require("util");exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),SquareOdbc.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.odbcValidation=function(a,b){console.log(a.body),SquareOdbc.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){SquareOdbc.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){SquareOdbc.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){SquareOdbc.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_ODBC"}):(a.body.id&&delete a.body.id,void SquareOdbc.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){SquareOdbc.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){SquareOdbc.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var SquareOdbc=require("../../models").SquareOdbc;exports.register=function(a){SquareOdbc.afterCreate(function(b){onSave(a,b)}),SquareOdbc.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/square/odbc",function(){it("should respond with JSON array",function(a){request(app).get("/api/square/odbc").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./square_project.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/download",controller.download),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.projectValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),path=require("path"),config=require("../../config/environment"),util=require("util"),SquareProject=require("../../models").SquareProject;exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),SquareProject.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.projectValidation=function(a,b){console.log(a.body),SquareProject.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){SquareProject.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){SquareProject.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,SquareProject.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){SquareProject.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){SquareProject.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.download=function(a,b,c){SquareProject.findById(a.params.id).then(function(c){return c?(console.log(a.query.filename),b.set({"Content-Disposition":'attachment; filename="'+a.query.filename+'.xml"',"Content-Type":"txt/xml"}),b.send(c.preproduction)):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var SquareProject=require("../../models").SquareProject;exports.register=function(a){SquareProject.afterCreate(function(b){onSave(a,b)}),SquareProject.afterUpdate(function(b){onSave(a,b)}),SquareProject.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/square/projects",function(){it("should respond with JSON array",function(a){request(app).get("/api/square/projects").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./sugarcrm_account.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id/check",auth.isAuthenticated(),controller.checkAccount),router.get("/:id/fields",auth.isAuthenticated(),controller.getFields),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/validate/:field",auth.isAuthenticated(),controller.accountValidation),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),sugarcrmAccountCtrlStub={index:"sugarcrmAccountCtrl.index",show:"sugarcrmAccountCtrl.show",create:"sugarcrmAccountCtrl.create",update:"sugarcrmAccountCtrl.update",destroy:"sugarcrmAccountCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},sugarcrmAccountIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./sugarcrm_account.controller":sugarcrmAccountCtrlStub});describe("SugarcrmAccount API Router:",function(){it("should return an express router instance",function(){expect(sugarcrmAccountIndex).to.equal(routerStub)}),describe("GET /api/sugarcrm/accounts",function(){it("should route to sugarcrmAccount.controller.index",function(){expect(routerStub.get.withArgs("/","sugarcrmAccountCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/sugarcrm/accounts/:id",function(){it("should route to sugarcrmAccount.controller.show",function(){expect(routerStub.get.withArgs("/:id","sugarcrmAccountCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/sugarcrm/accounts",function(){it("should route to sugarcrmAccount.controller.create",function(){expect(routerStub.post.withArgs("/","sugarcrmAccountCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/sugarcrm/accounts/:id",function(){it("should route to sugarcrmAccount.controller.update",function(){expect(routerStub.put.withArgs("/:id","sugarcrmAccountCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/sugarcrm/accounts/:id",function(){it("should route to sugarcrmAccount.controller.update",function(){expect(routerStub.patch.withArgs("/:id","sugarcrmAccountCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/sugarcrm/accounts/:id",function(){it("should route to sugarcrmAccount.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","sugarcrmAccountCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),util=require("util"),SugarcrmAccount=require("../../models").SugarcrmAccount;exports.index=function(a,b){var c=["description","name","username","remoteUri"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),SugarcrmAccount.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){SugarcrmAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SugarcrmAccount.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SugarcrmAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SugarcrmAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.checkAccount=function(a,b){SugarcrmAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){var c=require("node-sugarcrm-client");c.init({apiURL:stripTrailingSlash(a.remoteUri)+"/service/v4_1/rest.php",login:a.username,passwd:a.password}),c.login(function(a){return a?b.sendStatus(200):b.sendStatus(400)})})["catch"](handleError(b))},exports.bulkDestroy=function(a,b){SugarcrmAccount.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.accountValidation=function(a,b){var c={};c[a.params.field]=a.body.value,SugarcrmAccount.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.getFields=function(a,b){var c=require("node-sugarcrm-client");SugarcrmAccount.findById(a.params.id).then(handleEntityNotFound(b)).then(function(a){c.init({apiURL:stripTrailingSlash(a.remoteUri)+"/service/v4_1/rest.php",login:a.username,passwd:a.password}),c.login(function(a){if(!a)return b.sendStatus(400);var d={session:a,module_name:"Cases"};c.call("get_module_fields",d,function(a,c){return c?b.sendStatus(400):b.status(200).send(a.module_fields)})})})["catch"](handleError(b))};var SugarcrmAccount=require("../../models").SugarcrmAccount;exports.register=function(a){SugarcrmAccount.afterCreate(function(b){onSave(a,b)}),SugarcrmAccount.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./sugarcrm_configuration.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/:id/:type",auth.isAuthenticated(),controller.addConfigurationField),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),sugarcrmConfigurationCtrlStub={index:"sugarcrmConfigurationCtrl.index",show:"sugarcrmConfigurationCtrl.show",create:"sugarcrmConfigurationCtrl.create",update:"sugarcrmConfigurationCtrl.update",destroy:"sugarcrmConfigurationCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},sugarcrmConfigurationIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./sugarcrm_configuration.controller":sugarcrmConfigurationCtrlStub});describe("SugarcrmConfiguration API Router:",function(){it("should return an express router instance",function(){expect(sugarcrmConfigurationIndex).to.equal(routerStub)}),describe("GET /api/sugarcrm/configurations",function(){it("should route to sugarcrmConfiguration.controller.index",function(){expect(routerStub.get.withArgs("/","sugarcrmConfigurationCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/sugarcrm/configurations/:id",function(){it("should route to sugarcrmConfiguration.controller.show",function(){expect(routerStub.get.withArgs("/:id","sugarcrmConfigurationCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/sugarcrm/configurations",function(){it("should route to sugarcrmConfiguration.controller.create",function(){expect(routerStub.post.withArgs("/","sugarcrmConfigurationCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/sugarcrm/configurations/:id",function(){it("should route to sugarcrmConfiguration.controller.update",function(){expect(routerStub.put.withArgs("/:id","sugarcrmConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/sugarcrm/configurations/:id",function(){it("should route to sugarcrmConfiguration.controller.update",function(){expect(routerStub.patch.withArgs("/:id","sugarcrmConfigurationCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/sugarcrm/configurations/:id",function(){it("should route to sugarcrmConfiguration.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","sugarcrmConfigurationCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),SugarcrmConfiguration=require("../../models").SugarcrmConfiguration,SugarcrmField=require("../../models").SugarcrmField;exports.index=function(a,b){SugarcrmConfiguration.findAll({where:a.query}).then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){SugarcrmConfiguration.find({where:{id:a.params.id},include:[{model:SugarcrmField,as:"Subject"},{model:SugarcrmField,as:"Description"},{model:SugarcrmField,as:"Field"}]}).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SugarcrmConfiguration.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SugarcrmConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SugarcrmConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))},exports.addConfigurationField=function(a,b,c){var d;SugarcrmConfiguration.findById(a.params.id).then(handleEntityNotFound(b)).then(function(b){return d=b,SugarcrmField.create(a.body)}).then(function(b){switch(a.params.type){case"subject":return[d.addSubject(b),b];case"description":return[d.addDescription(b),b];case"field":return[d.addField(b),b]}}).spread(function(a,c){return b.status(201).json(c)})["catch"](handleError(b))};var SugarcrmConfiguration=require("../../models").SugarcrmConfiguration;exports.register=function(a){SugarcrmConfiguration.afterCreate(function(b){onSave(a,b)}),SugarcrmConfiguration.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./sugarcrm_field.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var proxyquire=require("proxyquire").noPreserveCache(),sugarcrmFieldCtrlStub={index:"sugarcrmFieldCtrl.index",show:"sugarcrmFieldCtrl.show",create:"sugarcrmFieldCtrl.create",update:"sugarcrmFieldCtrl.update",destroy:"sugarcrmFieldCtrl.destroy"},routerStub={get:sinon.spy(),put:sinon.spy(),patch:sinon.spy(),post:sinon.spy(),"delete":sinon.spy()},sugarcrmFieldIndex=proxyquire("./index.js",{express:{Router:function(){return routerStub}},"./sugarcrm_field.controller":sugarcrmFieldCtrlStub});describe("SugarcrmField API Router:",function(){it("should return an express router instance",function(){expect(sugarcrmFieldIndex).to.equal(routerStub)}),describe("GET /api/sugarcrm/fields",function(){it("should route to sugarcrmField.controller.index",function(){expect(routerStub.get.withArgs("/","sugarcrmFieldCtrl.index")).to.have.been.calledOnce})}),describe("GET /api/sugarcrm/fields/:id",function(){it("should route to sugarcrmField.controller.show",function(){expect(routerStub.get.withArgs("/:id","sugarcrmFieldCtrl.show")).to.have.been.calledOnce})}),describe("POST /api/sugarcrm/fields",function(){it("should route to sugarcrmField.controller.create",function(){expect(routerStub.post.withArgs("/","sugarcrmFieldCtrl.create")).to.have.been.calledOnce})}),describe("PUT /api/sugarcrm/fields/:id",function(){it("should route to sugarcrmField.controller.update",function(){expect(routerStub.put.withArgs("/:id","sugarcrmFieldCtrl.update")).to.have.been.calledOnce})}),describe("PATCH /api/sugarcrm/fields/:id",function(){it("should route to sugarcrmField.controller.update",function(){expect(routerStub.patch.withArgs("/:id","sugarcrmFieldCtrl.update")).to.have.been.calledOnce})}),describe("DELETE /api/sugarcrm/fields/:id",function(){it("should route to sugarcrmField.controller.destroy",function(){expect(routerStub["delete"].withArgs("/:id","sugarcrmFieldCtrl.destroy")).to.have.been.calledOnce})})});var _=require("lodash"),SugarcrmField=require("../../models").SugarcrmField;exports.index=function(a,b){SugarcrmField.findAll().then(responseWithResult(b))["catch"](handleError(b))},exports.show=function(a,b){SugarcrmField.findById(a.params.id).then(handleEntityNotFound(b)).then(responseWithResult(b))["catch"](handleError(b))},exports.create=function(a,b){SugarcrmField.create(a.body).then(responseWithResult(b,201))["catch"](handleError(b))},exports.update=function(a,b){a.body.id&&delete a.body.id,SugarcrmField.findById(a.params.id).then(handleEntityNotFound(b)).then(saveUpdates(a.body)).then(responseWithResult(b))["catch"](handleError(b))},exports.destroy=function(a,b){SugarcrmField.findById(a.params.id).then(handleEntityNotFound(b)).then(removeEntity(b))["catch"](handleError(b))};var SugarcrmField=require("../../models").SugarcrmField;exports.register=function(a){SugarcrmField.afterCreate(function(b){onSave(a,b)}),SugarcrmField.afterDestroy(function(b){onRemove(a,b)})};var express=require("express"),controller=require("./tag.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.tagValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Tag=require("../../models").Tag,util=require("util");exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),console.log(f),Tag.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.tagValidation=function(a,b){console.log(a.body),Tag.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Tag.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Tag.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){Tag.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_TAG"}):(a.body.id&&delete a.body.id,void Tag.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Tag.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Tag.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Tag=require("../../models").Tag;exports.register=function(a){Tag.afterCreate(function(b){onSave(a,b)}),Tag.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/tags",function(){it("should respond with JSON array",function(a){request(app).get("/api/tags").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./team.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/:id/agents",auth.isAuthenticated(),controller.addAgents),router.put("/:id",auth.isAuthenticated(),controller.update),router.put("/:id/agents",auth.isAuthenticated(),controller.removeAgents),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy), +router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),Team=require("../../models").Team,User=require("../../models").User;exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},include:[{all:!0}],limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Team.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Team.find({where:{id:a.params.id},include:[User]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Team.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Team.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Team.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Team.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.addAgents=function(a,b,c){Team.findById(a.params.id).then(function(d){return d?void d.addUsers(a.body.agents).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)}):c(new Error("no mail queue found"))})["catch"](function(a){return c(a)})},exports.removeAgents=function(a,b,c){Team.findById(a.params.id).then(function(d){return d?void d.removeUsers(a.body.agents).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)}):c(new Error("no mail queue found"))})["catch"](function(a){return c(a)})};var team=require("../../models").Team;exports.register=function(a){team.afterCreate(function(b){onSave(a,b)}),team.afterUpdate(function(b){onSave(a,b)}),team.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/teams",function(){it("should respond with JSON array",function(a){request(app).get("/api/teams").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./thing.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),Thing=require("../../models").Thing;exports.index=function(a,b,c){Thing.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return c(a)})},exports.show=function(a,b,c){Thing.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){Thing.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return c(a)})},exports.update=function(a,b,c){a.body.id&&delete a.body.id,Thing.findById(a.params.id).then(function(d){if(!d)return b.sendStatus(404);var e=_.merge(d,a.body);e.save().then(function(){return b.status(200).send(d)})["catch"](function(a){return c(a)})})["catch"](function(a){return c(a)})},exports.destroy=function(a,b,c){Thing.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return c(a)}):b.sendStatus(404)})["catch"](function(a){return c(a)})};var thing=require("../../models").Thing;exports.register=function(a){thing.afterCreate(function(b){onSave(a,b)}),thing.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/things",function(){it("should respond with JSON array",function(a){request(app).get("/api/things").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./trigger.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),module.exports=router;var _=require("lodash"),util=require("util"),Trigger=require("../../models").Trigger;exports.index=function(a,b,c){var d=["fullname","name","email"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),Trigger.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Trigger.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Trigger.create(a.body).then(function(){return b.sendStatus(201)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b,c){a.body.id&&delete a.body.id,Trigger.findById(a.params.id).then(function(d){if(!d)return b.sendStatus(404);var e=_.merge(d,a.body);e.save().then(function(){return b.status(200).send(d)})["catch"](function(a){return c(a)})})["catch"](function(a){return c(a)})},exports.destroy=function(a,b){Trigger.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Trigger.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Trigger=require("../../models").Trigger;exports.register=function(a){Trigger.afterCreate(function(b){onSave(a,b)}),Trigger.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/triggers",function(){it("should respond with JSON array",function(a){request(app).get("/api/triggers").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./trunk.controller"),config=require("../../config/environment"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.trunkValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var fs=require("fs"),util=require("util"),path=require("path"),config=require("../../config/environment"),Trunk=require("../../models").Trunk,VoiceExtension=require("../../models").VoiceExtension,_=require("lodash");exports.register=function(a){Trunk.afterCreate(function(b){console.log("create trunk hook"),trunksRewrite(b,a,!1)}),Trunk.afterUpdate(function(b){console.log("update trunk hook"),trunksRewrite(b,a,!1)}),Trunk.afterDelete(function(b){console.log("delete trunk hook"),trunksRewrite(b,a,!0)})};var _=require("lodash"),Trunk=require("../../models").Trunk,VoiceExtension=require("../../models").VoiceExtension,sequelize=require("../../models").sequelize,util=require("util");exports.index=function(a,b,c){var d=["name","host","context","description"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;case"role":g.where.role={$or:b.split(/[\s,]+/)};break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),Trunk.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){Trunk.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.trunkValidation=function(a,b){console.log(a.body),Trunk.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Trunk.findAll({where:{name:a.body.name}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_TRUNK"}):void Trunk.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return console.log("trunks create",a),handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return console.log("trunks findall",a),handleError(b,a)})},exports.update=function(a,b,c){Trunk.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(d){return console.log(d),console.log("Finding app row....."),d?d.length>0?b.status(500).send({message:"MESSAGE_EXIST_TRUNK"}):(a.body.id&&delete a.body.id,void Trunk.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);return sequelize.transaction(function(a){return d.save({transaction:a}).then(function(c){return VoiceExtension.update({trunk:c.name,cutdigits:sequelize.literal("cutdigits")},{where:{TrunkId:c.id}},{transaction:a}).then(function(){return b.status(200).send(c)})})})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return c(a)})):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.destroy=function(a,b){Trunk.findById(a.params.id).then(function(a){return a?void a.getVoiceExtensions({where:{isApp:!0}}).then(function(c){return console.log(c.length),c.length>0?b.status(500).send({message:"MESSAGE_TRUNK_OUTBOUND_ASSOCIATED"}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){var c=!1;Trunk.findAll({where:{id:a.query.id},include:[{all:!0}]}).then(function(d){return d?(d.forEach(function(a){a.VoiceExtensions.length>0&&(c=!0)}),c?b.status(500).send({message:"MESSAGE_TRUNK_OUTBOUND_ASSOCIATED"}):void Trunk.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var Trunk=require("../../models").User;exports.register=function(a){Trunk.afterCreate(function(b){onSave(a,b)}),Trunk.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/trunks",function(){it("should respond with JSON array",function(a){request(app).get("/api/trunks").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./update.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),auth.hasRole("admin"),controller.index),router.get("/pull",auth.isAuthenticated(),auth.hasRole("admin"),controller.pull),module.exports=router;var _=require("lodash"),path=require("path"),config=require("../../config/environment"),Git=require("simple-git")(path.join(config.root)),Update=require("../../models").Update;exports.index=function(a,b){Git.log(function(a,c){if(a)return handleError(b,a);var d=[];return c.all.forEach(function(a){a.version=a.message.split("|")[1]||"unknown",a.message=a.message.split("|")[0],d.push(a)}),console.log("Latest available tag: %s",c.latest),b.status(200).send(d)})},exports.show=function(a,b){},exports.pull=function(a,b){Git.pull(function(a,c){return a?handleError(b,a):b.status(201).send(c)})},exports.checkout=function(a,b){console.log("req.params.hash",a.params.hash),Git.checkout(a.params.hash,function(a,c){return a?handleError(b,a):b.status(201).send(c)})},exports.create=function(a,b){Update.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,Update.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Update.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},module.exports=function(a,b){var c=a.define("Update",{name:b.STRING,description:b.STRING});return c};var Update=require("../../models").Update;exports.register=function(a){Update.afterCreate(function(b){onSave(a,b)}),Update.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/updates",function(){it("should respond with JSON array",function(a){request(app).get("/api/updates").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./upload.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.download),router.get("/stream/:id",auth.isAuthenticated(),controller.stream),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Upload=require("../../models").Upload,uploadFile=require("upload-file"),sr=require("simple-random"),sox=require("sox"),path=require("path"),config=require("../../config/environment"),fs=require("fs"),sequelize=require("../../models").sequelize,base64=require("file-base64");exports.index=function(a,b){Upload.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Upload.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){var c=new uploadFile({dest:path.join(config.root,"server/files/sounds/original"),maxFileSize:15e6,maxNumberOfFiles:10,minNumberOfFiles:0,acceptFileTypes:/(\.|\/)(wav|mp3|gsm|ogg)$/i,rename:function(a,b){return sr()+path.extname(b.filename)},messages:{maxNumberOfFiles:"Max number of files upload exceeded",acceptFileTypes:"Only wav,mp3,gsm, and ogg format accepted",maxFileSize:"The maximum file size is 15 Mb",invalidRequest:"Invalid request"}});c.on("end",function(a,d){var e={};if(e.description="",e.name=e.display_name=path.basename(d.file.path,path.extname(d.file.filename)),e.save_name=path.basename(d.file.filename,path.extname(d.file.filename)),e.original_path=path.join(config.root,"server/files/sounds/original"),e.converted_path=path.join(config.root,"server/files/sounds/converted"),!d.file.filename)return b.status(500).send(c);var f=path.join(e.original_path,e.save_name+path.extname(d.file.filename)),g=path.join(e.converted_path,e.save_name+".wav");fs.chmodSync(f,parseInt("0777",8)),sox.identify(f,function(a,b){console.log("************** Result: ",b)});var h=sox.transcode(f,g,{sampleRate:8e3,format:"wav",channelCount:1,bitRate:196608,compressionQuality:5});h.on("error",function(a){return console.error("********** Conversion Error: ",a),fs.unlink(f,function(a){return a?handleError(b,a):void 0}),handleError(b,a)}),h.on("progress",function(a,b){}),h.on("src",function(a){e.original_format=a.format,e.original_duration=1e3*a.duration,e.original_sampleCount=a.sampleCount,e.original_channelCount=a.channelCount,e.original_bitRate=a.bitRate,e.original_sampleRate=a.sampleRate}),h.on("dest",function(a){e.converted_format=a.format,e.converted_duration=1e3*a.duration,e.converted_sampleCount=a.sampleCount,e.converted_channelCount=a.channelCount,e.converted_bitRate=a.bitRate,e.converted_sampleRate=a.sampleRate}),h.on("end",function(){console.log("Conversion completed"),fs.chmodSync(g,parseInt("0777",8)),Upload.create(e).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})}),h.start()}),c.on("error",function(a){return console.log("********** Upload error :",a),handleError(b,a)}),c.parse(a)},exports.update=function(a,b){a.body.id&&delete a.body.id,Upload.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Upload.findById(a.params.id).then(function(a){if(!a)return b.status(404).send(a);var c=path.join(a.original_path,a.save_name+"."+a.original_format),d=path.join(a.converted_path,a.save_name+"."+a.converted_format);return sequelize.transaction(function(e){return a.destroy({transaction:e}).then(function(){return fs.unlink(c,function(a){return a?handleError(b,a):void 0}),fs.unlink(d,function(a){return a?handleError(b,a):void 0}),b.status(200).send(a)})})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.download=function(a,b){Upload.findById(a.params.id).then(function(a){if(!a)return b.status(404).send(a);var c=path.join(a.original_path,a.save_name+"."+a.original_format);b.status(200).download(c,a.display_name+"."+a.original_format,function(a){return a?handleError(b,a):void console.log("Sent file under :",c)})})},exports.stream=function(a,b){Upload.findById(a.params.id).then(function(a){if(!a)return b.status(404).send(a);var c=path.join(a.converted_path,a.save_name+"."+a.original_format);base64.encode(c,function(a,c){return a?(console.log(a),handleError(b,a)):b.status(200).send(c)})})};var upload=require("../../models").Upload;exports.register=function(a){upload.afterCreate(function(b){onSave(a,b)}),upload.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/uploads",function(){it("should respond with JSON array",function(a){request(app).get("/api/uploads").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user.controller"),config=require("../../config/environment"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/me",auth.isAuthenticated(),controller.me),router.get("/all",auth.isAuthenticated(),controller.getUsers),router.get("/avatar/:filename",controller.getAvatar),router.get("/:id/avatar",controller.getAvatarById),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/validate/:field",auth.isAuthenticated(),controller.userValidation),router.put("/password",auth.isAuthenticated(),controller.changePassword),router.put("/password/:id/reset",auth.isAuthenticated(),auth.hasRole("admin"),controller.resetPassword),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/",auth.isAuthenticated(),controller.create),router.post("/:id/avatar",auth.isAuthenticated(),controller.changeAvatar),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var fs=require("fs"),util=require("util"),path=require("path"),config=require("../../config/environment"),User=require("../../models").User,VoiceExtension=require("../../models").VoiceExtension;exports.register=function(a){User.afterCreate(function(a){createCallerID(a),createExtension(a)}),User.afterUpdate(function(a){updateExtension(a),createCallerID(a)})};var User=require("../../models").User,Module=require("../../models").Module,config=require("../../config/environment"),passport=require("passport"),moment=require("moment"),jwt=require("jsonwebtoken"),uploadFile=require("upload-file"),path=require("path"),fs=require("fs"),_=require("lodash"),util=require("util"),sr=require("simple-random");exports.index=function(a,b,c){var d=["fullname","name","email","role"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{role:["admin","user"]},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),User.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.getUsers=function(a,b,c){User.findAll({where:{role:{$in:["admin","user","agent"]}}}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){var d=a.body;_(d).keys().sort().each(function(a){d[a]});var e=User.build(a.body);e.provider="local",e.save().then(function(a){var c=jwt.sign({id:a.id},config.session.secret,{expiresIn:"5h"});return b.status(201).send({token:c})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b,c){User.findById(a.params.id).then(function(a){return a?b.status(200).send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b,c){var d=a.params.id;User.findById(d).then(function(a){return a?void a.destroy().then(function(a){return console.log(a),b.status(200).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.changePassword=function(a,b,c){var d=a.user.id,e=String(a.body.oldPassword),f=String(a.body.newPassword);User.findById(d).then(function(a){return a.authenticate(e)?(a.password=f,void a.save().then(function(){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})):b.sendStatus(403)})},exports.resetPassword=function(a,b,c){var d=String(a.body.newPassword);User.findById(a.params.id).then(function(a){a.password=d,a.save().then(function(){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})})},exports.changeAvatar=function(a,b,c){var d=new uploadFile({dest:path.join(config.root,"server","files","images"),maxNumberOfFiles:1,minNumberOfFiles:0,acceptFileTypes:/(\.|\/)(jpeg|png)$/i,rename:function(a,b){return sr()+path.extname(b.filename)},messages:{maxNumberOfFiles:"Max number of files upload exceeded",acceptFileTypes:"Only jpeg and png format accepted",invalidRequest:"Invalid request"}});d.on("end",function(c,e){return e.file.filename?void User.findById(a.params.id).then(function(a){return a?void a.updateAttributes({userpic:e.file.filename}).then(function(){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)}):b.status(500).send(d)}),d.on("error",function(a){return console.log("********** Upload error :",a),handleError(b,a)}),d.parse(a)},exports.me=function(a,b,c){var d=a.user.id;User.findOne({where:{id:d},attributes:["id","fullname","name","email","role","userpic"],include:[Module]}).then(function(a){return a?b.status(200).send(a):b.sendStatus(401)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b,c){a.user.id;User.findById(a.params.id).then(function(c){return c?void c.updateAttributes(a.body).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(500)})["catch"](function(a){return handleError(b,a)})},exports.getAvatar=function(a,b,c){var d=path.join(config.root,"server","files","images",a.params.filename);fs.exists(d,function(a){return a||(d=path.join(config.root,"server","files","images","userpic.png")),b.sendFile(d)})},exports.getAvatarById=function(a,b,c){if("visitor"===a.params.id){var d=path.join(config.root,"server","files","images","customer.png");fs.exists(d,function(a){return b.sendFile(d)})}else User.findById(a.params.id).then(function(a){if(!a)return b.sendStatus(500);var c=path.join(config.root,"server","files","images",a.userpic?a.userpic:"userpic.png");fs.exists(c,function(a){return a||(c=path.join(config.root,"server","files","images","userpic.png")),b.sendFile(c)})})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){User.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.userValidation=function(a,b){var c={};c[a.params.field]=a.body.value,User.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.authCallback=function(a,b,c){b.redirect("/")};var user=require("../../models").User;exports.register=function(a){user.afterCreate(function(b){onSave(a,b)}),user.afterUpdate(function(b){onSave(a,b)}),user.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/users",function(){it("should respond with JSON array",function(a){request(app).get("/api/users").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_chat_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),UserHasChatQueue=require("../../models").UserHasChatQueue;exports.index=function(a,b){UserHasChatQueue.findAll(a.query?{where:a.query}:{}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasChatQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){UserHasChatQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,UserHasChatQueue.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasChatQueue.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var UserHasChatQueue=require("../../models").UserHasChatQueue;exports.register=function(a){UserHasChatQueue.afterCreate(function(b){onSave(a,b)}),UserHasChatQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/user_has_queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/user_has_queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_chat_room.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.put("/:userId/:chatRoomId",controller.update),module.exports=router;var _=require("lodash"),UserHasChatRoom=require("../../models").UserHasChatRoom;exports.index=function(a,b){UserHasChatRoom.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasChatRoom.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){UserHasChatRoom.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){UserHasChatRoom.findOne({ +where:{UserId:a.params.userId,ChatRoomId:a.params.chatRoomId},limit:1}).then(function(c){return c?void c.updateAttributes(a.body).then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasChatRoom.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var _=require("lodash"),User=require("../../models").User,ChatRoom=require("../../models").ChatRoom,ChatVisitor=require("../../models").ChatVisitor,UserHasChatRoom=require("../../models").UserHasChatRoom;exports.register=function(a){UserHasChatRoom.afterCreate(function(b){onSave(a,b)}),UserHasChatRoom.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/chat/user_has_rooms",function(){it("should respond with JSON array",function(a){request(app).get("/api/chat/user_has_rooms").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_fax_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),UserHasFaxQueue=require("../../models").UserHasFaxQueue;exports.index=function(a,b){UserHasFaxQueue.findAll(a.query?{where:a.query}:{}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasFaxQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){UserHasFaxQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,UserHasFaxQueue.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasFaxQueue.find({where:{id:a.params.id}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var UserHasFaxQueue=require("../../models").UserHasFaxQueue;exports.register=function(a){UserHasFaxQueue.afterCreate(function(b){onSave(a,b)}),UserHasFaxQueue.afterDestroy(function(b){onRemove(a,b)}),UserHasFaxQueue.afterBulkCreate(function(b){onSave(a,b)}),UserHasFaxQueue.afterBulkDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/fax/user_has_queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/fax/user_has_queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_mail_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),UserHasMailQueue=require("../../models").UserHasMailQueue;exports.index=function(a,b){UserHasMailQueue.findAll(a.query?{where:a.query}:{}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasMailQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){UserHasMailQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,UserHasMailQueue.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasMailQueue.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var UserHasMailQueue=require("../../models").UserHasMailQueue;exports.register=function(a){UserHasMailQueue.afterCreate(function(b){onSave(a,b)}),UserHasMailQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/mail/user_has_queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/mail/user_has_queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_team.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),UserHasTeam=require("../../models").UserHasTeam;exports.index=function(a,b){console.log(a.query),UserHasTeam.findAll({where:a.query}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasTeam.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){UserHasTeam.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,UserHasTeam.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasTeam.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},module["export"]=function(a,b){var c=a.define("UserHasTeam",{name:String,info:String,active:Boolean});return c};var user_has_team=require("../../models").UserHasTeam;exports.register=function(a){user_has_team.afterCreate(function(b){onSave(a,b)}),user_has_team.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/user_has_teams",function(){it("should respond with JSON array",function(a){request(app).get("/api/user_has_teams").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./user_has_voice_queue.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),UserHasVoiceQueue=require("../../models").UserHasVoiceQueue;exports.index=function(a,b){UserHasVoiceQueue.findAll(a.query?{where:a.query}:{}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){UserHasVoiceQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b,c){console.log(a.body),UserHasVoiceQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return console.error(a),c(a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,UserHasVoiceQueue.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){UserHasVoiceQueue.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var UserHasVoiceQueue=require("../../models").UserHasVoiceQueue;exports.register=function(a){UserHasVoiceQueue.afterCreate(function(b){onSave(a,b)}),UserHasVoiceQueue.afterUpdate(function(b){onSave(a,b)}),UserHasVoiceQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/user_has_queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/user_has_queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./variable.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.variableValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),Variable=require("../../models").Variable,util=require("util");exports.index=function(a,b){var c=["name","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),Variable.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.variableValidation=function(a,b){console.log(a.body),Variable.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){Variable.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){Variable.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){Variable.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_VARIABLE"}):(a.body.id&&delete a.body.id,void Variable.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){Variable.findById(a.params.id).then(function(a){return a?void a.getZendeskTexts().then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_VARIABLE_CONFIGURATION_ASSOCIATED"}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){Variable.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var Variable=require("../../models").Variable;exports.register=function(a){Variable.afterCreate(function(b){onSave(a,b)}),Variable.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/variables",function(){it("should respond with JSON array",function(a){request(app).get("/api/variables").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./voice_context.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.contextValidation),router.put("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var VoiceContext=require("../../models").VoiceContext,fs=require("fs"),util=require("util"),path=require("path"),config=require("../../config/environment");exports.register=function(a){VoiceContext.beforeUpdate(function(a){if(a.changed("name"))throw new Error("You can't modify a context name");if(a.defaultEntry)throw new Error("You can't modify a default context")}),VoiceContext.beforeDelete(function(a){if(a.defaultEntry)throw new Error("You can't delete a default context")}),VoiceContext.afterCreate(function(b){rewriteContexts(b,a,!1)}),VoiceContext.afterDelete(function(b){rewriteContexts(b,a,!0)})};var _=require("lodash"),util=require("util"),sequelize=require("../../models").sequelize,VoiceContext=require("../../models").VoiceContext,VoiceExtension=require("../../models").VoiceExtension;exports.index=function(a,b,c){var d=["name","description"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),VoiceContext.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){VoiceContext.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.contextValidation=function(a,b){console.log(a.body),VoiceContext.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){VoiceContext.findAll({where:{name:a.body.name}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_ROUTE"}):void VoiceContext.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){VoiceContext.findAll({where:{name:a.body.name,id:{$ne:a.body.id}}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_ROUTE"}):(a.body.id&&delete a.body.id,void VoiceContext.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){VoiceContext.findById(a.params.id).then(function(a){return a?void VoiceExtension.findAll({where:{context:a.name}}).then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_CONTEXT_ROUTE_ASSOCIATED"}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){VoiceContext.findAll({where:{id:a.query.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.pluck(_.pluck(c,"dataValues"),"name");VoiceExtension.findAll({where:{context:d}}).then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_CONTEXT_ROUTE_ASSOCIATED"}):void VoiceContext.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})};var VoiceContext=require("../../models").VoiceContext;exports.register=function(a){VoiceContext.afterCreate(function(b){onSave(a,b)}),VoiceContext.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/contexts",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/contexts").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./voice_extension.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/route/:route",auth.isAuthenticated(),controller.showByRoute),router.get("/applications/:id",auth.isAuthenticated(),controller.show),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/applications",auth.isAuthenticated(),controller.showAppsByRoute),router.post("/validate",auth.isAuthenticated(),controller.routeValidation),router.post("/:id/applications",auth.isAuthenticated(),controller.sortAppsByRoute),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.put("/applications/:id",auth.isAuthenticated(),controller.updateApplication),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),VoiceExtension=require("../../models").VoiceExtension,Interval=require("../../models").Interval,sequelize=require("../../models").sequelize,Sequelize=require("../../models").Sequelize,util=require("util"),async=require("async"),Applications={Dial:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Set",appdata:"CALLERID(all)=%s"},{app:"Dial",appdata:"%s,%s,%s,%s",isApp:!0},{app:"NoOp",appdata:"Dial Application End"}],Queue:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Answer",appdata:""},{app:"Queue",appdata:"%s,%s,%s,%s,%s",isApp:!0},{app:"NoOp",appdata:"Queue Application End"}],Playback:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Playback",appdata:"%s,%s",isApp:!0},{app:"NoOp",appdata:"Playback Application End"}],AGI:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"AGI",appdata:"agi://127.0.0.1/square,%s",isApp:!0},{app:"NoOp",appdata:"AGI Application End"}],Goto:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Goto",appdata:"%s,%s,%d",isApp:!0},{app:"NoOp",appdata:"Goto Application End"}],Hangup:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Hangup",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Hangup Application End"}],Set:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Set",appdata:"%s=%s",isApp:!0},{app:"NoOp",appdata:"Set Application End"}],custom:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}],Voicemail:[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"Voicemail",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Voicemail Application End"}]};exports.index=function(a,b){VoiceExtension.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.showByRoute=function(a,b,c){var d=["context","exten","app","appdata"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{type:a.params.route,VoiceExtensionId:null,app:"NoOp"},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),VoiceExtension.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){b.status(500).send({error:"Something blew up!"})})},exports.showAppsByRoute=function(a,b){VoiceExtension.findAll({where:{VoiceExtensionId:a.params.id,isApp:!0}}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){VoiceExtension.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.routeValidation=function(a,b){VoiceExtension.findAll({where:{exten:a.body.exten,context:a.body.context,type:a.body.type}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.sortAppsByRoute=function(a,b,c){VoiceExtension.findAll({where:{appGroup:a.body.applications}}).then(function(c){return sequelize.transaction(function(b){return VoiceExtension.destroy({where:{appGroup:a.body.applications}},{transaction:b}).then(function(){var d=2;a.body.applications.forEach(function(a){var e=_.find(c,function(b){return b.appGroup==a&&b.isApp===!0}),f=_.cloneDeep(Applications[e.app]);(!f||e.customApp)&&(f=[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}]);var g=util.format("%s%s%s",e.context,e.exten,d);e.interval=e.interval||"*,*,*,*";var h=_.filter(c,function(b){return b.appGroup==a&&b.isInterval===!0});return h&&h.length>0&&(f.splice(0,1),h.forEach(function(a,b){f.unshift({app:"GotoIfTimeFromArray",appdata:a.interval+"?%s,${EXTEN},%s:%s,${EXTEN},%s",IntervalId:a.IntervalId?a.IntervalId:null,isInterval:!0,interval:a.interval})})),d=appCreate(f,e,g,h||[],d),VoiceExtension.bulkCreate(f,{transaction:b})})})}).then(function(){return b.sendStatus(201)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){var d,e;VoiceExtension.max("priority",{where:{VoiceExtensionId:a.body.VoiceExtensionId}}).then(function(c){a.body.VoiceExtensionId?(a.body.priority=c?++c:2,d=_.cloneDeep(Applications[a.body.app]),(!d||a.body.customApp)&&(d=[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}]),e=util.format("%s%s%s",a.body.context,a.body.exten,a.body.priority),a.body.interval=a.body.interval||"*,*,*,*",Interval.findAll({where:{IntervalId:null!==a.body.IntervalId?a.body.IntervalId:0}}).then(function(c){var f,g=[];c&&c.length>0&&(d.splice(0,1),g=_.pluck(c,"dataValues"),g.forEach(function(a,b){d.unshift({app:"GotoIfTimeFromArray",appdata:a.interval+"?%s,${EXTEN},%s:%s,${EXTEN},%s",IntervalId:a.id,interval:f,isInterval:!0})})),appCreate(d,a.body,e,g),VoiceExtension.bulkCreate(d).then(function(a){return b.status(201).send(a)})})):VoiceExtension.findAll({where:{exten:a.body.exten,context:a.body.context,type:a.body.type}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_ROUTE"}):(a.body.priority=1,void VoiceExtension.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return b.handleError(a)})):b.sendStatus(404)})["catch"](function(a){return b.handleError(a)})})["catch"](function(a){c(a)})},exports.update=function(a,b){VoiceExtension.findAll({where:{exten:a.body.exten,context:a.body.context,type:a.body.type,VoiceExtensionId:null,id:{$ne:a.body.id}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_ROUTE"}):(a.body.id&&delete a.body.id,void VoiceExtension.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);return sequelize.transaction(function(a){return d.save({transaction:a}).then(function(c){VoiceExtension.findAll({where:{VoiceExtensionId:c.id,isApp:!0}}).then(function(d){return VoiceExtension.destroy({where:{VoiceExtensionId:c.id}},{transaction:a}).then(function(){d=_.pluck(d,"dataValues");var a=_.filter(_.pluck(d,"IntervalId"),function(a){return null!==a});Interval.findAll({where:{IntervalId:a}}).then(function(a){var e=2;return d.forEach(function(b){var d=_.cloneDeep(Applications[b.app]);(!d||b.customApp)&&(d=[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}]),b.context=c.context,b.exten=c.exten;var f=util.format("%s%s%s",b.context,b.exten,e);b.interval=b.interval||"*,*,*,*";var g=_.filter(a,function(a){return a.IntervalId==b.IntervalId});g&&g.length>0&&(d.splice(0,1),g.forEach(function(a,b){d.unshift({app:"GotoIfTimeFromArray",appdata:a.interval+"?%s,${EXTEN},%s:%s,${EXTEN},%s",IntervalId:a.id,isInterval:!0,interval:a.interval})})),e=appCreate(d,b,f,g||[],e),VoiceExtension.bulkCreate(d)}),b.sendStatus(200)})})})})})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.updateApplication=function(a,b,c){a.body.appdata&&delete a.body.appdata,VoiceExtension.findById(a.body.id).then(function(c){if(!c)return b.sendStatus(404);delete a.body.id;var d=_.merge(c,a.body);return sequelize.transaction(function(b){return d.save({transaction:b}).then(function(){VoiceExtension.findAll({where:{VoiceExtensionId:a.body.VoiceExtensionId,isApp:!0}}).then(function(c){return VoiceExtension.destroy({where:{VoiceExtensionId:a.body.VoiceExtensionId}},{transaction:b}).then(function(){c=_.pluck(c,"dataValues");var a=_.filter(_.pluck(c,"IntervalId"),function(a){return null!==a});return Interval.findAll({where:{IntervalId:a}}).then(function(a){var b=2;c.forEach(function(c){var d=_.cloneDeep(Applications[c.app]);(!d||c.customApp)&&(d=[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}]);var e=util.format("%s%s%s",c.context,c.exten,b);c.interval=c.interval||"*,*,*,*";var f=_.filter(a,function(a){return a.IntervalId==c.IntervalId});f&&f.length>0&&(d.splice(0,1),f.forEach(function(a,b){d.unshift({app:"GotoIfTimeFromArray",appdata:a.interval+"?%s,${EXTEN},%s:%s,${EXTEN},%s",IntervalId:a.id,isInterval:!0,interval:a.interval})})),b=appCreate(d,c,e,f||[],b),VoiceExtension.bulkCreate(d)})})})})})}).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b,c){VoiceExtension.findById(a.params.id).then(function(a){return a?void(a.VoiceExtensionId?VoiceExtension.findAll({where:{appGroup:a.appGroup}}).then(function(d){return d?sequelize.transaction(function(b){return VoiceExtension.destroy({where:{appGroup:a.appGroup}},{transaction:b}).then(function(c){return VoiceExtension.findAll({where:{VoiceExtensionId:a.VoiceExtensionId,priority:{$gt:d[c-1].priority}}}).then(function(e){return VoiceExtension.destroy({where:{VoiceExtensionId:a.VoiceExtensionId,priority:{$gt:d[c-1].priority}}},{transaction:b}).then(function(){var a=_.pluck(_.filter(_.pluck(e,"dataValues"),function(a){return a.isApp===!0}),"appGroup"),b=d[0].priority;a.forEach(function(a){var c=_.find(e,function(b){return b.appGroup==a&&b.isApp===!0}),d=_.cloneDeep(Applications[c.app]);(!d||c.customApp)&&(d=[{app:"GotoIfTime",appdata:"%s?%s,${EXTEN},%s:%s,${EXTEN},%s"},{app:"custom",appdata:"%s",isApp:!0},{app:"NoOp",appdata:"Custom Application End"}]);var f=util.format("%s%s%s",c.context,c.exten,b);c.interval=c.interval||"*,*,*,*";var g=_.filter(e,function(b){return b.appGroup==a&&b.isInterval===!0});g&&g.length>0&&(d.splice(0,1),g.forEach(function(a,b){d.unshift({app:"GotoIfTimeFromArray",appdata:a.interval+"?%s,${EXTEN},%s:%s,${EXTEN},%s",IntervalId:a.IntervalId?a.IntervalId:null,isInterval:!0,interval:a.interval})})),b=appCreate(d,c,f,g||[],b),VoiceExtension.bulkCreate(d)})})})})}).then(function(a){b.sendStatus(201)})["catch"](function(a){return c(a)}):b.sendStatus(404)}):a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.bulkDestroy=function(a,b){VoiceExtension.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})};var VoiceExtension=require("../../models").VoiceExtension;exports.register=function(a){VoiceExtension.afterCreate(function(b){onSave(a,b)}),VoiceExtension.afterDestroy(function(b){onRemove(a,b)}),VoiceExtension.afterBulkDestroy(function(b){onRemove(a,b)}),VoiceExtension.afterUpdate(function(b){onUpdate(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/extensions",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/extensions").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./voice_musiconhold.controller"),auth=require("../../auth/auth.service"),router=express.Router(); +router.get("/",auth.isAuthenticated(),controller.index),router.get("/stream",auth.isAuthenticated(),controller.stream),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/files",auth.isAuthenticated(),controller.showAudioFiles),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.mohValidation),router.post("/:id/files",auth.isAuthenticated(),controller.addFiles),router.put("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),router["delete"]("/:id/files",auth.isAuthenticated(),controller.deleteAudioFiles),module.exports=router;var _=require("lodash"),VoiceMusicOnHold=require("../../models").VoiceMusicOnHold,fs=require("fs"),remove=require("remove"),Upload=require("../../models").Upload,fse=require("fs-extra"),path=require("path"),config=require("../../config/environment"),sequelize=require("../../models").sequelize,util=require("util"),base64=require("file-base64");exports.index=function(a,b){var c=["name","directory","sort","description"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),VoiceMusicOnHold.findAndCountAll(f).then(function(c){var g=Math.ceil(c.count/d),h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){VoiceMusicOnHold.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.addFiles=function(a,b){VoiceMusicOnHold.findById(a.params.id).then(function(c){return c?void Upload.findAll({where:{id:a.body.sounds}}).then(function(a){var d,e=path.join(config.root,"server/files/sounds/converted");a.forEach(function(a){d=path.join(c.directory,a.display_name+".wav"),fse.copySync(path.join(e,a.save_name+".wav"),d)}),fs.chmodSync(d,parseInt("0777",8)),b.sendStatus(200)})["catch"](function(a){return console.log(a),handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.showAudioFiles=function(a,b){var c={};VoiceMusicOnHold.findById(a.params.id).then(function(a){return a?(c.path=a.directory,void fs.readdir(a.directory,function(a,d){if(a)throw a;return c.files=d,b.send(c)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.deleteAudioFiles=function(a,b){VoiceMusicOnHold.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=c.directory+"/"+a.query.filename;fs.unlink(d,function(a){return a?handleError(b,a):b.sendStatus(204)})})["catch"](function(a){return handleError(b,a)})},exports.mohValidation=function(a,b){console.log(a.body),VoiceMusicOnHold.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){VoiceMusicOnHold.findAll({where:{name:a.body.name}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?(console.log("error2"),b.status(500).send({message:"MESSAGE_EXIST_MOH"})):sequelize.transaction().then(function(c){return a.body.directory=path.join(config.root,"server/files/moh",a.body.name),VoiceMusicOnHold.create(a.body,{transaction:c}).then(function(a){fs.mkdir(a.directory,parseInt("0777",8),function(d){return d?(console.log("file error"),c.rollback(),b.status(500).send({message:"MESSAGE_FILE_CREATION_ERROR"})):(c.commit(),console.log("file created"),b.status(201).send(a))})})["catch"](function(a){c.rollback()})}):(console.log("error1"),b.sendStatus(404))})["catch"](function(a){return console.log("thaterror"),handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,VoiceMusicOnHold.find({where:{id:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){VoiceMusicOnHold.findById(a.params.id).then(function(a){return a?sequelize.transaction(function(c){return a.destroy({transaction:c}).then(function(){remove(a.directory,function(a){return a&&console.log(a),b.sendStatus(204)})})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){VoiceMusicOnHold.findAll({where:{id:a.query.id}}).then(function(a){return a?sequelize.transaction(function(b){a.forEach(function(a){return a.destroy({transaction:b}).then(function(){remove(a.directory,function(a){if(a)throw console.log(a),a})})})}).then(function(){return b.sendStatus(204)})["catch"](function(a){return console.log(a),handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.stream=function(a,b){var c=decodeURI(a.query.path);base64.encode(c,function(a,c){return a?handleError(b,a):b.status(200).send(c)})};var VoiceMusicOnHold=require("../../models").VoiceMusicOnHold;exports.register=function(a){VoiceMusicOnHold.afterCreate(function(b){onSave(a,b)}),VoiceMusicOnHold.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/musiconhold",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/musiconhold").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./voice_queue.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.queueValidation),router.post("/:id/agents",auth.isAuthenticated(),controller.addAgents),router.put("/:id",auth.isAuthenticated(),controller.update),router.put("/:id/agents",auth.isAuthenticated(),controller.removeAgents),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:name",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),User=require("../../models").User,VoiceQueue=require("../../models").VoiceQueue,UserHasVoiceQueue=require("../../models").UserHasVoiceQueue,VoiceExtension=require("../../models").VoiceExtension;exports.index=function(a,b){var c=["name","description","strategy","context"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),VoiceQueue.findAndCountAll(f).then(function(c){console.log(c.count);var g=Math.ceil(c.count/d);console.log(g);var h=g>f.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e+1):null,i=e>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,e-1):null;b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:g})})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){VoiceQueue.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.queueValidation=function(a,b){console.log(a.body),VoiceQueue.findAll({where:{name:a.body.name}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){VoiceQueue.findAll({where:{name:a.body.name}}).then(function(c){return console.log(c),console.log("Finding app row....."),c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_QUEUE"}):void VoiceQueue.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){VoiceQueue.find({where:{name:a.params.id}}).then(function(c){if(!c)return b.sendStatus(404);a.body.id&&delete a.body.id;var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.addAgents=function(a,b,c){console.log("addAgents"),console.log(a.params),console.log(a.body),User.findAll({where:{id:{$in:a.body.agents}}}).then(function(d){var e=_.map(d,function(b){return{queue:a.params.id,location:util.format("SIP/%s",b.name),membername:b.name,VoiceQueueName:a.params.id,UserId:b.id}});UserHasVoiceQueue.bulkCreate(e,{individualHooks:!0}).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)})})["catch"](function(a){return c(a)})},exports.removeAgents=function(a,b,c){VoiceQueue.find({where:{name:a.params.id}}).then(function(d){return d?void d.removeUsers(a.body.agents,{individualHooks:!0}).then(function(){return b.sendStatus(200)})["catch"](function(a){return c(a)}):c(new Error("no mail queue found"))})["catch"](function(a){return c(a)})},exports.destroy=function(a,b){VoiceQueue.find({where:{name:a.params.name}}).then(function(a){return a?void VoiceExtension.findAll({where:{queue:a.name}}).then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_QUEUE_ROUTE_ASSOCIATED"}):void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){VoiceQueue.findAll({where:{name:a.query.name}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.pluck(_.pluck(c,"dataValues"),"name");console.log(d),VoiceExtension.findAll({where:{queue:d}}).then(function(c){return c.length>0?b.status(500).send({message:"MESSAGE_QUEUE_ROUTE_ASSOCIATED"}):void VoiceQueue.destroy({where:{name:a.query.name},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})};var VoiceQueue=require("../../models").VoiceQueue;exports.register=function(a){VoiceQueue.afterCreate(function(b){onSave(a,b)}),VoiceQueue.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/queues",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/queues").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./voice_voicemail.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:uniqueid",auth.isAuthenticated(),controller.show),router.get("/:uniqueid/messages",auth.isAuthenticated(),controller.getMessages),router.get("/messages/:id/download",auth.isAuthenticated(),controller.downloadMessage),router.post("/",auth.isAuthenticated(),controller.create),router.post("/validate",auth.isAuthenticated(),controller.mailboxValidation),router.put("/:uniqueid",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:uniqueid",auth.isAuthenticated(),controller.destroy),router["delete"]("/messages/:id/delete",auth.isAuthenticated(),controller.destroyMessage),module.exports=router;var _=require("lodash"),VoiceVoicemail=require("../../models").VoiceVoicemail,VoiceVoicemailMessages=require("../../models").VoiceVoicemailMessages,stream=require("stream");exports.index=function(a,b){var c=["mailbox","fullname","email"],d=a.query.per_page?parseInt(a.query.per_page,10):100,e=a.query.page?parseInt(a.query.page,10):0,f={where:{},limit:d,offset:e*d};_.forIn(a.query,function(b,d){switch(d){case"per_page":case"page":break;case"sort_by":f.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":f.where.$or=[],c.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},f.where.$or.push(c)});break;default:f.where[d]={$like:{}},f.where[d].$like="%"+b+"%"}}),VoiceVoicemail.findAndCountAll(f).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){VoiceVoicemail.find({where:{uniqueid:a.params.uniqueid}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.getMessages=function(a,b){VoiceVoicemail.find({where:{uniqueid:a.params.uniqueid}}).then(function(a){VoiceVoicemailMessages.findAll({where:{mailboxcontext:a.context,mailboxuser:a.mailbox}}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.mailboxValidation=function(a,b){console.log(a.body),VoiceVoicemail.findAll({where:{mailbox:a.body.mailbox,context:a.body.context}}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){VoiceVoicemail.findAll({where:{mailbox:a.body.mailbox,context:a.body.context}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_MAILBOX"}):void VoiceVoicemail.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})},exports.update=function(a,b){VoiceVoicemail.findAll({where:{mailbox:a.body.mailbox,context:a.body.context,uniqueid:{$ne:a.body.uniqueid}}}).then(function(c){return c?c.length>0?b.status(500).send({message:"MESSAGE_EXIST_MAILBOX"}):(a.body.uniqueid&&delete a.body.uniqueid,void VoiceVoicemail.find({where:{uniqueid:a.params.uniqueid}}).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){VoiceVoicemail.find({where:{uniqueid:a.params.uniqueid}}).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){VoiceVoicemail.destroy({where:{uniqueid:a.query.uniqueid},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.destroyMessage=function(a,b){VoiceVoicemailMessages.findById(a.params.id).then(function(a){return a?(a.recording=null,void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.downloadMessage=function(a,b){VoiceVoicemailMessages.findById(a.params.id).then(function(a){if(!a)return b.sendStatus(404);var c=a.msg_id+".wav";b.writeHead(200,"OK",{"Content-Type":"audio/wav","Content-Disposition":"attachment; filename="+c,"Content-Length":a.recording.length});var d=new stream.PassThrough;d.end(new Buffer(a.recording)),d.pipe(b)})};var VoiceVoicemail=require("../../models").VoiceVoicemail,VoiceVoicemailMessages=require("../../models").VoiceVoicemailMessages;exports.register=function(a){VoiceVoicemail.afterCreate(function(b){onSave(a,b)}),VoiceVoicemail.afterDestroy(function(b){onRemove(a,b)}),VoiceVoicemailMessages.afterCreate(function(b){onMessageSave(a,b)}),VoiceVoicemailMessages.afterDestroy(function(b){onMessageRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/voice/voicemails",function(){it("should respond with JSON array",function(a){request(app).get("/api/voice/voicemails").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./widget.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/counter",auth.isAuthenticated(),controller.counter),router.get("/table",auth.isAuthenticated(),controller.table),router.get("/piechart",auth.isAuthenticated(),controller.piechart),module.exports=router;var _=require("lodash"),util=require("util"),sequelize=require("../../models").sequelize;exports.counter=function(a,b){var c=util.format("report_%s",a.query.table);sequelize.query(util.format("SELECT COUNT(*) as count FROM %s WHERE %s",c,decodeURIComponent(a.query.condition)),{type:sequelize.QueryTypes.SELECT}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.table=function(a,b){var c,d=[];a.query.fields&&(_.isArray(a.query.fields)?a.query.fields.forEach(function(a){c=JSON.parse(a),c.column&&c.alias&&d.push(c.column+' AS "'+c.alias+'"')}):_.isString(a.query.fields)&&(c=JSON.parse(a.query.fields),c.column&&c.alias&&d.push(c.column+' AS "'+c.alias+'"')));var e=d.length?d.join(","):"*",f=util.format("report_%s",a.query.table);sequelize.query(util.format("SELECT %s FROM %s WHERE %s",e,f,decodeURIComponent(a.query.condition)),{type:sequelize.QueryTypes.SELECT}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.piechart=function(a,b){var c,d=[];a.query.fields&&(_.isArray(a.query.fields)?a.query.fields.forEach(function(a){c=JSON.parse(a),c.column&&c.alias&&d.push(c.column+' AS "'+c.alias+'"')}):_.isString(a.query.fields)&&(c=JSON.parse(a.query.fields),c.column&&c.alias&&d.push(c.column+' AS "'+c.alias+'"')));var e=d.length?d.join(","):"*",f=util.format("report_%s",a.query.table),g="";"undefined"!==a.query.condition&&(g=util.format(" WHERE %s",decodeURIComponent(a.query.condition))),sequelize.query(util.format("SELECT %s FROM %s%s",e,f,g),{type:sequelize.QueryTypes.SELECT}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/widgets",function(){it("should respond with JSON array",function(a){request(app).get("/api/widgets").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./xchatty.controller"),router=express.Router();router.get("/jquery",controller.getJqueryLibrary),router.get("/assets",controller.getAssets),router.get("/status",controller.getStatus),router.get("/signup",controller.signupCustomer),router.get("/read",controller.getMessages),router.get("/send",controller.sendMessage),router.get("/signout",controller.signoutCustomer),router.get("/submit",controller.submitEnquiry),router.get("/unserved",controller.unservedCustomer),router.get("/transcript",controller.getTranscript),router.get("/:id",controller.getJsByWebsite),router.get("/:id/styles",controller.getStyleByWebsite),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),jsmin=require("jsmin").jsmin,Mustache=require("mustache"),querystring=require("querystring"),path=require("path"),md5=require("md5"),fs=require("fs"),UAParser=require("ua-parser-js"),uaParser=new UAParser,languageParser=require("accept-language-parser"),ipaddr=require("ipaddr.js"),geoip=require("geoip-lite"),csv=require("to-csv"),config=require("../../config/environment"),Xchatty=require("../../models").Xchatty,xchatty_components=require("../../components/xchatty"),sequelize=require("../../models").sequelize,User=require("../../models").User,Agent=require("../../models").User,ChatRoom=require("../../models").ChatRoom,ChatEnquiry=require("../../models").ChatEnquiry,ChatVisitor=require("../../models").ChatVisitor,ChatMessage=require("../../models").ChatMessage,ChatWebsite=require("../../models").ChatWebsite,id=1;exports.index=function(a,b,c){Xchatty.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return c(a)})},exports.getJsByWebsite=function(a,b,c){ChatWebsite.findById(a.params.id).then(function(c){b.set({"Content-Type":"application/javascript","x-timestamp":Date.now(),"x-sent":!0});var d=xchatty_components.js,e={websiteId:a.params.id,rootPath:c.remote+"/api/xchatty/",signedUp:"false",windowFocused:"false",isNewPage:"true",processingSignUp:"false",processingEnquiry:"false",processingSend:"false",visitorFullname:'""',visitorAvatar:'""',timerReadMessages:"null",timerCheckStatus:"null",isOnline:"false",decayHistory:0,defaultTimeout:2e3,isInitialStatusCheck:"true",muteNotificationSoundOnce:"false",assetBase:c.remote+"/api/xchatty/assets",hasSessionSaf:"false",restoreOpenState:"false",animateHover:c.animation,hideEmail:"false",hideOffline:"false",headerShape:c.header_shape,HeaderOnline:c.header_online,OnlineMessage:c.online_message,UsernamePlaceholder:c.username_placeholder,EmailPlaceholder:c.email_placeholder,StartChatButtonText:c.start_chat_button,OfflineMessage:c.offline_message,HeaderOffline:c.header_offline,EnquiryMessagePlaceholder:c.enquiry_message_placeholder,EnquiryButtonText:c.enquiry_button,downloadTranscript:c.download_transcript};d=Mustache.render(d,e),b.send(d)})["catch"](function(a){c(a)})},exports.getStyleByWebsite=function(a,b,c){ChatWebsite.findById(a.params.id).then(function(a){b.set({"Content-Type":"text/css","x-timestamp":Date.now(),"x-sent":!0});var c=xchatty_components.css,d={assetBase:a.remote+"/api/xchatty/assets",color:a.color,colorFocus:a.color_focus,colorButton:a.color_button};c=Mustache.render(c,d),b.send(c)})["catch"](function(a){c(a)})},exports.getAssets=function(a,b,c){b.download(path.join(config.root,"server","components","xchatty","assets",a.query.resource))},exports.getJqueryLibrary=function(a,b,c){b.set({"Content-Type":"application/javascript","x-timestamp":Date.now(),"x-sent":!0});var d=xchatty_components.jquery;b.send(d)},exports.getStatus=function(a,b,c){a.session.xchatty||(a.session.xchatty={}),Agent.findAll({where:{online:!0,role:"agent"}}).then(function(d){a.query.data.open_state&&(a.session.xchatty.open_state="true"===a.query.data.open_state?!0:!1);var e={success:!0,has_session_saf:null,signed_up:a.session.xchatty.room_id?!0:!1,fullname:a.session.xchatty.fullname?a.session.xchatty.fullname:"",email:a.session.xchatty.email?a.session.xchatty.email:"",avatar:a.session.xchatty.avatar?a.session.xchatty.avatar:"",open_state:a.session.xchatty.open_state||a.session.xchatty.room_id?!0:!1,online:d.length?!0:!1};a.session.xchatty.visitor_id?ChatRoom.findOne({where:{ChatVisitorId:a.session.xchatty.visitor_id,status:"open"}}).then(function(c){c?(a.session.xchatty.room_id=c.id,e.signed_up=!0):e.signed_up=!1,b.send(a.query.callback+"("+JSON.stringify(e)+")")})["catch"](function(a){c(a)}):b.send(a.query.callback+"("+JSON.stringify(e)+")")})["catch"](function(a){return c(a)})},exports.getMessages=function(a,b,c){var d={where:{ChatRoomId:a.session.xchatty.room_id},include:[{model:User,attributes:["id","name","fullname","email"]},{model:ChatVisitor,attributes:["id","fullname","email"]}]};"false"===a.query.data.is_new_page&&(d.where.id={$gt:a.query.data.last_id}),ChatMessage.findAll(d).then(function(c){var d={success:!0,messages:c,composing:!1,composing_fullname:null};b.send(a.query.callback+"("+JSON.stringify(d)+")")})["catch"](function(a){c(a)})},exports.sendMessage=function(a,b,c){var d={where:{ChatRoomId:a.session.xchatty.room_id},include:[{model:User,attributes:["id","name","fullname","email"]},{model:ChatVisitor,attributes:["id","fullname","email"]}]};"false"===a.query.data.is_new_page&&(d.where.id={$gt:a.query.data.last_id}),ChatMessage.create({body:a.query.data.Message.message,ChatRoomId:a.session.xchatty.room_id,ChatVisitorId:a.session.xchatty.visitor_id}).then(function(e){console.log("chatMessage",e),ChatMessage.findAll(d).then(function(c){var d={errors:[],success:!0,data:e,messages:c};b.send(a.query.callback+"("+JSON.stringify(d)+")")})["catch"](function(a){c(a)})})["catch"](function(a){c(a)})},exports.signoutCustomer=function(a,b,c){ChatRoom.findById(a.session.xchatty.room_id).then(function(d){d.updateAttributes({status:"close"}).then(function(c){var d={success:!0,errors:[]};a.session.xchatty={},b.send(a.query.callback+"("+JSON.stringify(d)+")")})["catch"](function(a){c(a)})})["catch"](function(a){c(a)})},exports.signupCustomer=function(a,b,c){var d=uaParser.setUA(a.headers["user-agent"]).getResult(),e=(languageParser.parse(a.headers["accept-language"]),{fullname:a.query.data.Discussion.fullname,email:a.query.data.Discussion.email,remote_address:a.connection.remoteAddress,user_agent:a.headers["user-agent"],visitor_language:a.headers["accept-language"],referer:a.headers.referer,origin:"webchat",browser:d.browser.name+" "+d.browser.version,engine:d.engine.name+" "+d.engine.version,os:d.os.name+" "+d.os.version,device:d.device.model?d.device.model+" "+d.device.vendor+" "+d.device.type:null,ChatWebsiteId:a.query.chatWebsiteId});return ChatVisitor.create(e).then(function(c){a.session.xchatty.fullname=c.fullname,a.session.xchatty.email=c.email,a.session.xchatty.avatar=md5(c.email).toString("base64"),a.session.xchatty.visitor_id=c.id;var d={success:!0,errors:[],chatVisitor:{id:c.id}};b.send(a.query.callback+"("+JSON.stringify(d)+")")})["catch"](function(a){c(a)})},exports.unservedCustomer=function(a,b,c){ChatVisitor.findById(a.session.xchatty.visitor_id).then(function(d){d.updateAttributes({status:"unserved"}).then(function(){var c={success:!0,errors:[]};a.session.xchatty={},b.send(a.query.callback+"("+JSON.stringify(c)+")")})["catch"](function(a){c(a)})})["catch"](function(a){c(a)})},exports.submitEnquiry=function(a,b,c){var d;return sequelize.transaction().then(function(e){return ChatVisitor.create({fullname:a.query.data.Enquiry.fullname,email:a.query.data.Enquiry.email,remote_address:a.connection.remoteAddress,user_agent:a.headers["user-agent"],visitor_language:a.headers["accept-language"],referer:a.headers.referer,origin:"enquiry",status:"unserved"},{transaction:e}).then(function(b){return d=b,ChatEnquiry.create({username:a.query.data.Enquiry.fullname,email:b.email,text:a.query.data.Enquiry.message,ChatWebsiteId:parseInt(a.query.data.Enquiry.websiteId,10),ChatVisitorId:d.id},{include:[{all:!0,include:[{all:!0}]}],transaction:e})}).then(function(){e.commit(),b.send(a.query.callback+"("+JSON.stringify({success:!0,errors:[]})+")")})["catch"](function(a){e.rollback(),c(a)})})},exports.getTranscript=function(a,b,c){a.session.xchatty&&a.session.xchatty.visitor_id&&a.session.xchatty.room_id&&ChatMessage.findAll({where:{ChatRoomId:a.session.xchatty.room_id}}).then(function(a){var c=[];a.forEach(function(a){var b={name:a.fullname,text:a.body.replace(/(\r\n|\n|\r)/gm," "),date:a.createdAt};c.push(b)});var d=csv(c);b.type("text/csv").status(200).send(d)})["catch"](function(a){c(a)})},exports.show=function(a,b,c){Xchatty.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return c(a)})},exports.create=function(a,b,c){Xchatty.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return c(a)})},exports.update=function(a,b,c){a.body.id&&delete a.body.id,Xchatty.findById(a.params.id).then(function(d){if(!d)return b.sendStatus(404);var e=_.merge(d,a.body);e.save().then(function(){return b.status(200).send(d)})["catch"](function(a){return c(a)})})["catch"](function(a){return c(a)})},exports.destroy=function(a,b,c){Xchatty.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return c(a)}):b.sendStatus(404)})["catch"](function(a){return c(a)})};var xchatty=require("../../models").Xchatty;exports.register=function(a){};var express=require("express"),auth=require("../../auth/auth.service"),controller=require("./zendesk_account.controller"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.get("/:id/check",auth.isAuthenticated(),controller.checkAccount),router.get("/:id/fields",auth.isAuthenticated(),controller.getFields),router.post("/validate/:field",auth.isAuthenticated(),controller.zendeskValidation),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/",auth.isAuthenticated(),controller.bulkDestroy),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),util=require("util"),zendesk=require("node-zendesk"),ZendeskAccount=require("../../models").ZendeskAccount;exports.index=function(a,b,c){var d=["description","name","username","remoteUri"],e=a.query.per_page?parseInt(a.query.per_page,10):100,f=a.query.page?parseInt(a.query.page,10):0,g={where:{},limit:e,offset:f*e};_.forIn(a.query,function(b,c){switch(c){case"per_page":case"page":break;case"sort_by":g.order=util.format("%s %s",a.query.sort_by,a.query.sort_order||"ASC")||null;break;case"sort_order":break;case"$":g.where.$or=[],d.forEach(function(a){var c={};c[a]={$like:"%"+b+"%"},g.where.$or.push(c)});break;default:g.where[c]={$like:{}},g.where[c].$like="%"+b+"%"}}),ZendeskAccount.findAndCountAll(g).then(function(c){var d=Math.ceil(c.count/e),h=d>g.offset+1?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f+1):null,i=f>0?util.format("%s://%s%s?page=%d",a.protocol,a.headers.host,a.baseUrl,f-1):null;return b.status(200).send({count:c.count,rows:c.rows,next_page:h,previous_page:i,total_pages:d})})["catch"](function(a){return b.status(500).send({error:"Something blew up!"})})},exports.show=function(a,b){ZendeskAccount.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ZendeskAccount.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ZendeskAccount.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ZendeskAccount.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.bulkDestroy=function(a,b){ +ZendeskAccount.destroy({where:{id:a.query.id},individualHooks:!0}).then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)})},exports.zendeskValidation=function(a,b){var c={};c[a.params.field]=a.body.value,ZendeskAccount.findAndCountAll({where:c}).then(function(c){return c.count?b.status(200).send({isValid:!1,value:a.body.value}):b.status(200).send({isValid:!0,value:a.body.value})})["catch"](function(a){return handleError(b,a)})},exports.checkAccount=function(a,b){ZendeskAccount.findById(a.params.id).then(function(a){var c={username:a.username,remoteUri:a.remoteUri};"password"===a.authType?c.password=a.password:"token"===a.authType&&(c.token=a.token);var d=zendesk.createClient(c);d.users.auth(function(a,c,d){return a?(console.error(a),handleError(b,a)):d.verified?b.sendStatus(204):b.sendStatus(500)})})["catch"](function(a){return handleError(b,a)})},exports.getFields=function(a,b){ZendeskAccount.findById(a.params.id).then(function(a){var c={username:a.username,remoteUri:a.remoteUri};"password"===a.authType?c.password=a.password:"token"===a.authType&&(c.token=a.token);var d=zendesk.createClient(c);d.ticketfields.list(function(a,c,d){return a?(console.error(a),handleError(b,a)):b.status(200).send(d)})})["catch"](function(a){return handleError(b,a)})};var ZendeskAccount=require("../../models").ZendeskAccount;exports.register=function(a){ZendeskAccount.afterCreate(function(b){onSave(a,b)}),ZendeskAccount.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/zendesk/accounts",function(){it("should respond with JSON array",function(a){request(app).get("/api/zendesk/accounts").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./zendesk_configuration.controller"),auth=require("../../auth/auth.service"),router=express.Router();router.get("/",auth.isAuthenticated(),controller.index),router.get("/:id",auth.isAuthenticated(),controller.show),router.post("/",auth.isAuthenticated(),controller.create),router.put("/:id",auth.isAuthenticated(),controller.update),router.post("/:id/:type",auth.isAuthenticated(),controller.addConfigurationField),router.patch("/:id",auth.isAuthenticated(),controller.update),router["delete"]("/:id",auth.isAuthenticated(),controller.destroy),module.exports=router;var _=require("lodash"),ZendeskConfiguration=require("../../models").ZendeskConfiguration,ZendeskField=require("../../models").ZendeskField;exports.index=function(a,b){ZendeskConfiguration.findAll({where:a.query}).then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ZendeskConfiguration.find({where:{id:a.params.id},include:[{model:ZendeskField,as:"Subject"},{model:ZendeskField,as:"Description"},{model:ZendeskField,as:"Field"}]}).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ZendeskConfiguration.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ZendeskConfiguration.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ZendeskConfiguration.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.addConfigurationField=function(a,b,c){ZendeskConfiguration.findById(a.params.id).then(function(c){return c?void ZendeskField.create(a.body).then(function(d){switch(a.params.type){case"subject":c.addSubject(d).then(function(){return b.status(200).send(d)})["catch"](function(a){return handleError(b,a)});break;case"description":c.addDescription(d).then(function(){return b.status(200).send(d)})["catch"](function(a){return handleError(b,a)});break;case"field":c.addField(d).then(function(){return b.status(200).send(d)})["catch"](function(a){return handleError(b,a)})}})["catch"](function(a){return handleError(b,a)}):b.sendStatus(500)})["catch"](function(a){return handleError(b,a)})};var ZendeskConfiguration=require("../../models").ZendeskConfiguration;exports.register=function(a){ZendeskConfiguration.afterCreate(function(b){onSave(a,b)}),ZendeskConfiguration.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/zendesk/configurations",function(){it("should respond with JSON array",function(a){request(app).get("/api/zendesk/configurations").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})});var express=require("express"),controller=require("./zendesk_field.controller"),router=express.Router();router.get("/",controller.index),router.get("/:id",controller.show),router.post("/",controller.create),router.put("/:id",controller.update),router.patch("/:id",controller.update),router["delete"]("/:id",controller.destroy),module.exports=router;var _=require("lodash"),ZendeskField=require("../../models").ZendeskField;exports.index=function(a,b){ZendeskField.findAll().then(function(a){return b.status(200).send(a)})["catch"](function(a){return handleError(b,a)})},exports.show=function(a,b){ZendeskField.findById(a.params.id).then(function(a){return a?b.send(a):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})},exports.create=function(a,b){ZendeskField.create(a.body).then(function(a){return b.status(201).send(a)})["catch"](function(a){return handleError(b,a)})},exports.update=function(a,b){a.body.id&&delete a.body.id,ZendeskField.findById(a.params.id).then(function(c){if(!c)return b.sendStatus(404);var d=_.merge(c,a.body);d.save().then(function(){return b.status(200).send(c)})["catch"](function(a){return handleError(b,a)})})["catch"](function(a){return handleError(b,a)})},exports.destroy=function(a,b){ZendeskField.findById(a.params.id).then(function(a){return a?void a.destroy().then(function(){return b.sendStatus(204)})["catch"](function(a){return handleError(b,a)}):b.sendStatus(404)})["catch"](function(a){return handleError(b,a)})};var ZendeskField=require("../../models").ZendeskField;exports.register=function(a){ZendeskField.afterCreate(function(b){onSave(a,b)}),ZendeskField.afterDestroy(function(b){onRemove(a,b)})};var should=require("should"),app=require("../../app"),request=require("supertest");describe("GET /api/zendesk/fields",function(){it("should respond with JSON array",function(a){request(app).get("/api/zendesk/fields").expect(200).expect("Content-Type",/json/).end(function(b,c){return b?a(b):(c.body.should.be["instanceof"](Array),void a())})})}); \ No newline at end of file