Built motion from commit e6806ed6.|2.6.0
[motion2.git] / server / api / intSalesforceConfiguration / intSalesforceConfiguration.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 _0xf958=['desk.js','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','end','json','offset','undefined','limit','count','status','Content-Range','apply','save','update','destroy','then','stack','name','send','index','map','SalesforceConfiguration','type','key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','options','includeAll','include','findAll','rows','show','params','merge','catch','create','body','findOne','rawAttributes','getFields','getSubjects','getDescriptions','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce'];(function(_0x4a9fb1,_0x273043){var _0x348672=function(_0x142b69){while(--_0x142b69){_0x4a9fb1['push'](_0x4a9fb1['shift']());}};_0x348672(++_0x273043);}(_0xf958,0x1c0));var _0x8f95=function(_0x5eb1ac,_0x46e65b){_0x5eb1ac=_0x5eb1ac-0x0;var _0x2e712c=_0xf958[_0x5eb1ac];return _0x2e712c;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x8f95('0x0'));var rp=require(_0x8f95('0x1'));var moment=require(_0x8f95('0x2'));var BPromise=require(_0x8f95('0x3'));var Mustache=require(_0x8f95('0x4'));var util=require(_0x8f95('0x5'));var path=require('path');var sox=require(_0x8f95('0x6'));var csv=require(_0x8f95('0x7'));var ejs=require(_0x8f95('0x8'));var fs=require('fs');var fs_extra=require(_0x8f95('0x9'));var _=require(_0x8f95('0xa'));var squel=require(_0x8f95('0xb'));var crypto=require(_0x8f95('0xc'));var jsforce=require(_0x8f95('0xd'));var deskjs=require(_0x8f95('0xe'));var toCsv=require(_0x8f95('0x7'));var querystring=require('querystring');var Papa=require(_0x8f95('0xf'));var Redis=require('ioredis');var authService=require(_0x8f95('0x10'));var qs=require(_0x8f95('0x11'));var as=require(_0x8f95('0x12'));var hardwareService=require(_0x8f95('0x13'));var logger=require('../../config/logger')(_0x8f95('0x14'));var utils=require(_0x8f95('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x8f95('0x16'));var db=require(_0x8f95('0x17'))['db'];function respondWithStatusCode(_0x4089ce,_0x360523){_0x360523=_0x360523||0xcc;return function(_0x53897e){if(_0x53897e){return _0x4089ce[_0x8f95('0x18')](_0x360523);}return _0x4089ce['status'](_0x360523)[_0x8f95('0x19')]();};}function respondWithResult(_0x5e7b50,_0x19d0fc){_0x19d0fc=_0x19d0fc||0xc8;return function(_0x3c665f){if(_0x3c665f){return _0x5e7b50['status'](_0x19d0fc)[_0x8f95('0x1a')](_0x3c665f);}};}function respondWithFilteredResult(_0x4af84f,_0x2051ca){return function(_0x5c675a){if(_0x5c675a){var _0x2ff25d=typeof _0x2051ca[_0x8f95('0x1b')]===_0x8f95('0x1c')&&typeof _0x2051ca[_0x8f95('0x1d')]==='undefined';var _0x242b26=_0x5c675a[_0x8f95('0x1e')];var _0x7b64bf=_0x2ff25d?0x0:_0x2051ca[_0x8f95('0x1b')];var _0x4dacb1=_0x2ff25d?_0x5c675a[_0x8f95('0x1e')]:_0x2051ca[_0x8f95('0x1b')]+_0x2051ca['limit'];var _0x18e43d;if(_0x4dacb1>=_0x242b26){_0x4dacb1=_0x242b26;_0x18e43d=0xc8;}else{_0x18e43d=0xce;}_0x4af84f[_0x8f95('0x1f')](_0x18e43d);return _0x4af84f['set'](_0x8f95('0x20'),_0x7b64bf+'-'+_0x4dacb1+'/'+_0x242b26)[_0x8f95('0x1a')](_0x5c675a);}return null;};}function patchUpdates(_0x78e30a){return function(_0x353c14){try{jsonpatch[_0x8f95('0x21')](_0x353c14,_0x78e30a,!![]);}catch(_0x19899a){return BPromise['reject'](_0x19899a);}return _0x353c14[_0x8f95('0x22')]();};}function saveUpdates(_0xc99d55,_0x4535b1){return function(_0xc6d696){if(_0xc6d696){return _0xc6d696[_0x8f95('0x23')](_0xc99d55)['then'](function(_0x315c18){return _0x315c18;});}return null;};}function removeEntity(_0xf14011,_0x3f083d){return function(_0x180b27){if(_0x180b27){return _0x180b27[_0x8f95('0x24')]()[_0x8f95('0x25')](function(){_0xf14011[_0x8f95('0x1f')](0xcc)[_0x8f95('0x19')]();});}};}function handleEntityNotFound(_0x5afc6a,_0x345b33){return function(_0x2edf2c){if(!_0x2edf2c){_0x5afc6a['sendStatus'](0x194);}return _0x2edf2c;};}function handleError(_0x8a0fbc,_0x568a6a){_0x568a6a=_0x568a6a||0x1f4;return function(_0x2df6bd){logger['error'](_0x2df6bd[_0x8f95('0x26')]);if(_0x2df6bd[_0x8f95('0x27')]){delete _0x2df6bd[_0x8f95('0x27')];}_0x8a0fbc[_0x8f95('0x1f')](_0x568a6a)[_0x8f95('0x28')](_0x2df6bd);};}exports[_0x8f95('0x29')]=function(_0x4941d1,_0x401c4d){var _0xdff317={},_0x3062d8={},_0x52d63b={'count':0x0,'rows':[]};var _0x1f5209=_[_0x8f95('0x2a')](db[_0x8f95('0x2b')]['rawAttributes'],function(_0x5ef363){return{'name':_0x5ef363['fieldName'],'type':_0x5ef363[_0x8f95('0x2c')][_0x8f95('0x2d')]};});_0x3062d8['model']=_[_0x8f95('0x2a')](_0x1f5209,'name');_0x3062d8[_0x8f95('0x2e')]=_[_0x8f95('0x2f')](_0x4941d1[_0x8f95('0x2e')]);_0x3062d8[_0x8f95('0x30')]=_[_0x8f95('0x31')](_0x3062d8[_0x8f95('0x32')],_0x3062d8[_0x8f95('0x2e')]);_0xdff317[_0x8f95('0x33')]=_['intersection'](_0x3062d8[_0x8f95('0x32')],qs[_0x8f95('0x34')](_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x34')]));_0xdff317[_0x8f95('0x33')]=_0xdff317['attributes'][_0x8f95('0x35')]?_0xdff317['attributes']:_0x3062d8[_0x8f95('0x32')];if(!_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x36')](_0x8f95('0x37'))){_0xdff317['limit']=qs[_0x8f95('0x1d')](_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x1d')]);_0xdff317['offset']=qs['offset'](_0x4941d1['query']['offset']);}_0xdff317[_0x8f95('0x38')]=qs['sort'](_0x4941d1['query'][_0x8f95('0x39')]);_0xdff317['where']=qs[_0x8f95('0x30')](_[_0x8f95('0x3a')](_0x4941d1['query'],_0x3062d8[_0x8f95('0x30')]),_0x1f5209);if(_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x3b')]){_0xdff317[_0x8f95('0x3c')]=_['merge'](_0xdff317[_0x8f95('0x3c')],{'$or':_['map'](_0x1f5209,function(_0x1f7122){if(_0x1f7122[_0x8f95('0x2c')]!=='VIRTUAL'){var _0x2b230d={};_0x2b230d[_0x1f7122[_0x8f95('0x27')]]={'$like':'%'+_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x3b')]+'%'};return _0x2b230d;}})});}_0xdff317=_['merge']({},_0xdff317,_0x4941d1[_0x8f95('0x3d')]);var _0x53d79f={'where':_0xdff317[_0x8f95('0x3c')]};return db[_0x8f95('0x2b')][_0x8f95('0x1e')](_0x53d79f)[_0x8f95('0x25')](function(_0xa10690){_0x52d63b[_0x8f95('0x1e')]=_0xa10690;if(_0x4941d1[_0x8f95('0x2e')][_0x8f95('0x3e')]){_0xdff317[_0x8f95('0x3f')]=[{'all':!![]}];}return db[_0x8f95('0x2b')][_0x8f95('0x40')](_0xdff317);})[_0x8f95('0x25')](function(_0x2b1fff){_0x52d63b[_0x8f95('0x41')]=_0x2b1fff;return _0x52d63b;})[_0x8f95('0x25')](respondWithFilteredResult(_0x401c4d,_0xdff317))['catch'](handleError(_0x401c4d,null));};exports[_0x8f95('0x42')]=function(_0xfd87f5,_0x4e43d6){var _0x2aa35c={'raw':![],'where':{'id':_0xfd87f5[_0x8f95('0x43')]['id']}},_0x5ba117={};_0x5ba117[_0x8f95('0x32')]=_[_0x8f95('0x2f')](db[_0x8f95('0x2b')]['rawAttributes']);_0x5ba117['query']=_[_0x8f95('0x2f')](_0xfd87f5[_0x8f95('0x2e')]);_0x5ba117['filters']=_[_0x8f95('0x31')](_0x5ba117[_0x8f95('0x32')],_0x5ba117[_0x8f95('0x2e')]);_0x2aa35c[_0x8f95('0x33')]=_[_0x8f95('0x31')](_0x5ba117[_0x8f95('0x32')],qs[_0x8f95('0x34')](_0xfd87f5[_0x8f95('0x2e')][_0x8f95('0x34')]));_0x2aa35c[_0x8f95('0x33')]=_0x2aa35c[_0x8f95('0x33')][_0x8f95('0x35')]?_0x2aa35c[_0x8f95('0x33')]:_0x5ba117['model'];if(_0xfd87f5[_0x8f95('0x2e')][_0x8f95('0x3e')]){_0x2aa35c[_0x8f95('0x3f')]=[{'all':!![]}];}_0x2aa35c=_[_0x8f95('0x44')]({},_0x2aa35c,_0xfd87f5[_0x8f95('0x3d')]);return db[_0x8f95('0x2b')]['find'](_0x2aa35c)['then'](handleEntityNotFound(_0x4e43d6,null))['then'](respondWithResult(_0x4e43d6,null))[_0x8f95('0x45')](handleError(_0x4e43d6,null));};exports[_0x8f95('0x46')]=function(_0x96d618,_0x24d419){return db[_0x8f95('0x2b')][_0x8f95('0x46')](_0x96d618[_0x8f95('0x47')],{})['then'](respondWithResult(_0x24d419,0xc9))[_0x8f95('0x45')](handleError(_0x24d419,null));};exports['update']=function(_0xfa045d,_0x19b1f1){if(_0xfa045d[_0x8f95('0x47')]['id']){delete _0xfa045d[_0x8f95('0x47')]['id'];}return db[_0x8f95('0x2b')]['find']({'where':{'id':_0xfa045d[_0x8f95('0x43')]['id']}})['then'](handleEntityNotFound(_0x19b1f1,null))[_0x8f95('0x25')](saveUpdates(_0xfa045d['body'],null))[_0x8f95('0x25')](respondWithResult(_0x19b1f1,null))['catch'](handleError(_0x19b1f1,null));};exports[_0x8f95('0x24')]=function(_0x426aa4,_0x4b8cd9){return db[_0x8f95('0x2b')]['find']({'where':{'id':_0x426aa4[_0x8f95('0x43')]['id']}})[_0x8f95('0x25')](handleEntityNotFound(_0x4b8cd9,null))['then'](removeEntity(_0x4b8cd9,null))[_0x8f95('0x45')](handleError(_0x4b8cd9,null));};exports['getFields']=function(_0x587a11,_0x24b618,_0xaa0945){var _0xe2a725={};var _0x1036ba={};var _0xf1a662;var _0x3e87f3;return db[_0x8f95('0x2b')][_0x8f95('0x48')]({'where':{'id':_0x587a11['params']['id']}})[_0x8f95('0x25')](handleEntityNotFound(_0x24b618,null))[_0x8f95('0x25')](function(_0x41eacd){if(_0x41eacd){_0xf1a662=_0x41eacd;_0x1036ba[_0x8f95('0x32')]=_[_0x8f95('0x2f')](db['SalesforceField'][_0x8f95('0x49')]);_0x1036ba[_0x8f95('0x2e')]=_[_0x8f95('0x2f')](_0x587a11[_0x8f95('0x2e')]);_0x1036ba['filters']=_['intersection'](_0x1036ba[_0x8f95('0x32')],_0x1036ba[_0x8f95('0x2e')]);_0xe2a725['attributes']=_['intersection'](_0x1036ba[_0x8f95('0x32')],qs[_0x8f95('0x34')](_0x587a11[_0x8f95('0x2e')]['fields']));_0xe2a725[_0x8f95('0x33')]=_0xe2a725['attributes'][_0x8f95('0x35')]?_0xe2a725[_0x8f95('0x33')]:_0x1036ba[_0x8f95('0x32')];_0xe2a725['order']=qs['sort'](_0x587a11[_0x8f95('0x2e')][_0x8f95('0x39')]);_0xe2a725[_0x8f95('0x3c')]=qs[_0x8f95('0x30')](_['pick'](_0x587a11[_0x8f95('0x2e')],_0x1036ba['filters']));if(_0x587a11[_0x8f95('0x2e')][_0x8f95('0x3b')]){_0xe2a725[_0x8f95('0x3c')]=_[_0x8f95('0x44')](_0xe2a725['where'],{'$or':_[_0x8f95('0x2a')](_0xe2a725['attributes'],function(_0x338c85){var _0x2280c5={};_0x2280c5[_0x338c85]={'$like':'%'+_0x587a11[_0x8f95('0x2e')][_0x8f95('0x3b')]+'%'};return _0x2280c5;})});}_0xe2a725=_[_0x8f95('0x44')]({},_0xe2a725,_0x587a11[_0x8f95('0x3d')]);return _0xf1a662[_0x8f95('0x4a')](_0xe2a725);}})[_0x8f95('0x25')](function(_0x5a5b62){if(_0x5a5b62){_0x3e87f3=_0x5a5b62[_0x8f95('0x35')];if(!_0x587a11[_0x8f95('0x2e')]['hasOwnProperty'](_0x8f95('0x37'))){_0xe2a725['limit']=qs[_0x8f95('0x1d')](_0x587a11['query'][_0x8f95('0x1d')]);_0xe2a725['offset']=qs['offset'](_0x587a11[_0x8f95('0x2e')][_0x8f95('0x1b')]);}return _0xf1a662[_0x8f95('0x4a')](_0xe2a725);}})[_0x8f95('0x25')](function(_0x2b01f2){if(_0x2b01f2){return _0x2b01f2?{'count':_0x3e87f3,'rows':_0x2b01f2}:null;}})[_0x8f95('0x25')](respondWithResult(_0x24b618,null))[_0x8f95('0x45')](handleError(_0x24b618,null));};exports[_0x8f95('0x4b')]=function(_0x45e5a9,_0x12f043,_0x505228){var _0x2f9ab6={};var _0x32e1dd={};var _0x5609d9;var _0x245ee8;return db[_0x8f95('0x2b')][_0x8f95('0x48')]({'where':{'id':_0x45e5a9[_0x8f95('0x43')]['id']}})['then'](handleEntityNotFound(_0x12f043,null))[_0x8f95('0x25')](function(_0x42300c){if(_0x42300c){_0x5609d9=_0x42300c;_0x32e1dd[_0x8f95('0x32')]=_[_0x8f95('0x2f')](db['SalesforceField'][_0x8f95('0x49')]);_0x32e1dd[_0x8f95('0x2e')]=_['keys'](_0x45e5a9[_0x8f95('0x2e')]);_0x32e1dd[_0x8f95('0x30')]=_[_0x8f95('0x31')](_0x32e1dd[_0x8f95('0x32')],_0x32e1dd[_0x8f95('0x2e')]);_0x2f9ab6[_0x8f95('0x33')]=_[_0x8f95('0x31')](_0x32e1dd[_0x8f95('0x32')],qs[_0x8f95('0x34')](_0x45e5a9[_0x8f95('0x2e')][_0x8f95('0x34')]));_0x2f9ab6[_0x8f95('0x33')]=_0x2f9ab6[_0x8f95('0x33')][_0x8f95('0x35')]?_0x2f9ab6[_0x8f95('0x33')]:_0x32e1dd[_0x8f95('0x32')];_0x2f9ab6[_0x8f95('0x38')]=qs['sort'](_0x45e5a9[_0x8f95('0x2e')]['sort']);_0x2f9ab6[_0x8f95('0x3c')]=qs[_0x8f95('0x30')](_['pick'](_0x45e5a9[_0x8f95('0x2e')],_0x32e1dd['filters']));if(_0x45e5a9[_0x8f95('0x2e')][_0x8f95('0x3b')]){_0x2f9ab6[_0x8f95('0x3c')]=_[_0x8f95('0x44')](_0x2f9ab6[_0x8f95('0x3c')],{'$or':_['map'](_0x2f9ab6['attributes'],function(_0x3ad945){var _0xbb9ee={};_0xbb9ee[_0x3ad945]={'$like':'%'+_0x45e5a9[_0x8f95('0x2e')]['filter']+'%'};return _0xbb9ee;})});}_0x2f9ab6=_[_0x8f95('0x44')]({},_0x2f9ab6,_0x45e5a9[_0x8f95('0x3d')]);return _0x5609d9[_0x8f95('0x4b')](_0x2f9ab6);}})['then'](function(_0x1fe057){if(_0x1fe057){_0x245ee8=_0x1fe057[_0x8f95('0x35')];if(!_0x45e5a9[_0x8f95('0x2e')][_0x8f95('0x36')](_0x8f95('0x37'))){_0x2f9ab6['limit']=qs[_0x8f95('0x1d')](_0x45e5a9[_0x8f95('0x2e')][_0x8f95('0x1d')]);_0x2f9ab6['offset']=qs['offset'](_0x45e5a9[_0x8f95('0x2e')][_0x8f95('0x1b')]);}return _0x5609d9[_0x8f95('0x4b')](_0x2f9ab6);}})[_0x8f95('0x25')](function(_0x2f5339){if(_0x2f5339){return _0x2f5339?{'count':_0x245ee8,'rows':_0x2f5339}:null;}})['then'](respondWithResult(_0x12f043,null))[_0x8f95('0x45')](handleError(_0x12f043,null));};exports[_0x8f95('0x4c')]=function(_0x5a48e8,_0x38cc05,_0x2f5b23){var _0x4d5cee={};var _0x16ad60={};var _0x3e8292;var _0x3df8b3;return db[_0x8f95('0x2b')][_0x8f95('0x48')]({'where':{'id':_0x5a48e8[_0x8f95('0x43')]['id']}})['then'](handleEntityNotFound(_0x38cc05,null))[_0x8f95('0x25')](function(_0x40875d){if(_0x40875d){_0x3e8292=_0x40875d;_0x16ad60[_0x8f95('0x32')]=_[_0x8f95('0x2f')](db['SalesforceField'][_0x8f95('0x49')]);_0x16ad60[_0x8f95('0x2e')]=_['keys'](_0x5a48e8['query']);_0x16ad60[_0x8f95('0x30')]=_[_0x8f95('0x31')](_0x16ad60[_0x8f95('0x32')],_0x16ad60[_0x8f95('0x2e')]);_0x4d5cee[_0x8f95('0x33')]=_[_0x8f95('0x31')](_0x16ad60[_0x8f95('0x32')],qs[_0x8f95('0x34')](_0x5a48e8['query'][_0x8f95('0x34')]));_0x4d5cee['attributes']=_0x4d5cee[_0x8f95('0x33')][_0x8f95('0x35')]?_0x4d5cee['attributes']:_0x16ad60[_0x8f95('0x32')];_0x4d5cee[_0x8f95('0x38')]=qs['sort'](_0x5a48e8[_0x8f95('0x2e')]['sort']);_0x4d5cee[_0x8f95('0x3c')]=qs[_0x8f95('0x30')](_['pick'](_0x5a48e8[_0x8f95('0x2e')],_0x16ad60[_0x8f95('0x30')]));if(_0x5a48e8[_0x8f95('0x2e')][_0x8f95('0x3b')]){_0x4d5cee['where']=_[_0x8f95('0x44')](_0x4d5cee[_0x8f95('0x3c')],{'$or':_['map'](_0x4d5cee[_0x8f95('0x33')],function(_0x444ffd){var _0x453a61={};_0x453a61[_0x444ffd]={'$like':'%'+_0x5a48e8[_0x8f95('0x2e')][_0x8f95('0x3b')]+'%'};return _0x453a61;})});}_0x4d5cee=_[_0x8f95('0x44')]({},_0x4d5cee,_0x5a48e8['options']);return _0x3e8292['getDescriptions'](_0x4d5cee);}})[_0x8f95('0x25')](function(_0xa0954d){if(_0xa0954d){_0x3df8b3=_0xa0954d[_0x8f95('0x35')];if(!_0x5a48e8[_0x8f95('0x2e')][_0x8f95('0x36')](_0x8f95('0x37'))){_0x4d5cee[_0x8f95('0x1d')]=qs['limit'](_0x5a48e8[_0x8f95('0x2e')][_0x8f95('0x1d')]);_0x4d5cee[_0x8f95('0x1b')]=qs[_0x8f95('0x1b')](_0x5a48e8[_0x8f95('0x2e')][_0x8f95('0x1b')]);}return _0x3e8292[_0x8f95('0x4c')](_0x4d5cee);}})[_0x8f95('0x25')](function(_0x434a63){if(_0x434a63){return _0x434a63?{'count':_0x3df8b3,'rows':_0x434a63}:null;}})[_0x8f95('0x25')](respondWithResult(_0x38cc05,null))[_0x8f95('0x45')](handleError(_0x38cc05,null));};