d5e73747d12ba0d06e6635624b361895deed682c
[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 _0x48f4=['clientSecret','password','slice','substring','Connection','login','concat','securityToken','isNil','sobject','describe','IsReminderSet','IsRecurrence','Subject','Description','string','int','picklist','boolean','email','textarea','phone','includes','label','custom','picklistValues','value','True','true','False','false','statusCode','stringify','eml-format','fast-json-patch','request-promise','moment','util','path','sox','to-csv','fs-extra','squel','jsforce','desk.js','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','limit','undefined','count','offset','set','Content-Range','json','apply','save','then','destroy','error','stack','name','send','index','map','SalesforceAccount','rawAttributes','fieldName','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','include','rows','show','params','catch','create','body','update','find','getConfigurations','findOne','SalesforceConfiguration','addConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','getFields','Task','remoteUri','clientId'];(function(_0x267096,_0x549583){var _0x4c4847=function(_0x5a56f5){while(--_0x5a56f5){_0x267096['push'](_0x267096['shift']());}};_0x4c4847(++_0x549583);}(_0x48f4,0x9c));var _0x448f=function(_0x1602ef,_0x447aba){_0x1602ef=_0x1602ef-0x0;var _0x5870ee=_0x48f4[_0x1602ef];return _0x5870ee;};'use strict';var emlformat=require(_0x448f('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x448f('0x1'));var rp=require(_0x448f('0x2'));var moment=require(_0x448f('0x3'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x448f('0x4'));var path=require(_0x448f('0x5'));var sox=require(_0x448f('0x6'));var csv=require(_0x448f('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x448f('0x8'));var _=require('lodash');var squel=require(_0x448f('0x9'));var crypto=require('crypto');var jsforce=require(_0x448f('0xa'));var deskjs=require(_0x448f('0xb'));var toCsv=require(_0x448f('0x7'));var querystring=require('querystring');var Papa=require(_0x448f('0xc'));var Redis=require(_0x448f('0xd'));var authService=require('../../components/auth/service');var qs=require(_0x448f('0xe'));var as=require(_0x448f('0xf'));var hardwareService=require(_0x448f('0x10'));var logger=require(_0x448f('0x11'))(_0x448f('0x12'));var utils=require(_0x448f('0x13'));var config=require(_0x448f('0x14'));var licenseUtil=require(_0x448f('0x15'));var db=require(_0x448f('0x16'))['db'];var integrations=require(_0x448f('0x17'));function respondWithStatusCode(_0x16b942,_0x4bd142){_0x4bd142=_0x4bd142||0xcc;return function(_0x2cf3eb){if(_0x2cf3eb){return _0x16b942[_0x448f('0x18')](_0x4bd142);}return _0x16b942[_0x448f('0x19')](_0x4bd142)[_0x448f('0x1a')]();};}function respondWithResult(_0x5e7d88,_0x4bae0f){_0x4bae0f=_0x4bae0f||0xc8;return function(_0x1ae7ba){if(_0x1ae7ba){return _0x5e7d88[_0x448f('0x19')](_0x4bae0f)['json'](_0x1ae7ba);}};}function respondWithFilteredResult(_0x161d10,_0x37ff73){return function(_0x5a8443){if(_0x5a8443){var _0x11e2bf=typeof _0x37ff73['offset']==='undefined'&&typeof _0x37ff73[_0x448f('0x1b')]===_0x448f('0x1c');var _0x254b12=_0x5a8443[_0x448f('0x1d')];var _0x2382fe=_0x11e2bf?0x0:_0x37ff73[_0x448f('0x1e')];var _0x4b667e=_0x11e2bf?_0x5a8443[_0x448f('0x1d')]:_0x37ff73[_0x448f('0x1e')]+_0x37ff73['limit'];var _0x3e8d48;if(_0x4b667e>=_0x254b12){_0x4b667e=_0x254b12;_0x3e8d48=0xc8;}else{_0x3e8d48=0xce;}_0x161d10[_0x448f('0x19')](_0x3e8d48);return _0x161d10[_0x448f('0x1f')](_0x448f('0x20'),_0x2382fe+'-'+_0x4b667e+'/'+_0x254b12)[_0x448f('0x21')](_0x5a8443);}return null;};}function patchUpdates(_0x1205ae){return function(_0x1526bc){try{jsonpatch[_0x448f('0x22')](_0x1526bc,_0x1205ae,!![]);}catch(_0x2c6c1c){return BPromise['reject'](_0x2c6c1c);}return _0x1526bc[_0x448f('0x23')]();};}function saveUpdates(_0x1a0ed9,_0x5ea365){return function(_0x3d90eb){if(_0x3d90eb){return _0x3d90eb['update'](_0x1a0ed9)[_0x448f('0x24')](function(_0x4a06ba){return _0x4a06ba;});}return null;};}function removeEntity(_0x36142a,_0x417b64){return function(_0x2ac526){if(_0x2ac526){return _0x2ac526[_0x448f('0x25')]()[_0x448f('0x24')](function(){_0x36142a[_0x448f('0x19')](0xcc)[_0x448f('0x1a')]();});}};}function handleEntityNotFound(_0x24b062,_0xb8303d){return function(_0x30fd7a){if(!_0x30fd7a){_0x24b062['sendStatus'](0x194);}return _0x30fd7a;};}function handleError(_0x5a46f0,_0x57bee4){_0x57bee4=_0x57bee4||0x1f4;return function(_0xe1a418){logger[_0x448f('0x26')](_0xe1a418[_0x448f('0x27')]);if(_0xe1a418['name']){delete _0xe1a418[_0x448f('0x28')];}_0x5a46f0[_0x448f('0x19')](_0x57bee4)[_0x448f('0x29')](_0xe1a418);};}exports[_0x448f('0x2a')]=function(_0xe55692,_0x2c3958){var _0x220e8e={},_0x70a041={},_0x4475d8={'count':0x0,'rows':[]};var _0xd4662e=_[_0x448f('0x2b')](db[_0x448f('0x2c')][_0x448f('0x2d')],function(_0x130386){return{'name':_0x130386[_0x448f('0x2e')],'type':_0x130386['type'][_0x448f('0x2f')]};});_0x70a041[_0x448f('0x30')]=_[_0x448f('0x2b')](_0xd4662e,_0x448f('0x28'));_0x70a041[_0x448f('0x31')]=_[_0x448f('0x32')](_0xe55692[_0x448f('0x31')]);_0x70a041[_0x448f('0x33')]=_[_0x448f('0x34')](_0x70a041[_0x448f('0x30')],_0x70a041['query']);_0x220e8e['attributes']=_[_0x448f('0x34')](_0x70a041[_0x448f('0x30')],qs[_0x448f('0x35')](_0xe55692[_0x448f('0x31')]['fields']));_0x220e8e[_0x448f('0x36')]=_0x220e8e[_0x448f('0x36')][_0x448f('0x37')]?_0x220e8e[_0x448f('0x36')]:_0x70a041['model'];if(!_0xe55692[_0x448f('0x31')][_0x448f('0x38')](_0x448f('0x39'))){_0x220e8e[_0x448f('0x1b')]=qs[_0x448f('0x1b')](_0xe55692[_0x448f('0x31')][_0x448f('0x1b')]);_0x220e8e['offset']=qs[_0x448f('0x1e')](_0xe55692[_0x448f('0x31')][_0x448f('0x1e')]);}_0x220e8e[_0x448f('0x3a')]=qs[_0x448f('0x3b')](_0xe55692[_0x448f('0x31')][_0x448f('0x3b')]);_0x220e8e[_0x448f('0x3c')]=qs[_0x448f('0x33')](_[_0x448f('0x3d')](_0xe55692[_0x448f('0x31')],_0x70a041['filters']),_0xd4662e);if(_0xe55692[_0x448f('0x31')][_0x448f('0x3e')]){_0x220e8e[_0x448f('0x3c')]=_[_0x448f('0x3f')](_0x220e8e['where'],{'$or':_[_0x448f('0x2b')](_0xd4662e,function(_0x1886d0){if(_0x1886d0[_0x448f('0x40')]!==_0x448f('0x41')){var _0x5854e9={};_0x5854e9[_0x1886d0[_0x448f('0x28')]]={'$like':'%'+_0xe55692[_0x448f('0x31')][_0x448f('0x3e')]+'%'};return _0x5854e9;}})});}_0x220e8e=_[_0x448f('0x3f')]({},_0x220e8e,_0xe55692[_0x448f('0x42')]);var _0x38f208={'where':_0x220e8e['where']};return db[_0x448f('0x2c')][_0x448f('0x1d')](_0x38f208)[_0x448f('0x24')](function(_0x5f713b){_0x4475d8[_0x448f('0x1d')]=_0x5f713b;if(_0xe55692[_0x448f('0x31')][_0x448f('0x43')]){_0x220e8e[_0x448f('0x44')]=[{'all':!![]}];}return db[_0x448f('0x2c')]['findAll'](_0x220e8e);})[_0x448f('0x24')](function(_0x42e658){_0x4475d8[_0x448f('0x45')]=_0x42e658;return _0x4475d8;})[_0x448f('0x24')](respondWithFilteredResult(_0x2c3958,_0x220e8e))['catch'](handleError(_0x2c3958,null));};exports[_0x448f('0x46')]=function(_0x228204,_0x5d519e){var _0x1d1f98={'raw':![],'where':{'id':_0x228204[_0x448f('0x47')]['id']}},_0x4bff03={};_0x4bff03[_0x448f('0x30')]=_['keys'](db[_0x448f('0x2c')][_0x448f('0x2d')]);_0x4bff03[_0x448f('0x31')]=_[_0x448f('0x32')](_0x228204[_0x448f('0x31')]);_0x4bff03[_0x448f('0x33')]=_[_0x448f('0x34')](_0x4bff03[_0x448f('0x30')],_0x4bff03['query']);_0x1d1f98[_0x448f('0x36')]=_[_0x448f('0x34')](_0x4bff03[_0x448f('0x30')],qs[_0x448f('0x35')](_0x228204[_0x448f('0x31')]['fields']));_0x1d1f98[_0x448f('0x36')]=_0x1d1f98[_0x448f('0x36')][_0x448f('0x37')]?_0x1d1f98[_0x448f('0x36')]:_0x4bff03['model'];if(_0x228204[_0x448f('0x31')][_0x448f('0x43')]){_0x1d1f98[_0x448f('0x44')]=[{'all':!![]}];}_0x1d1f98=_[_0x448f('0x3f')]({},_0x1d1f98,_0x228204['options']);return db[_0x448f('0x2c')]['find'](_0x1d1f98)['then'](handleEntityNotFound(_0x5d519e,null))[_0x448f('0x24')](respondWithResult(_0x5d519e,null))[_0x448f('0x48')](handleError(_0x5d519e,null));};exports[_0x448f('0x49')]=function(_0x2aa7a8,_0x554939){return db[_0x448f('0x2c')][_0x448f('0x49')](_0x2aa7a8[_0x448f('0x4a')],{})[_0x448f('0x24')](respondWithResult(_0x554939,0xc9))['catch'](handleError(_0x554939,null));};exports[_0x448f('0x4b')]=function(_0xfc45a1,_0x56a966){if(_0xfc45a1[_0x448f('0x4a')]['id']){delete _0xfc45a1['body']['id'];}return db[_0x448f('0x2c')][_0x448f('0x4c')]({'where':{'id':_0xfc45a1[_0x448f('0x47')]['id']}})[_0x448f('0x24')](handleEntityNotFound(_0x56a966,null))[_0x448f('0x24')](saveUpdates(_0xfc45a1[_0x448f('0x4a')],null))[_0x448f('0x24')](respondWithResult(_0x56a966,null))[_0x448f('0x48')](handleError(_0x56a966,null));};exports[_0x448f('0x25')]=function(_0x5aed5b,_0x35534b){return db['SalesforceAccount'][_0x448f('0x4c')]({'where':{'id':_0x5aed5b[_0x448f('0x47')]['id']}})[_0x448f('0x24')](handleEntityNotFound(_0x35534b,null))['then'](removeEntity(_0x35534b,null))[_0x448f('0x48')](handleError(_0x35534b,null));};exports[_0x448f('0x4d')]=function(_0x169af3,_0x1d3dc0,_0x45e04b){var _0x51b06f={};var _0x2f807e={};var _0xaa36f4;var _0xdf7612;return db[_0x448f('0x2c')][_0x448f('0x4e')]({'where':{'id':_0x169af3[_0x448f('0x47')]['id']}})[_0x448f('0x24')](handleEntityNotFound(_0x1d3dc0,null))[_0x448f('0x24')](function(_0x16b065){if(_0x16b065){_0xaa36f4=_0x16b065;_0x2f807e[_0x448f('0x30')]=_[_0x448f('0x32')](db[_0x448f('0x4f')][_0x448f('0x2d')]);_0x2f807e[_0x448f('0x31')]=_[_0x448f('0x32')](_0x169af3['query']);_0x2f807e[_0x448f('0x33')]=_[_0x448f('0x34')](_0x2f807e[_0x448f('0x30')],_0x2f807e[_0x448f('0x31')]);_0x51b06f['attributes']=_['intersection'](_0x2f807e[_0x448f('0x30')],qs[_0x448f('0x35')](_0x169af3['query'][_0x448f('0x35')]));_0x51b06f[_0x448f('0x36')]=_0x51b06f[_0x448f('0x36')][_0x448f('0x37')]?_0x51b06f[_0x448f('0x36')]:_0x2f807e[_0x448f('0x30')];_0x51b06f['order']=qs[_0x448f('0x3b')](_0x169af3[_0x448f('0x31')][_0x448f('0x3b')]);_0x51b06f[_0x448f('0x3c')]=qs[_0x448f('0x33')](_[_0x448f('0x3d')](_0x169af3['query'],_0x2f807e[_0x448f('0x33')]));if(_0x169af3[_0x448f('0x31')]['filter']){_0x51b06f['where']=_[_0x448f('0x3f')](_0x51b06f[_0x448f('0x3c')],{'$or':_[_0x448f('0x2b')](_0x51b06f[_0x448f('0x36')],function(_0x59a02a){var _0x1447d5={};_0x1447d5[_0x59a02a]={'$like':'%'+_0x169af3['query'][_0x448f('0x3e')]+'%'};return _0x1447d5;})});}_0x51b06f=_[_0x448f('0x3f')]({},_0x51b06f,_0x169af3['options']);return _0xaa36f4[_0x448f('0x4d')](_0x51b06f);}})[_0x448f('0x24')](function(_0x4a8f68){if(_0x4a8f68){_0xdf7612=_0x4a8f68['length'];if(!_0x169af3['query']['hasOwnProperty'](_0x448f('0x39'))){_0x51b06f[_0x448f('0x1b')]=qs[_0x448f('0x1b')](_0x169af3['query'][_0x448f('0x1b')]);_0x51b06f[_0x448f('0x1e')]=qs[_0x448f('0x1e')](_0x169af3[_0x448f('0x31')][_0x448f('0x1e')]);}return _0xaa36f4[_0x448f('0x4d')](_0x51b06f);}})[_0x448f('0x24')](function(_0x49ed10){if(_0x49ed10){return _0x49ed10?{'count':_0xdf7612,'rows':_0x49ed10}:null;}})[_0x448f('0x24')](respondWithResult(_0x1d3dc0,null))[_0x448f('0x48')](handleError(_0x1d3dc0,null));};exports[_0x448f('0x50')]=function(_0x20d844,_0x3cf608,_0x3f429b){if(_0x20d844[_0x448f('0x4a')]['id']){delete _0x20d844['body']['id'];}return db[_0x448f('0x2c')][_0x448f('0x4e')]({'where':{'id':_0x20d844[_0x448f('0x47')]['id']}})[_0x448f('0x24')](handleEntityNotFound(_0x3cf608,null))[_0x448f('0x24')](function(_0x5d2528){if(_0x5d2528){_0x20d844[_0x448f('0x4a')][_0x448f('0x51')]=_0x5d2528['id'];_0x20d844[_0x448f('0x4a')][_0x448f('0x52')]=integrations[_0x448f('0x53')](_0x20d844['body'][_0x448f('0x54')],_0x20d844[_0x448f('0x4a')][_0x448f('0x40')]);_0x20d844['body'][_0x448f('0x55')]=integrations['getDescriptions'](_0x20d844[_0x448f('0x4a')][_0x448f('0x54')],_0x20d844[_0x448f('0x4a')][_0x448f('0x40')]);return db[_0x448f('0x4f')]['create'](_0x20d844[_0x448f('0x4a')],{'include':[{'model':db['SalesforceField'],'as':'Subjects'},{'model':db['SalesforceField'],'as':_0x448f('0x55')}]});}return null;})[_0x448f('0x24')](respondWithResult(_0x3cf608,null))[_0x448f('0x48')](handleError(_0x3cf608,null));};exports[_0x448f('0x56')]=function(_0x183934,_0x49eaa2,_0x123208){var _0x5327d0;var _0x5de917='';var _0x3ff22a=_0x448f('0x57');return db['SalesforceAccount'][_0x448f('0x4e')]({'where':{'id':_0x183934[_0x448f('0x47')]['id']},'attributes':['id',_0x448f('0x58'),_0x448f('0x59'),_0x448f('0x5a'),'username',_0x448f('0x5b'),'securityToken']})[_0x448f('0x24')](handleEntityNotFound(_0x49eaa2,null))['then'](function(_0x362c99){if(_0x362c99){_0x5de917=_0x362c99['remoteUri'];var _0x250720=_0x5de917[_0x448f('0x5c')](-0x1);if(_0x250720==='/'){_0x5de917=_0x5de917[_0x448f('0x5d')](0x0,_0x5de917['lastIndexOf'](_0x250720));}_0x5327d0=new jsforce[(_0x448f('0x5e'))]({'oauth2':{'loginUrl':_0x5de917,'clientId':_0x362c99[_0x448f('0x59')],'clientSecret':_0x362c99[_0x448f('0x5a')]}});return _0x5327d0[_0x448f('0x5f')](_0x362c99['username'],_0x362c99[_0x448f('0x5b')][_0x448f('0x60')](_0x362c99[_0x448f('0x61')]));}})[_0x448f('0x24')](function(_0x330418){if(_0x330418){if(!_[_0x448f('0x62')](_0x183934[_0x448f('0x31')]['ticketType'])){_0x3ff22a=_0x183934['query']['ticketType'];}return _0x5327d0[_0x448f('0x63')](_0x3ff22a)[_0x448f('0x64')]();}})[_0x448f('0x24')](function(_0x414313){var _0xf14e56=['OwnerId','ActivityDate',_0x448f('0x65'),_0x448f('0x66'),_0x448f('0x67'),_0x448f('0x68')];var _0x47e4ca=[_0x448f('0x69'),_0x448f('0x6a'),_0x448f('0x6b'),_0x448f('0x6c'),_0x448f('0x6d'),_0x448f('0x6e'),_0x448f('0x6f')];if(_0x414313){_['remove'](_0x414313[_0x448f('0x35')],function(_0x5a42f4){return _0xf14e56[_0x448f('0x70')](_0x5a42f4['name'])||!_0x47e4ca[_0x448f('0x70')](_0x5a42f4[_0x448f('0x40')]);});return{'count':_0x414313[_0x448f('0x35')][_0x448f('0x37')],'rows':_[_0x448f('0x2b')](_0x414313[_0x448f('0x35')],function(_0x4949a4){return{'id':_0x4949a4[_0x448f('0x28')],'name':_0x4949a4[_0x448f('0x71')],'custom':_0x4949a4[_0x448f('0x72')],'options':_0x4949a4[_0x448f('0x73')]['length']>0x0?_[_0x448f('0x2b')](_0x4949a4[_0x448f('0x73')],function(_0x2a0020){return{'name':_0x2a0020['label'],'value':_0x2a0020[_0x448f('0x74')]};}):_0x4949a4[_0x448f('0x40')]==_0x448f('0x6c')?[{'name':_0x448f('0x75'),'value':_0x448f('0x76')},{'name':_0x448f('0x77'),'value':_0x448f('0x78')}]:[]};})};}})[_0x448f('0x24')](respondWithResult(_0x49eaa2,null))[_0x448f('0x48')](function(_0x2e5c6c){var _0x16b221=_0x183934[_0x448f('0x31')]['test']?0x1f4:_0x2e5c6c[_0x448f('0x79')]||0x1f4;logger[_0x448f('0x26')]('getFields,\x20%s,\x20%s,\x20%s',_0x448f('0x56'),_0x16b221,JSON[_0x448f('0x7a')](_0x2e5c6c));delete _0x2e5c6c['name'];if(_0x16b221===0x191){_0x16b221=0x190;}_0x49eaa2['status'](_0x16b221)['send'](_0x183934['query']['test']?{'message':'Wrong\x20credentials','statusCode':_0x2e5c6c[_0x448f('0x79')]}:_0x2e5c6c);});};