a43fc71eff11171047478167483243eba61f2471
[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 _0xc3ea=['stringify','Wrong\x20credentials','statusCode','eml-format','fast-json-patch','bluebird','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','save','then','destroy','error','name','send','index','map','SalesforceAccount','fieldName','type','key','intersection','model','query','length','attributes','hasOwnProperty','nolimit','order','sort','where','pick','filters','filter','merge','includeAll','include','rows','catch','rawAttributes','keys','fields','find','create','body','update','params','getConfigurations','findOne','SalesforceConfiguration','options','addConfiguration','getSubjects','channel','Descriptions','SalesforceField','getFields','clientSecret','password','securityToken','remoteUri','slice','lastIndexOf','Connection','clientId','username','concat','isNil','ticketType','OwnerId','ActivityDate','IsReminderSet','IsRecurrence','Subject','Description','string','picklist','boolean','textarea','remove','includes','picklistValues','value','True','test','getFields,\x20%s,\x20%s,\x20%s'];(function(_0x1c3370,_0xf617e8){var _0xa9b358=function(_0x39cb44){while(--_0x39cb44){_0x1c3370['push'](_0x1c3370['shift']());}};_0xa9b358(++_0xf617e8);}(_0xc3ea,0x75));var _0xac3e=function(_0x44b827,_0x1bb1aa){_0x44b827=_0x44b827-0x0;var _0x527342=_0xc3ea[_0x44b827];return _0x527342;};'use strict';var emlformat=require(_0xac3e('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xac3e('0x1'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xac3e('0x2'));var Mustache=require('mustache');var util=require(_0xac3e('0x3'));var path=require(_0xac3e('0x4'));var sox=require(_0xac3e('0x5'));var csv=require(_0xac3e('0x6'));var ejs=require(_0xac3e('0x7'));var fs=require('fs');var fs_extra=require(_0xac3e('0x8'));var _=require(_0xac3e('0x9'));var squel=require(_0xac3e('0xa'));var crypto=require(_0xac3e('0xb'));var jsforce=require('jsforce');var deskjs=require(_0xac3e('0xc'));var toCsv=require(_0xac3e('0x6'));var querystring=require(_0xac3e('0xd'));var Papa=require(_0xac3e('0xe'));var Redis=require(_0xac3e('0xf'));var authService=require(_0xac3e('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xac3e('0x11'));var hardwareService=require(_0xac3e('0x12'));var logger=require('../../config/logger')('api');var utils=require(_0xac3e('0x13'));var config=require(_0xac3e('0x14'));var licenseUtil=require('../../config/license/util');var db=require(_0xac3e('0x15'))['db'];var integrations=require(_0xac3e('0x16'));function respondWithStatusCode(_0x1319dc,_0x48ab34){_0x48ab34=_0x48ab34||0xcc;return function(_0x2186e7){if(_0x2186e7){return _0x1319dc[_0xac3e('0x17')](_0x48ab34);}return _0x1319dc[_0xac3e('0x18')](_0x48ab34)[_0xac3e('0x19')]();};}function respondWithResult(_0x58c96d,_0x7615ac){_0x7615ac=_0x7615ac||0xc8;return function(_0x14d841){if(_0x14d841){return _0x58c96d[_0xac3e('0x18')](_0x7615ac)[_0xac3e('0x1a')](_0x14d841);}};}function respondWithFilteredResult(_0x2e2a06,_0x3397c3){return function(_0xd00400){if(_0xd00400){var _0x1752a3=typeof _0x3397c3[_0xac3e('0x1b')]===_0xac3e('0x1c')&&typeof _0x3397c3[_0xac3e('0x1d')]===_0xac3e('0x1c');var _0x336c2a=_0xd00400[_0xac3e('0x1e')];var _0x1b3a62=_0x1752a3?0x0:_0x3397c3[_0xac3e('0x1b')];var _0x3854a3=_0x1752a3?_0xd00400['count']:_0x3397c3['offset']+_0x3397c3['limit'];var _0x47de98;if(_0x3854a3>=_0x336c2a){_0x3854a3=_0x336c2a;_0x47de98=0xc8;}else{_0x47de98=0xce;}_0x2e2a06[_0xac3e('0x18')](_0x47de98);return _0x2e2a06[_0xac3e('0x1f')](_0xac3e('0x20'),_0x1b3a62+'-'+_0x3854a3+'/'+_0x336c2a)[_0xac3e('0x1a')](_0xd00400);}return null;};}function patchUpdates(_0x58b61d){return function(_0x32b6c1){try{jsonpatch[_0xac3e('0x21')](_0x32b6c1,_0x58b61d,!![]);}catch(_0x249a2f){return BPromise['reject'](_0x249a2f);}return _0x32b6c1[_0xac3e('0x22')]();};}function saveUpdates(_0x1c7e1d,_0x19ea42){return function(_0x259796){if(_0x259796){return _0x259796['update'](_0x1c7e1d)[_0xac3e('0x23')](function(_0x3956fe){return _0x3956fe;});}return null;};}function removeEntity(_0x443e1a,_0x2ce4f9){return function(_0x46623d){if(_0x46623d){return _0x46623d[_0xac3e('0x24')]()[_0xac3e('0x23')](function(){_0x443e1a[_0xac3e('0x18')](0xcc)[_0xac3e('0x19')]();});}};}function handleEntityNotFound(_0x480bbd,_0x27864a){return function(_0x503d16){if(!_0x503d16){_0x480bbd[_0xac3e('0x17')](0x194);}return _0x503d16;};}function handleError(_0x4c6dd3,_0xc92f7a){_0xc92f7a=_0xc92f7a||0x1f4;return function(_0x50dbb5){logger[_0xac3e('0x25')](_0x50dbb5['stack']);if(_0x50dbb5[_0xac3e('0x26')]){delete _0x50dbb5[_0xac3e('0x26')];}_0x4c6dd3[_0xac3e('0x18')](_0xc92f7a)[_0xac3e('0x27')](_0x50dbb5);};}exports[_0xac3e('0x28')]=function(_0x8e1ec1,_0x436de1){var _0xe717e4={},_0x1c8358={},_0x2db99c={'count':0x0,'rows':[]};var _0x4db60f=_[_0xac3e('0x29')](db[_0xac3e('0x2a')]['rawAttributes'],function(_0x5e87e8){return{'name':_0x5e87e8[_0xac3e('0x2b')],'type':_0x5e87e8[_0xac3e('0x2c')][_0xac3e('0x2d')]};});_0x1c8358['model']=_[_0xac3e('0x29')](_0x4db60f,_0xac3e('0x26'));_0x1c8358['query']=_['keys'](_0x8e1ec1['query']);_0x1c8358['filters']=_[_0xac3e('0x2e')](_0x1c8358[_0xac3e('0x2f')],_0x1c8358[_0xac3e('0x30')]);_0xe717e4['attributes']=_[_0xac3e('0x2e')](_0x1c8358[_0xac3e('0x2f')],qs['fields'](_0x8e1ec1[_0xac3e('0x30')]['fields']));_0xe717e4['attributes']=_0xe717e4['attributes'][_0xac3e('0x31')]?_0xe717e4[_0xac3e('0x32')]:_0x1c8358[_0xac3e('0x2f')];if(!_0x8e1ec1[_0xac3e('0x30')][_0xac3e('0x33')](_0xac3e('0x34'))){_0xe717e4[_0xac3e('0x1d')]=qs[_0xac3e('0x1d')](_0x8e1ec1['query']['limit']);_0xe717e4[_0xac3e('0x1b')]=qs[_0xac3e('0x1b')](_0x8e1ec1[_0xac3e('0x30')]['offset']);}_0xe717e4[_0xac3e('0x35')]=qs[_0xac3e('0x36')](_0x8e1ec1['query'][_0xac3e('0x36')]);_0xe717e4[_0xac3e('0x37')]=qs['filters'](_[_0xac3e('0x38')](_0x8e1ec1[_0xac3e('0x30')],_0x1c8358[_0xac3e('0x39')]),_0x4db60f);if(_0x8e1ec1['query'][_0xac3e('0x3a')]){_0xe717e4[_0xac3e('0x37')]=_[_0xac3e('0x3b')](_0xe717e4[_0xac3e('0x37')],{'$or':_['map'](_0x4db60f,function(_0x40b48a){if(_0x40b48a[_0xac3e('0x2c')]!=='VIRTUAL'){var _0xd648b7={};_0xd648b7[_0x40b48a[_0xac3e('0x26')]]={'$like':'%'+_0x8e1ec1[_0xac3e('0x30')]['filter']+'%'};return _0xd648b7;}})});}_0xe717e4=_[_0xac3e('0x3b')]({},_0xe717e4,_0x8e1ec1['options']);var _0x390a92={'where':_0xe717e4[_0xac3e('0x37')]};return db[_0xac3e('0x2a')][_0xac3e('0x1e')](_0x390a92)['then'](function(_0x48fc48){_0x2db99c[_0xac3e('0x1e')]=_0x48fc48;if(_0x8e1ec1[_0xac3e('0x30')][_0xac3e('0x3c')]){_0xe717e4[_0xac3e('0x3d')]=[{'all':!![]}];}return db[_0xac3e('0x2a')]['findAll'](_0xe717e4);})[_0xac3e('0x23')](function(_0x35cdf5){_0x2db99c[_0xac3e('0x3e')]=_0x35cdf5;return _0x2db99c;})[_0xac3e('0x23')](respondWithFilteredResult(_0x436de1,_0xe717e4))[_0xac3e('0x3f')](handleError(_0x436de1,null));};exports['show']=function(_0x1082bd,_0x287a26){var _0x2b405a={'raw':![],'where':{'id':_0x1082bd['params']['id']}},_0x29a9ce={};_0x29a9ce['model']=_['keys'](db[_0xac3e('0x2a')][_0xac3e('0x40')]);_0x29a9ce[_0xac3e('0x30')]=_[_0xac3e('0x41')](_0x1082bd[_0xac3e('0x30')]);_0x29a9ce['filters']=_['intersection'](_0x29a9ce[_0xac3e('0x2f')],_0x29a9ce['query']);_0x2b405a[_0xac3e('0x32')]=_[_0xac3e('0x2e')](_0x29a9ce[_0xac3e('0x2f')],qs['fields'](_0x1082bd[_0xac3e('0x30')][_0xac3e('0x42')]));_0x2b405a[_0xac3e('0x32')]=_0x2b405a[_0xac3e('0x32')][_0xac3e('0x31')]?_0x2b405a[_0xac3e('0x32')]:_0x29a9ce[_0xac3e('0x2f')];if(_0x1082bd[_0xac3e('0x30')][_0xac3e('0x3c')]){_0x2b405a[_0xac3e('0x3d')]=[{'all':!![]}];}_0x2b405a=_[_0xac3e('0x3b')]({},_0x2b405a,_0x1082bd['options']);return db[_0xac3e('0x2a')][_0xac3e('0x43')](_0x2b405a)[_0xac3e('0x23')](handleEntityNotFound(_0x287a26,null))[_0xac3e('0x23')](respondWithResult(_0x287a26,null))[_0xac3e('0x3f')](handleError(_0x287a26,null));};exports[_0xac3e('0x44')]=function(_0x4773f8,_0x73c8bd){return db[_0xac3e('0x2a')]['create'](_0x4773f8[_0xac3e('0x45')],{})['then'](respondWithResult(_0x73c8bd,0xc9))['catch'](handleError(_0x73c8bd,null));};exports[_0xac3e('0x46')]=function(_0x4be133,_0x82cc70){if(_0x4be133[_0xac3e('0x45')]['id']){delete _0x4be133['body']['id'];}return db[_0xac3e('0x2a')][_0xac3e('0x43')]({'where':{'id':_0x4be133[_0xac3e('0x47')]['id']}})[_0xac3e('0x23')](handleEntityNotFound(_0x82cc70,null))[_0xac3e('0x23')](saveUpdates(_0x4be133[_0xac3e('0x45')],null))[_0xac3e('0x23')](respondWithResult(_0x82cc70,null))[_0xac3e('0x3f')](handleError(_0x82cc70,null));};exports['destroy']=function(_0x2e0dbd,_0x33d461){return db['SalesforceAccount']['find']({'where':{'id':_0x2e0dbd[_0xac3e('0x47')]['id']}})['then'](handleEntityNotFound(_0x33d461,null))['then'](removeEntity(_0x33d461,null))[_0xac3e('0x3f')](handleError(_0x33d461,null));};exports[_0xac3e('0x48')]=function(_0x24d3a5,_0x1f1327,_0x30f647){var _0x5939b5={};var _0x551283={};var _0x43c629;var _0x45d505;return db[_0xac3e('0x2a')][_0xac3e('0x49')]({'where':{'id':_0x24d3a5[_0xac3e('0x47')]['id']}})[_0xac3e('0x23')](handleEntityNotFound(_0x1f1327,null))[_0xac3e('0x23')](function(_0x42e05c){if(_0x42e05c){_0x43c629=_0x42e05c;_0x551283[_0xac3e('0x2f')]=_[_0xac3e('0x41')](db[_0xac3e('0x4a')]['rawAttributes']);_0x551283[_0xac3e('0x30')]=_['keys'](_0x24d3a5['query']);_0x551283[_0xac3e('0x39')]=_['intersection'](_0x551283['model'],_0x551283['query']);_0x5939b5[_0xac3e('0x32')]=_[_0xac3e('0x2e')](_0x551283['model'],qs['fields'](_0x24d3a5[_0xac3e('0x30')][_0xac3e('0x42')]));_0x5939b5['attributes']=_0x5939b5['attributes'][_0xac3e('0x31')]?_0x5939b5[_0xac3e('0x32')]:_0x551283[_0xac3e('0x2f')];_0x5939b5[_0xac3e('0x35')]=qs[_0xac3e('0x36')](_0x24d3a5[_0xac3e('0x30')]['sort']);_0x5939b5[_0xac3e('0x37')]=qs['filters'](_['pick'](_0x24d3a5[_0xac3e('0x30')],_0x551283[_0xac3e('0x39')]));if(_0x24d3a5[_0xac3e('0x30')][_0xac3e('0x3a')]){_0x5939b5[_0xac3e('0x37')]=_[_0xac3e('0x3b')](_0x5939b5['where'],{'$or':_['map'](_0x5939b5[_0xac3e('0x32')],function(_0x9d1103){var _0x44c4cc={};_0x44c4cc[_0x9d1103]={'$like':'%'+_0x24d3a5[_0xac3e('0x30')][_0xac3e('0x3a')]+'%'};return _0x44c4cc;})});}_0x5939b5=_['merge']({},_0x5939b5,_0x24d3a5[_0xac3e('0x4b')]);return _0x43c629[_0xac3e('0x48')](_0x5939b5);}})[_0xac3e('0x23')](function(_0x160934){if(_0x160934){_0x45d505=_0x160934[_0xac3e('0x31')];if(!_0x24d3a5[_0xac3e('0x30')]['hasOwnProperty']('nolimit')){_0x5939b5['limit']=qs[_0xac3e('0x1d')](_0x24d3a5['query'][_0xac3e('0x1d')]);_0x5939b5[_0xac3e('0x1b')]=qs[_0xac3e('0x1b')](_0x24d3a5['query']['offset']);}return _0x43c629[_0xac3e('0x48')](_0x5939b5);}})['then'](function(_0x223d19){if(_0x223d19){return _0x223d19?{'count':_0x45d505,'rows':_0x223d19}:null;}})['then'](respondWithResult(_0x1f1327,null))[_0xac3e('0x3f')](handleError(_0x1f1327,null));};exports[_0xac3e('0x4c')]=function(_0x4658f8,_0x1592f6,_0xd90795){if(_0x4658f8[_0xac3e('0x45')]['id']){delete _0x4658f8['body']['id'];}return db[_0xac3e('0x2a')]['findOne']({'where':{'id':_0x4658f8[_0xac3e('0x47')]['id']}})['then'](handleEntityNotFound(_0x1592f6,null))['then'](function(_0x1ec3b2){if(_0x1ec3b2){_0x4658f8[_0xac3e('0x45')]['AccountId']=_0x1ec3b2['id'];_0x4658f8[_0xac3e('0x45')]['Subjects']=integrations[_0xac3e('0x4d')](_0x4658f8[_0xac3e('0x45')][_0xac3e('0x4e')],_0x4658f8[_0xac3e('0x45')][_0xac3e('0x2c')]);_0x4658f8['body'][_0xac3e('0x4f')]=integrations['getDescriptions'](_0x4658f8[_0xac3e('0x45')][_0xac3e('0x4e')],_0x4658f8['body'][_0xac3e('0x2c')]);return db[_0xac3e('0x4a')][_0xac3e('0x44')](_0x4658f8[_0xac3e('0x45')],{'include':[{'model':db['SalesforceField'],'as':'Subjects'},{'model':db[_0xac3e('0x50')],'as':_0xac3e('0x4f')}]});}return null;})[_0xac3e('0x23')](respondWithResult(_0x1592f6,null))['catch'](handleError(_0x1592f6,null));};exports[_0xac3e('0x51')]=function(_0x4f8e36,_0x76f537,_0x4309a9){var _0x374aac;var _0x37bc3f='';var _0x39cefb='Task';return db[_0xac3e('0x2a')][_0xac3e('0x49')]({'where':{'id':_0x4f8e36['params']['id']},'attributes':['id','remoteUri','clientId',_0xac3e('0x52'),'username',_0xac3e('0x53'),_0xac3e('0x54')]})['then'](handleEntityNotFound(_0x76f537,null))['then'](function(_0x27630c){if(_0x27630c){_0x37bc3f=_0x27630c[_0xac3e('0x55')];var _0x4aecd5=_0x37bc3f[_0xac3e('0x56')](-0x1);if(_0x4aecd5==='/'){_0x37bc3f=_0x37bc3f['substring'](0x0,_0x37bc3f[_0xac3e('0x57')](_0x4aecd5));}_0x374aac=new jsforce[(_0xac3e('0x58'))]({'oauth2':{'loginUrl':_0x37bc3f,'clientId':_0x27630c[_0xac3e('0x59')],'clientSecret':_0x27630c[_0xac3e('0x52')]}});return _0x374aac['login'](_0x27630c[_0xac3e('0x5a')],_0x27630c[_0xac3e('0x53')][_0xac3e('0x5b')](_0x27630c[_0xac3e('0x54')]));}})['then'](function(_0x376dab){if(_0x376dab){if(!_[_0xac3e('0x5c')](_0x4f8e36[_0xac3e('0x30')][_0xac3e('0x5d')])){_0x39cefb=_0x4f8e36[_0xac3e('0x30')]['ticketType'];}return _0x374aac['sobject'](_0x39cefb)['describe']();}})['then'](function(_0x399ea0){var _0x612f65=[_0xac3e('0x5e'),_0xac3e('0x5f'),_0xac3e('0x60'),_0xac3e('0x61'),_0xac3e('0x62'),_0xac3e('0x63')];var _0x4f9bed=[_0xac3e('0x64'),'int',_0xac3e('0x65'),_0xac3e('0x66'),'email',_0xac3e('0x67'),'phone'];if(_0x399ea0){_[_0xac3e('0x68')](_0x399ea0['fields'],function(_0x25faa8){return _0x612f65['includes'](_0x25faa8[_0xac3e('0x26')])||!_0x4f9bed[_0xac3e('0x69')](_0x25faa8[_0xac3e('0x2c')]);});return{'count':_0x399ea0[_0xac3e('0x42')][_0xac3e('0x31')],'rows':_['map'](_0x399ea0[_0xac3e('0x42')],function(_0xae2a62){return{'id':_0xae2a62['name'],'name':_0xae2a62['label'],'custom':_0xae2a62['custom'],'options':_0xae2a62[_0xac3e('0x6a')][_0xac3e('0x31')]>0x0?_[_0xac3e('0x29')](_0xae2a62['picklistValues'],function(_0x220f51){return{'name':_0x220f51['label'],'value':_0x220f51[_0xac3e('0x6b')]};}):_0xae2a62[_0xac3e('0x2c')]==_0xac3e('0x66')?[{'name':_0xac3e('0x6c'),'value':'true'},{'name':'False','value':'false'}]:[]};})};}})['then'](respondWithResult(_0x76f537,null))['catch'](function(_0x432be7){var _0x34c0e5=_0x4f8e36[_0xac3e('0x30')][_0xac3e('0x6d')]?0x1f4:_0x432be7['statusCode']||0x1f4;logger[_0xac3e('0x25')](_0xac3e('0x6e'),_0xac3e('0x51'),_0x34c0e5,JSON[_0xac3e('0x6f')](_0x432be7));delete _0x432be7[_0xac3e('0x26')];if(_0x34c0e5===0x191){_0x34c0e5=0x190;}_0x76f537[_0xac3e('0x18')](_0x34c0e5)[_0xac3e('0x27')](_0x4f8e36[_0xac3e('0x30')][_0xac3e('0x6d')]?{'message':_0xac3e('0x70'),'statusCode':_0x432be7[_0xac3e('0x71')]}:_0x432be7);});};