b9ba841b5f698d810c23caf3d1d30009ebc01b32
[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 _0x439f=['undefined','limit','count','json','apply','save','update','then','end','sendStatus','error','send','index','map','SalesforceAccount','rawAttributes','type','key','model','name','query','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filter','merge','VIRTUAL','options','include','rows','show','params','keys','includeAll','find','catch','create','body','getConfigurations','findOne','SalesforceConfiguration','pick','nolimit','addConfiguration','AccountId','Subjects','channel','Descriptions','getDescriptions','SalesforceField','remoteUri','clientSecret','username','securityToken','slice','substring','lastIndexOf','Connection','clientId','login','password','concat','isNil','ticketType','sobject','describe','OwnerId','ActivityDate','IsReminderSet','IsRecurrence','Subject','string','int','picklist','email','phone','remove','includes','label','custom','picklistValues','boolean','True','False','false','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','Wrong\x20credentials','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','status','offset'];(function(_0x5f037a,_0x174055){var _0x68027b=function(_0x3d5edd){while(--_0x3d5edd){_0x5f037a['push'](_0x5f037a['shift']());}};_0x68027b(++_0x174055);}(_0x439f,0x1de));var _0xf439=function(_0x17371c,_0x1a1e9c){_0x17371c=_0x17371c-0x0;var _0x422265=_0x439f[_0x17371c];return _0x422265;};'use strict';var emlformat=require(_0xf439('0x0'));var rimraf=require(_0xf439('0x1'));var zipdir=require(_0xf439('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf439('0x3'));var moment=require(_0xf439('0x4'));var BPromise=require(_0xf439('0x5'));var Mustache=require(_0xf439('0x6'));var util=require('util');var path=require(_0xf439('0x7'));var sox=require(_0xf439('0x8'));var csv=require(_0xf439('0x9'));var ejs=require(_0xf439('0xa'));var fs=require('fs');var fs_extra=require(_0xf439('0xb'));var _=require(_0xf439('0xc'));var squel=require(_0xf439('0xd'));var crypto=require(_0xf439('0xe'));var jsforce=require('jsforce');var deskjs=require(_0xf439('0xf'));var toCsv=require(_0xf439('0x9'));var querystring=require(_0xf439('0x10'));var Papa=require('papaparse');var Redis=require(_0xf439('0x11'));var authService=require('../../components/auth/service');var qs=require(_0xf439('0x12'));var as=require(_0xf439('0x13'));var hardwareService=require(_0xf439('0x14'));var logger=require(_0xf439('0x15'))(_0xf439('0x16'));var utils=require(_0xf439('0x17'));var config=require(_0xf439('0x18'));var licenseUtil=require(_0xf439('0x19'));var db=require(_0xf439('0x1a'))['db'];var integrations=require(_0xf439('0x1b'));function respondWithStatusCode(_0x16c954,_0x3288a2){_0x3288a2=_0x3288a2||0xcc;return function(_0x3197da){if(_0x3197da){return _0x16c954['sendStatus'](_0x3288a2);}return _0x16c954[_0xf439('0x1c')](_0x3288a2)['end']();};}function respondWithResult(_0x4f3054,_0x1f5c8d){_0x1f5c8d=_0x1f5c8d||0xc8;return function(_0x310dcc){if(_0x310dcc){return _0x4f3054[_0xf439('0x1c')](_0x1f5c8d)['json'](_0x310dcc);}};}function respondWithFilteredResult(_0x4aa2a9,_0x55a16b){return function(_0x5501ba){if(_0x5501ba){var _0x1cd501=typeof _0x55a16b[_0xf439('0x1d')]===_0xf439('0x1e')&&typeof _0x55a16b[_0xf439('0x1f')]===_0xf439('0x1e');var _0x3c959c=_0x5501ba[_0xf439('0x20')];var _0x1d4964=_0x1cd501?0x0:_0x55a16b[_0xf439('0x1d')];var _0x143a26=_0x1cd501?_0x5501ba[_0xf439('0x20')]:_0x55a16b['offset']+_0x55a16b[_0xf439('0x1f')];var _0x136e79;if(_0x143a26>=_0x3c959c){_0x143a26=_0x3c959c;_0x136e79=0xc8;}else{_0x136e79=0xce;}_0x4aa2a9[_0xf439('0x1c')](_0x136e79);return _0x4aa2a9['set']('Content-Range',_0x1d4964+'-'+_0x143a26+'/'+_0x3c959c)[_0xf439('0x21')](_0x5501ba);}return null;};}function patchUpdates(_0x3178ef){return function(_0x15789d){try{jsonpatch[_0xf439('0x22')](_0x15789d,_0x3178ef,!![]);}catch(_0x3cd567){return BPromise['reject'](_0x3cd567);}return _0x15789d[_0xf439('0x23')]();};}function saveUpdates(_0x46a348,_0x37ecd7){return function(_0x51d2f2){if(_0x51d2f2){return _0x51d2f2[_0xf439('0x24')](_0x46a348)['then'](function(_0x5cbe8b){return _0x5cbe8b;});}return null;};}function removeEntity(_0xa28ba0,_0x266bf9){return function(_0x700b5f){if(_0x700b5f){return _0x700b5f['destroy']()[_0xf439('0x25')](function(){_0xa28ba0[_0xf439('0x1c')](0xcc)[_0xf439('0x26')]();});}};}function handleEntityNotFound(_0x25bd92,_0xd3ee92){return function(_0x1687bc){if(!_0x1687bc){_0x25bd92[_0xf439('0x27')](0x194);}return _0x1687bc;};}function handleError(_0x400996,_0x1bbc67){_0x1bbc67=_0x1bbc67||0x1f4;return function(_0x18c808){logger[_0xf439('0x28')](_0x18c808['stack']);if(_0x18c808['name']){delete _0x18c808['name'];}_0x400996[_0xf439('0x1c')](_0x1bbc67)[_0xf439('0x29')](_0x18c808);};}exports[_0xf439('0x2a')]=function(_0x42f688,_0x291229){var _0x37878e={},_0x1918fe={},_0x25cf9b={'count':0x0,'rows':[]};var _0x1c0764=_[_0xf439('0x2b')](db[_0xf439('0x2c')][_0xf439('0x2d')],function(_0x2759e1){return{'name':_0x2759e1['fieldName'],'type':_0x2759e1[_0xf439('0x2e')][_0xf439('0x2f')]};});_0x1918fe[_0xf439('0x30')]=_[_0xf439('0x2b')](_0x1c0764,_0xf439('0x31'));_0x1918fe['query']=_['keys'](_0x42f688[_0xf439('0x32')]);_0x1918fe[_0xf439('0x33')]=_[_0xf439('0x34')](_0x1918fe[_0xf439('0x30')],_0x1918fe[_0xf439('0x32')]);_0x37878e[_0xf439('0x35')]=_[_0xf439('0x34')](_0x1918fe[_0xf439('0x30')],qs[_0xf439('0x36')](_0x42f688[_0xf439('0x32')][_0xf439('0x36')]));_0x37878e[_0xf439('0x35')]=_0x37878e[_0xf439('0x35')][_0xf439('0x37')]?_0x37878e[_0xf439('0x35')]:_0x1918fe[_0xf439('0x30')];if(!_0x42f688[_0xf439('0x32')][_0xf439('0x38')]('nolimit')){_0x37878e[_0xf439('0x1f')]=qs[_0xf439('0x1f')](_0x42f688[_0xf439('0x32')][_0xf439('0x1f')]);_0x37878e[_0xf439('0x1d')]=qs['offset'](_0x42f688['query']['offset']);}_0x37878e[_0xf439('0x39')]=qs[_0xf439('0x3a')](_0x42f688['query'][_0xf439('0x3a')]);_0x37878e[_0xf439('0x3b')]=qs['filters'](_['pick'](_0x42f688[_0xf439('0x32')],_0x1918fe['filters']),_0x1c0764);if(_0x42f688['query'][_0xf439('0x3c')]){_0x37878e[_0xf439('0x3b')]=_[_0xf439('0x3d')](_0x37878e[_0xf439('0x3b')],{'$or':_[_0xf439('0x2b')](_0x1c0764,function(_0x36bd4f){if(_0x36bd4f[_0xf439('0x2e')]!==_0xf439('0x3e')){var _0x4bee19={};_0x4bee19[_0x36bd4f[_0xf439('0x31')]]={'$like':'%'+_0x42f688[_0xf439('0x32')]['filter']+'%'};return _0x4bee19;}})});}_0x37878e=_[_0xf439('0x3d')]({},_0x37878e,_0x42f688[_0xf439('0x3f')]);var _0x2755b7={'where':_0x37878e[_0xf439('0x3b')]};return db['SalesforceAccount']['count'](_0x2755b7)[_0xf439('0x25')](function(_0x309622){_0x25cf9b[_0xf439('0x20')]=_0x309622;if(_0x42f688[_0xf439('0x32')]['includeAll']){_0x37878e[_0xf439('0x40')]=[{'all':!![]}];}return db[_0xf439('0x2c')]['findAll'](_0x37878e);})['then'](function(_0x1f8667){_0x25cf9b[_0xf439('0x41')]=_0x1f8667;return _0x25cf9b;})[_0xf439('0x25')](respondWithFilteredResult(_0x291229,_0x37878e))['catch'](handleError(_0x291229,null));};exports[_0xf439('0x42')]=function(_0x42eadc,_0x5b3773){var _0x1f2f40={'raw':![],'where':{'id':_0x42eadc[_0xf439('0x43')]['id']}},_0x1f4d04={};_0x1f4d04['model']=_[_0xf439('0x44')](db[_0xf439('0x2c')][_0xf439('0x2d')]);_0x1f4d04[_0xf439('0x32')]=_['keys'](_0x42eadc[_0xf439('0x32')]);_0x1f4d04[_0xf439('0x33')]=_[_0xf439('0x34')](_0x1f4d04[_0xf439('0x30')],_0x1f4d04[_0xf439('0x32')]);_0x1f2f40[_0xf439('0x35')]=_[_0xf439('0x34')](_0x1f4d04[_0xf439('0x30')],qs['fields'](_0x42eadc[_0xf439('0x32')][_0xf439('0x36')]));_0x1f2f40[_0xf439('0x35')]=_0x1f2f40['attributes'][_0xf439('0x37')]?_0x1f2f40[_0xf439('0x35')]:_0x1f4d04[_0xf439('0x30')];if(_0x42eadc[_0xf439('0x32')][_0xf439('0x45')]){_0x1f2f40[_0xf439('0x40')]=[{'all':!![]}];}_0x1f2f40=_[_0xf439('0x3d')]({},_0x1f2f40,_0x42eadc[_0xf439('0x3f')]);return db[_0xf439('0x2c')][_0xf439('0x46')](_0x1f2f40)['then'](handleEntityNotFound(_0x5b3773,null))[_0xf439('0x25')](respondWithResult(_0x5b3773,null))[_0xf439('0x47')](handleError(_0x5b3773,null));};exports[_0xf439('0x48')]=function(_0x3f55ab,_0x5cb2af){return db[_0xf439('0x2c')]['create'](_0x3f55ab['body'],{})['then'](respondWithResult(_0x5cb2af,0xc9))[_0xf439('0x47')](handleError(_0x5cb2af,null));};exports['update']=function(_0x174e52,_0x1cf20d){if(_0x174e52[_0xf439('0x49')]['id']){delete _0x174e52[_0xf439('0x49')]['id'];}return db[_0xf439('0x2c')][_0xf439('0x46')]({'where':{'id':_0x174e52[_0xf439('0x43')]['id']}})[_0xf439('0x25')](handleEntityNotFound(_0x1cf20d,null))[_0xf439('0x25')](saveUpdates(_0x174e52[_0xf439('0x49')],null))[_0xf439('0x25')](respondWithResult(_0x1cf20d,null))[_0xf439('0x47')](handleError(_0x1cf20d,null));};exports['destroy']=function(_0x24db11,_0x2f4b3a){return db[_0xf439('0x2c')]['find']({'where':{'id':_0x24db11[_0xf439('0x43')]['id']}})[_0xf439('0x25')](handleEntityNotFound(_0x2f4b3a,null))[_0xf439('0x25')](removeEntity(_0x2f4b3a,null))['catch'](handleError(_0x2f4b3a,null));};exports[_0xf439('0x4a')]=function(_0x551bac,_0x338bf0,_0x31f400){var _0x43492e={};var _0x59c5eb={};var _0x59017a;var _0x59d85f;return db[_0xf439('0x2c')][_0xf439('0x4b')]({'where':{'id':_0x551bac[_0xf439('0x43')]['id']}})[_0xf439('0x25')](handleEntityNotFound(_0x338bf0,null))[_0xf439('0x25')](function(_0x37b71d){if(_0x37b71d){_0x59017a=_0x37b71d;_0x59c5eb[_0xf439('0x30')]=_[_0xf439('0x44')](db[_0xf439('0x4c')][_0xf439('0x2d')]);_0x59c5eb[_0xf439('0x32')]=_[_0xf439('0x44')](_0x551bac[_0xf439('0x32')]);_0x59c5eb[_0xf439('0x33')]=_[_0xf439('0x34')](_0x59c5eb[_0xf439('0x30')],_0x59c5eb[_0xf439('0x32')]);_0x43492e['attributes']=_[_0xf439('0x34')](_0x59c5eb['model'],qs['fields'](_0x551bac[_0xf439('0x32')][_0xf439('0x36')]));_0x43492e[_0xf439('0x35')]=_0x43492e['attributes']['length']?_0x43492e[_0xf439('0x35')]:_0x59c5eb['model'];_0x43492e['order']=qs[_0xf439('0x3a')](_0x551bac[_0xf439('0x32')][_0xf439('0x3a')]);_0x43492e[_0xf439('0x3b')]=qs['filters'](_[_0xf439('0x4d')](_0x551bac[_0xf439('0x32')],_0x59c5eb['filters']));if(_0x551bac[_0xf439('0x32')][_0xf439('0x3c')]){_0x43492e[_0xf439('0x3b')]=_['merge'](_0x43492e[_0xf439('0x3b')],{'$or':_[_0xf439('0x2b')](_0x43492e['attributes'],function(_0x4b7931){var _0x52c7d0={};_0x52c7d0[_0x4b7931]={'$like':'%'+_0x551bac[_0xf439('0x32')][_0xf439('0x3c')]+'%'};return _0x52c7d0;})});}_0x43492e=_[_0xf439('0x3d')]({},_0x43492e,_0x551bac[_0xf439('0x3f')]);return _0x59017a[_0xf439('0x4a')](_0x43492e);}})[_0xf439('0x25')](function(_0x29bc19){if(_0x29bc19){_0x59d85f=_0x29bc19[_0xf439('0x37')];if(!_0x551bac[_0xf439('0x32')][_0xf439('0x38')](_0xf439('0x4e'))){_0x43492e['limit']=qs[_0xf439('0x1f')](_0x551bac[_0xf439('0x32')]['limit']);_0x43492e['offset']=qs['offset'](_0x551bac[_0xf439('0x32')][_0xf439('0x1d')]);}return _0x59017a['getConfigurations'](_0x43492e);}})[_0xf439('0x25')](function(_0x43e7f3){if(_0x43e7f3){return _0x43e7f3?{'count':_0x59d85f,'rows':_0x43e7f3}:null;}})[_0xf439('0x25')](respondWithResult(_0x338bf0,null))[_0xf439('0x47')](handleError(_0x338bf0,null));};exports[_0xf439('0x4f')]=function(_0x598a34,_0x45667e,_0x1bdbd5){if(_0x598a34['body']['id']){delete _0x598a34[_0xf439('0x49')]['id'];}return db[_0xf439('0x2c')]['findOne']({'where':{'id':_0x598a34[_0xf439('0x43')]['id']}})[_0xf439('0x25')](handleEntityNotFound(_0x45667e,null))[_0xf439('0x25')](function(_0x113003){if(_0x113003){_0x598a34['body'][_0xf439('0x50')]=_0x113003['id'];_0x598a34[_0xf439('0x49')][_0xf439('0x51')]=integrations['getSubjects'](_0x598a34[_0xf439('0x49')][_0xf439('0x52')],_0x598a34[_0xf439('0x49')][_0xf439('0x2e')]);_0x598a34[_0xf439('0x49')][_0xf439('0x53')]=integrations[_0xf439('0x54')](_0x598a34[_0xf439('0x49')]['channel'],_0x598a34['body'][_0xf439('0x2e')]);return db[_0xf439('0x4c')][_0xf439('0x48')](_0x598a34[_0xf439('0x49')],{'include':[{'model':db[_0xf439('0x55')],'as':_0xf439('0x51')},{'model':db[_0xf439('0x55')],'as':_0xf439('0x53')}]});}return null;})[_0xf439('0x25')](respondWithResult(_0x45667e,null))['catch'](handleError(_0x45667e,null));};exports['getFields']=function(_0x509650,_0x2da685,_0xc192d9){var _0x35feb3;var _0x470a2a='';var _0x47d457='Task';return db[_0xf439('0x2c')][_0xf439('0x4b')]({'where':{'id':_0x509650[_0xf439('0x43')]['id']},'attributes':['id',_0xf439('0x56'),'clientId',_0xf439('0x57'),_0xf439('0x58'),'password',_0xf439('0x59')]})[_0xf439('0x25')](handleEntityNotFound(_0x2da685,null))[_0xf439('0x25')](function(_0x44bce2){if(_0x44bce2){_0x470a2a=_0x44bce2['remoteUri'];var _0x2ca146=_0x470a2a[_0xf439('0x5a')](-0x1);if(_0x2ca146==='/'){_0x470a2a=_0x470a2a[_0xf439('0x5b')](0x0,_0x470a2a[_0xf439('0x5c')](_0x2ca146));}_0x35feb3=new jsforce[(_0xf439('0x5d'))]({'oauth2':{'loginUrl':_0x470a2a,'clientId':_0x44bce2[_0xf439('0x5e')],'clientSecret':_0x44bce2[_0xf439('0x57')]}});return _0x35feb3[_0xf439('0x5f')](_0x44bce2[_0xf439('0x58')],_0x44bce2[_0xf439('0x60')][_0xf439('0x61')](_0x44bce2[_0xf439('0x59')]));}})[_0xf439('0x25')](function(_0x580c65){if(_0x580c65){if(!_[_0xf439('0x62')](_0x509650[_0xf439('0x32')][_0xf439('0x63')])){_0x47d457=_0x509650[_0xf439('0x32')][_0xf439('0x63')];}return _0x35feb3[_0xf439('0x64')](_0x47d457)[_0xf439('0x65')]();}})[_0xf439('0x25')](function(_0x2b695e){var _0x455baf=[_0xf439('0x66'),_0xf439('0x67'),_0xf439('0x68'),_0xf439('0x69'),_0xf439('0x6a'),'Description'];var _0x52711a=[_0xf439('0x6b'),_0xf439('0x6c'),_0xf439('0x6d'),'boolean',_0xf439('0x6e'),'textarea',_0xf439('0x6f')];if(_0x2b695e){_[_0xf439('0x70')](_0x2b695e['fields'],function(_0xe7d2ad){return _0x455baf[_0xf439('0x71')](_0xe7d2ad['name'])||!_0x52711a[_0xf439('0x71')](_0xe7d2ad['type']);});return{'count':_0x2b695e[_0xf439('0x36')][_0xf439('0x37')],'rows':_['map'](_0x2b695e[_0xf439('0x36')],function(_0x51986a){return{'id':_0x51986a[_0xf439('0x31')],'name':_0x51986a[_0xf439('0x72')],'custom':_0x51986a[_0xf439('0x73')],'options':_0x51986a[_0xf439('0x74')]['length']>0x0?_[_0xf439('0x2b')](_0x51986a[_0xf439('0x74')],function(_0x2694a1){return{'name':_0x2694a1[_0xf439('0x72')],'value':_0x2694a1['value']};}):_0x51986a[_0xf439('0x2e')]==_0xf439('0x75')?[{'name':_0xf439('0x76'),'value':'true'},{'name':_0xf439('0x77'),'value':_0xf439('0x78')}]:[]};})};}})[_0xf439('0x25')](respondWithResult(_0x2da685,null))[_0xf439('0x47')](function(_0x435074){var _0x2c4c86=_0x509650['query'][_0xf439('0x79')]?0x1f4:_0x435074[_0xf439('0x7a')]||0x1f4;logger[_0xf439('0x28')](_0xf439('0x7b'),_0xf439('0x7c'),_0x2c4c86,JSON[_0xf439('0x7d')](_0x435074));delete _0x435074[_0xf439('0x31')];if(_0x2c4c86===0x191){_0x2c4c86=0x190;}_0x2da685['status'](_0x2c4c86)[_0xf439('0x29')](_0x509650['query'][_0xf439('0x79')]?{'message':_0xf439('0x7e'),'statusCode':_0x435074[_0xf439('0x7a')]}:_0x435074);});};