107c57a5900d613a487bacb5e0990e0740cb3fba
[motion2.git] / server / api / intFreshsalesConfiguration / intFreshsalesConfiguration.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x4ccc=['fieldName','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','VIRTUAL','merge','includeAll','include','rows','catch','show','params','keys','options','find','create','body','destroy','getSubjects','findOne','nolimit','getDescriptions','FreshsalesField','getFields','rimraf','fast-json-patch','request-promise','bluebird','mustache','ejs','fs-extra','lodash','squel','to-csv','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','json','undefined','limit','count','offset','Content-Range','apply','reject','update','then','end','stack','name','index','map','FreshsalesConfiguration','rawAttributes'];(function(_0x3fd0bb,_0x42f4c8){var _0x2e02b2=function(_0x3f7ef0){while(--_0x3f7ef0){_0x3fd0bb['push'](_0x3fd0bb['shift']());}};_0x2e02b2(++_0x42f4c8);}(_0x4ccc,0x18a));var _0xc4cc=function(_0x264440,_0x20ab6e){_0x264440=_0x264440-0x0;var _0x1892e7=_0x4ccc[_0x264440];return _0x1892e7;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xc4cc('0x0'));var zipdir=require('zip-dir');var jsonpatch=require(_0xc4cc('0x1'));var rp=require(_0xc4cc('0x2'));var moment=require('moment');var BPromise=require(_0xc4cc('0x3'));var Mustache=require(_0xc4cc('0x4'));var util=require('util');var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xc4cc('0x5'));var fs=require('fs');var fs_extra=require(_0xc4cc('0x6'));var _=require(_0xc4cc('0x7'));var squel=require(_0xc4cc('0x8'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xc4cc('0x9'));var querystring=require(_0xc4cc('0xa'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0xc4cc('0xb'));var qs=require(_0xc4cc('0xc'));var as=require(_0xc4cc('0xd'));var hardwareService=require(_0xc4cc('0xe'));var logger=require(_0xc4cc('0xf'))('api');var utils=require(_0xc4cc('0x10'));var config=require('../../config/environment');var licenseUtil=require(_0xc4cc('0x11'));var db=require(_0xc4cc('0x12'))['db'];function respondWithStatusCode(_0x52304c,_0x1ef354){_0x1ef354=_0x1ef354||0xcc;return function(_0x21237f){if(_0x21237f){return _0x52304c[_0xc4cc('0x13')](_0x1ef354);}return _0x52304c[_0xc4cc('0x14')](_0x1ef354)['end']();};}function respondWithResult(_0xe13390,_0x4f70e2){_0x4f70e2=_0x4f70e2||0xc8;return function(_0x47e203){if(_0x47e203){return _0xe13390[_0xc4cc('0x14')](_0x4f70e2)[_0xc4cc('0x15')](_0x47e203);}};}function respondWithFilteredResult(_0x9381ad,_0x3b4f80){return function(_0x153c93){if(_0x153c93){var _0x3fff28=typeof _0x3b4f80['offset']===_0xc4cc('0x16')&&typeof _0x3b4f80[_0xc4cc('0x17')]===_0xc4cc('0x16');var _0x5713b1=_0x153c93[_0xc4cc('0x18')];var _0xf6bf8=_0x3fff28?0x0:_0x3b4f80[_0xc4cc('0x19')];var _0x35309e=_0x3fff28?_0x153c93['count']:_0x3b4f80[_0xc4cc('0x19')]+_0x3b4f80['limit'];var _0x206b1a;if(_0x35309e>=_0x5713b1){_0x35309e=_0x5713b1;_0x206b1a=0xc8;}else{_0x206b1a=0xce;}_0x9381ad[_0xc4cc('0x14')](_0x206b1a);return _0x9381ad['set'](_0xc4cc('0x1a'),_0xf6bf8+'-'+_0x35309e+'/'+_0x5713b1)[_0xc4cc('0x15')](_0x153c93);}return null;};}function patchUpdates(_0x336adc){return function(_0x52cbb9){try{jsonpatch[_0xc4cc('0x1b')](_0x52cbb9,_0x336adc,!![]);}catch(_0x9e65b7){return BPromise[_0xc4cc('0x1c')](_0x9e65b7);}return _0x52cbb9['save']();};}function saveUpdates(_0x2672e7,_0x341050){return function(_0x4b9b72){if(_0x4b9b72){return _0x4b9b72[_0xc4cc('0x1d')](_0x2672e7)['then'](function(_0x21850d){return _0x21850d;});}return null;};}function removeEntity(_0x5f4cd0,_0x25dc2e){return function(_0x38a79d){if(_0x38a79d){return _0x38a79d['destroy']()[_0xc4cc('0x1e')](function(){_0x5f4cd0[_0xc4cc('0x14')](0xcc)[_0xc4cc('0x1f')]();});}};}function handleEntityNotFound(_0x2ec87b,_0x2eacdf){return function(_0x29039){if(!_0x29039){_0x2ec87b[_0xc4cc('0x13')](0x194);}return _0x29039;};}function handleError(_0x3fc4a0,_0x296600){_0x296600=_0x296600||0x1f4;return function(_0x1f7ad1){logger['error'](_0x1f7ad1[_0xc4cc('0x20')]);if(_0x1f7ad1[_0xc4cc('0x21')]){delete _0x1f7ad1['name'];}_0x3fc4a0['status'](_0x296600)['send'](_0x1f7ad1);};}exports[_0xc4cc('0x22')]=function(_0x3db4cf,_0x4ac1c9){var _0x2a2d5a={},_0x2e33b5={},_0x2be71a={'count':0x0,'rows':[]};var _0x484d1f=_[_0xc4cc('0x23')](db[_0xc4cc('0x24')][_0xc4cc('0x25')],function(_0x42c6a7){return{'name':_0x42c6a7[_0xc4cc('0x26')],'type':_0x42c6a7['type']['key']};});_0x2e33b5[_0xc4cc('0x27')]=_['map'](_0x484d1f,_0xc4cc('0x21'));_0x2e33b5[_0xc4cc('0x28')]=_['keys'](_0x3db4cf['query']);_0x2e33b5[_0xc4cc('0x29')]=_[_0xc4cc('0x2a')](_0x2e33b5[_0xc4cc('0x27')],_0x2e33b5[_0xc4cc('0x28')]);_0x2a2d5a[_0xc4cc('0x2b')]=_[_0xc4cc('0x2a')](_0x2e33b5['model'],qs[_0xc4cc('0x2c')](_0x3db4cf[_0xc4cc('0x28')][_0xc4cc('0x2c')]));_0x2a2d5a[_0xc4cc('0x2b')]=_0x2a2d5a[_0xc4cc('0x2b')][_0xc4cc('0x2d')]?_0x2a2d5a[_0xc4cc('0x2b')]:_0x2e33b5['model'];if(!_0x3db4cf['query'][_0xc4cc('0x2e')]('nolimit')){_0x2a2d5a['limit']=qs[_0xc4cc('0x17')](_0x3db4cf['query'][_0xc4cc('0x17')]);_0x2a2d5a['offset']=qs[_0xc4cc('0x19')](_0x3db4cf['query'][_0xc4cc('0x19')]);}_0x2a2d5a[_0xc4cc('0x2f')]=qs[_0xc4cc('0x30')](_0x3db4cf[_0xc4cc('0x28')]['sort']);_0x2a2d5a[_0xc4cc('0x31')]=qs['filters'](_[_0xc4cc('0x32')](_0x3db4cf[_0xc4cc('0x28')],_0x2e33b5['filters']),_0x484d1f);if(_0x3db4cf[_0xc4cc('0x28')][_0xc4cc('0x33')]){_0x2a2d5a[_0xc4cc('0x31')]=_['merge'](_0x2a2d5a[_0xc4cc('0x31')],{'$or':_['map'](_0x484d1f,function(_0x36eeb1){if(_0x36eeb1['type']!==_0xc4cc('0x34')){var _0x3e28e1={};_0x3e28e1[_0x36eeb1[_0xc4cc('0x21')]]={'$like':'%'+_0x3db4cf[_0xc4cc('0x28')][_0xc4cc('0x33')]+'%'};return _0x3e28e1;}})});}_0x2a2d5a=_[_0xc4cc('0x35')]({},_0x2a2d5a,_0x3db4cf['options']);var _0x4093ff={'where':_0x2a2d5a['where']};return db['FreshsalesConfiguration'][_0xc4cc('0x18')](_0x4093ff)[_0xc4cc('0x1e')](function(_0xdb0a89){_0x2be71a[_0xc4cc('0x18')]=_0xdb0a89;if(_0x3db4cf[_0xc4cc('0x28')][_0xc4cc('0x36')]){_0x2a2d5a[_0xc4cc('0x37')]=[{'all':!![]}];}return db[_0xc4cc('0x24')]['findAll'](_0x2a2d5a);})[_0xc4cc('0x1e')](function(_0x23e6f0){_0x2be71a[_0xc4cc('0x38')]=_0x23e6f0;return _0x2be71a;})[_0xc4cc('0x1e')](respondWithFilteredResult(_0x4ac1c9,_0x2a2d5a))[_0xc4cc('0x39')](handleError(_0x4ac1c9,null));};exports[_0xc4cc('0x3a')]=function(_0x180ed6,_0x4fdf3c){var _0x36e662={'raw':![],'where':{'id':_0x180ed6[_0xc4cc('0x3b')]['id']}},_0x82e4f8={};_0x82e4f8[_0xc4cc('0x27')]=_['keys'](db[_0xc4cc('0x24')][_0xc4cc('0x25')]);_0x82e4f8[_0xc4cc('0x28')]=_[_0xc4cc('0x3c')](_0x180ed6[_0xc4cc('0x28')]);_0x82e4f8[_0xc4cc('0x29')]=_['intersection'](_0x82e4f8[_0xc4cc('0x27')],_0x82e4f8[_0xc4cc('0x28')]);_0x36e662['attributes']=_['intersection'](_0x82e4f8[_0xc4cc('0x27')],qs[_0xc4cc('0x2c')](_0x180ed6[_0xc4cc('0x28')][_0xc4cc('0x2c')]));_0x36e662['attributes']=_0x36e662[_0xc4cc('0x2b')][_0xc4cc('0x2d')]?_0x36e662[_0xc4cc('0x2b')]:_0x82e4f8['model'];if(_0x180ed6['query'][_0xc4cc('0x36')]){_0x36e662['include']=[{'all':!![]}];}_0x36e662=_[_0xc4cc('0x35')]({},_0x36e662,_0x180ed6[_0xc4cc('0x3d')]);return db[_0xc4cc('0x24')][_0xc4cc('0x3e')](_0x36e662)[_0xc4cc('0x1e')](handleEntityNotFound(_0x4fdf3c,null))[_0xc4cc('0x1e')](respondWithResult(_0x4fdf3c,null))[_0xc4cc('0x39')](handleError(_0x4fdf3c,null));};exports[_0xc4cc('0x3f')]=function(_0x31a5dc,_0x231d04){return db[_0xc4cc('0x24')][_0xc4cc('0x3f')](_0x31a5dc[_0xc4cc('0x40')],{})['then'](respondWithResult(_0x231d04,0xc9))[_0xc4cc('0x39')](handleError(_0x231d04,null));};exports[_0xc4cc('0x1d')]=function(_0x25a7bc,_0x2e6296){if(_0x25a7bc['body']['id']){delete _0x25a7bc['body']['id'];}return db[_0xc4cc('0x24')][_0xc4cc('0x3e')]({'where':{'id':_0x25a7bc['params']['id']}})['then'](handleEntityNotFound(_0x2e6296,null))[_0xc4cc('0x1e')](saveUpdates(_0x25a7bc[_0xc4cc('0x40')],null))[_0xc4cc('0x1e')](respondWithResult(_0x2e6296,null))[_0xc4cc('0x39')](handleError(_0x2e6296,null));};exports[_0xc4cc('0x41')]=function(_0x16b8ff,_0x281def){return db[_0xc4cc('0x24')]['find']({'where':{'id':_0x16b8ff[_0xc4cc('0x3b')]['id']}})['then'](handleEntityNotFound(_0x281def,null))['then'](removeEntity(_0x281def,null))[_0xc4cc('0x39')](handleError(_0x281def,null));};exports[_0xc4cc('0x42')]=function(_0xfc50b9,_0x2fd1d3,_0x26df9f){var _0x2dda46={};var _0xecb8b5={};var _0xcde189;var _0x546a0b;return db['FreshsalesConfiguration'][_0xc4cc('0x43')]({'where':{'id':_0xfc50b9[_0xc4cc('0x3b')]['id']}})[_0xc4cc('0x1e')](handleEntityNotFound(_0x2fd1d3,null))[_0xc4cc('0x1e')](function(_0xb78d82){if(_0xb78d82){_0xcde189=_0xb78d82;_0xecb8b5['model']=_[_0xc4cc('0x3c')](db['FreshsalesField'][_0xc4cc('0x25')]);_0xecb8b5[_0xc4cc('0x28')]=_[_0xc4cc('0x3c')](_0xfc50b9[_0xc4cc('0x28')]);_0xecb8b5[_0xc4cc('0x29')]=_[_0xc4cc('0x2a')](_0xecb8b5['model'],_0xecb8b5[_0xc4cc('0x28')]);_0x2dda46[_0xc4cc('0x2b')]=_['intersection'](_0xecb8b5[_0xc4cc('0x27')],qs[_0xc4cc('0x2c')](_0xfc50b9[_0xc4cc('0x28')]['fields']));_0x2dda46['attributes']=_0x2dda46[_0xc4cc('0x2b')][_0xc4cc('0x2d')]?_0x2dda46[_0xc4cc('0x2b')]:_0xecb8b5[_0xc4cc('0x27')];_0x2dda46[_0xc4cc('0x2f')]=qs[_0xc4cc('0x30')](_0xfc50b9[_0xc4cc('0x28')][_0xc4cc('0x30')]);_0x2dda46[_0xc4cc('0x31')]=qs[_0xc4cc('0x29')](_[_0xc4cc('0x32')](_0xfc50b9[_0xc4cc('0x28')],_0xecb8b5[_0xc4cc('0x29')]));if(_0xfc50b9[_0xc4cc('0x28')][_0xc4cc('0x33')]){_0x2dda46[_0xc4cc('0x31')]=_['merge'](_0x2dda46[_0xc4cc('0x31')],{'$or':_['map'](_0x2dda46[_0xc4cc('0x2b')],function(_0xd82a22){var _0xec54b1={};_0xec54b1[_0xd82a22]={'$like':'%'+_0xfc50b9['query']['filter']+'%'};return _0xec54b1;})});}_0x2dda46=_[_0xc4cc('0x35')]({},_0x2dda46,_0xfc50b9[_0xc4cc('0x3d')]);return _0xcde189[_0xc4cc('0x42')](_0x2dda46);}})[_0xc4cc('0x1e')](function(_0x1320f9){if(_0x1320f9){_0x546a0b=_0x1320f9['length'];if(!_0xfc50b9[_0xc4cc('0x28')]['hasOwnProperty'](_0xc4cc('0x44'))){_0x2dda46[_0xc4cc('0x17')]=qs['limit'](_0xfc50b9[_0xc4cc('0x28')]['limit']);_0x2dda46[_0xc4cc('0x19')]=qs[_0xc4cc('0x19')](_0xfc50b9[_0xc4cc('0x28')]['offset']);}return _0xcde189['getSubjects'](_0x2dda46);}})[_0xc4cc('0x1e')](function(_0x1132c4){if(_0x1132c4){return _0x1132c4?{'count':_0x546a0b,'rows':_0x1132c4}:null;}})['then'](respondWithResult(_0x2fd1d3,null))[_0xc4cc('0x39')](handleError(_0x2fd1d3,null));};exports[_0xc4cc('0x45')]=function(_0x4d3eaa,_0x1d007d,_0x190b37){var _0x40c669={};var _0x1e536f={};var _0x3d8c37;var _0x468d00;return db[_0xc4cc('0x24')]['findOne']({'where':{'id':_0x4d3eaa[_0xc4cc('0x3b')]['id']}})['then'](handleEntityNotFound(_0x1d007d,null))[_0xc4cc('0x1e')](function(_0x29d28d){if(_0x29d28d){_0x3d8c37=_0x29d28d;_0x1e536f[_0xc4cc('0x27')]=_['keys'](db[_0xc4cc('0x46')][_0xc4cc('0x25')]);_0x1e536f[_0xc4cc('0x28')]=_[_0xc4cc('0x3c')](_0x4d3eaa[_0xc4cc('0x28')]);_0x1e536f['filters']=_[_0xc4cc('0x2a')](_0x1e536f[_0xc4cc('0x27')],_0x1e536f[_0xc4cc('0x28')]);_0x40c669[_0xc4cc('0x2b')]=_[_0xc4cc('0x2a')](_0x1e536f['model'],qs[_0xc4cc('0x2c')](_0x4d3eaa[_0xc4cc('0x28')][_0xc4cc('0x2c')]));_0x40c669[_0xc4cc('0x2b')]=_0x40c669[_0xc4cc('0x2b')][_0xc4cc('0x2d')]?_0x40c669['attributes']:_0x1e536f[_0xc4cc('0x27')];_0x40c669['order']=qs['sort'](_0x4d3eaa['query']['sort']);_0x40c669[_0xc4cc('0x31')]=qs[_0xc4cc('0x29')](_[_0xc4cc('0x32')](_0x4d3eaa[_0xc4cc('0x28')],_0x1e536f['filters']));if(_0x4d3eaa['query']['filter']){_0x40c669[_0xc4cc('0x31')]=_[_0xc4cc('0x35')](_0x40c669[_0xc4cc('0x31')],{'$or':_[_0xc4cc('0x23')](_0x40c669[_0xc4cc('0x2b')],function(_0x2fab22){var _0x1f4468={};_0x1f4468[_0x2fab22]={'$like':'%'+_0x4d3eaa[_0xc4cc('0x28')][_0xc4cc('0x33')]+'%'};return _0x1f4468;})});}_0x40c669=_[_0xc4cc('0x35')]({},_0x40c669,_0x4d3eaa[_0xc4cc('0x3d')]);return _0x3d8c37[_0xc4cc('0x45')](_0x40c669);}})[_0xc4cc('0x1e')](function(_0x1d573b){if(_0x1d573b){_0x468d00=_0x1d573b[_0xc4cc('0x2d')];if(!_0x4d3eaa[_0xc4cc('0x28')][_0xc4cc('0x2e')]('nolimit')){_0x40c669['limit']=qs['limit'](_0x4d3eaa['query'][_0xc4cc('0x17')]);_0x40c669[_0xc4cc('0x19')]=qs[_0xc4cc('0x19')](_0x4d3eaa[_0xc4cc('0x28')]['offset']);}return _0x3d8c37[_0xc4cc('0x45')](_0x40c669);}})['then'](function(_0x2be39a){if(_0x2be39a){return _0x2be39a?{'count':_0x468d00,'rows':_0x2be39a}:null;}})['then'](respondWithResult(_0x1d007d,null))[_0xc4cc('0x39')](handleError(_0x1d007d,null));};exports['getFields']=function(_0x251751,_0x1e87a9,_0x199d18){var _0x34ee9e={};var _0x39e20c={};var _0x3c5cbc;var _0x458569;return db[_0xc4cc('0x24')][_0xc4cc('0x43')]({'where':{'id':_0x251751[_0xc4cc('0x3b')]['id']}})[_0xc4cc('0x1e')](handleEntityNotFound(_0x1e87a9,null))[_0xc4cc('0x1e')](function(_0x2c4680){if(_0x2c4680){_0x3c5cbc=_0x2c4680;_0x39e20c[_0xc4cc('0x27')]=_['keys'](db['FreshsalesField'][_0xc4cc('0x25')]);_0x39e20c['query']=_[_0xc4cc('0x3c')](_0x251751[_0xc4cc('0x28')]);_0x39e20c[_0xc4cc('0x29')]=_[_0xc4cc('0x2a')](_0x39e20c['model'],_0x39e20c[_0xc4cc('0x28')]);_0x34ee9e[_0xc4cc('0x2b')]=_[_0xc4cc('0x2a')](_0x39e20c[_0xc4cc('0x27')],qs[_0xc4cc('0x2c')](_0x251751[_0xc4cc('0x28')][_0xc4cc('0x2c')]));_0x34ee9e[_0xc4cc('0x2b')]=_0x34ee9e['attributes']['length']?_0x34ee9e[_0xc4cc('0x2b')]:_0x39e20c['model'];_0x34ee9e[_0xc4cc('0x2f')]=qs[_0xc4cc('0x30')](_0x251751[_0xc4cc('0x28')][_0xc4cc('0x30')]);_0x34ee9e['where']=qs[_0xc4cc('0x29')](_[_0xc4cc('0x32')](_0x251751['query'],_0x39e20c[_0xc4cc('0x29')]));if(_0x251751[_0xc4cc('0x28')][_0xc4cc('0x33')]){_0x34ee9e['where']=_[_0xc4cc('0x35')](_0x34ee9e[_0xc4cc('0x31')],{'$or':_[_0xc4cc('0x23')](_0x34ee9e[_0xc4cc('0x2b')],function(_0x3173b2){var _0x648224={};_0x648224[_0x3173b2]={'$like':'%'+_0x251751['query'][_0xc4cc('0x33')]+'%'};return _0x648224;})});}_0x34ee9e=_[_0xc4cc('0x35')]({},_0x34ee9e,_0x251751[_0xc4cc('0x3d')]);return _0x3c5cbc['getFields'](_0x34ee9e);}})['then'](function(_0xa7e4c3){if(_0xa7e4c3){_0x458569=_0xa7e4c3[_0xc4cc('0x2d')];if(!_0x251751[_0xc4cc('0x28')][_0xc4cc('0x2e')](_0xc4cc('0x44'))){_0x34ee9e['limit']=qs['limit'](_0x251751['query'][_0xc4cc('0x17')]);_0x34ee9e[_0xc4cc('0x19')]=qs[_0xc4cc('0x19')](_0x251751[_0xc4cc('0x28')][_0xc4cc('0x19')]);}return _0x3c5cbc[_0xc4cc('0x47')](_0x34ee9e);}})[_0xc4cc('0x1e')](function(_0xc57032){if(_0xc57032){return _0xc57032?{'count':_0x458569,'rows':_0xc57032}:null;}})[_0xc4cc('0x1e')](respondWithResult(_0x1e87a9,null))[_0xc4cc('0x39')](handleError(_0x1e87a9,null));};