Built motion from commit (unavailable).|2.0.74
[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 _0xa5cf=['remoteUri','clientId','clientSecret','username','login','password','concat','securityToken','sobject','Task','CallDisposition','CallDurationInSeconds','CallObject','CallType','IsArchived','Priority','Type','remove','custom','label','picklistValues','value','test','Wrong\x20credentials','statusCode','fast-json-patch','bluebird','mustache','path','sox','lodash','squel','jsforce','desk.js','to-csv','papaparse','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../components/integrations/configuration','jayson/promise','http','request','then','info','request\x20sent','debug','SalesforceAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','SalesforceAccount,\x20%s,\x20%s','message','result','sendStatus','status','end','count','offset','limit','json','reject','save','update','destroy','stack','name','send','index','SalesforceAccount','model','keys','dateStart','query','dateEnd','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','filters','pick','add','day','merge','filter','map','options','includeAll','include','findAll','rows','catch','show','params','length','find','create','body','getConfigurations','SalesforceConfiguration','rawAttributes','addConfiguration','Subjects','getSubjects','channel','type','Descriptions','getDescriptions','SalesforceField','getFields','findOne'];(function(_0x2edec8,_0x4a36f6){var _0xc3dbd4=function(_0x13af55){while(--_0x13af55){_0x2edec8['push'](_0x2edec8['shift']());}};_0xc3dbd4(++_0x4a36f6);}(_0xa5cf,0x1e9));var _0xfa5c=function(_0x418554,_0x581e7d){_0x418554=_0x418554-0x0;var _0x281ece=_0xa5cf[_0x418554];return _0x281ece;};'use strict';var jsonpatch=require(_0xfa5c('0x0'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xfa5c('0x1'));var Mustache=require(_0xfa5c('0x2'));var util=require('util');var path=require(_0xfa5c('0x3'));var sox=require(_0xfa5c('0x4'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0xfa5c('0x5'));var squel=require(_0xfa5c('0x6'));var crypto=require('crypto');var jsforce=require(_0xfa5c('0x7'));var deskjs=require(_0xfa5c('0x8'));var toCsv=require(_0xfa5c('0x9'));var querystring=require('querystring');var Papa=require(_0xfa5c('0xa'));var qs=require(_0xfa5c('0xb'));var logger=require(_0xfa5c('0xc'))(_0xfa5c('0xd'));var utils=require('../../config/utils');var config=require(_0xfa5c('0xe'));var db=require('../../mysqldb')['db'];var integrations=require(_0xfa5c('0xf'));var jayson=require(_0xfa5c('0x10'));var client=jayson['client'][_0xfa5c('0x11')]({'port':0x232a});function respondWithRpcPromise(_0x4c7f73,_0x27788b,_0x4daf46,_0x732d4b){return new BPromise(function(_0x59b4dd,_0x571cbc){var _0x56862b=_0x732d4b||client;return _0x56862b[_0xfa5c('0x12')](_0x4c7f73,_0x4daf46)[_0xfa5c('0x13')](function(_0x437c73){logger[_0xfa5c('0x14')]('SalesforceAccount,\x20%s,\x20%s',_0x27788b,_0xfa5c('0x15'));logger[_0xfa5c('0x16')](_0xfa5c('0x17'),_0x27788b,_0xfa5c('0x15'),JSON[_0xfa5c('0x18')](_0x437c73));if(_0x437c73[_0xfa5c('0x19')]){if(_0x437c73[_0xfa5c('0x19')][_0xfa5c('0x1a')]===0x1f4){logger[_0xfa5c('0x19')](_0xfa5c('0x1b'),_0x27788b,_0x437c73[_0xfa5c('0x19')][_0xfa5c('0x1c')]);return _0x571cbc(_0x437c73[_0xfa5c('0x19')][_0xfa5c('0x1c')]);}logger[_0xfa5c('0x19')]('SalesforceAccount,\x20%s,\x20%s',_0x27788b,_0x437c73[_0xfa5c('0x19')]['message']);return _0x59b4dd(_0x437c73['error'][_0xfa5c('0x1c')]);}else{logger[_0xfa5c('0x14')](_0xfa5c('0x1b'),_0x27788b,_0xfa5c('0x15'));_0x59b4dd(_0x437c73[_0xfa5c('0x1d')][_0xfa5c('0x1c')]);}})['catch'](function(_0x376ccb){logger[_0xfa5c('0x19')]('SalesforceAccount,\x20%s,\x20%s',_0x27788b,_0x376ccb);_0x571cbc(_0x376ccb);});});}function respondWithStatusCode(_0x2bde07,_0xa6cae7){_0xa6cae7=_0xa6cae7||0xcc;return function(_0x18f943){if(_0x18f943){return _0x2bde07[_0xfa5c('0x1e')](_0xa6cae7);}return _0x2bde07[_0xfa5c('0x1f')](_0xa6cae7)[_0xfa5c('0x20')]();};}function respondWithResult(_0x423238,_0x55e324){_0x55e324=_0x55e324||0xc8;return function(_0xfb8040){if(_0xfb8040){return _0x423238[_0xfa5c('0x1f')](_0x55e324)['json'](_0xfb8040);}};}function respondWithFilteredResult(_0x21f6a2,_0x1dd611){return function(_0x59f680){if(_0x59f680){var _0x2caa8b=_0x59f680[_0xfa5c('0x21')],_0x4a8ea5=_0x1dd611['offset'],_0x130755=_0x1dd611[_0xfa5c('0x22')]+_0x1dd611[_0xfa5c('0x23')],_0x4473ab;if(_0x130755>=_0x2caa8b){_0x130755=_0x2caa8b;_0x4473ab=0xc8;}else{_0x4473ab=0xce;}_0x21f6a2[_0xfa5c('0x1f')](_0x4473ab);return _0x21f6a2['set']('Content-Range',_0x4a8ea5+'-'+_0x130755+'/'+_0x2caa8b)[_0xfa5c('0x24')](_0x59f680);}return null;};}function patchUpdates(_0x1d0965){return function(_0xe4dde8){try{jsonpatch['apply'](_0xe4dde8,_0x1d0965,!![]);}catch(_0x203ef1){return BPromise[_0xfa5c('0x25')](_0x203ef1);}return _0xe4dde8[_0xfa5c('0x26')]();};}function saveUpdates(_0x451318,_0x48ae5d){return function(_0x55cbab){if(_0x55cbab){return _0x55cbab[_0xfa5c('0x27')](_0x451318)[_0xfa5c('0x13')](function(_0x520c8f){return _0x520c8f;});}return null;};}function removeEntity(_0x1ac504,_0x303abf){return function(_0x4449a4){if(_0x4449a4){return _0x4449a4[_0xfa5c('0x28')]()['then'](function(){_0x1ac504['status'](0xcc)[_0xfa5c('0x20')]();});}};}function handleEntityNotFound(_0x2415c3,_0x227e23){return function(_0x55ee70){if(!_0x55ee70){_0x2415c3['sendStatus'](0x194);}return _0x55ee70;};}function handleError(_0x5be290,_0x56bd2b){_0x56bd2b=_0x56bd2b||0x1f4;return function(_0x15c142){logger[_0xfa5c('0x19')](_0x15c142[_0xfa5c('0x29')]);if(_0x15c142[_0xfa5c('0x2a')]){delete _0x15c142['name'];}_0x5be290[_0xfa5c('0x1f')](_0x56bd2b)[_0xfa5c('0x2b')](_0x15c142);};}exports[_0xfa5c('0x2c')]=function(_0x2d7601,_0x50fa80){var _0x5787c3={},_0xbeedac={},_0xd714fc={'count':0x0,'rows':[]};var _0xe35ed4=db[_0xfa5c('0x2d')]['rawAttributes'];_0xbeedac[_0xfa5c('0x2e')]=_[_0xfa5c('0x2f')](_0xe35ed4);var _0x41cdeb={'dateStart':_0x2d7601['query'][_0xfa5c('0x30')],'dateEnd':_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x32')]};delete _0x2d7601[_0xfa5c('0x31')]['dateStart'];delete _0x2d7601[_0xfa5c('0x31')]['dateEnd'];_0xbeedac['query']=_['keys'](_0x2d7601[_0xfa5c('0x31')]);_0xbeedac['filters']=_[_0xfa5c('0x33')](_0xbeedac[_0xfa5c('0x2e')],_0xbeedac['query']);_0x5787c3[_0xfa5c('0x34')]=_[_0xfa5c('0x33')](_0xbeedac[_0xfa5c('0x2e')],qs['fields'](_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x35')]));_0x5787c3[_0xfa5c('0x34')]=_0x5787c3['attributes']['length']?_0x5787c3[_0xfa5c('0x34')]:_0xbeedac[_0xfa5c('0x2e')];if(!_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x36')](_0xfa5c('0x37'))){_0x5787c3[_0xfa5c('0x23')]=qs[_0xfa5c('0x23')](_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x23')]);_0x5787c3['offset']=qs[_0xfa5c('0x22')](_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x22')]);}_0x5787c3[_0xfa5c('0x38')]=qs[_0xfa5c('0x39')](_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x39')]);_0x5787c3[_0xfa5c('0x3a')]=qs[_0xfa5c('0x3b')](_[_0xfa5c('0x3c')](_0x2d7601[_0xfa5c('0x31')],_0xbeedac[_0xfa5c('0x3b')]));if(_0x41cdeb[_0xfa5c('0x30')]){if(_0x41cdeb[_0xfa5c('0x32')]){_0x41cdeb[_0xfa5c('0x32')]=moment(_0x41cdeb[_0xfa5c('0x32')])[_0xfa5c('0x3d')](0x1,_0xfa5c('0x3e'));}else{_0x41cdeb[_0xfa5c('0x32')]=moment(_0x41cdeb['dateStart'])['add'](0x1,_0xfa5c('0x3e'));}_[_0xfa5c('0x3f')](_0x5787c3['where'],{'createdAt':{'$gte':_0x41cdeb[_0xfa5c('0x30')],'$lte':_0x41cdeb[_0xfa5c('0x32')]}});}if(_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x40')]){_0x5787c3[_0xfa5c('0x3a')]=_[_0xfa5c('0x3f')](_0x5787c3[_0xfa5c('0x3a')],{'$or':_[_0xfa5c('0x41')](_0x5787c3[_0xfa5c('0x34')],function(_0x1407bc){var _0x4a0fa1={};_0x4a0fa1[_0x1407bc]={'$like':'%'+_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x40')]+'%'};return _0x4a0fa1;})});}_0x5787c3=_['merge']({},_0x5787c3,_0x2d7601[_0xfa5c('0x42')]);var _0x560c96={'where':_0x5787c3[_0xfa5c('0x3a')]};return db[_0xfa5c('0x2d')][_0xfa5c('0x21')](_0x560c96)[_0xfa5c('0x13')](function(_0xeb3af4){_0xd714fc[_0xfa5c('0x21')]=_0xeb3af4;if(_0x2d7601[_0xfa5c('0x31')][_0xfa5c('0x43')]){_0x5787c3[_0xfa5c('0x44')]=[{'all':!![]}];}return db['SalesforceAccount'][_0xfa5c('0x45')](_0x5787c3);})[_0xfa5c('0x13')](function(_0xbbd34c){_0xd714fc[_0xfa5c('0x46')]=_0xbbd34c;return _0xd714fc;})[_0xfa5c('0x13')](respondWithFilteredResult(_0x50fa80,_0x5787c3))[_0xfa5c('0x47')](handleError(_0x50fa80,null));};exports[_0xfa5c('0x48')]=function(_0x1a1749,_0x64dc7f){var _0x5b81e3={'raw':![],'where':{'id':_0x1a1749[_0xfa5c('0x49')]['id']}},_0x4bb535={};_0x4bb535[_0xfa5c('0x2e')]=_[_0xfa5c('0x2f')](db[_0xfa5c('0x2d')]['rawAttributes']);_0x4bb535['query']=_[_0xfa5c('0x2f')](_0x1a1749[_0xfa5c('0x31')]);_0x4bb535[_0xfa5c('0x3b')]=_[_0xfa5c('0x33')](_0x4bb535[_0xfa5c('0x2e')],_0x4bb535['query']);_0x5b81e3[_0xfa5c('0x34')]=_['intersection'](_0x4bb535[_0xfa5c('0x2e')],qs[_0xfa5c('0x35')](_0x1a1749[_0xfa5c('0x31')][_0xfa5c('0x35')]));_0x5b81e3[_0xfa5c('0x34')]=_0x5b81e3['attributes'][_0xfa5c('0x4a')]?_0x5b81e3[_0xfa5c('0x34')]:_0x4bb535['model'];if(_0x1a1749[_0xfa5c('0x31')]['includeAll']){_0x5b81e3[_0xfa5c('0x44')]=[{'all':!![]}];}_0x5b81e3=_[_0xfa5c('0x3f')]({},_0x5b81e3,_0x1a1749[_0xfa5c('0x42')]);return db['SalesforceAccount'][_0xfa5c('0x4b')](_0x5b81e3)[_0xfa5c('0x13')](handleEntityNotFound(_0x64dc7f,null))[_0xfa5c('0x13')](respondWithResult(_0x64dc7f,null))[_0xfa5c('0x47')](handleError(_0x64dc7f,null));};exports[_0xfa5c('0x4c')]=function(_0x471b54,_0xeb04f9){return db[_0xfa5c('0x2d')][_0xfa5c('0x4c')](_0x471b54[_0xfa5c('0x4d')],{})['then'](respondWithResult(_0xeb04f9,0xc9))['catch'](handleError(_0xeb04f9,null));};exports[_0xfa5c('0x27')]=function(_0x140969,_0x57539b){if(_0x140969[_0xfa5c('0x4d')]['id']){delete _0x140969[_0xfa5c('0x4d')]['id'];}return db[_0xfa5c('0x2d')][_0xfa5c('0x4b')]({'where':{'id':_0x140969[_0xfa5c('0x49')]['id']}})['then'](handleEntityNotFound(_0x57539b,null))['then'](saveUpdates(_0x140969['body'],null))['then'](respondWithResult(_0x57539b,null))[_0xfa5c('0x47')](handleError(_0x57539b,null));};exports[_0xfa5c('0x28')]=function(_0xfec797,_0x2ae7d6){return db[_0xfa5c('0x2d')][_0xfa5c('0x4b')]({'where':{'id':_0xfec797[_0xfa5c('0x49')]['id']}})['then'](handleEntityNotFound(_0x2ae7d6,null))['then'](removeEntity(_0x2ae7d6,null))[_0xfa5c('0x47')](handleError(_0x2ae7d6,null));};exports[_0xfa5c('0x4e')]=function(_0x129a49,_0x411b7e,_0x1299de){var _0x5ea5bd={};var _0xdc42b5={};var _0x6ba4ce;var _0x3c76e3;return db['SalesforceAccount']['findOne']({'where':{'id':_0x129a49[_0xfa5c('0x49')]['id']}})[_0xfa5c('0x13')](handleEntityNotFound(_0x411b7e,null))['then'](function(_0x194a54){if(_0x194a54){_0x6ba4ce=_0x194a54;_0xdc42b5[_0xfa5c('0x2e')]=_[_0xfa5c('0x2f')](db[_0xfa5c('0x4f')][_0xfa5c('0x50')]);_0xdc42b5[_0xfa5c('0x31')]=_[_0xfa5c('0x2f')](_0x129a49[_0xfa5c('0x31')]);_0xdc42b5[_0xfa5c('0x3b')]=_[_0xfa5c('0x33')](_0xdc42b5[_0xfa5c('0x2e')],_0xdc42b5[_0xfa5c('0x31')]);_0x5ea5bd[_0xfa5c('0x34')]=_[_0xfa5c('0x33')](_0xdc42b5[_0xfa5c('0x2e')],qs[_0xfa5c('0x35')](_0x129a49[_0xfa5c('0x31')]['fields']));_0x5ea5bd[_0xfa5c('0x34')]=_0x5ea5bd[_0xfa5c('0x34')][_0xfa5c('0x4a')]?_0x5ea5bd[_0xfa5c('0x34')]:_0xdc42b5['model'];_0x5ea5bd[_0xfa5c('0x38')]=qs[_0xfa5c('0x39')](_0x129a49['query'][_0xfa5c('0x39')]);_0x5ea5bd[_0xfa5c('0x3a')]=qs[_0xfa5c('0x3b')](_['pick'](_0x129a49[_0xfa5c('0x31')],_0xdc42b5[_0xfa5c('0x3b')]));if(_0x129a49[_0xfa5c('0x31')][_0xfa5c('0x40')]){_0x5ea5bd['where']=_[_0xfa5c('0x3f')](_0x5ea5bd['where'],{'$or':_[_0xfa5c('0x41')](_0x5ea5bd[_0xfa5c('0x34')],function(_0x437b0b){var _0x43ed44={};_0x43ed44[_0x437b0b]={'$like':'%'+_0x129a49['query'][_0xfa5c('0x40')]+'%'};return _0x43ed44;})});}_0x5ea5bd=_[_0xfa5c('0x3f')]({},_0x5ea5bd,_0x129a49[_0xfa5c('0x42')]);return _0x6ba4ce['getConfigurations'](_0x5ea5bd);}})[_0xfa5c('0x13')](function(_0x3ade84){if(_0x3ade84){_0x3c76e3=_0x3ade84['length'];if(!_0x129a49['query'][_0xfa5c('0x36')](_0xfa5c('0x37'))){_0x5ea5bd[_0xfa5c('0x23')]=qs[_0xfa5c('0x23')](_0x129a49[_0xfa5c('0x31')][_0xfa5c('0x23')]);_0x5ea5bd[_0xfa5c('0x22')]=qs[_0xfa5c('0x22')](_0x129a49[_0xfa5c('0x31')][_0xfa5c('0x22')]);}return _0x6ba4ce[_0xfa5c('0x4e')](_0x5ea5bd);}})[_0xfa5c('0x13')](function(_0x3c47d2){if(_0x3c47d2){return _0x3c47d2?{'count':_0x3c76e3,'rows':_0x3c47d2}:null;}})[_0xfa5c('0x13')](respondWithResult(_0x411b7e,null))[_0xfa5c('0x47')](handleError(_0x411b7e,null));};exports[_0xfa5c('0x51')]=function(_0x39b4b6,_0x2160c0,_0x506006){if(_0x39b4b6[_0xfa5c('0x4d')]['id']){delete _0x39b4b6['body']['id'];}return db['SalesforceAccount']['findOne']({'where':{'id':_0x39b4b6[_0xfa5c('0x49')]['id']}})[_0xfa5c('0x13')](handleEntityNotFound(_0x2160c0,null))[_0xfa5c('0x13')](function(_0x38fa0b){if(_0x38fa0b){_0x39b4b6['body']['AccountId']=_0x38fa0b['id'];_0x39b4b6[_0xfa5c('0x4d')][_0xfa5c('0x52')]=integrations[_0xfa5c('0x53')](_0x39b4b6[_0xfa5c('0x4d')][_0xfa5c('0x54')],_0x39b4b6[_0xfa5c('0x4d')][_0xfa5c('0x55')]);_0x39b4b6[_0xfa5c('0x4d')][_0xfa5c('0x56')]=integrations[_0xfa5c('0x57')](_0x39b4b6['body'][_0xfa5c('0x54')],_0x39b4b6[_0xfa5c('0x4d')][_0xfa5c('0x55')]);return db[_0xfa5c('0x4f')]['create'](_0x39b4b6['body'],{'include':[{'model':db[_0xfa5c('0x58')],'as':_0xfa5c('0x52')},{'model':db['SalesforceField'],'as':_0xfa5c('0x56')}]});}return null;})['then'](respondWithResult(_0x2160c0,null))['catch'](handleError(_0x2160c0,null));};exports[_0xfa5c('0x59')]=function(_0x4b2a15,_0x1d5ce0,_0x500355){var _0x559c31;return db['SalesforceAccount'][_0xfa5c('0x5a')]({'where':{'id':_0x4b2a15[_0xfa5c('0x49')]['id']},'attributes':['id',_0xfa5c('0x5b'),_0xfa5c('0x5c'),_0xfa5c('0x5d'),_0xfa5c('0x5e'),'password','securityToken']})[_0xfa5c('0x13')](handleEntityNotFound(_0x1d5ce0,null))[_0xfa5c('0x13')](function(_0x4f0ea0){if(_0x4f0ea0){_0x559c31=new jsforce['Connection']({'oauth2':{'loginUrl':_0x4f0ea0['remoteUri'],'clientId':_0x4f0ea0[_0xfa5c('0x5c')],'clientSecret':_0x4f0ea0['clientSecret']}});return _0x559c31[_0xfa5c('0x5f')](_0x4f0ea0['username'],_0x4f0ea0[_0xfa5c('0x60')][_0xfa5c('0x61')](_0x4f0ea0[_0xfa5c('0x62')]));}})['then'](function(_0x2b9e7f){if(_0x2b9e7f){return _0x559c31[_0xfa5c('0x63')](_0xfa5c('0x64'))['describe']();}})['then'](function(_0xcaeee3){var _0x3383c3=[_0xfa5c('0x65'),_0xfa5c('0x66'),_0xfa5c('0x67'),_0xfa5c('0x68'),_0xfa5c('0x69'),_0xfa5c('0x6a'),'Status',_0xfa5c('0x6b')];if(_0xcaeee3){_[_0xfa5c('0x6c')](_0xcaeee3[_0xfa5c('0x35')],function(_0x3f5e72){return _0x3f5e72[_0xfa5c('0x6d')]===![]&&!_0x3383c3['includes'](_0x3f5e72[_0xfa5c('0x2a')]);});return{'count':_0xcaeee3['fields'][_0xfa5c('0x4a')],'rows':_[_0xfa5c('0x41')](_0xcaeee3[_0xfa5c('0x35')],function(_0x177304){return{'id':_0x177304[_0xfa5c('0x2a')],'name':_0x177304[_0xfa5c('0x6e')],'custom':_0x177304['custom'],'options':_0x177304[_0xfa5c('0x6f')]?_[_0xfa5c('0x41')](_0x177304[_0xfa5c('0x6f')],function(_0x12343b){return{'name':_0x12343b['label'],'value':_0x12343b[_0xfa5c('0x70')]};}):[]};})};}})[_0xfa5c('0x13')](respondWithResult(_0x1d5ce0,null))[_0xfa5c('0x47')](function(_0x346ae3){var _0x3aaa4d=_0x4b2a15[_0xfa5c('0x31')][_0xfa5c('0x71')]?0x1f4:_0x346ae3['statusCode']||0x1f4;logger['error']('getFields,\x20%s,\x20%s,\x20%s',_0xfa5c('0x59'),_0x3aaa4d,JSON[_0xfa5c('0x18')](_0x346ae3));delete _0x346ae3[_0xfa5c('0x2a')];if(_0x3aaa4d===0x191){_0x3aaa4d=0x190;}_0x1d5ce0[_0xfa5c('0x1f')](_0x3aaa4d)[_0xfa5c('0x2b')](_0x4b2a15[_0xfa5c('0x31')][_0xfa5c('0x71')]?{'message':_0xfa5c('0x72'),'statusCode':_0x346ae3[_0xfa5c('0x73')]}:_0x346ae3);});};