Built motion from commit 04e86ced.|2.6.25
[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 _0x1f15=['type','key','model','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','keys','find','create','body','destroy','getConfigurations','SalesforceConfiguration','addConfiguration','findOne','AccountId','getSubjects','channel','Descriptions','getDescriptions','SalesforceField','remoteUri','clientId','clientSecret','securityToken','lastIndexOf','Connection','login','concat','isNil','ticketType','describe','OwnerId','IsReminderSet','IsRecurrence','Subject','Description','string','int','picklist','boolean','email','textarea','phone','remove','includes','label','custom','picklistValues','value','true','False','false','test','getFields,\x20%s,\x20%s,\x20%s','getFields','Wrong\x20credentials','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','lodash','crypto','jsforce','desk.js','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../mysqldb','../../components/integrations/configuration','sendStatus','status','offset','undefined','count','set','Content-Range','json','reject','save','update','then','end','error','stack','name','send','index','map','SalesforceAccount','rawAttributes','fieldName'];(function(_0x503862,_0x583ac5){var _0x4ed715=function(_0x2d3944){while(--_0x2d3944){_0x503862['push'](_0x503862['shift']());}};_0x4ed715(++_0x583ac5);}(_0x1f15,0xc8));var _0x51f1=function(_0xfb479,_0x5984ae){_0xfb479=_0xfb479-0x0;var _0x380173=_0x1f15[_0xfb479];return _0x380173;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x51f1('0x0'));var zipdir=require('zip-dir');var jsonpatch=require(_0x51f1('0x1'));var rp=require(_0x51f1('0x2'));var moment=require(_0x51f1('0x3'));var BPromise=require(_0x51f1('0x4'));var Mustache=require(_0x51f1('0x5'));var util=require(_0x51f1('0x6'));var path=require(_0x51f1('0x7'));var sox=require(_0x51f1('0x8'));var csv=require(_0x51f1('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x51f1('0xa'));var squel=require('squel');var crypto=require(_0x51f1('0xb'));var jsforce=require(_0x51f1('0xc'));var deskjs=require(_0x51f1('0xd'));var toCsv=require(_0x51f1('0x9'));var querystring=require('querystring');var Papa=require(_0x51f1('0xe'));var Redis=require('ioredis');var authService=require(_0x51f1('0xf'));var qs=require(_0x51f1('0x10'));var as=require(_0x51f1('0x11'));var hardwareService=require(_0x51f1('0x12'));var logger=require('../../config/logger')('api');var utils=require(_0x51f1('0x13'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x51f1('0x14'))['db'];var integrations=require(_0x51f1('0x15'));function respondWithStatusCode(_0x594663,_0x3a0811){_0x3a0811=_0x3a0811||0xcc;return function(_0x574063){if(_0x574063){return _0x594663[_0x51f1('0x16')](_0x3a0811);}return _0x594663[_0x51f1('0x17')](_0x3a0811)['end']();};}function respondWithResult(_0x42c5b6,_0x36819c){_0x36819c=_0x36819c||0xc8;return function(_0x35dc15){if(_0x35dc15){return _0x42c5b6[_0x51f1('0x17')](_0x36819c)['json'](_0x35dc15);}};}function respondWithFilteredResult(_0x447f02,_0x5b814b){return function(_0xa3cedc){if(_0xa3cedc){var _0x803d30=typeof _0x5b814b[_0x51f1('0x18')]===_0x51f1('0x19')&&typeof _0x5b814b['limit']===_0x51f1('0x19');var _0xc980fd=_0xa3cedc[_0x51f1('0x1a')];var _0x428707=_0x803d30?0x0:_0x5b814b['offset'];var _0x142f6f=_0x803d30?_0xa3cedc[_0x51f1('0x1a')]:_0x5b814b[_0x51f1('0x18')]+_0x5b814b['limit'];var _0x2a604f;if(_0x142f6f>=_0xc980fd){_0x142f6f=_0xc980fd;_0x2a604f=0xc8;}else{_0x2a604f=0xce;}_0x447f02['status'](_0x2a604f);return _0x447f02[_0x51f1('0x1b')](_0x51f1('0x1c'),_0x428707+'-'+_0x142f6f+'/'+_0xc980fd)[_0x51f1('0x1d')](_0xa3cedc);}return null;};}function patchUpdates(_0x4ba283){return function(_0x387280){try{jsonpatch['apply'](_0x387280,_0x4ba283,!![]);}catch(_0x8c606b){return BPromise[_0x51f1('0x1e')](_0x8c606b);}return _0x387280[_0x51f1('0x1f')]();};}function saveUpdates(_0x5c5c93,_0x1fef5a){return function(_0x46c93a){if(_0x46c93a){return _0x46c93a[_0x51f1('0x20')](_0x5c5c93)[_0x51f1('0x21')](function(_0x22126c){return _0x22126c;});}return null;};}function removeEntity(_0x191cc0,_0xc5eb72){return function(_0x257e5b){if(_0x257e5b){return _0x257e5b['destroy']()[_0x51f1('0x21')](function(){_0x191cc0[_0x51f1('0x17')](0xcc)[_0x51f1('0x22')]();});}};}function handleEntityNotFound(_0x20bda6,_0x451546){return function(_0x29438e){if(!_0x29438e){_0x20bda6[_0x51f1('0x16')](0x194);}return _0x29438e;};}function handleError(_0x4b53d0,_0x582f18){_0x582f18=_0x582f18||0x1f4;return function(_0x58f181){logger[_0x51f1('0x23')](_0x58f181[_0x51f1('0x24')]);if(_0x58f181[_0x51f1('0x25')]){delete _0x58f181['name'];}_0x4b53d0[_0x51f1('0x17')](_0x582f18)[_0x51f1('0x26')](_0x58f181);};}exports[_0x51f1('0x27')]=function(_0x518439,_0xac2e4c){var _0x39b3b9={},_0x367bda={},_0x25d870={'count':0x0,'rows':[]};var _0x5e6ced=_[_0x51f1('0x28')](db[_0x51f1('0x29')][_0x51f1('0x2a')],function(_0x18b01e){return{'name':_0x18b01e[_0x51f1('0x2b')],'type':_0x18b01e[_0x51f1('0x2c')][_0x51f1('0x2d')]};});_0x367bda[_0x51f1('0x2e')]=_['map'](_0x5e6ced,_0x51f1('0x25'));_0x367bda[_0x51f1('0x2f')]=_['keys'](_0x518439[_0x51f1('0x2f')]);_0x367bda[_0x51f1('0x30')]=_['intersection'](_0x367bda[_0x51f1('0x2e')],_0x367bda['query']);_0x39b3b9['attributes']=_[_0x51f1('0x31')](_0x367bda[_0x51f1('0x2e')],qs[_0x51f1('0x32')](_0x518439[_0x51f1('0x2f')]['fields']));_0x39b3b9['attributes']=_0x39b3b9[_0x51f1('0x33')][_0x51f1('0x34')]?_0x39b3b9[_0x51f1('0x33')]:_0x367bda['model'];if(!_0x518439[_0x51f1('0x2f')][_0x51f1('0x35')](_0x51f1('0x36'))){_0x39b3b9['limit']=qs[_0x51f1('0x37')](_0x518439[_0x51f1('0x2f')][_0x51f1('0x37')]);_0x39b3b9[_0x51f1('0x18')]=qs[_0x51f1('0x18')](_0x518439['query'][_0x51f1('0x18')]);}_0x39b3b9[_0x51f1('0x38')]=qs[_0x51f1('0x39')](_0x518439[_0x51f1('0x2f')][_0x51f1('0x39')]);_0x39b3b9[_0x51f1('0x3a')]=qs['filters'](_[_0x51f1('0x3b')](_0x518439[_0x51f1('0x2f')],_0x367bda['filters']),_0x5e6ced);if(_0x518439[_0x51f1('0x2f')][_0x51f1('0x3c')]){_0x39b3b9[_0x51f1('0x3a')]=_[_0x51f1('0x3d')](_0x39b3b9['where'],{'$or':_[_0x51f1('0x28')](_0x5e6ced,function(_0x4e1083){if(_0x4e1083[_0x51f1('0x2c')]!==_0x51f1('0x3e')){var _0x4383c8={};_0x4383c8[_0x4e1083[_0x51f1('0x25')]]={'$like':'%'+_0x518439['query'][_0x51f1('0x3c')]+'%'};return _0x4383c8;}})});}_0x39b3b9=_[_0x51f1('0x3d')]({},_0x39b3b9,_0x518439[_0x51f1('0x3f')]);var _0x5be824={'where':_0x39b3b9[_0x51f1('0x3a')]};return db[_0x51f1('0x29')][_0x51f1('0x1a')](_0x5be824)['then'](function(_0xb48c){_0x25d870[_0x51f1('0x1a')]=_0xb48c;if(_0x518439['query'][_0x51f1('0x40')]){_0x39b3b9[_0x51f1('0x41')]=[{'all':!![]}];}return db['SalesforceAccount'][_0x51f1('0x42')](_0x39b3b9);})['then'](function(_0x349849){_0x25d870[_0x51f1('0x43')]=_0x349849;return _0x25d870;})[_0x51f1('0x21')](respondWithFilteredResult(_0xac2e4c,_0x39b3b9))[_0x51f1('0x44')](handleError(_0xac2e4c,null));};exports[_0x51f1('0x45')]=function(_0x360729,_0x8bf42f){var _0x29d25a={'raw':![],'where':{'id':_0x360729[_0x51f1('0x46')]['id']}},_0x224e08={};_0x224e08[_0x51f1('0x2e')]=_['keys'](db['SalesforceAccount'][_0x51f1('0x2a')]);_0x224e08[_0x51f1('0x2f')]=_[_0x51f1('0x47')](_0x360729[_0x51f1('0x2f')]);_0x224e08[_0x51f1('0x30')]=_[_0x51f1('0x31')](_0x224e08['model'],_0x224e08[_0x51f1('0x2f')]);_0x29d25a[_0x51f1('0x33')]=_[_0x51f1('0x31')](_0x224e08[_0x51f1('0x2e')],qs['fields'](_0x360729[_0x51f1('0x2f')]['fields']));_0x29d25a[_0x51f1('0x33')]=_0x29d25a['attributes']['length']?_0x29d25a[_0x51f1('0x33')]:_0x224e08[_0x51f1('0x2e')];if(_0x360729['query'][_0x51f1('0x40')]){_0x29d25a[_0x51f1('0x41')]=[{'all':!![]}];}_0x29d25a=_[_0x51f1('0x3d')]({},_0x29d25a,_0x360729[_0x51f1('0x3f')]);return db[_0x51f1('0x29')][_0x51f1('0x48')](_0x29d25a)['then'](handleEntityNotFound(_0x8bf42f,null))['then'](respondWithResult(_0x8bf42f,null))['catch'](handleError(_0x8bf42f,null));};exports[_0x51f1('0x49')]=function(_0xc22256,_0x3aba6f){return db[_0x51f1('0x29')][_0x51f1('0x49')](_0xc22256[_0x51f1('0x4a')],{})[_0x51f1('0x21')](respondWithResult(_0x3aba6f,0xc9))[_0x51f1('0x44')](handleError(_0x3aba6f,null));};exports['update']=function(_0x213fef,_0x291b25){if(_0x213fef['body']['id']){delete _0x213fef['body']['id'];}return db[_0x51f1('0x29')][_0x51f1('0x48')]({'where':{'id':_0x213fef[_0x51f1('0x46')]['id']}})['then'](handleEntityNotFound(_0x291b25,null))[_0x51f1('0x21')](saveUpdates(_0x213fef[_0x51f1('0x4a')],null))[_0x51f1('0x21')](respondWithResult(_0x291b25,null))['catch'](handleError(_0x291b25,null));};exports[_0x51f1('0x4b')]=function(_0x18634f,_0x36382b){return db[_0x51f1('0x29')][_0x51f1('0x48')]({'where':{'id':_0x18634f[_0x51f1('0x46')]['id']}})['then'](handleEntityNotFound(_0x36382b,null))['then'](removeEntity(_0x36382b,null))['catch'](handleError(_0x36382b,null));};exports[_0x51f1('0x4c')]=function(_0x3b08d0,_0x56aa7c,_0x2f90cb){var _0x521e6e={};var _0x5dba92={};var _0x283f62;var _0x1d6d80;return db[_0x51f1('0x29')]['findOne']({'where':{'id':_0x3b08d0[_0x51f1('0x46')]['id']}})[_0x51f1('0x21')](handleEntityNotFound(_0x56aa7c,null))['then'](function(_0x1ceb3b){if(_0x1ceb3b){_0x283f62=_0x1ceb3b;_0x5dba92['model']=_['keys'](db[_0x51f1('0x4d')][_0x51f1('0x2a')]);_0x5dba92[_0x51f1('0x2f')]=_[_0x51f1('0x47')](_0x3b08d0[_0x51f1('0x2f')]);_0x5dba92['filters']=_[_0x51f1('0x31')](_0x5dba92[_0x51f1('0x2e')],_0x5dba92[_0x51f1('0x2f')]);_0x521e6e[_0x51f1('0x33')]=_['intersection'](_0x5dba92[_0x51f1('0x2e')],qs[_0x51f1('0x32')](_0x3b08d0[_0x51f1('0x2f')][_0x51f1('0x32')]));_0x521e6e[_0x51f1('0x33')]=_0x521e6e[_0x51f1('0x33')][_0x51f1('0x34')]?_0x521e6e['attributes']:_0x5dba92[_0x51f1('0x2e')];_0x521e6e[_0x51f1('0x38')]=qs[_0x51f1('0x39')](_0x3b08d0['query']['sort']);_0x521e6e[_0x51f1('0x3a')]=qs[_0x51f1('0x30')](_['pick'](_0x3b08d0['query'],_0x5dba92['filters']));if(_0x3b08d0[_0x51f1('0x2f')]['filter']){_0x521e6e[_0x51f1('0x3a')]=_[_0x51f1('0x3d')](_0x521e6e['where'],{'$or':_[_0x51f1('0x28')](_0x521e6e[_0x51f1('0x33')],function(_0x3f05fe){var _0x25e134={};_0x25e134[_0x3f05fe]={'$like':'%'+_0x3b08d0[_0x51f1('0x2f')][_0x51f1('0x3c')]+'%'};return _0x25e134;})});}_0x521e6e=_[_0x51f1('0x3d')]({},_0x521e6e,_0x3b08d0[_0x51f1('0x3f')]);return _0x283f62['getConfigurations'](_0x521e6e);}})['then'](function(_0x5d2a11){if(_0x5d2a11){_0x1d6d80=_0x5d2a11[_0x51f1('0x34')];if(!_0x3b08d0[_0x51f1('0x2f')][_0x51f1('0x35')](_0x51f1('0x36'))){_0x521e6e[_0x51f1('0x37')]=qs['limit'](_0x3b08d0['query'][_0x51f1('0x37')]);_0x521e6e[_0x51f1('0x18')]=qs[_0x51f1('0x18')](_0x3b08d0[_0x51f1('0x2f')][_0x51f1('0x18')]);}return _0x283f62[_0x51f1('0x4c')](_0x521e6e);}})['then'](function(_0x4b3f59){if(_0x4b3f59){return _0x4b3f59?{'count':_0x1d6d80,'rows':_0x4b3f59}:null;}})[_0x51f1('0x21')](respondWithResult(_0x56aa7c,null))[_0x51f1('0x44')](handleError(_0x56aa7c,null));};exports[_0x51f1('0x4e')]=function(_0x2d0cd8,_0x2b27aa,_0x3a6f84){if(_0x2d0cd8[_0x51f1('0x4a')]['id']){delete _0x2d0cd8['body']['id'];}return db[_0x51f1('0x29')][_0x51f1('0x4f')]({'where':{'id':_0x2d0cd8[_0x51f1('0x46')]['id']}})[_0x51f1('0x21')](handleEntityNotFound(_0x2b27aa,null))[_0x51f1('0x21')](function(_0xf91893){if(_0xf91893){_0x2d0cd8[_0x51f1('0x4a')][_0x51f1('0x50')]=_0xf91893['id'];_0x2d0cd8['body']['Subjects']=integrations[_0x51f1('0x51')](_0x2d0cd8[_0x51f1('0x4a')][_0x51f1('0x52')],_0x2d0cd8[_0x51f1('0x4a')][_0x51f1('0x2c')]);_0x2d0cd8[_0x51f1('0x4a')][_0x51f1('0x53')]=integrations[_0x51f1('0x54')](_0x2d0cd8[_0x51f1('0x4a')][_0x51f1('0x52')],_0x2d0cd8[_0x51f1('0x4a')]['type']);return db[_0x51f1('0x4d')][_0x51f1('0x49')](_0x2d0cd8[_0x51f1('0x4a')],{'include':[{'model':db['SalesforceField'],'as':'Subjects'},{'model':db[_0x51f1('0x55')],'as':_0x51f1('0x53')}]});}return null;})['then'](respondWithResult(_0x2b27aa,null))[_0x51f1('0x44')](handleError(_0x2b27aa,null));};exports['getFields']=function(_0x54f41e,_0x2274a3,_0x10869d){var _0x4aaf78;var _0x5c6ca1='';var _0x4e82ef='Task';return db[_0x51f1('0x29')][_0x51f1('0x4f')]({'where':{'id':_0x54f41e[_0x51f1('0x46')]['id']},'attributes':['id',_0x51f1('0x56'),_0x51f1('0x57'),_0x51f1('0x58'),'username','password',_0x51f1('0x59')]})[_0x51f1('0x21')](handleEntityNotFound(_0x2274a3,null))[_0x51f1('0x21')](function(_0x2df4b8){if(_0x2df4b8){_0x5c6ca1=_0x2df4b8['remoteUri'];var _0x27cf70=_0x5c6ca1['slice'](-0x1);if(_0x27cf70==='/'){_0x5c6ca1=_0x5c6ca1['substring'](0x0,_0x5c6ca1[_0x51f1('0x5a')](_0x27cf70));}_0x4aaf78=new jsforce[(_0x51f1('0x5b'))]({'oauth2':{'loginUrl':_0x5c6ca1,'clientId':_0x2df4b8[_0x51f1('0x57')],'clientSecret':_0x2df4b8['clientSecret']}});return _0x4aaf78[_0x51f1('0x5c')](_0x2df4b8['username'],_0x2df4b8['password'][_0x51f1('0x5d')](_0x2df4b8[_0x51f1('0x59')]));}})['then'](function(_0x467c){if(_0x467c){if(!_[_0x51f1('0x5e')](_0x54f41e[_0x51f1('0x2f')]['ticketType'])){_0x4e82ef=_0x54f41e['query'][_0x51f1('0x5f')];}return _0x4aaf78['sobject'](_0x4e82ef)[_0x51f1('0x60')]();}})['then'](function(_0x4f2fbd){var _0x46458d=[_0x51f1('0x61'),'ActivityDate',_0x51f1('0x62'),_0x51f1('0x63'),_0x51f1('0x64'),_0x51f1('0x65')];var _0x4a95d8=[_0x51f1('0x66'),_0x51f1('0x67'),_0x51f1('0x68'),_0x51f1('0x69'),_0x51f1('0x6a'),_0x51f1('0x6b'),_0x51f1('0x6c')];if(_0x4f2fbd){_[_0x51f1('0x6d')](_0x4f2fbd['fields'],function(_0x1d426e){return _0x46458d[_0x51f1('0x6e')](_0x1d426e[_0x51f1('0x25')])||!_0x4a95d8['includes'](_0x1d426e[_0x51f1('0x2c')]);});return{'count':_0x4f2fbd[_0x51f1('0x32')][_0x51f1('0x34')],'rows':_[_0x51f1('0x28')](_0x4f2fbd[_0x51f1('0x32')],function(_0x3a179a){return{'id':_0x3a179a['name'],'name':_0x3a179a[_0x51f1('0x6f')],'custom':_0x3a179a[_0x51f1('0x70')],'options':_0x3a179a[_0x51f1('0x71')][_0x51f1('0x34')]>0x0?_[_0x51f1('0x28')](_0x3a179a[_0x51f1('0x71')],function(_0x3d5e5d){return{'name':_0x3d5e5d[_0x51f1('0x6f')],'value':_0x3d5e5d[_0x51f1('0x72')]};}):_0x3a179a['type']==_0x51f1('0x69')?[{'name':'True','value':_0x51f1('0x73')},{'name':_0x51f1('0x74'),'value':_0x51f1('0x75')}]:[]};})};}})[_0x51f1('0x21')](respondWithResult(_0x2274a3,null))[_0x51f1('0x44')](function(_0x4c38fa){var _0x259897=_0x54f41e[_0x51f1('0x2f')][_0x51f1('0x76')]?0x1f4:_0x4c38fa['statusCode']||0x1f4;logger[_0x51f1('0x23')](_0x51f1('0x77'),_0x51f1('0x78'),_0x259897,JSON['stringify'](_0x4c38fa));delete _0x4c38fa['name'];if(_0x259897===0x191){_0x259897=0x190;}_0x2274a3[_0x51f1('0x17')](_0x259897)[_0x51f1('0x26')](_0x54f41e[_0x51f1('0x2f')][_0x51f1('0x76')]?{'message':_0x51f1('0x79'),'statusCode':_0x4c38fa['statusCode']}:_0x4c38fa);});};