6ee4d411b4744efbc77df73bf359e45d8975e388
[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 _0x79fb=['options','includeAll','findAll','rows','catch','show','params','rawAttributes','filters','length','include','find','create','body','destroy','getSubjects','findOne','pick','getDescriptions','FreshsalesField','hasOwnProperty','getFields','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','ejs','fs-extra','lodash','jsforce','desk.js','to-csv','papaparse','ioredis','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','status','offset','undefined','limit','count','set','Content-Range','json','apply','save','update','then','error','stack','name','index','map','FreshsalesConfiguration','fieldName','key','model','keys','intersection','query','fields','attributes','nolimit','order','sort','where','merge','VIRTUAL','filter'];(function(_0x17114a,_0x60171){var _0x3571ca=function(_0x59b48f){while(--_0x59b48f){_0x17114a['push'](_0x17114a['shift']());}};_0x3571ca(++_0x60171);}(_0x79fb,0xb2));var _0xb79f=function(_0x587595,_0x4b5ced){_0x587595=_0x587595-0x0;var _0x5e1ab4=_0x79fb[_0x587595];return _0x5e1ab4;};'use strict';var emlformat=require(_0xb79f('0x0'));var rimraf=require(_0xb79f('0x1'));var zipdir=require(_0xb79f('0x2'));var jsonpatch=require(_0xb79f('0x3'));var rp=require(_0xb79f('0x4'));var moment=require(_0xb79f('0x5'));var BPromise=require(_0xb79f('0x6'));var Mustache=require(_0xb79f('0x7'));var util=require(_0xb79f('0x8'));var path=require('path');var sox=require(_0xb79f('0x9'));var csv=require('to-csv');var ejs=require(_0xb79f('0xa'));var fs=require('fs');var fs_extra=require(_0xb79f('0xb'));var _=require(_0xb79f('0xc'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0xb79f('0xd'));var deskjs=require(_0xb79f('0xe'));var toCsv=require(_0xb79f('0xf'));var querystring=require('querystring');var Papa=require(_0xb79f('0x10'));var Redis=require(_0xb79f('0x11'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xb79f('0x12'));var hardwareService=require(_0xb79f('0x13'));var logger=require('../../config/logger')(_0xb79f('0x14'));var utils=require(_0xb79f('0x15'));var config=require(_0xb79f('0x16'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0xd05b79,_0x1fbd1c){_0x1fbd1c=_0x1fbd1c||0xcc;return function(_0x20128f){if(_0x20128f){return _0xd05b79['sendStatus'](_0x1fbd1c);}return _0xd05b79[_0xb79f('0x17')](_0x1fbd1c)['end']();};}function respondWithResult(_0x32b096,_0x3fcf95){_0x3fcf95=_0x3fcf95||0xc8;return function(_0x4d2e1d){if(_0x4d2e1d){return _0x32b096[_0xb79f('0x17')](_0x3fcf95)['json'](_0x4d2e1d);}};}function respondWithFilteredResult(_0x184d20,_0x2d368e){return function(_0x7c9d1c){if(_0x7c9d1c){var _0x9073bc=typeof _0x2d368e[_0xb79f('0x18')]===_0xb79f('0x19')&&typeof _0x2d368e[_0xb79f('0x1a')]===_0xb79f('0x19');var _0x3cc2a4=_0x7c9d1c[_0xb79f('0x1b')];var _0x22c7f8=_0x9073bc?0x0:_0x2d368e[_0xb79f('0x18')];var _0x471c05=_0x9073bc?_0x7c9d1c[_0xb79f('0x1b')]:_0x2d368e[_0xb79f('0x18')]+_0x2d368e[_0xb79f('0x1a')];var _0x4343a8;if(_0x471c05>=_0x3cc2a4){_0x471c05=_0x3cc2a4;_0x4343a8=0xc8;}else{_0x4343a8=0xce;}_0x184d20[_0xb79f('0x17')](_0x4343a8);return _0x184d20[_0xb79f('0x1c')](_0xb79f('0x1d'),_0x22c7f8+'-'+_0x471c05+'/'+_0x3cc2a4)[_0xb79f('0x1e')](_0x7c9d1c);}return null;};}function patchUpdates(_0x19b5c9){return function(_0xef03fe){try{jsonpatch[_0xb79f('0x1f')](_0xef03fe,_0x19b5c9,!![]);}catch(_0x54109a){return BPromise['reject'](_0x54109a);}return _0xef03fe[_0xb79f('0x20')]();};}function saveUpdates(_0x4e6abe,_0x1b8f06){return function(_0x4b927a){if(_0x4b927a){return _0x4b927a[_0xb79f('0x21')](_0x4e6abe)[_0xb79f('0x22')](function(_0x3e3809){return _0x3e3809;});}return null;};}function removeEntity(_0x7ba723,_0x332175){return function(_0x28299b){if(_0x28299b){return _0x28299b['destroy']()[_0xb79f('0x22')](function(){_0x7ba723['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0xa52fbc,_0x427fd5){return function(_0x5bbde2){if(!_0x5bbde2){_0xa52fbc['sendStatus'](0x194);}return _0x5bbde2;};}function handleError(_0x1bb6a7,_0x11e990){_0x11e990=_0x11e990||0x1f4;return function(_0x53765b){logger[_0xb79f('0x23')](_0x53765b[_0xb79f('0x24')]);if(_0x53765b[_0xb79f('0x25')]){delete _0x53765b['name'];}_0x1bb6a7[_0xb79f('0x17')](_0x11e990)['send'](_0x53765b);};}exports[_0xb79f('0x26')]=function(_0x181bf0,_0x541978){var _0x16cc59={},_0x293482={},_0x520da4={'count':0x0,'rows':[]};var _0x6ab071=_[_0xb79f('0x27')](db[_0xb79f('0x28')]['rawAttributes'],function(_0x3b543a){return{'name':_0x3b543a[_0xb79f('0x29')],'type':_0x3b543a['type'][_0xb79f('0x2a')]};});_0x293482[_0xb79f('0x2b')]=_[_0xb79f('0x27')](_0x6ab071,'name');_0x293482['query']=_[_0xb79f('0x2c')](_0x181bf0['query']);_0x293482['filters']=_[_0xb79f('0x2d')](_0x293482[_0xb79f('0x2b')],_0x293482[_0xb79f('0x2e')]);_0x16cc59['attributes']=_['intersection'](_0x293482[_0xb79f('0x2b')],qs[_0xb79f('0x2f')](_0x181bf0['query']['fields']));_0x16cc59[_0xb79f('0x30')]=_0x16cc59[_0xb79f('0x30')]['length']?_0x16cc59['attributes']:_0x293482[_0xb79f('0x2b')];if(!_0x181bf0[_0xb79f('0x2e')]['hasOwnProperty'](_0xb79f('0x31'))){_0x16cc59[_0xb79f('0x1a')]=qs['limit'](_0x181bf0['query'][_0xb79f('0x1a')]);_0x16cc59['offset']=qs[_0xb79f('0x18')](_0x181bf0[_0xb79f('0x2e')][_0xb79f('0x18')]);}_0x16cc59[_0xb79f('0x32')]=qs[_0xb79f('0x33')](_0x181bf0['query'][_0xb79f('0x33')]);_0x16cc59[_0xb79f('0x34')]=qs['filters'](_['pick'](_0x181bf0[_0xb79f('0x2e')],_0x293482['filters']),_0x6ab071);if(_0x181bf0[_0xb79f('0x2e')]['filter']){_0x16cc59[_0xb79f('0x34')]=_[_0xb79f('0x35')](_0x16cc59[_0xb79f('0x34')],{'$or':_[_0xb79f('0x27')](_0x6ab071,function(_0x4c8837){if(_0x4c8837['type']!==_0xb79f('0x36')){var _0x533e5d={};_0x533e5d[_0x4c8837['name']]={'$like':'%'+_0x181bf0[_0xb79f('0x2e')][_0xb79f('0x37')]+'%'};return _0x533e5d;}})});}_0x16cc59=_['merge']({},_0x16cc59,_0x181bf0[_0xb79f('0x38')]);var _0x4e9b44={'where':_0x16cc59[_0xb79f('0x34')]};return db[_0xb79f('0x28')][_0xb79f('0x1b')](_0x4e9b44)['then'](function(_0x56dfa0){_0x520da4['count']=_0x56dfa0;if(_0x181bf0[_0xb79f('0x2e')][_0xb79f('0x39')]){_0x16cc59['include']=[{'all':!![]}];}return db[_0xb79f('0x28')][_0xb79f('0x3a')](_0x16cc59);})['then'](function(_0x3bdf99){_0x520da4[_0xb79f('0x3b')]=_0x3bdf99;return _0x520da4;})[_0xb79f('0x22')](respondWithFilteredResult(_0x541978,_0x16cc59))[_0xb79f('0x3c')](handleError(_0x541978,null));};exports[_0xb79f('0x3d')]=function(_0x13310f,_0x4d2b07){var _0xa15228={'raw':![],'where':{'id':_0x13310f[_0xb79f('0x3e')]['id']}},_0x1a3aa0={};_0x1a3aa0[_0xb79f('0x2b')]=_[_0xb79f('0x2c')](db[_0xb79f('0x28')][_0xb79f('0x3f')]);_0x1a3aa0[_0xb79f('0x2e')]=_[_0xb79f('0x2c')](_0x13310f[_0xb79f('0x2e')]);_0x1a3aa0[_0xb79f('0x40')]=_['intersection'](_0x1a3aa0[_0xb79f('0x2b')],_0x1a3aa0[_0xb79f('0x2e')]);_0xa15228['attributes']=_[_0xb79f('0x2d')](_0x1a3aa0[_0xb79f('0x2b')],qs[_0xb79f('0x2f')](_0x13310f[_0xb79f('0x2e')]['fields']));_0xa15228['attributes']=_0xa15228[_0xb79f('0x30')][_0xb79f('0x41')]?_0xa15228['attributes']:_0x1a3aa0['model'];if(_0x13310f[_0xb79f('0x2e')][_0xb79f('0x39')]){_0xa15228[_0xb79f('0x42')]=[{'all':!![]}];}_0xa15228=_['merge']({},_0xa15228,_0x13310f[_0xb79f('0x38')]);return db[_0xb79f('0x28')][_0xb79f('0x43')](_0xa15228)[_0xb79f('0x22')](handleEntityNotFound(_0x4d2b07,null))[_0xb79f('0x22')](respondWithResult(_0x4d2b07,null))['catch'](handleError(_0x4d2b07,null));};exports[_0xb79f('0x44')]=function(_0x384388,_0x2b1ab4){return db[_0xb79f('0x28')]['create'](_0x384388[_0xb79f('0x45')],{})[_0xb79f('0x22')](respondWithResult(_0x2b1ab4,0xc9))['catch'](handleError(_0x2b1ab4,null));};exports['update']=function(_0x3146ed,_0x261883){if(_0x3146ed['body']['id']){delete _0x3146ed[_0xb79f('0x45')]['id'];}return db['FreshsalesConfiguration'][_0xb79f('0x43')]({'where':{'id':_0x3146ed[_0xb79f('0x3e')]['id']}})[_0xb79f('0x22')](handleEntityNotFound(_0x261883,null))[_0xb79f('0x22')](saveUpdates(_0x3146ed[_0xb79f('0x45')],null))['then'](respondWithResult(_0x261883,null))['catch'](handleError(_0x261883,null));};exports[_0xb79f('0x46')]=function(_0x2b3a66,_0x421940){return db[_0xb79f('0x28')][_0xb79f('0x43')]({'where':{'id':_0x2b3a66[_0xb79f('0x3e')]['id']}})[_0xb79f('0x22')](handleEntityNotFound(_0x421940,null))[_0xb79f('0x22')](removeEntity(_0x421940,null))[_0xb79f('0x3c')](handleError(_0x421940,null));};exports[_0xb79f('0x47')]=function(_0x181de7,_0x5e2e95,_0x3e13ee){var _0x5c5dcf={};var _0x17a894={};var _0x154bd5;var _0x5e1382;return db[_0xb79f('0x28')][_0xb79f('0x48')]({'where':{'id':_0x181de7['params']['id']}})[_0xb79f('0x22')](handleEntityNotFound(_0x5e2e95,null))[_0xb79f('0x22')](function(_0x27d6d7){if(_0x27d6d7){_0x154bd5=_0x27d6d7;_0x17a894[_0xb79f('0x2b')]=_['keys'](db['FreshsalesField'][_0xb79f('0x3f')]);_0x17a894['query']=_[_0xb79f('0x2c')](_0x181de7[_0xb79f('0x2e')]);_0x17a894[_0xb79f('0x40')]=_[_0xb79f('0x2d')](_0x17a894[_0xb79f('0x2b')],_0x17a894[_0xb79f('0x2e')]);_0x5c5dcf[_0xb79f('0x30')]=_[_0xb79f('0x2d')](_0x17a894[_0xb79f('0x2b')],qs['fields'](_0x181de7[_0xb79f('0x2e')]['fields']));_0x5c5dcf['attributes']=_0x5c5dcf['attributes'][_0xb79f('0x41')]?_0x5c5dcf[_0xb79f('0x30')]:_0x17a894[_0xb79f('0x2b')];_0x5c5dcf[_0xb79f('0x32')]=qs[_0xb79f('0x33')](_0x181de7[_0xb79f('0x2e')][_0xb79f('0x33')]);_0x5c5dcf[_0xb79f('0x34')]=qs[_0xb79f('0x40')](_[_0xb79f('0x49')](_0x181de7[_0xb79f('0x2e')],_0x17a894[_0xb79f('0x40')]));if(_0x181de7['query'][_0xb79f('0x37')]){_0x5c5dcf[_0xb79f('0x34')]=_['merge'](_0x5c5dcf[_0xb79f('0x34')],{'$or':_[_0xb79f('0x27')](_0x5c5dcf[_0xb79f('0x30')],function(_0x3dd7f0){var _0x299fdb={};_0x299fdb[_0x3dd7f0]={'$like':'%'+_0x181de7['query']['filter']+'%'};return _0x299fdb;})});}_0x5c5dcf=_['merge']({},_0x5c5dcf,_0x181de7[_0xb79f('0x38')]);return _0x154bd5['getSubjects'](_0x5c5dcf);}})[_0xb79f('0x22')](function(_0x331595){if(_0x331595){_0x5e1382=_0x331595['length'];if(!_0x181de7['query']['hasOwnProperty']('nolimit')){_0x5c5dcf[_0xb79f('0x1a')]=qs[_0xb79f('0x1a')](_0x181de7['query'][_0xb79f('0x1a')]);_0x5c5dcf[_0xb79f('0x18')]=qs[_0xb79f('0x18')](_0x181de7[_0xb79f('0x2e')][_0xb79f('0x18')]);}return _0x154bd5[_0xb79f('0x47')](_0x5c5dcf);}})['then'](function(_0x5a8fd7){if(_0x5a8fd7){return _0x5a8fd7?{'count':_0x5e1382,'rows':_0x5a8fd7}:null;}})['then'](respondWithResult(_0x5e2e95,null))[_0xb79f('0x3c')](handleError(_0x5e2e95,null));};exports[_0xb79f('0x4a')]=function(_0xc8cf6e,_0x1a59b8,_0x3c2ab3){var _0x41b1d3={};var _0x388203={};var _0x41ce89;var _0x45d0e5;return db[_0xb79f('0x28')][_0xb79f('0x48')]({'where':{'id':_0xc8cf6e[_0xb79f('0x3e')]['id']}})['then'](handleEntityNotFound(_0x1a59b8,null))[_0xb79f('0x22')](function(_0x2c3b01){if(_0x2c3b01){_0x41ce89=_0x2c3b01;_0x388203[_0xb79f('0x2b')]=_[_0xb79f('0x2c')](db[_0xb79f('0x4b')][_0xb79f('0x3f')]);_0x388203[_0xb79f('0x2e')]=_[_0xb79f('0x2c')](_0xc8cf6e[_0xb79f('0x2e')]);_0x388203[_0xb79f('0x40')]=_['intersection'](_0x388203['model'],_0x388203[_0xb79f('0x2e')]);_0x41b1d3[_0xb79f('0x30')]=_['intersection'](_0x388203[_0xb79f('0x2b')],qs[_0xb79f('0x2f')](_0xc8cf6e[_0xb79f('0x2e')][_0xb79f('0x2f')]));_0x41b1d3['attributes']=_0x41b1d3['attributes'][_0xb79f('0x41')]?_0x41b1d3[_0xb79f('0x30')]:_0x388203[_0xb79f('0x2b')];_0x41b1d3[_0xb79f('0x32')]=qs[_0xb79f('0x33')](_0xc8cf6e[_0xb79f('0x2e')]['sort']);_0x41b1d3[_0xb79f('0x34')]=qs[_0xb79f('0x40')](_[_0xb79f('0x49')](_0xc8cf6e['query'],_0x388203[_0xb79f('0x40')]));if(_0xc8cf6e[_0xb79f('0x2e')][_0xb79f('0x37')]){_0x41b1d3['where']=_[_0xb79f('0x35')](_0x41b1d3['where'],{'$or':_[_0xb79f('0x27')](_0x41b1d3[_0xb79f('0x30')],function(_0x51d4cb){var _0x191793={};_0x191793[_0x51d4cb]={'$like':'%'+_0xc8cf6e[_0xb79f('0x2e')][_0xb79f('0x37')]+'%'};return _0x191793;})});}_0x41b1d3=_[_0xb79f('0x35')]({},_0x41b1d3,_0xc8cf6e[_0xb79f('0x38')]);return _0x41ce89[_0xb79f('0x4a')](_0x41b1d3);}})[_0xb79f('0x22')](function(_0x8a9671){if(_0x8a9671){_0x45d0e5=_0x8a9671[_0xb79f('0x41')];if(!_0xc8cf6e['query'][_0xb79f('0x4c')](_0xb79f('0x31'))){_0x41b1d3[_0xb79f('0x1a')]=qs[_0xb79f('0x1a')](_0xc8cf6e[_0xb79f('0x2e')]['limit']);_0x41b1d3[_0xb79f('0x18')]=qs[_0xb79f('0x18')](_0xc8cf6e[_0xb79f('0x2e')][_0xb79f('0x18')]);}return _0x41ce89['getDescriptions'](_0x41b1d3);}})[_0xb79f('0x22')](function(_0xfde8ed){if(_0xfde8ed){return _0xfde8ed?{'count':_0x45d0e5,'rows':_0xfde8ed}:null;}})['then'](respondWithResult(_0x1a59b8,null))[_0xb79f('0x3c')](handleError(_0x1a59b8,null));};exports[_0xb79f('0x4d')]=function(_0x8a9ece,_0x44d137,_0x17ecfc){var _0xc4d1af={};var _0x413a24={};var _0x1f4873;var _0x579f91;return db[_0xb79f('0x28')][_0xb79f('0x48')]({'where':{'id':_0x8a9ece[_0xb79f('0x3e')]['id']}})[_0xb79f('0x22')](handleEntityNotFound(_0x44d137,null))[_0xb79f('0x22')](function(_0x7b34df){if(_0x7b34df){_0x1f4873=_0x7b34df;_0x413a24[_0xb79f('0x2b')]=_['keys'](db['FreshsalesField'][_0xb79f('0x3f')]);_0x413a24['query']=_['keys'](_0x8a9ece['query']);_0x413a24[_0xb79f('0x40')]=_['intersection'](_0x413a24[_0xb79f('0x2b')],_0x413a24[_0xb79f('0x2e')]);_0xc4d1af['attributes']=_[_0xb79f('0x2d')](_0x413a24[_0xb79f('0x2b')],qs[_0xb79f('0x2f')](_0x8a9ece['query'][_0xb79f('0x2f')]));_0xc4d1af[_0xb79f('0x30')]=_0xc4d1af[_0xb79f('0x30')]['length']?_0xc4d1af[_0xb79f('0x30')]:_0x413a24['model'];_0xc4d1af[_0xb79f('0x32')]=qs[_0xb79f('0x33')](_0x8a9ece[_0xb79f('0x2e')][_0xb79f('0x33')]);_0xc4d1af[_0xb79f('0x34')]=qs[_0xb79f('0x40')](_['pick'](_0x8a9ece[_0xb79f('0x2e')],_0x413a24['filters']));if(_0x8a9ece['query'][_0xb79f('0x37')]){_0xc4d1af['where']=_[_0xb79f('0x35')](_0xc4d1af[_0xb79f('0x34')],{'$or':_[_0xb79f('0x27')](_0xc4d1af[_0xb79f('0x30')],function(_0x275e14){var _0x2f83f6={};_0x2f83f6[_0x275e14]={'$like':'%'+_0x8a9ece[_0xb79f('0x2e')][_0xb79f('0x37')]+'%'};return _0x2f83f6;})});}_0xc4d1af=_[_0xb79f('0x35')]({},_0xc4d1af,_0x8a9ece[_0xb79f('0x38')]);return _0x1f4873[_0xb79f('0x4d')](_0xc4d1af);}})['then'](function(_0x5c97d8){if(_0x5c97d8){_0x579f91=_0x5c97d8['length'];if(!_0x8a9ece[_0xb79f('0x2e')]['hasOwnProperty'](_0xb79f('0x31'))){_0xc4d1af[_0xb79f('0x1a')]=qs[_0xb79f('0x1a')](_0x8a9ece['query'][_0xb79f('0x1a')]);_0xc4d1af[_0xb79f('0x18')]=qs['offset'](_0x8a9ece[_0xb79f('0x2e')][_0xb79f('0x18')]);}return _0x1f4873[_0xb79f('0x4d')](_0xc4d1af);}})['then'](function(_0x4e140e){if(_0x4e140e){return _0x4e140e?{'count':_0x579f91,'rows':_0x4e140e}:null;}})['then'](respondWithResult(_0x44d137,null))[_0xb79f('0x3c')](handleError(_0x44d137,null));};