Built motion from commit ddca4d0d.|2.5.47
[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 _0x4c1d=['sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../mysqldb','../../components/integrations/configuration','sendStatus','end','status','offset','undefined','limit','count','Content-Range','json','apply','reject','save','update','destroy','stack','send','index','map','SalesforceAccount','rawAttributes','fieldName','name','query','keys','model','fields','attributes','length','hasOwnProperty','order','sort','where','filters','pick','filter','merge','type','VIRTUAL','options','then','includeAll','rows','catch','show','params','intersection','include','find','create','body','getConfigurations','findOne','SalesforceConfiguration','nolimit','addConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','SalesforceField','getFields','clientSecret','username','securityToken','remoteUri','slice','Connection','clientId','login','concat','ticketType','sobject','describe','ActivityDate','IsReminderSet','IsRecurrence','Subject','Description','int','boolean','email','textarea','phone','remove','includes','label','custom','picklistValues','value','True','true','False','false','test','statusCode','error','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','eml-format','zip-dir','fast-json-patch','moment','bluebird','util','path'];(function(_0x397256,_0x22d86d){var _0x52dccb=function(_0x3458c8){while(--_0x3458c8){_0x397256['push'](_0x397256['shift']());}};_0x52dccb(++_0x22d86d);}(_0x4c1d,0xf3));var _0xd4c1=function(_0x5dec4c,_0x5ae5f9){_0x5dec4c=_0x5dec4c-0x0;var _0x131fb5=_0x4c1d[_0x5dec4c];return _0x131fb5;};'use strict';var emlformat=require(_0xd4c1('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xd4c1('0x1'));var jsonpatch=require(_0xd4c1('0x2'));var rp=require('request-promise');var moment=require(_0xd4c1('0x3'));var BPromise=require(_0xd4c1('0x4'));var Mustache=require('mustache');var util=require(_0xd4c1('0x5'));var path=require(_0xd4c1('0x6'));var sox=require(_0xd4c1('0x7'));var csv=require(_0xd4c1('0x8'));var ejs=require(_0xd4c1('0x9'));var fs=require('fs');var fs_extra=require(_0xd4c1('0xa'));var _=require('lodash');var squel=require(_0xd4c1('0xb'));var crypto=require(_0xd4c1('0xc'));var jsforce=require(_0xd4c1('0xd'));var deskjs=require(_0xd4c1('0xe'));var toCsv=require('to-csv');var querystring=require(_0xd4c1('0xf'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0xd4c1('0x10'));var qs=require(_0xd4c1('0x11'));var as=require(_0xd4c1('0x12'));var hardwareService=require(_0xd4c1('0x13'));var logger=require(_0xd4c1('0x14'))(_0xd4c1('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xd4c1('0x16'))['db'];var integrations=require(_0xd4c1('0x17'));function respondWithStatusCode(_0x173603,_0x4639d2){_0x4639d2=_0x4639d2||0xcc;return function(_0x5e110e){if(_0x5e110e){return _0x173603[_0xd4c1('0x18')](_0x4639d2);}return _0x173603['status'](_0x4639d2)[_0xd4c1('0x19')]();};}function respondWithResult(_0x837676,_0x585f1a){_0x585f1a=_0x585f1a||0xc8;return function(_0xb1c147){if(_0xb1c147){return _0x837676[_0xd4c1('0x1a')](_0x585f1a)['json'](_0xb1c147);}};}function respondWithFilteredResult(_0x49f385,_0x2d6a5d){return function(_0x50239f){if(_0x50239f){var _0x48cab2=typeof _0x2d6a5d[_0xd4c1('0x1b')]===_0xd4c1('0x1c')&&typeof _0x2d6a5d[_0xd4c1('0x1d')]==='undefined';var _0x4a80bc=_0x50239f[_0xd4c1('0x1e')];var _0x1c3983=_0x48cab2?0x0:_0x2d6a5d[_0xd4c1('0x1b')];var _0x95fb40=_0x48cab2?_0x50239f[_0xd4c1('0x1e')]:_0x2d6a5d[_0xd4c1('0x1b')]+_0x2d6a5d[_0xd4c1('0x1d')];var _0x2f42c2;if(_0x95fb40>=_0x4a80bc){_0x95fb40=_0x4a80bc;_0x2f42c2=0xc8;}else{_0x2f42c2=0xce;}_0x49f385[_0xd4c1('0x1a')](_0x2f42c2);return _0x49f385['set'](_0xd4c1('0x1f'),_0x1c3983+'-'+_0x95fb40+'/'+_0x4a80bc)[_0xd4c1('0x20')](_0x50239f);}return null;};}function patchUpdates(_0x4e2831){return function(_0x3ecee6){try{jsonpatch[_0xd4c1('0x21')](_0x3ecee6,_0x4e2831,!![]);}catch(_0x32ae98){return BPromise[_0xd4c1('0x22')](_0x32ae98);}return _0x3ecee6[_0xd4c1('0x23')]();};}function saveUpdates(_0x517996,_0x3d5650){return function(_0x2152ba){if(_0x2152ba){return _0x2152ba[_0xd4c1('0x24')](_0x517996)['then'](function(_0x6613ed){return _0x6613ed;});}return null;};}function removeEntity(_0xef567d,_0x130b66){return function(_0x301f22){if(_0x301f22){return _0x301f22[_0xd4c1('0x25')]()['then'](function(){_0xef567d[_0xd4c1('0x1a')](0xcc)[_0xd4c1('0x19')]();});}};}function handleEntityNotFound(_0x250150,_0x2a81d0){return function(_0x36da73){if(!_0x36da73){_0x250150[_0xd4c1('0x18')](0x194);}return _0x36da73;};}function handleError(_0x25e878,_0x19c7ce){_0x19c7ce=_0x19c7ce||0x1f4;return function(_0x4fafee){logger['error'](_0x4fafee[_0xd4c1('0x26')]);if(_0x4fafee['name']){delete _0x4fafee['name'];}_0x25e878[_0xd4c1('0x1a')](_0x19c7ce)[_0xd4c1('0x27')](_0x4fafee);};}exports[_0xd4c1('0x28')]=function(_0xf64a2d,_0x503344){var _0x3279a3={},_0x32bcfb={},_0x544131={'count':0x0,'rows':[]};var _0x588a2e=_[_0xd4c1('0x29')](db[_0xd4c1('0x2a')][_0xd4c1('0x2b')],function(_0x2bf768){return{'name':_0x2bf768[_0xd4c1('0x2c')],'type':_0x2bf768['type']['key']};});_0x32bcfb['model']=_['map'](_0x588a2e,_0xd4c1('0x2d'));_0x32bcfb[_0xd4c1('0x2e')]=_[_0xd4c1('0x2f')](_0xf64a2d[_0xd4c1('0x2e')]);_0x32bcfb['filters']=_['intersection'](_0x32bcfb[_0xd4c1('0x30')],_0x32bcfb[_0xd4c1('0x2e')]);_0x3279a3['attributes']=_['intersection'](_0x32bcfb['model'],qs[_0xd4c1('0x31')](_0xf64a2d[_0xd4c1('0x2e')]['fields']));_0x3279a3[_0xd4c1('0x32')]=_0x3279a3[_0xd4c1('0x32')][_0xd4c1('0x33')]?_0x3279a3[_0xd4c1('0x32')]:_0x32bcfb[_0xd4c1('0x30')];if(!_0xf64a2d[_0xd4c1('0x2e')][_0xd4c1('0x34')]('nolimit')){_0x3279a3[_0xd4c1('0x1d')]=qs['limit'](_0xf64a2d[_0xd4c1('0x2e')][_0xd4c1('0x1d')]);_0x3279a3[_0xd4c1('0x1b')]=qs[_0xd4c1('0x1b')](_0xf64a2d['query'][_0xd4c1('0x1b')]);}_0x3279a3[_0xd4c1('0x35')]=qs['sort'](_0xf64a2d[_0xd4c1('0x2e')][_0xd4c1('0x36')]);_0x3279a3[_0xd4c1('0x37')]=qs[_0xd4c1('0x38')](_[_0xd4c1('0x39')](_0xf64a2d[_0xd4c1('0x2e')],_0x32bcfb['filters']),_0x588a2e);if(_0xf64a2d[_0xd4c1('0x2e')][_0xd4c1('0x3a')]){_0x3279a3[_0xd4c1('0x37')]=_[_0xd4c1('0x3b')](_0x3279a3[_0xd4c1('0x37')],{'$or':_[_0xd4c1('0x29')](_0x588a2e,function(_0x53f950){if(_0x53f950[_0xd4c1('0x3c')]!==_0xd4c1('0x3d')){var _0x382839={};_0x382839[_0x53f950['name']]={'$like':'%'+_0xf64a2d[_0xd4c1('0x2e')][_0xd4c1('0x3a')]+'%'};return _0x382839;}})});}_0x3279a3=_[_0xd4c1('0x3b')]({},_0x3279a3,_0xf64a2d[_0xd4c1('0x3e')]);var _0x4195af={'where':_0x3279a3[_0xd4c1('0x37')]};return db[_0xd4c1('0x2a')][_0xd4c1('0x1e')](_0x4195af)[_0xd4c1('0x3f')](function(_0x3e5a8d){_0x544131[_0xd4c1('0x1e')]=_0x3e5a8d;if(_0xf64a2d['query'][_0xd4c1('0x40')]){_0x3279a3['include']=[{'all':!![]}];}return db['SalesforceAccount']['findAll'](_0x3279a3);})['then'](function(_0x47d430){_0x544131[_0xd4c1('0x41')]=_0x47d430;return _0x544131;})[_0xd4c1('0x3f')](respondWithFilteredResult(_0x503344,_0x3279a3))[_0xd4c1('0x42')](handleError(_0x503344,null));};exports[_0xd4c1('0x43')]=function(_0x4b6c7f,_0xb84b65){var _0x580abb={'raw':![],'where':{'id':_0x4b6c7f[_0xd4c1('0x44')]['id']}},_0x5dc2c8={};_0x5dc2c8['model']=_['keys'](db[_0xd4c1('0x2a')]['rawAttributes']);_0x5dc2c8[_0xd4c1('0x2e')]=_[_0xd4c1('0x2f')](_0x4b6c7f[_0xd4c1('0x2e')]);_0x5dc2c8[_0xd4c1('0x38')]=_[_0xd4c1('0x45')](_0x5dc2c8[_0xd4c1('0x30')],_0x5dc2c8[_0xd4c1('0x2e')]);_0x580abb[_0xd4c1('0x32')]=_[_0xd4c1('0x45')](_0x5dc2c8[_0xd4c1('0x30')],qs['fields'](_0x4b6c7f[_0xd4c1('0x2e')]['fields']));_0x580abb[_0xd4c1('0x32')]=_0x580abb[_0xd4c1('0x32')][_0xd4c1('0x33')]?_0x580abb[_0xd4c1('0x32')]:_0x5dc2c8[_0xd4c1('0x30')];if(_0x4b6c7f[_0xd4c1('0x2e')][_0xd4c1('0x40')]){_0x580abb[_0xd4c1('0x46')]=[{'all':!![]}];}_0x580abb=_[_0xd4c1('0x3b')]({},_0x580abb,_0x4b6c7f[_0xd4c1('0x3e')]);return db[_0xd4c1('0x2a')][_0xd4c1('0x47')](_0x580abb)[_0xd4c1('0x3f')](handleEntityNotFound(_0xb84b65,null))[_0xd4c1('0x3f')](respondWithResult(_0xb84b65,null))[_0xd4c1('0x42')](handleError(_0xb84b65,null));};exports[_0xd4c1('0x48')]=function(_0x1bd9ad,_0x1bc249){return db['SalesforceAccount']['create'](_0x1bd9ad[_0xd4c1('0x49')],{})[_0xd4c1('0x3f')](respondWithResult(_0x1bc249,0xc9))[_0xd4c1('0x42')](handleError(_0x1bc249,null));};exports[_0xd4c1('0x24')]=function(_0x4cd40b,_0x11865b){if(_0x4cd40b['body']['id']){delete _0x4cd40b[_0xd4c1('0x49')]['id'];}return db['SalesforceAccount'][_0xd4c1('0x47')]({'where':{'id':_0x4cd40b[_0xd4c1('0x44')]['id']}})['then'](handleEntityNotFound(_0x11865b,null))['then'](saveUpdates(_0x4cd40b[_0xd4c1('0x49')],null))[_0xd4c1('0x3f')](respondWithResult(_0x11865b,null))[_0xd4c1('0x42')](handleError(_0x11865b,null));};exports[_0xd4c1('0x25')]=function(_0x4bffc9,_0x36b1ca){return db[_0xd4c1('0x2a')][_0xd4c1('0x47')]({'where':{'id':_0x4bffc9[_0xd4c1('0x44')]['id']}})[_0xd4c1('0x3f')](handleEntityNotFound(_0x36b1ca,null))[_0xd4c1('0x3f')](removeEntity(_0x36b1ca,null))[_0xd4c1('0x42')](handleError(_0x36b1ca,null));};exports[_0xd4c1('0x4a')]=function(_0x192788,_0xc76bd0,_0x952493){var _0x2eb3e9={};var _0x50f6b3={};var _0x20ee5d;var _0x479587;return db[_0xd4c1('0x2a')][_0xd4c1('0x4b')]({'where':{'id':_0x192788[_0xd4c1('0x44')]['id']}})[_0xd4c1('0x3f')](handleEntityNotFound(_0xc76bd0,null))[_0xd4c1('0x3f')](function(_0x21bb02){if(_0x21bb02){_0x20ee5d=_0x21bb02;_0x50f6b3['model']=_[_0xd4c1('0x2f')](db[_0xd4c1('0x4c')]['rawAttributes']);_0x50f6b3['query']=_[_0xd4c1('0x2f')](_0x192788[_0xd4c1('0x2e')]);_0x50f6b3[_0xd4c1('0x38')]=_[_0xd4c1('0x45')](_0x50f6b3[_0xd4c1('0x30')],_0x50f6b3[_0xd4c1('0x2e')]);_0x2eb3e9[_0xd4c1('0x32')]=_[_0xd4c1('0x45')](_0x50f6b3['model'],qs[_0xd4c1('0x31')](_0x192788[_0xd4c1('0x2e')]['fields']));_0x2eb3e9[_0xd4c1('0x32')]=_0x2eb3e9[_0xd4c1('0x32')][_0xd4c1('0x33')]?_0x2eb3e9[_0xd4c1('0x32')]:_0x50f6b3['model'];_0x2eb3e9[_0xd4c1('0x35')]=qs['sort'](_0x192788[_0xd4c1('0x2e')][_0xd4c1('0x36')]);_0x2eb3e9[_0xd4c1('0x37')]=qs[_0xd4c1('0x38')](_['pick'](_0x192788[_0xd4c1('0x2e')],_0x50f6b3['filters']));if(_0x192788[_0xd4c1('0x2e')][_0xd4c1('0x3a')]){_0x2eb3e9[_0xd4c1('0x37')]=_['merge'](_0x2eb3e9['where'],{'$or':_[_0xd4c1('0x29')](_0x2eb3e9['attributes'],function(_0x43ed90){var _0x2a8611={};_0x2a8611[_0x43ed90]={'$like':'%'+_0x192788['query']['filter']+'%'};return _0x2a8611;})});}_0x2eb3e9=_[_0xd4c1('0x3b')]({},_0x2eb3e9,_0x192788[_0xd4c1('0x3e')]);return _0x20ee5d[_0xd4c1('0x4a')](_0x2eb3e9);}})['then'](function(_0x80ddec){if(_0x80ddec){_0x479587=_0x80ddec['length'];if(!_0x192788['query'][_0xd4c1('0x34')](_0xd4c1('0x4d'))){_0x2eb3e9['limit']=qs[_0xd4c1('0x1d')](_0x192788['query'][_0xd4c1('0x1d')]);_0x2eb3e9[_0xd4c1('0x1b')]=qs[_0xd4c1('0x1b')](_0x192788[_0xd4c1('0x2e')][_0xd4c1('0x1b')]);}return _0x20ee5d['getConfigurations'](_0x2eb3e9);}})[_0xd4c1('0x3f')](function(_0x398532){if(_0x398532){return _0x398532?{'count':_0x479587,'rows':_0x398532}:null;}})[_0xd4c1('0x3f')](respondWithResult(_0xc76bd0,null))[_0xd4c1('0x42')](handleError(_0xc76bd0,null));};exports[_0xd4c1('0x4e')]=function(_0x3bd292,_0x13e2e7,_0x4c0383){if(_0x3bd292[_0xd4c1('0x49')]['id']){delete _0x3bd292['body']['id'];}return db[_0xd4c1('0x2a')][_0xd4c1('0x4b')]({'where':{'id':_0x3bd292[_0xd4c1('0x44')]['id']}})[_0xd4c1('0x3f')](handleEntityNotFound(_0x13e2e7,null))[_0xd4c1('0x3f')](function(_0x2b6417){if(_0x2b6417){_0x3bd292[_0xd4c1('0x49')][_0xd4c1('0x4f')]=_0x2b6417['id'];_0x3bd292[_0xd4c1('0x49')][_0xd4c1('0x50')]=integrations[_0xd4c1('0x51')](_0x3bd292['body'][_0xd4c1('0x52')],_0x3bd292[_0xd4c1('0x49')][_0xd4c1('0x3c')]);_0x3bd292[_0xd4c1('0x49')][_0xd4c1('0x53')]=integrations[_0xd4c1('0x54')](_0x3bd292[_0xd4c1('0x49')][_0xd4c1('0x52')],_0x3bd292['body']['type']);return db[_0xd4c1('0x4c')]['create'](_0x3bd292[_0xd4c1('0x49')],{'include':[{'model':db[_0xd4c1('0x55')],'as':_0xd4c1('0x50')},{'model':db[_0xd4c1('0x55')],'as':'Descriptions'}]});}return null;})[_0xd4c1('0x3f')](respondWithResult(_0x13e2e7,null))['catch'](handleError(_0x13e2e7,null));};exports[_0xd4c1('0x56')]=function(_0x562a66,_0x333e5d,_0x36db0e){var _0x28ab2b;var _0x58f3a7='';var _0x4084b4='Task';return db['SalesforceAccount']['findOne']({'where':{'id':_0x562a66['params']['id']},'attributes':['id','remoteUri','clientId',_0xd4c1('0x57'),_0xd4c1('0x58'),'password',_0xd4c1('0x59')]})[_0xd4c1('0x3f')](handleEntityNotFound(_0x333e5d,null))[_0xd4c1('0x3f')](function(_0x206da4){if(_0x206da4){_0x58f3a7=_0x206da4[_0xd4c1('0x5a')];var _0xeaa57d=_0x58f3a7[_0xd4c1('0x5b')](-0x1);if(_0xeaa57d==='/'){_0x58f3a7=_0x58f3a7['substring'](0x0,_0x58f3a7['lastIndexOf'](_0xeaa57d));}_0x28ab2b=new jsforce[(_0xd4c1('0x5c'))]({'oauth2':{'loginUrl':_0x58f3a7,'clientId':_0x206da4[_0xd4c1('0x5d')],'clientSecret':_0x206da4[_0xd4c1('0x57')]}});return _0x28ab2b[_0xd4c1('0x5e')](_0x206da4[_0xd4c1('0x58')],_0x206da4['password'][_0xd4c1('0x5f')](_0x206da4[_0xd4c1('0x59')]));}})[_0xd4c1('0x3f')](function(_0x2be3af){if(_0x2be3af){if(!_['isNil'](_0x562a66[_0xd4c1('0x2e')]['ticketType'])){_0x4084b4=_0x562a66[_0xd4c1('0x2e')][_0xd4c1('0x60')];}return _0x28ab2b[_0xd4c1('0x61')](_0x4084b4)[_0xd4c1('0x62')]();}})['then'](function(_0x51d304){var _0x31c221=['OwnerId',_0xd4c1('0x63'),_0xd4c1('0x64'),_0xd4c1('0x65'),_0xd4c1('0x66'),_0xd4c1('0x67')];var _0x1aafe5=['string',_0xd4c1('0x68'),'picklist',_0xd4c1('0x69'),_0xd4c1('0x6a'),_0xd4c1('0x6b'),_0xd4c1('0x6c')];if(_0x51d304){_[_0xd4c1('0x6d')](_0x51d304[_0xd4c1('0x31')],function(_0x43f23e){return _0x31c221[_0xd4c1('0x6e')](_0x43f23e[_0xd4c1('0x2d')])||!_0x1aafe5['includes'](_0x43f23e[_0xd4c1('0x3c')]);});return{'count':_0x51d304[_0xd4c1('0x31')][_0xd4c1('0x33')],'rows':_[_0xd4c1('0x29')](_0x51d304[_0xd4c1('0x31')],function(_0x14394a){return{'id':_0x14394a[_0xd4c1('0x2d')],'name':_0x14394a[_0xd4c1('0x6f')],'custom':_0x14394a[_0xd4c1('0x70')],'options':_0x14394a[_0xd4c1('0x71')]['length']>0x0?_[_0xd4c1('0x29')](_0x14394a['picklistValues'],function(_0x4b4a3d){return{'name':_0x4b4a3d['label'],'value':_0x4b4a3d[_0xd4c1('0x72')]};}):_0x14394a['type']=='boolean'?[{'name':_0xd4c1('0x73'),'value':_0xd4c1('0x74')},{'name':_0xd4c1('0x75'),'value':_0xd4c1('0x76')}]:[]};})};}})[_0xd4c1('0x3f')](respondWithResult(_0x333e5d,null))[_0xd4c1('0x42')](function(_0x4a401b){var _0x2baf1f=_0x562a66['query'][_0xd4c1('0x77')]?0x1f4:_0x4a401b[_0xd4c1('0x78')]||0x1f4;logger[_0xd4c1('0x79')](_0xd4c1('0x7a'),_0xd4c1('0x56'),_0x2baf1f,JSON[_0xd4c1('0x7b')](_0x4a401b));delete _0x4a401b[_0xd4c1('0x2d')];if(_0x2baf1f===0x191){_0x2baf1f=0x190;}_0x333e5d[_0xd4c1('0x1a')](_0x2baf1f)[_0xd4c1('0x27')](_0x562a66['query'][_0xd4c1('0x77')]?{'message':_0xd4c1('0x7c'),'statusCode':_0x4a401b[_0xd4c1('0x78')]}:_0x4a401b);});};