7a8c968e84a24d52ebc10fbcd65634cd2624def7
[motion2.git] / server / api / intSalesforceAccount / intSalesforceAccount.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 _0x68cf=['VIRTUAL','options','includeAll','include','SalesforceAccount','findAll','rows','params','intersection','find','catch','create','body','destroy','getConfigurations','findOne','SalesforceConfiguration','AccountId','getSubjects','channel','Descriptions','getDescriptions','SalesforceField','remoteUri','clientId','clientSecret','username','slice','substring','lastIndexOf','Connection','login','password','concat','securityToken','isNil','ticketType','sobject','describe','OwnerId','ActivityDate','IsRecurrence','Subject','Description','string','int','picklist','boolean','email','textarea','phone','includes','label','custom','picklistValues','value','true','False','false','test','statusCode','error','getFields','stringify','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','util','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../components/integrations/configuration','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','save','update','then','sendStatus','stack','name','send','map','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','fields','length','nolimit','order','sort','where','pick','filter','merge'];(function(_0xe833cc,_0x467d44){var _0x3ac7d6=function(_0x138911){while(--_0x138911){_0xe833cc['push'](_0xe833cc['shift']());}};_0x3ac7d6(++_0x467d44);}(_0x68cf,0x1b8));var _0xf68c=function(_0x1f3637,_0x14d11f){_0x1f3637=_0x1f3637-0x0;var _0x536456=_0x68cf[_0x1f3637];return _0x536456;};'use strict';var emlformat=require(_0xf68c('0x0'));var rimraf=require(_0xf68c('0x1'));var zipdir=require(_0xf68c('0x2'));var jsonpatch=require(_0xf68c('0x3'));var rp=require(_0xf68c('0x4'));var moment=require(_0xf68c('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xf68c('0x6'));var path=require('path');var sox=require('sox');var csv=require(_0xf68c('0x7'));var ejs=require(_0xf68c('0x8'));var fs=require('fs');var fs_extra=require(_0xf68c('0x9'));var _=require(_0xf68c('0xa'));var squel=require(_0xf68c('0xb'));var crypto=require('crypto');var jsforce=require(_0xf68c('0xc'));var deskjs=require(_0xf68c('0xd'));var toCsv=require(_0xf68c('0x7'));var querystring=require(_0xf68c('0xe'));var Papa=require('papaparse');var Redis=require(_0xf68c('0xf'));var authService=require(_0xf68c('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xf68c('0x11'));var hardwareService=require(_0xf68c('0x12'));var logger=require(_0xf68c('0x13'))(_0xf68c('0x14'));var utils=require(_0xf68c('0x15'));var config=require(_0xf68c('0x16'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];var integrations=require(_0xf68c('0x17'));function respondWithStatusCode(_0x158dda,_0x2f764c){_0x2f764c=_0x2f764c||0xcc;return function(_0xac0e19){if(_0xac0e19){return _0x158dda['sendStatus'](_0x2f764c);}return _0x158dda[_0xf68c('0x18')](_0x2f764c)[_0xf68c('0x19')]();};}function respondWithResult(_0x349551,_0xeb709d){_0xeb709d=_0xeb709d||0xc8;return function(_0x39098c){if(_0x39098c){return _0x349551['status'](_0xeb709d)[_0xf68c('0x1a')](_0x39098c);}};}function respondWithFilteredResult(_0x591993,_0x1e5409){return function(_0x424f27){if(_0x424f27){var _0x482f4c=typeof _0x1e5409[_0xf68c('0x1b')]===_0xf68c('0x1c')&&typeof _0x1e5409[_0xf68c('0x1d')]===_0xf68c('0x1c');var _0x4b033a=_0x424f27[_0xf68c('0x1e')];var _0x14b7fd=_0x482f4c?0x0:_0x1e5409[_0xf68c('0x1b')];var _0x45e629=_0x482f4c?_0x424f27[_0xf68c('0x1e')]:_0x1e5409['offset']+_0x1e5409[_0xf68c('0x1d')];var _0x199b9c;if(_0x45e629>=_0x4b033a){_0x45e629=_0x4b033a;_0x199b9c=0xc8;}else{_0x199b9c=0xce;}_0x591993[_0xf68c('0x18')](_0x199b9c);return _0x591993[_0xf68c('0x1f')](_0xf68c('0x20'),_0x14b7fd+'-'+_0x45e629+'/'+_0x4b033a)[_0xf68c('0x1a')](_0x424f27);}return null;};}function patchUpdates(_0x214d94){return function(_0x57608a){try{jsonpatch[_0xf68c('0x21')](_0x57608a,_0x214d94,!![]);}catch(_0x397509){return BPromise['reject'](_0x397509);}return _0x57608a[_0xf68c('0x22')]();};}function saveUpdates(_0x5845ca,_0x2d8d5e){return function(_0x191eb1){if(_0x191eb1){return _0x191eb1[_0xf68c('0x23')](_0x5845ca)[_0xf68c('0x24')](function(_0x52b635){return _0x52b635;});}return null;};}function removeEntity(_0x1b364a,_0x16f212){return function(_0x4f7952){if(_0x4f7952){return _0x4f7952['destroy']()[_0xf68c('0x24')](function(){_0x1b364a[_0xf68c('0x18')](0xcc)[_0xf68c('0x19')]();});}};}function handleEntityNotFound(_0x5ecafb,_0x1c86b7){return function(_0x28725a){if(!_0x28725a){_0x5ecafb[_0xf68c('0x25')](0x194);}return _0x28725a;};}function handleError(_0x38d150,_0x3b27bc){_0x3b27bc=_0x3b27bc||0x1f4;return function(_0x28eb23){logger['error'](_0x28eb23[_0xf68c('0x26')]);if(_0x28eb23[_0xf68c('0x27')]){delete _0x28eb23[_0xf68c('0x27')];}_0x38d150[_0xf68c('0x18')](_0x3b27bc)[_0xf68c('0x28')](_0x28eb23);};}exports['index']=function(_0xb81a92,_0x2eb682){var _0x2427e0={},_0x1f2414={},_0xbd47e={'count':0x0,'rows':[]};var _0x1a13ef=_[_0xf68c('0x29')](db['SalesforceAccount'][_0xf68c('0x2a')],function(_0x45e982){return{'name':_0x45e982[_0xf68c('0x2b')],'type':_0x45e982[_0xf68c('0x2c')][_0xf68c('0x2d')]};});_0x1f2414[_0xf68c('0x2e')]=_[_0xf68c('0x29')](_0x1a13ef,_0xf68c('0x27'));_0x1f2414[_0xf68c('0x2f')]=_[_0xf68c('0x30')](_0xb81a92[_0xf68c('0x2f')]);_0x1f2414[_0xf68c('0x31')]=_['intersection'](_0x1f2414['model'],_0x1f2414[_0xf68c('0x2f')]);_0x2427e0[_0xf68c('0x32')]=_['intersection'](_0x1f2414[_0xf68c('0x2e')],qs[_0xf68c('0x33')](_0xb81a92[_0xf68c('0x2f')][_0xf68c('0x33')]));_0x2427e0[_0xf68c('0x32')]=_0x2427e0['attributes'][_0xf68c('0x34')]?_0x2427e0[_0xf68c('0x32')]:_0x1f2414[_0xf68c('0x2e')];if(!_0xb81a92[_0xf68c('0x2f')]['hasOwnProperty'](_0xf68c('0x35'))){_0x2427e0['limit']=qs[_0xf68c('0x1d')](_0xb81a92[_0xf68c('0x2f')][_0xf68c('0x1d')]);_0x2427e0[_0xf68c('0x1b')]=qs[_0xf68c('0x1b')](_0xb81a92[_0xf68c('0x2f')][_0xf68c('0x1b')]);}_0x2427e0[_0xf68c('0x36')]=qs[_0xf68c('0x37')](_0xb81a92['query'][_0xf68c('0x37')]);_0x2427e0[_0xf68c('0x38')]=qs[_0xf68c('0x31')](_[_0xf68c('0x39')](_0xb81a92[_0xf68c('0x2f')],_0x1f2414[_0xf68c('0x31')]),_0x1a13ef);if(_0xb81a92[_0xf68c('0x2f')][_0xf68c('0x3a')]){_0x2427e0[_0xf68c('0x38')]=_[_0xf68c('0x3b')](_0x2427e0[_0xf68c('0x38')],{'$or':_[_0xf68c('0x29')](_0x1a13ef,function(_0x2df273){if(_0x2df273['type']!==_0xf68c('0x3c')){var _0x90e6d6={};_0x90e6d6[_0x2df273[_0xf68c('0x27')]]={'$like':'%'+_0xb81a92[_0xf68c('0x2f')]['filter']+'%'};return _0x90e6d6;}})});}_0x2427e0=_['merge']({},_0x2427e0,_0xb81a92[_0xf68c('0x3d')]);var _0x1a6f59={'where':_0x2427e0[_0xf68c('0x38')]};return db['SalesforceAccount'][_0xf68c('0x1e')](_0x1a6f59)['then'](function(_0x320470){_0xbd47e[_0xf68c('0x1e')]=_0x320470;if(_0xb81a92[_0xf68c('0x2f')][_0xf68c('0x3e')]){_0x2427e0[_0xf68c('0x3f')]=[{'all':!![]}];}return db[_0xf68c('0x40')][_0xf68c('0x41')](_0x2427e0);})['then'](function(_0x2b63d2){_0xbd47e[_0xf68c('0x42')]=_0x2b63d2;return _0xbd47e;})[_0xf68c('0x24')](respondWithFilteredResult(_0x2eb682,_0x2427e0))['catch'](handleError(_0x2eb682,null));};exports['show']=function(_0x2b13d2,_0x174f24){var _0xeee805={'raw':![],'where':{'id':_0x2b13d2[_0xf68c('0x43')]['id']}},_0x2716a4={};_0x2716a4['model']=_[_0xf68c('0x30')](db[_0xf68c('0x40')][_0xf68c('0x2a')]);_0x2716a4[_0xf68c('0x2f')]=_[_0xf68c('0x30')](_0x2b13d2[_0xf68c('0x2f')]);_0x2716a4[_0xf68c('0x31')]=_[_0xf68c('0x44')](_0x2716a4[_0xf68c('0x2e')],_0x2716a4[_0xf68c('0x2f')]);_0xeee805[_0xf68c('0x32')]=_[_0xf68c('0x44')](_0x2716a4[_0xf68c('0x2e')],qs[_0xf68c('0x33')](_0x2b13d2['query'][_0xf68c('0x33')]));_0xeee805[_0xf68c('0x32')]=_0xeee805[_0xf68c('0x32')][_0xf68c('0x34')]?_0xeee805[_0xf68c('0x32')]:_0x2716a4['model'];if(_0x2b13d2[_0xf68c('0x2f')][_0xf68c('0x3e')]){_0xeee805[_0xf68c('0x3f')]=[{'all':!![]}];}_0xeee805=_[_0xf68c('0x3b')]({},_0xeee805,_0x2b13d2[_0xf68c('0x3d')]);return db[_0xf68c('0x40')][_0xf68c('0x45')](_0xeee805)[_0xf68c('0x24')](handleEntityNotFound(_0x174f24,null))[_0xf68c('0x24')](respondWithResult(_0x174f24,null))[_0xf68c('0x46')](handleError(_0x174f24,null));};exports['create']=function(_0x571f49,_0x5c08b6){return db[_0xf68c('0x40')][_0xf68c('0x47')](_0x571f49[_0xf68c('0x48')],{})[_0xf68c('0x24')](respondWithResult(_0x5c08b6,0xc9))[_0xf68c('0x46')](handleError(_0x5c08b6,null));};exports['update']=function(_0x41fd3f,_0x333a4d){if(_0x41fd3f[_0xf68c('0x48')]['id']){delete _0x41fd3f[_0xf68c('0x48')]['id'];}return db[_0xf68c('0x40')][_0xf68c('0x45')]({'where':{'id':_0x41fd3f[_0xf68c('0x43')]['id']}})[_0xf68c('0x24')](handleEntityNotFound(_0x333a4d,null))['then'](saveUpdates(_0x41fd3f['body'],null))['then'](respondWithResult(_0x333a4d,null))[_0xf68c('0x46')](handleError(_0x333a4d,null));};exports[_0xf68c('0x49')]=function(_0x153470,_0x16949a){return db[_0xf68c('0x40')][_0xf68c('0x45')]({'where':{'id':_0x153470[_0xf68c('0x43')]['id']}})[_0xf68c('0x24')](handleEntityNotFound(_0x16949a,null))[_0xf68c('0x24')](removeEntity(_0x16949a,null))[_0xf68c('0x46')](handleError(_0x16949a,null));};exports[_0xf68c('0x4a')]=function(_0x1a0155,_0x513744,_0x44c469){var _0x36e6e2={};var _0x139b8a={};var _0x4e86c5;var _0x1a27b5;return db[_0xf68c('0x40')][_0xf68c('0x4b')]({'where':{'id':_0x1a0155[_0xf68c('0x43')]['id']}})[_0xf68c('0x24')](handleEntityNotFound(_0x513744,null))[_0xf68c('0x24')](function(_0x4e2a4e){if(_0x4e2a4e){_0x4e86c5=_0x4e2a4e;_0x139b8a['model']=_[_0xf68c('0x30')](db[_0xf68c('0x4c')][_0xf68c('0x2a')]);_0x139b8a[_0xf68c('0x2f')]=_[_0xf68c('0x30')](_0x1a0155[_0xf68c('0x2f')]);_0x139b8a[_0xf68c('0x31')]=_[_0xf68c('0x44')](_0x139b8a[_0xf68c('0x2e')],_0x139b8a[_0xf68c('0x2f')]);_0x36e6e2[_0xf68c('0x32')]=_[_0xf68c('0x44')](_0x139b8a[_0xf68c('0x2e')],qs['fields'](_0x1a0155[_0xf68c('0x2f')][_0xf68c('0x33')]));_0x36e6e2[_0xf68c('0x32')]=_0x36e6e2[_0xf68c('0x32')][_0xf68c('0x34')]?_0x36e6e2[_0xf68c('0x32')]:_0x139b8a[_0xf68c('0x2e')];_0x36e6e2[_0xf68c('0x36')]=qs[_0xf68c('0x37')](_0x1a0155['query'][_0xf68c('0x37')]);_0x36e6e2[_0xf68c('0x38')]=qs[_0xf68c('0x31')](_[_0xf68c('0x39')](_0x1a0155[_0xf68c('0x2f')],_0x139b8a[_0xf68c('0x31')]));if(_0x1a0155[_0xf68c('0x2f')]['filter']){_0x36e6e2[_0xf68c('0x38')]=_[_0xf68c('0x3b')](_0x36e6e2['where'],{'$or':_[_0xf68c('0x29')](_0x36e6e2[_0xf68c('0x32')],function(_0x1d589c){var _0x50090b={};_0x50090b[_0x1d589c]={'$like':'%'+_0x1a0155[_0xf68c('0x2f')][_0xf68c('0x3a')]+'%'};return _0x50090b;})});}_0x36e6e2=_['merge']({},_0x36e6e2,_0x1a0155[_0xf68c('0x3d')]);return _0x4e86c5[_0xf68c('0x4a')](_0x36e6e2);}})[_0xf68c('0x24')](function(_0x4d0a87){if(_0x4d0a87){_0x1a27b5=_0x4d0a87[_0xf68c('0x34')];if(!_0x1a0155[_0xf68c('0x2f')]['hasOwnProperty'](_0xf68c('0x35'))){_0x36e6e2[_0xf68c('0x1d')]=qs[_0xf68c('0x1d')](_0x1a0155[_0xf68c('0x2f')][_0xf68c('0x1d')]);_0x36e6e2['offset']=qs['offset'](_0x1a0155[_0xf68c('0x2f')][_0xf68c('0x1b')]);}return _0x4e86c5[_0xf68c('0x4a')](_0x36e6e2);}})[_0xf68c('0x24')](function(_0x55abab){if(_0x55abab){return _0x55abab?{'count':_0x1a27b5,'rows':_0x55abab}:null;}})[_0xf68c('0x24')](respondWithResult(_0x513744,null))['catch'](handleError(_0x513744,null));};exports['addConfiguration']=function(_0x7c0796,_0x12e81a,_0x52edac){if(_0x7c0796[_0xf68c('0x48')]['id']){delete _0x7c0796[_0xf68c('0x48')]['id'];}return db[_0xf68c('0x40')][_0xf68c('0x4b')]({'where':{'id':_0x7c0796[_0xf68c('0x43')]['id']}})[_0xf68c('0x24')](handleEntityNotFound(_0x12e81a,null))[_0xf68c('0x24')](function(_0x200de5){if(_0x200de5){_0x7c0796[_0xf68c('0x48')][_0xf68c('0x4d')]=_0x200de5['id'];_0x7c0796[_0xf68c('0x48')]['Subjects']=integrations[_0xf68c('0x4e')](_0x7c0796['body'][_0xf68c('0x4f')],_0x7c0796[_0xf68c('0x48')][_0xf68c('0x2c')]);_0x7c0796[_0xf68c('0x48')][_0xf68c('0x50')]=integrations[_0xf68c('0x51')](_0x7c0796[_0xf68c('0x48')][_0xf68c('0x4f')],_0x7c0796[_0xf68c('0x48')][_0xf68c('0x2c')]);return db[_0xf68c('0x4c')][_0xf68c('0x47')](_0x7c0796[_0xf68c('0x48')],{'include':[{'model':db[_0xf68c('0x52')],'as':'Subjects'},{'model':db['SalesforceField'],'as':'Descriptions'}]});}return null;})[_0xf68c('0x24')](respondWithResult(_0x12e81a,null))[_0xf68c('0x46')](handleError(_0x12e81a,null));};exports['getFields']=function(_0x4f9a69,_0x20c213,_0x5d74af){var _0x59f990;var _0xe6828a='';var _0x51d644='Task';return db[_0xf68c('0x40')][_0xf68c('0x4b')]({'where':{'id':_0x4f9a69[_0xf68c('0x43')]['id']},'attributes':['id',_0xf68c('0x53'),_0xf68c('0x54'),_0xf68c('0x55'),_0xf68c('0x56'),'password','securityToken']})[_0xf68c('0x24')](handleEntityNotFound(_0x20c213,null))['then'](function(_0x526325){if(_0x526325){_0xe6828a=_0x526325[_0xf68c('0x53')];var _0x2d9026=_0xe6828a[_0xf68c('0x57')](-0x1);if(_0x2d9026==='/'){_0xe6828a=_0xe6828a[_0xf68c('0x58')](0x0,_0xe6828a[_0xf68c('0x59')](_0x2d9026));}_0x59f990=new jsforce[(_0xf68c('0x5a'))]({'oauth2':{'loginUrl':_0xe6828a,'clientId':_0x526325['clientId'],'clientSecret':_0x526325[_0xf68c('0x55')]}});return _0x59f990[_0xf68c('0x5b')](_0x526325['username'],_0x526325[_0xf68c('0x5c')][_0xf68c('0x5d')](_0x526325[_0xf68c('0x5e')]));}})[_0xf68c('0x24')](function(_0x4e40a3){if(_0x4e40a3){if(!_[_0xf68c('0x5f')](_0x4f9a69['query']['ticketType'])){_0x51d644=_0x4f9a69[_0xf68c('0x2f')][_0xf68c('0x60')];}return _0x59f990[_0xf68c('0x61')](_0x51d644)[_0xf68c('0x62')]();}})[_0xf68c('0x24')](function(_0x4c5b0d){var _0xe4675b=[_0xf68c('0x63'),_0xf68c('0x64'),'IsReminderSet',_0xf68c('0x65'),_0xf68c('0x66'),_0xf68c('0x67')];var _0x3912da=[_0xf68c('0x68'),_0xf68c('0x69'),_0xf68c('0x6a'),_0xf68c('0x6b'),_0xf68c('0x6c'),_0xf68c('0x6d'),_0xf68c('0x6e')];if(_0x4c5b0d){_['remove'](_0x4c5b0d[_0xf68c('0x33')],function(_0x40da61){return _0xe4675b[_0xf68c('0x6f')](_0x40da61[_0xf68c('0x27')])||!_0x3912da[_0xf68c('0x6f')](_0x40da61['type']);});return{'count':_0x4c5b0d['fields']['length'],'rows':_[_0xf68c('0x29')](_0x4c5b0d[_0xf68c('0x33')],function(_0x52e68e){return{'id':_0x52e68e['name'],'name':_0x52e68e[_0xf68c('0x70')],'custom':_0x52e68e[_0xf68c('0x71')],'options':_0x52e68e['picklistValues'][_0xf68c('0x34')]>0x0?_[_0xf68c('0x29')](_0x52e68e[_0xf68c('0x72')],function(_0x3d6b82){return{'name':_0x3d6b82['label'],'value':_0x3d6b82[_0xf68c('0x73')]};}):_0x52e68e['type']==_0xf68c('0x6b')?[{'name':'True','value':_0xf68c('0x74')},{'name':_0xf68c('0x75'),'value':_0xf68c('0x76')}]:[]};})};}})[_0xf68c('0x24')](respondWithResult(_0x20c213,null))[_0xf68c('0x46')](function(_0xd35178){var _0x184063=_0x4f9a69[_0xf68c('0x2f')][_0xf68c('0x77')]?0x1f4:_0xd35178[_0xf68c('0x78')]||0x1f4;logger[_0xf68c('0x79')]('getFields,\x20%s,\x20%s,\x20%s',_0xf68c('0x7a'),_0x184063,JSON[_0xf68c('0x7b')](_0xd35178));delete _0xd35178[_0xf68c('0x27')];if(_0x184063===0x191){_0x184063=0x190;}_0x20c213[_0xf68c('0x18')](_0x184063)['send'](_0x4f9a69[_0xf68c('0x2f')][_0xf68c('0x77')]?{'message':_0xf68c('0x7c'),'statusCode':_0xd35178[_0xf68c('0x78')]}:_0xd35178);});};