Built motion from commit 08fb8613.|2.6.31
[motion2.git] / server / api / intSalesforceConfiguration / intSalesforceConfiguration.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 _0xbcc7=['hasOwnProperty','where','pick','filter','merge','options','includeAll','include','findAll','rows','show','params','rawAttributes','catch','create','find','body','getFields','SalesforceField','sort','nolimit','getSubjects','order','getDescriptions','findOne','eml-format','rimraf','fast-json-patch','bluebird','util','path','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','../../components/auth/service','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','limit','count','set','Content-Range','reject','save','update','then','destroy','stack','name','send','index','map','SalesforceConfiguration','fieldName','key','model','keys','query','filters','attributes','intersection','fields','length'];(function(_0x1d2bbb,_0x202267){var _0x38082b=function(_0x27d153){while(--_0x27d153){_0x1d2bbb['push'](_0x1d2bbb['shift']());}};_0x38082b(++_0x202267);}(_0xbcc7,0x19a));var _0x7bcc=function(_0x1c7627,_0x52a542){_0x1c7627=_0x1c7627-0x0;var _0x3cf02a=_0xbcc7[_0x1c7627];return _0x3cf02a;};'use strict';var emlformat=require(_0x7bcc('0x0'));var rimraf=require(_0x7bcc('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x7bcc('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x7bcc('0x3'));var Mustache=require('mustache');var util=require(_0x7bcc('0x4'));var path=require(_0x7bcc('0x5'));var sox=require('sox');var csv=require(_0x7bcc('0x6'));var ejs=require(_0x7bcc('0x7'));var fs=require('fs');var fs_extra=require(_0x7bcc('0x8'));var _=require(_0x7bcc('0x9'));var squel=require(_0x7bcc('0xa'));var crypto=require(_0x7bcc('0xb'));var jsforce=require('jsforce');var deskjs=require(_0x7bcc('0xc'));var toCsv=require('to-csv');var querystring=require(_0x7bcc('0xd'));var Papa=require(_0x7bcc('0xe'));var Redis=require('ioredis');var authService=require(_0x7bcc('0xf'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x7bcc('0x10'))(_0x7bcc('0x11'));var utils=require(_0x7bcc('0x12'));var config=require(_0x7bcc('0x13'));var licenseUtil=require(_0x7bcc('0x14'));var db=require(_0x7bcc('0x15'))['db'];function respondWithStatusCode(_0xae46e8,_0x51b0e8){_0x51b0e8=_0x51b0e8||0xcc;return function(_0x3e3580){if(_0x3e3580){return _0xae46e8[_0x7bcc('0x16')](_0x51b0e8);}return _0xae46e8[_0x7bcc('0x17')](_0x51b0e8)[_0x7bcc('0x18')]();};}function respondWithResult(_0x39f912,_0x217896){_0x217896=_0x217896||0xc8;return function(_0x4a2539){if(_0x4a2539){return _0x39f912[_0x7bcc('0x17')](_0x217896)[_0x7bcc('0x19')](_0x4a2539);}};}function respondWithFilteredResult(_0x148618,_0x461ff7){return function(_0x30caa2){if(_0x30caa2){var _0x134d55=typeof _0x461ff7[_0x7bcc('0x1a')]==='undefined'&&typeof _0x461ff7[_0x7bcc('0x1b')]==='undefined';var _0x3156f3=_0x30caa2[_0x7bcc('0x1c')];var _0x340bd6=_0x134d55?0x0:_0x461ff7[_0x7bcc('0x1a')];var _0x49168c=_0x134d55?_0x30caa2['count']:_0x461ff7[_0x7bcc('0x1a')]+_0x461ff7[_0x7bcc('0x1b')];var _0x1a1b7f;if(_0x49168c>=_0x3156f3){_0x49168c=_0x3156f3;_0x1a1b7f=0xc8;}else{_0x1a1b7f=0xce;}_0x148618[_0x7bcc('0x17')](_0x1a1b7f);return _0x148618[_0x7bcc('0x1d')](_0x7bcc('0x1e'),_0x340bd6+'-'+_0x49168c+'/'+_0x3156f3)[_0x7bcc('0x19')](_0x30caa2);}return null;};}function patchUpdates(_0x3d0719){return function(_0x15b2c3){try{jsonpatch['apply'](_0x15b2c3,_0x3d0719,!![]);}catch(_0x553865){return BPromise[_0x7bcc('0x1f')](_0x553865);}return _0x15b2c3[_0x7bcc('0x20')]();};}function saveUpdates(_0x34ee2a,_0x3050cf){return function(_0x201f40){if(_0x201f40){return _0x201f40[_0x7bcc('0x21')](_0x34ee2a)[_0x7bcc('0x22')](function(_0x345671){return _0x345671;});}return null;};}function removeEntity(_0x32b3ea,_0x54c611){return function(_0x2d25b8){if(_0x2d25b8){return _0x2d25b8[_0x7bcc('0x23')]()[_0x7bcc('0x22')](function(){_0x32b3ea[_0x7bcc('0x17')](0xcc)[_0x7bcc('0x18')]();});}};}function handleEntityNotFound(_0x1d085c,_0x4f8562){return function(_0x48b296){if(!_0x48b296){_0x1d085c[_0x7bcc('0x16')](0x194);}return _0x48b296;};}function handleError(_0x4a91d0,_0x4db3c0){_0x4db3c0=_0x4db3c0||0x1f4;return function(_0xd3ca3b){logger['error'](_0xd3ca3b[_0x7bcc('0x24')]);if(_0xd3ca3b[_0x7bcc('0x25')]){delete _0xd3ca3b[_0x7bcc('0x25')];}_0x4a91d0['status'](_0x4db3c0)[_0x7bcc('0x26')](_0xd3ca3b);};}exports[_0x7bcc('0x27')]=function(_0x2e92e1,_0x319943){var _0x20c458={},_0x1ecf95={},_0x9fdacf={'count':0x0,'rows':[]};var _0x57aa56=_[_0x7bcc('0x28')](db[_0x7bcc('0x29')]['rawAttributes'],function(_0x5d4131){return{'name':_0x5d4131[_0x7bcc('0x2a')],'type':_0x5d4131['type'][_0x7bcc('0x2b')]};});_0x1ecf95[_0x7bcc('0x2c')]=_[_0x7bcc('0x28')](_0x57aa56,_0x7bcc('0x25'));_0x1ecf95['query']=_[_0x7bcc('0x2d')](_0x2e92e1[_0x7bcc('0x2e')]);_0x1ecf95[_0x7bcc('0x2f')]=_['intersection'](_0x1ecf95[_0x7bcc('0x2c')],_0x1ecf95[_0x7bcc('0x2e')]);_0x20c458[_0x7bcc('0x30')]=_[_0x7bcc('0x31')](_0x1ecf95[_0x7bcc('0x2c')],qs[_0x7bcc('0x32')](_0x2e92e1[_0x7bcc('0x2e')][_0x7bcc('0x32')]));_0x20c458[_0x7bcc('0x30')]=_0x20c458[_0x7bcc('0x30')][_0x7bcc('0x33')]?_0x20c458['attributes']:_0x1ecf95[_0x7bcc('0x2c')];if(!_0x2e92e1[_0x7bcc('0x2e')][_0x7bcc('0x34')]('nolimit')){_0x20c458['limit']=qs[_0x7bcc('0x1b')](_0x2e92e1[_0x7bcc('0x2e')][_0x7bcc('0x1b')]);_0x20c458[_0x7bcc('0x1a')]=qs[_0x7bcc('0x1a')](_0x2e92e1[_0x7bcc('0x2e')][_0x7bcc('0x1a')]);}_0x20c458['order']=qs['sort'](_0x2e92e1['query']['sort']);_0x20c458[_0x7bcc('0x35')]=qs[_0x7bcc('0x2f')](_[_0x7bcc('0x36')](_0x2e92e1['query'],_0x1ecf95[_0x7bcc('0x2f')]),_0x57aa56);if(_0x2e92e1[_0x7bcc('0x2e')][_0x7bcc('0x37')]){_0x20c458['where']=_['merge'](_0x20c458[_0x7bcc('0x35')],{'$or':_[_0x7bcc('0x28')](_0x57aa56,function(_0x1d9c06){if(_0x1d9c06['type']!=='VIRTUAL'){var _0x44a71e={};_0x44a71e[_0x1d9c06[_0x7bcc('0x25')]]={'$like':'%'+_0x2e92e1[_0x7bcc('0x2e')]['filter']+'%'};return _0x44a71e;}})});}_0x20c458=_[_0x7bcc('0x38')]({},_0x20c458,_0x2e92e1[_0x7bcc('0x39')]);var _0x1ab116={'where':_0x20c458[_0x7bcc('0x35')]};return db[_0x7bcc('0x29')][_0x7bcc('0x1c')](_0x1ab116)['then'](function(_0x252a46){_0x9fdacf[_0x7bcc('0x1c')]=_0x252a46;if(_0x2e92e1['query'][_0x7bcc('0x3a')]){_0x20c458[_0x7bcc('0x3b')]=[{'all':!![]}];}return db[_0x7bcc('0x29')][_0x7bcc('0x3c')](_0x20c458);})[_0x7bcc('0x22')](function(_0x1b4d75){_0x9fdacf[_0x7bcc('0x3d')]=_0x1b4d75;return _0x9fdacf;})[_0x7bcc('0x22')](respondWithFilteredResult(_0x319943,_0x20c458))['catch'](handleError(_0x319943,null));};exports[_0x7bcc('0x3e')]=function(_0x16ec80,_0x3d999f){var _0x10e3a2={'raw':![],'where':{'id':_0x16ec80[_0x7bcc('0x3f')]['id']}},_0x4074d7={};_0x4074d7['model']=_['keys'](db[_0x7bcc('0x29')][_0x7bcc('0x40')]);_0x4074d7['query']=_[_0x7bcc('0x2d')](_0x16ec80[_0x7bcc('0x2e')]);_0x4074d7[_0x7bcc('0x2f')]=_[_0x7bcc('0x31')](_0x4074d7[_0x7bcc('0x2c')],_0x4074d7[_0x7bcc('0x2e')]);_0x10e3a2[_0x7bcc('0x30')]=_['intersection'](_0x4074d7[_0x7bcc('0x2c')],qs[_0x7bcc('0x32')](_0x16ec80[_0x7bcc('0x2e')][_0x7bcc('0x32')]));_0x10e3a2[_0x7bcc('0x30')]=_0x10e3a2['attributes'][_0x7bcc('0x33')]?_0x10e3a2[_0x7bcc('0x30')]:_0x4074d7['model'];if(_0x16ec80[_0x7bcc('0x2e')][_0x7bcc('0x3a')]){_0x10e3a2[_0x7bcc('0x3b')]=[{'all':!![]}];}_0x10e3a2=_['merge']({},_0x10e3a2,_0x16ec80[_0x7bcc('0x39')]);return db[_0x7bcc('0x29')]['find'](_0x10e3a2)[_0x7bcc('0x22')](handleEntityNotFound(_0x3d999f,null))[_0x7bcc('0x22')](respondWithResult(_0x3d999f,null))[_0x7bcc('0x41')](handleError(_0x3d999f,null));};exports[_0x7bcc('0x42')]=function(_0x230fca,_0x538a2e){return db[_0x7bcc('0x29')][_0x7bcc('0x42')](_0x230fca['body'],{})[_0x7bcc('0x22')](respondWithResult(_0x538a2e,0xc9))['catch'](handleError(_0x538a2e,null));};exports[_0x7bcc('0x21')]=function(_0x5d4654,_0x29cb88){if(_0x5d4654['body']['id']){delete _0x5d4654['body']['id'];}return db[_0x7bcc('0x29')][_0x7bcc('0x43')]({'where':{'id':_0x5d4654[_0x7bcc('0x3f')]['id']}})[_0x7bcc('0x22')](handleEntityNotFound(_0x29cb88,null))[_0x7bcc('0x22')](saveUpdates(_0x5d4654[_0x7bcc('0x44')],null))[_0x7bcc('0x22')](respondWithResult(_0x29cb88,null))[_0x7bcc('0x41')](handleError(_0x29cb88,null));};exports[_0x7bcc('0x23')]=function(_0x2304f0,_0x8e50eb){return db[_0x7bcc('0x29')][_0x7bcc('0x43')]({'where':{'id':_0x2304f0[_0x7bcc('0x3f')]['id']}})[_0x7bcc('0x22')](handleEntityNotFound(_0x8e50eb,null))['then'](removeEntity(_0x8e50eb,null))[_0x7bcc('0x41')](handleError(_0x8e50eb,null));};exports[_0x7bcc('0x45')]=function(_0x54bb61,_0x4bfffa,_0x2f8e0d){var _0x38240d={};var _0x55deaf={};var _0x5cf572;var _0x5268f2;return db['SalesforceConfiguration']['findOne']({'where':{'id':_0x54bb61[_0x7bcc('0x3f')]['id']}})[_0x7bcc('0x22')](handleEntityNotFound(_0x4bfffa,null))['then'](function(_0x5b1bbd){if(_0x5b1bbd){_0x5cf572=_0x5b1bbd;_0x55deaf[_0x7bcc('0x2c')]=_[_0x7bcc('0x2d')](db[_0x7bcc('0x46')][_0x7bcc('0x40')]);_0x55deaf[_0x7bcc('0x2e')]=_[_0x7bcc('0x2d')](_0x54bb61['query']);_0x55deaf['filters']=_[_0x7bcc('0x31')](_0x55deaf[_0x7bcc('0x2c')],_0x55deaf[_0x7bcc('0x2e')]);_0x38240d[_0x7bcc('0x30')]=_['intersection'](_0x55deaf['model'],qs['fields'](_0x54bb61[_0x7bcc('0x2e')]['fields']));_0x38240d[_0x7bcc('0x30')]=_0x38240d[_0x7bcc('0x30')][_0x7bcc('0x33')]?_0x38240d['attributes']:_0x55deaf[_0x7bcc('0x2c')];_0x38240d['order']=qs['sort'](_0x54bb61[_0x7bcc('0x2e')][_0x7bcc('0x47')]);_0x38240d[_0x7bcc('0x35')]=qs[_0x7bcc('0x2f')](_[_0x7bcc('0x36')](_0x54bb61[_0x7bcc('0x2e')],_0x55deaf[_0x7bcc('0x2f')]));if(_0x54bb61[_0x7bcc('0x2e')][_0x7bcc('0x37')]){_0x38240d['where']=_[_0x7bcc('0x38')](_0x38240d[_0x7bcc('0x35')],{'$or':_[_0x7bcc('0x28')](_0x38240d['attributes'],function(_0x2a7229){var _0x24c9b9={};_0x24c9b9[_0x2a7229]={'$like':'%'+_0x54bb61[_0x7bcc('0x2e')][_0x7bcc('0x37')]+'%'};return _0x24c9b9;})});}_0x38240d=_[_0x7bcc('0x38')]({},_0x38240d,_0x54bb61[_0x7bcc('0x39')]);return _0x5cf572[_0x7bcc('0x45')](_0x38240d);}})[_0x7bcc('0x22')](function(_0x11be16){if(_0x11be16){_0x5268f2=_0x11be16[_0x7bcc('0x33')];if(!_0x54bb61['query'][_0x7bcc('0x34')](_0x7bcc('0x48'))){_0x38240d['limit']=qs[_0x7bcc('0x1b')](_0x54bb61[_0x7bcc('0x2e')][_0x7bcc('0x1b')]);_0x38240d['offset']=qs[_0x7bcc('0x1a')](_0x54bb61['query'][_0x7bcc('0x1a')]);}return _0x5cf572[_0x7bcc('0x45')](_0x38240d);}})[_0x7bcc('0x22')](function(_0xc7232c){if(_0xc7232c){return _0xc7232c?{'count':_0x5268f2,'rows':_0xc7232c}:null;}})[_0x7bcc('0x22')](respondWithResult(_0x4bfffa,null))[_0x7bcc('0x41')](handleError(_0x4bfffa,null));};exports[_0x7bcc('0x49')]=function(_0x5e9af4,_0x57baed,_0x4dc93){var _0x5b979b={};var _0xa6e359={};var _0x39fc84;var _0x137147;return db[_0x7bcc('0x29')]['findOne']({'where':{'id':_0x5e9af4[_0x7bcc('0x3f')]['id']}})[_0x7bcc('0x22')](handleEntityNotFound(_0x57baed,null))[_0x7bcc('0x22')](function(_0x2a67d1){if(_0x2a67d1){_0x39fc84=_0x2a67d1;_0xa6e359[_0x7bcc('0x2c')]=_[_0x7bcc('0x2d')](db[_0x7bcc('0x46')][_0x7bcc('0x40')]);_0xa6e359[_0x7bcc('0x2e')]=_[_0x7bcc('0x2d')](_0x5e9af4[_0x7bcc('0x2e')]);_0xa6e359['filters']=_['intersection'](_0xa6e359['model'],_0xa6e359[_0x7bcc('0x2e')]);_0x5b979b['attributes']=_[_0x7bcc('0x31')](_0xa6e359['model'],qs[_0x7bcc('0x32')](_0x5e9af4[_0x7bcc('0x2e')][_0x7bcc('0x32')]));_0x5b979b[_0x7bcc('0x30')]=_0x5b979b[_0x7bcc('0x30')][_0x7bcc('0x33')]?_0x5b979b[_0x7bcc('0x30')]:_0xa6e359['model'];_0x5b979b[_0x7bcc('0x4a')]=qs[_0x7bcc('0x47')](_0x5e9af4['query'][_0x7bcc('0x47')]);_0x5b979b['where']=qs[_0x7bcc('0x2f')](_['pick'](_0x5e9af4[_0x7bcc('0x2e')],_0xa6e359[_0x7bcc('0x2f')]));if(_0x5e9af4[_0x7bcc('0x2e')][_0x7bcc('0x37')]){_0x5b979b['where']=_[_0x7bcc('0x38')](_0x5b979b['where'],{'$or':_[_0x7bcc('0x28')](_0x5b979b[_0x7bcc('0x30')],function(_0x27ce03){var _0x3f9ca0={};_0x3f9ca0[_0x27ce03]={'$like':'%'+_0x5e9af4['query'][_0x7bcc('0x37')]+'%'};return _0x3f9ca0;})});}_0x5b979b=_[_0x7bcc('0x38')]({},_0x5b979b,_0x5e9af4[_0x7bcc('0x39')]);return _0x39fc84[_0x7bcc('0x49')](_0x5b979b);}})[_0x7bcc('0x22')](function(_0x1fdde5){if(_0x1fdde5){_0x137147=_0x1fdde5['length'];if(!_0x5e9af4[_0x7bcc('0x2e')][_0x7bcc('0x34')](_0x7bcc('0x48'))){_0x5b979b[_0x7bcc('0x1b')]=qs[_0x7bcc('0x1b')](_0x5e9af4[_0x7bcc('0x2e')][_0x7bcc('0x1b')]);_0x5b979b['offset']=qs[_0x7bcc('0x1a')](_0x5e9af4[_0x7bcc('0x2e')][_0x7bcc('0x1a')]);}return _0x39fc84[_0x7bcc('0x49')](_0x5b979b);}})[_0x7bcc('0x22')](function(_0x2687f3){if(_0x2687f3){return _0x2687f3?{'count':_0x137147,'rows':_0x2687f3}:null;}})[_0x7bcc('0x22')](respondWithResult(_0x57baed,null))[_0x7bcc('0x41')](handleError(_0x57baed,null));};exports[_0x7bcc('0x4b')]=function(_0x5966de,_0x47db74,_0x403bbb){var _0x1a5d83={};var _0x4516d5={};var _0x19d375;var _0x54f161;return db[_0x7bcc('0x29')][_0x7bcc('0x4c')]({'where':{'id':_0x5966de[_0x7bcc('0x3f')]['id']}})[_0x7bcc('0x22')](handleEntityNotFound(_0x47db74,null))['then'](function(_0x3f7bb9){if(_0x3f7bb9){_0x19d375=_0x3f7bb9;_0x4516d5[_0x7bcc('0x2c')]=_[_0x7bcc('0x2d')](db[_0x7bcc('0x46')][_0x7bcc('0x40')]);_0x4516d5[_0x7bcc('0x2e')]=_[_0x7bcc('0x2d')](_0x5966de[_0x7bcc('0x2e')]);_0x4516d5[_0x7bcc('0x2f')]=_[_0x7bcc('0x31')](_0x4516d5[_0x7bcc('0x2c')],_0x4516d5[_0x7bcc('0x2e')]);_0x1a5d83['attributes']=_[_0x7bcc('0x31')](_0x4516d5[_0x7bcc('0x2c')],qs['fields'](_0x5966de[_0x7bcc('0x2e')][_0x7bcc('0x32')]));_0x1a5d83['attributes']=_0x1a5d83[_0x7bcc('0x30')][_0x7bcc('0x33')]?_0x1a5d83['attributes']:_0x4516d5[_0x7bcc('0x2c')];_0x1a5d83[_0x7bcc('0x4a')]=qs[_0x7bcc('0x47')](_0x5966de['query'][_0x7bcc('0x47')]);_0x1a5d83['where']=qs[_0x7bcc('0x2f')](_[_0x7bcc('0x36')](_0x5966de[_0x7bcc('0x2e')],_0x4516d5[_0x7bcc('0x2f')]));if(_0x5966de['query'][_0x7bcc('0x37')]){_0x1a5d83[_0x7bcc('0x35')]=_[_0x7bcc('0x38')](_0x1a5d83[_0x7bcc('0x35')],{'$or':_['map'](_0x1a5d83[_0x7bcc('0x30')],function(_0x59ba0c){var _0x4094c6={};_0x4094c6[_0x59ba0c]={'$like':'%'+_0x5966de['query']['filter']+'%'};return _0x4094c6;})});}_0x1a5d83=_[_0x7bcc('0x38')]({},_0x1a5d83,_0x5966de[_0x7bcc('0x39')]);return _0x19d375[_0x7bcc('0x4b')](_0x1a5d83);}})[_0x7bcc('0x22')](function(_0x1c273b){if(_0x1c273b){_0x54f161=_0x1c273b[_0x7bcc('0x33')];if(!_0x5966de['query'][_0x7bcc('0x34')]('nolimit')){_0x1a5d83[_0x7bcc('0x1b')]=qs[_0x7bcc('0x1b')](_0x5966de[_0x7bcc('0x2e')][_0x7bcc('0x1b')]);_0x1a5d83[_0x7bcc('0x1a')]=qs[_0x7bcc('0x1a')](_0x5966de[_0x7bcc('0x2e')]['offset']);}return _0x19d375[_0x7bcc('0x4b')](_0x1a5d83);}})['then'](function(_0x41bea4){if(_0x41bea4){return _0x41bea4?{'count':_0x54f161,'rows':_0x41bea4}:null;}})[_0x7bcc('0x22')](respondWithResult(_0x47db74,null))[_0x7bcc('0x41')](handleError(_0x47db74,null));};