6d206028954c67c541db4ad501bb6f67a48d9292
[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 _0xb344=['mustache','util','sox','ejs','fs-extra','lodash','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','status','end','json','offset','undefined','count','set','Content-Range','reject','save','update','then','destroy','sendStatus','error','stack','name','send','index','map','SalesforceAccount','fieldName','type','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','merge','filter','include','findAll','catch','show','params','options','find','create','body','getConfigurations','findOne','SalesforceConfiguration','pick','AccountId','Subjects','channel','Descriptions','SalesforceField','getFields','Task','remoteUri','clientId','clientSecret','username','password','securityToken','substring','lastIndexOf','Connection','login','concat','isNil','ticketType','sobject','describe','ActivityDate','IsReminderSet','Subject','Description','int','picklist','email','textarea','phone','includes','custom','picklistValues','label','value','boolean','True','true','False','test','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','statusCode','eml-format','zip-dir','request-promise','moment','bluebird'];(function(_0x552dec,_0x27a96d){var _0xcd5ca6=function(_0x455e1f){while(--_0x455e1f){_0x552dec['push'](_0x552dec['shift']());}};_0xcd5ca6(++_0x27a96d);}(_0xb344,0x72));var _0x4b34=function(_0xa40219,_0x21027e){_0xa40219=_0xa40219-0x0;var _0x391974=_0xb344[_0xa40219];return _0x391974;};'use strict';var emlformat=require(_0x4b34('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x4b34('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x4b34('0x2'));var moment=require(_0x4b34('0x3'));var BPromise=require(_0x4b34('0x4'));var Mustache=require(_0x4b34('0x5'));var util=require(_0x4b34('0x6'));var path=require('path');var sox=require(_0x4b34('0x7'));var csv=require('to-csv');var ejs=require(_0x4b34('0x8'));var fs=require('fs');var fs_extra=require(_0x4b34('0x9'));var _=require(_0x4b34('0xa'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x4b34('0xb'));var deskjs=require(_0x4b34('0xc'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x4b34('0xd'));var Redis=require(_0x4b34('0xe'));var authService=require(_0x4b34('0xf'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x4b34('0x10'))('api');var utils=require(_0x4b34('0x11'));var config=require(_0x4b34('0x12'));var licenseUtil=require(_0x4b34('0x13'));var db=require(_0x4b34('0x14'))['db'];var integrations=require(_0x4b34('0x15'));function respondWithStatusCode(_0x14b22a,_0xe317cc){_0xe317cc=_0xe317cc||0xcc;return function(_0x1b156b){if(_0x1b156b){return _0x14b22a['sendStatus'](_0xe317cc);}return _0x14b22a[_0x4b34('0x16')](_0xe317cc)[_0x4b34('0x17')]();};}function respondWithResult(_0x30aa74,_0x2d0a04){_0x2d0a04=_0x2d0a04||0xc8;return function(_0x1e8ed9){if(_0x1e8ed9){return _0x30aa74[_0x4b34('0x16')](_0x2d0a04)[_0x4b34('0x18')](_0x1e8ed9);}};}function respondWithFilteredResult(_0x3d30a3,_0x2da49a){return function(_0x19754d){if(_0x19754d){var _0x240d0d=typeof _0x2da49a[_0x4b34('0x19')]===_0x4b34('0x1a')&&typeof _0x2da49a['limit']==='undefined';var _0x1bcd8a=_0x19754d[_0x4b34('0x1b')];var _0x1f0e3b=_0x240d0d?0x0:_0x2da49a[_0x4b34('0x19')];var _0x21274f=_0x240d0d?_0x19754d[_0x4b34('0x1b')]:_0x2da49a[_0x4b34('0x19')]+_0x2da49a['limit'];var _0x42c10a;if(_0x21274f>=_0x1bcd8a){_0x21274f=_0x1bcd8a;_0x42c10a=0xc8;}else{_0x42c10a=0xce;}_0x3d30a3['status'](_0x42c10a);return _0x3d30a3[_0x4b34('0x1c')](_0x4b34('0x1d'),_0x1f0e3b+'-'+_0x21274f+'/'+_0x1bcd8a)[_0x4b34('0x18')](_0x19754d);}return null;};}function patchUpdates(_0x29164c){return function(_0x5e3a37){try{jsonpatch['apply'](_0x5e3a37,_0x29164c,!![]);}catch(_0x4fb28c){return BPromise[_0x4b34('0x1e')](_0x4fb28c);}return _0x5e3a37[_0x4b34('0x1f')]();};}function saveUpdates(_0x5aaf8c,_0x32ed9c){return function(_0x3162f2){if(_0x3162f2){return _0x3162f2[_0x4b34('0x20')](_0x5aaf8c)[_0x4b34('0x21')](function(_0x69ac84){return _0x69ac84;});}return null;};}function removeEntity(_0x393b18,_0x1ca6b6){return function(_0x10be65){if(_0x10be65){return _0x10be65[_0x4b34('0x22')]()[_0x4b34('0x21')](function(){_0x393b18[_0x4b34('0x16')](0xcc)[_0x4b34('0x17')]();});}};}function handleEntityNotFound(_0x1e9e36,_0x32a117){return function(_0x58b173){if(!_0x58b173){_0x1e9e36[_0x4b34('0x23')](0x194);}return _0x58b173;};}function handleError(_0x8f86cb,_0x2f0735){_0x2f0735=_0x2f0735||0x1f4;return function(_0xdba054){logger[_0x4b34('0x24')](_0xdba054[_0x4b34('0x25')]);if(_0xdba054[_0x4b34('0x26')]){delete _0xdba054[_0x4b34('0x26')];}_0x8f86cb[_0x4b34('0x16')](_0x2f0735)[_0x4b34('0x27')](_0xdba054);};}exports[_0x4b34('0x28')]=function(_0x28ca04,_0x21b128){var _0x48fda0={},_0x62aabb={},_0x2df832={'count':0x0,'rows':[]};var _0x110c30=_[_0x4b34('0x29')](db[_0x4b34('0x2a')]['rawAttributes'],function(_0x4694fa){return{'name':_0x4694fa[_0x4b34('0x2b')],'type':_0x4694fa[_0x4b34('0x2c')]['key']};});_0x62aabb[_0x4b34('0x2d')]=_[_0x4b34('0x29')](_0x110c30,_0x4b34('0x26'));_0x62aabb[_0x4b34('0x2e')]=_[_0x4b34('0x2f')](_0x28ca04['query']);_0x62aabb[_0x4b34('0x30')]=_[_0x4b34('0x31')](_0x62aabb[_0x4b34('0x2d')],_0x62aabb[_0x4b34('0x2e')]);_0x48fda0['attributes']=_[_0x4b34('0x31')](_0x62aabb[_0x4b34('0x2d')],qs['fields'](_0x28ca04[_0x4b34('0x2e')][_0x4b34('0x32')]));_0x48fda0[_0x4b34('0x33')]=_0x48fda0[_0x4b34('0x33')][_0x4b34('0x34')]?_0x48fda0[_0x4b34('0x33')]:_0x62aabb[_0x4b34('0x2d')];if(!_0x28ca04[_0x4b34('0x2e')][_0x4b34('0x35')](_0x4b34('0x36'))){_0x48fda0['limit']=qs[_0x4b34('0x37')](_0x28ca04[_0x4b34('0x2e')][_0x4b34('0x37')]);_0x48fda0[_0x4b34('0x19')]=qs['offset'](_0x28ca04[_0x4b34('0x2e')][_0x4b34('0x19')]);}_0x48fda0[_0x4b34('0x38')]=qs[_0x4b34('0x39')](_0x28ca04[_0x4b34('0x2e')]['sort']);_0x48fda0[_0x4b34('0x3a')]=qs[_0x4b34('0x30')](_['pick'](_0x28ca04[_0x4b34('0x2e')],_0x62aabb[_0x4b34('0x30')]),_0x110c30);if(_0x28ca04['query']['filter']){_0x48fda0[_0x4b34('0x3a')]=_[_0x4b34('0x3b')](_0x48fda0[_0x4b34('0x3a')],{'$or':_[_0x4b34('0x29')](_0x110c30,function(_0x3a140b){if(_0x3a140b[_0x4b34('0x2c')]!=='VIRTUAL'){var _0x3996ff={};_0x3996ff[_0x3a140b[_0x4b34('0x26')]]={'$like':'%'+_0x28ca04['query'][_0x4b34('0x3c')]+'%'};return _0x3996ff;}})});}_0x48fda0=_[_0x4b34('0x3b')]({},_0x48fda0,_0x28ca04['options']);var _0x573a66={'where':_0x48fda0[_0x4b34('0x3a')]};return db['SalesforceAccount']['count'](_0x573a66)[_0x4b34('0x21')](function(_0x37d194){_0x2df832[_0x4b34('0x1b')]=_0x37d194;if(_0x28ca04[_0x4b34('0x2e')]['includeAll']){_0x48fda0[_0x4b34('0x3d')]=[{'all':!![]}];}return db[_0x4b34('0x2a')][_0x4b34('0x3e')](_0x48fda0);})[_0x4b34('0x21')](function(_0x39ea5d){_0x2df832['rows']=_0x39ea5d;return _0x2df832;})['then'](respondWithFilteredResult(_0x21b128,_0x48fda0))[_0x4b34('0x3f')](handleError(_0x21b128,null));};exports[_0x4b34('0x40')]=function(_0x5f55b7,_0xe04d45){var _0x45c6f5={'raw':![],'where':{'id':_0x5f55b7[_0x4b34('0x41')]['id']}},_0xe731c1={};_0xe731c1['model']=_[_0x4b34('0x2f')](db[_0x4b34('0x2a')]['rawAttributes']);_0xe731c1[_0x4b34('0x2e')]=_[_0x4b34('0x2f')](_0x5f55b7[_0x4b34('0x2e')]);_0xe731c1[_0x4b34('0x30')]=_[_0x4b34('0x31')](_0xe731c1['model'],_0xe731c1[_0x4b34('0x2e')]);_0x45c6f5[_0x4b34('0x33')]=_['intersection'](_0xe731c1[_0x4b34('0x2d')],qs[_0x4b34('0x32')](_0x5f55b7[_0x4b34('0x2e')][_0x4b34('0x32')]));_0x45c6f5['attributes']=_0x45c6f5['attributes'][_0x4b34('0x34')]?_0x45c6f5[_0x4b34('0x33')]:_0xe731c1[_0x4b34('0x2d')];if(_0x5f55b7['query']['includeAll']){_0x45c6f5['include']=[{'all':!![]}];}_0x45c6f5=_['merge']({},_0x45c6f5,_0x5f55b7[_0x4b34('0x42')]);return db[_0x4b34('0x2a')][_0x4b34('0x43')](_0x45c6f5)['then'](handleEntityNotFound(_0xe04d45,null))[_0x4b34('0x21')](respondWithResult(_0xe04d45,null))[_0x4b34('0x3f')](handleError(_0xe04d45,null));};exports['create']=function(_0x46547d,_0x5ca825){return db[_0x4b34('0x2a')][_0x4b34('0x44')](_0x46547d[_0x4b34('0x45')],{})[_0x4b34('0x21')](respondWithResult(_0x5ca825,0xc9))['catch'](handleError(_0x5ca825,null));};exports[_0x4b34('0x20')]=function(_0xbc25de,_0x488f1f){if(_0xbc25de[_0x4b34('0x45')]['id']){delete _0xbc25de[_0x4b34('0x45')]['id'];}return db[_0x4b34('0x2a')][_0x4b34('0x43')]({'where':{'id':_0xbc25de[_0x4b34('0x41')]['id']}})[_0x4b34('0x21')](handleEntityNotFound(_0x488f1f,null))[_0x4b34('0x21')](saveUpdates(_0xbc25de[_0x4b34('0x45')],null))[_0x4b34('0x21')](respondWithResult(_0x488f1f,null))['catch'](handleError(_0x488f1f,null));};exports[_0x4b34('0x22')]=function(_0x377f09,_0xf9a48c){return db[_0x4b34('0x2a')][_0x4b34('0x43')]({'where':{'id':_0x377f09[_0x4b34('0x41')]['id']}})[_0x4b34('0x21')](handleEntityNotFound(_0xf9a48c,null))[_0x4b34('0x21')](removeEntity(_0xf9a48c,null))[_0x4b34('0x3f')](handleError(_0xf9a48c,null));};exports[_0x4b34('0x46')]=function(_0x23757e,_0x3079cb,_0x30c49c){var _0x559f02={};var _0x1e1c4b={};var _0x901402;var _0x45d206;return db[_0x4b34('0x2a')][_0x4b34('0x47')]({'where':{'id':_0x23757e[_0x4b34('0x41')]['id']}})[_0x4b34('0x21')](handleEntityNotFound(_0x3079cb,null))[_0x4b34('0x21')](function(_0x3f58dd){if(_0x3f58dd){_0x901402=_0x3f58dd;_0x1e1c4b[_0x4b34('0x2d')]=_[_0x4b34('0x2f')](db[_0x4b34('0x48')]['rawAttributes']);_0x1e1c4b[_0x4b34('0x2e')]=_['keys'](_0x23757e[_0x4b34('0x2e')]);_0x1e1c4b[_0x4b34('0x30')]=_[_0x4b34('0x31')](_0x1e1c4b[_0x4b34('0x2d')],_0x1e1c4b[_0x4b34('0x2e')]);_0x559f02[_0x4b34('0x33')]=_[_0x4b34('0x31')](_0x1e1c4b[_0x4b34('0x2d')],qs[_0x4b34('0x32')](_0x23757e[_0x4b34('0x2e')][_0x4b34('0x32')]));_0x559f02[_0x4b34('0x33')]=_0x559f02[_0x4b34('0x33')][_0x4b34('0x34')]?_0x559f02['attributes']:_0x1e1c4b[_0x4b34('0x2d')];_0x559f02[_0x4b34('0x38')]=qs[_0x4b34('0x39')](_0x23757e['query'][_0x4b34('0x39')]);_0x559f02[_0x4b34('0x3a')]=qs[_0x4b34('0x30')](_[_0x4b34('0x49')](_0x23757e[_0x4b34('0x2e')],_0x1e1c4b[_0x4b34('0x30')]));if(_0x23757e['query'][_0x4b34('0x3c')]){_0x559f02[_0x4b34('0x3a')]=_[_0x4b34('0x3b')](_0x559f02[_0x4b34('0x3a')],{'$or':_['map'](_0x559f02[_0x4b34('0x33')],function(_0x46fcb8){var _0x2c2770={};_0x2c2770[_0x46fcb8]={'$like':'%'+_0x23757e[_0x4b34('0x2e')][_0x4b34('0x3c')]+'%'};return _0x2c2770;})});}_0x559f02=_['merge']({},_0x559f02,_0x23757e[_0x4b34('0x42')]);return _0x901402[_0x4b34('0x46')](_0x559f02);}})[_0x4b34('0x21')](function(_0x101c81){if(_0x101c81){_0x45d206=_0x101c81[_0x4b34('0x34')];if(!_0x23757e[_0x4b34('0x2e')][_0x4b34('0x35')](_0x4b34('0x36'))){_0x559f02[_0x4b34('0x37')]=qs[_0x4b34('0x37')](_0x23757e[_0x4b34('0x2e')][_0x4b34('0x37')]);_0x559f02['offset']=qs['offset'](_0x23757e[_0x4b34('0x2e')]['offset']);}return _0x901402[_0x4b34('0x46')](_0x559f02);}})[_0x4b34('0x21')](function(_0x562ee9){if(_0x562ee9){return _0x562ee9?{'count':_0x45d206,'rows':_0x562ee9}:null;}})[_0x4b34('0x21')](respondWithResult(_0x3079cb,null))[_0x4b34('0x3f')](handleError(_0x3079cb,null));};exports['addConfiguration']=function(_0x10710f,_0x5bd2ad,_0x2c7aeb){if(_0x10710f[_0x4b34('0x45')]['id']){delete _0x10710f[_0x4b34('0x45')]['id'];}return db[_0x4b34('0x2a')][_0x4b34('0x47')]({'where':{'id':_0x10710f[_0x4b34('0x41')]['id']}})[_0x4b34('0x21')](handleEntityNotFound(_0x5bd2ad,null))[_0x4b34('0x21')](function(_0x344ad4){if(_0x344ad4){_0x10710f[_0x4b34('0x45')][_0x4b34('0x4a')]=_0x344ad4['id'];_0x10710f[_0x4b34('0x45')][_0x4b34('0x4b')]=integrations['getSubjects'](_0x10710f[_0x4b34('0x45')][_0x4b34('0x4c')],_0x10710f[_0x4b34('0x45')][_0x4b34('0x2c')]);_0x10710f[_0x4b34('0x45')][_0x4b34('0x4d')]=integrations['getDescriptions'](_0x10710f[_0x4b34('0x45')][_0x4b34('0x4c')],_0x10710f[_0x4b34('0x45')][_0x4b34('0x2c')]);return db[_0x4b34('0x48')][_0x4b34('0x44')](_0x10710f[_0x4b34('0x45')],{'include':[{'model':db[_0x4b34('0x4e')],'as':_0x4b34('0x4b')},{'model':db[_0x4b34('0x4e')],'as':_0x4b34('0x4d')}]});}return null;})[_0x4b34('0x21')](respondWithResult(_0x5bd2ad,null))['catch'](handleError(_0x5bd2ad,null));};exports[_0x4b34('0x4f')]=function(_0x40268b,_0x50c074,_0x486642){var _0x30acbc;var _0x334e91='';var _0x372a2f=_0x4b34('0x50');return db[_0x4b34('0x2a')][_0x4b34('0x47')]({'where':{'id':_0x40268b[_0x4b34('0x41')]['id']},'attributes':['id',_0x4b34('0x51'),_0x4b34('0x52'),_0x4b34('0x53'),_0x4b34('0x54'),_0x4b34('0x55'),_0x4b34('0x56')]})[_0x4b34('0x21')](handleEntityNotFound(_0x50c074,null))['then'](function(_0xd8730e){if(_0xd8730e){_0x334e91=_0xd8730e['remoteUri'];var _0x54f83c=_0x334e91['slice'](-0x1);if(_0x54f83c==='/'){_0x334e91=_0x334e91[_0x4b34('0x57')](0x0,_0x334e91[_0x4b34('0x58')](_0x54f83c));}_0x30acbc=new jsforce[(_0x4b34('0x59'))]({'oauth2':{'loginUrl':_0x334e91,'clientId':_0xd8730e['clientId'],'clientSecret':_0xd8730e[_0x4b34('0x53')]}});return _0x30acbc[_0x4b34('0x5a')](_0xd8730e['username'],_0xd8730e[_0x4b34('0x55')][_0x4b34('0x5b')](_0xd8730e[_0x4b34('0x56')]));}})[_0x4b34('0x21')](function(_0x163b74){if(_0x163b74){if(!_[_0x4b34('0x5c')](_0x40268b[_0x4b34('0x2e')][_0x4b34('0x5d')])){_0x372a2f=_0x40268b[_0x4b34('0x2e')][_0x4b34('0x5d')];}return _0x30acbc[_0x4b34('0x5e')](_0x372a2f)[_0x4b34('0x5f')]();}})['then'](function(_0x20bcd2){var _0x4fdc9f=['OwnerId',_0x4b34('0x60'),_0x4b34('0x61'),'IsRecurrence',_0x4b34('0x62'),_0x4b34('0x63')];var _0xa3ac6f=['string',_0x4b34('0x64'),_0x4b34('0x65'),'boolean',_0x4b34('0x66'),_0x4b34('0x67'),_0x4b34('0x68')];if(_0x20bcd2){_['remove'](_0x20bcd2[_0x4b34('0x32')],function(_0x5c3770){return _0x4fdc9f['includes'](_0x5c3770[_0x4b34('0x26')])||!_0xa3ac6f[_0x4b34('0x69')](_0x5c3770['type']);});return{'count':_0x20bcd2['fields'][_0x4b34('0x34')],'rows':_[_0x4b34('0x29')](_0x20bcd2[_0x4b34('0x32')],function(_0x3284ff){return{'id':_0x3284ff[_0x4b34('0x26')],'name':_0x3284ff['label'],'custom':_0x3284ff[_0x4b34('0x6a')],'options':_0x3284ff['picklistValues'][_0x4b34('0x34')]>0x0?_['map'](_0x3284ff[_0x4b34('0x6b')],function(_0x23690a){return{'name':_0x23690a[_0x4b34('0x6c')],'value':_0x23690a[_0x4b34('0x6d')]};}):_0x3284ff[_0x4b34('0x2c')]==_0x4b34('0x6e')?[{'name':_0x4b34('0x6f'),'value':_0x4b34('0x70')},{'name':_0x4b34('0x71'),'value':'false'}]:[]};})};}})[_0x4b34('0x21')](respondWithResult(_0x50c074,null))['catch'](function(_0x22726c){var _0xeb3d5f=_0x40268b[_0x4b34('0x2e')][_0x4b34('0x72')]?0x1f4:_0x22726c['statusCode']||0x1f4;logger[_0x4b34('0x24')](_0x4b34('0x73'),_0x4b34('0x4f'),_0xeb3d5f,JSON[_0x4b34('0x74')](_0x22726c));delete _0x22726c[_0x4b34('0x26')];if(_0xeb3d5f===0x191){_0xeb3d5f=0x190;}_0x50c074['status'](_0xeb3d5f)[_0x4b34('0x27')](_0x40268b['query'][_0x4b34('0x72')]?{'message':_0x4b34('0x75'),'statusCode':_0x22726c[_0x4b34('0x76')]}:_0x22726c);});};