Built motion from commit d5e4af8c.|2.6.23
[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 _0x1f17=['query','keys','filters','intersection','fields','length','attributes','hasOwnProperty','sort','pick','filter','merge','where','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','rawAttributes','find','create','body','findOne','order','nolimit','getFields','getSubjects','getDescriptions','SalesforceField','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','to-csv','lodash','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','sendStatus','error','stack','name','index','SalesforceConfiguration','fieldName','type','model','map'];(function(_0x1cd8d0,_0x9fd33c){var _0xd91b2=function(_0x229fce){while(--_0x229fce){_0x1cd8d0['push'](_0x1cd8d0['shift']());}};_0xd91b2(++_0x9fd33c);}(_0x1f17,0x71));var _0x71f1=function(_0x3cc9d9,_0x69ed38){_0x3cc9d9=_0x3cc9d9-0x0;var _0x1a425d=_0x1f17[_0x3cc9d9];return _0x1a425d;};'use strict';var emlformat=require(_0x71f1('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x71f1('0x1'));var jsonpatch=require(_0x71f1('0x2'));var rp=require(_0x71f1('0x3'));var moment=require('moment');var BPromise=require(_0x71f1('0x4'));var Mustache=require(_0x71f1('0x5'));var util=require('util');var path=require(_0x71f1('0x6'));var sox=require('sox');var csv=require(_0x71f1('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x71f1('0x8'));var squel=require('squel');var crypto=require(_0x71f1('0x9'));var jsforce=require(_0x71f1('0xa'));var deskjs=require(_0x71f1('0xb'));var toCsv=require(_0x71f1('0x7'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x71f1('0xc'));var authService=require(_0x71f1('0xd'));var qs=require(_0x71f1('0xe'));var as=require(_0x71f1('0xf'));var hardwareService=require(_0x71f1('0x10'));var logger=require('../../config/logger')(_0x71f1('0x11'));var utils=require(_0x71f1('0x12'));var config=require(_0x71f1('0x13'));var licenseUtil=require(_0x71f1('0x14'));var db=require(_0x71f1('0x15'))['db'];function respondWithStatusCode(_0x36d424,_0x33b1a1){_0x33b1a1=_0x33b1a1||0xcc;return function(_0x2f0e1e){if(_0x2f0e1e){return _0x36d424['sendStatus'](_0x33b1a1);}return _0x36d424[_0x71f1('0x16')](_0x33b1a1)[_0x71f1('0x17')]();};}function respondWithResult(_0x57fad5,_0x1e60f0){_0x1e60f0=_0x1e60f0||0xc8;return function(_0x399d21){if(_0x399d21){return _0x57fad5[_0x71f1('0x16')](_0x1e60f0)[_0x71f1('0x18')](_0x399d21);}};}function respondWithFilteredResult(_0x4ae480,_0x4ba8c8){return function(_0x37c695){if(_0x37c695){var _0x1abae3=typeof _0x4ba8c8[_0x71f1('0x19')]===_0x71f1('0x1a')&&typeof _0x4ba8c8[_0x71f1('0x1b')]==='undefined';var _0x4ac912=_0x37c695[_0x71f1('0x1c')];var _0x5e49fc=_0x1abae3?0x0:_0x4ba8c8[_0x71f1('0x19')];var _0x44ee79=_0x1abae3?_0x37c695[_0x71f1('0x1c')]:_0x4ba8c8[_0x71f1('0x19')]+_0x4ba8c8[_0x71f1('0x1b')];var _0x536e76;if(_0x44ee79>=_0x4ac912){_0x44ee79=_0x4ac912;_0x536e76=0xc8;}else{_0x536e76=0xce;}_0x4ae480[_0x71f1('0x16')](_0x536e76);return _0x4ae480[_0x71f1('0x1d')](_0x71f1('0x1e'),_0x5e49fc+'-'+_0x44ee79+'/'+_0x4ac912)[_0x71f1('0x18')](_0x37c695);}return null;};}function patchUpdates(_0x1a2410){return function(_0xde337e){try{jsonpatch[_0x71f1('0x1f')](_0xde337e,_0x1a2410,!![]);}catch(_0x1be421){return BPromise[_0x71f1('0x20')](_0x1be421);}return _0xde337e[_0x71f1('0x21')]();};}function saveUpdates(_0x44b338,_0x392101){return function(_0x1aca33){if(_0x1aca33){return _0x1aca33[_0x71f1('0x22')](_0x44b338)[_0x71f1('0x23')](function(_0x300563){return _0x300563;});}return null;};}function removeEntity(_0x387546,_0x5116c4){return function(_0x469147){if(_0x469147){return _0x469147[_0x71f1('0x24')]()[_0x71f1('0x23')](function(){_0x387546[_0x71f1('0x16')](0xcc)[_0x71f1('0x17')]();});}};}function handleEntityNotFound(_0x419b8a,_0x46d977){return function(_0x1e79da){if(!_0x1e79da){_0x419b8a[_0x71f1('0x25')](0x194);}return _0x1e79da;};}function handleError(_0x222ce9,_0x46aba2){_0x46aba2=_0x46aba2||0x1f4;return function(_0xc493ba){logger[_0x71f1('0x26')](_0xc493ba[_0x71f1('0x27')]);if(_0xc493ba[_0x71f1('0x28')]){delete _0xc493ba[_0x71f1('0x28')];}_0x222ce9[_0x71f1('0x16')](_0x46aba2)['send'](_0xc493ba);};}exports[_0x71f1('0x29')]=function(_0x2b5716,_0x1aea12){var _0x338b51={},_0x4287bd={},_0x109ac4={'count':0x0,'rows':[]};var _0x5ce2bc=_['map'](db[_0x71f1('0x2a')]['rawAttributes'],function(_0x54a50b){return{'name':_0x54a50b[_0x71f1('0x2b')],'type':_0x54a50b[_0x71f1('0x2c')]['key']};});_0x4287bd[_0x71f1('0x2d')]=_[_0x71f1('0x2e')](_0x5ce2bc,_0x71f1('0x28'));_0x4287bd[_0x71f1('0x2f')]=_[_0x71f1('0x30')](_0x2b5716[_0x71f1('0x2f')]);_0x4287bd[_0x71f1('0x31')]=_[_0x71f1('0x32')](_0x4287bd[_0x71f1('0x2d')],_0x4287bd['query']);_0x338b51['attributes']=_[_0x71f1('0x32')](_0x4287bd[_0x71f1('0x2d')],qs[_0x71f1('0x33')](_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x33')]));_0x338b51['attributes']=_0x338b51['attributes'][_0x71f1('0x34')]?_0x338b51[_0x71f1('0x35')]:_0x4287bd[_0x71f1('0x2d')];if(!_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x36')]('nolimit')){_0x338b51[_0x71f1('0x1b')]=qs['limit'](_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x1b')]);_0x338b51['offset']=qs['offset'](_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x19')]);}_0x338b51['order']=qs[_0x71f1('0x37')](_0x2b5716[_0x71f1('0x2f')]['sort']);_0x338b51['where']=qs[_0x71f1('0x31')](_[_0x71f1('0x38')](_0x2b5716['query'],_0x4287bd[_0x71f1('0x31')]),_0x5ce2bc);if(_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x39')]){_0x338b51['where']=_[_0x71f1('0x3a')](_0x338b51[_0x71f1('0x3b')],{'$or':_['map'](_0x5ce2bc,function(_0x20f9e1){if(_0x20f9e1[_0x71f1('0x2c')]!==_0x71f1('0x3c')){var _0x36d3e3={};_0x36d3e3[_0x20f9e1['name']]={'$like':'%'+_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x39')]+'%'};return _0x36d3e3;}})});}_0x338b51=_[_0x71f1('0x3a')]({},_0x338b51,_0x2b5716[_0x71f1('0x3d')]);var _0xd78829={'where':_0x338b51['where']};return db[_0x71f1('0x2a')][_0x71f1('0x1c')](_0xd78829)['then'](function(_0x27bb40){_0x109ac4['count']=_0x27bb40;if(_0x2b5716[_0x71f1('0x2f')][_0x71f1('0x3e')]){_0x338b51[_0x71f1('0x3f')]=[{'all':!![]}];}return db['SalesforceConfiguration'][_0x71f1('0x40')](_0x338b51);})[_0x71f1('0x23')](function(_0x1b9a75){_0x109ac4[_0x71f1('0x41')]=_0x1b9a75;return _0x109ac4;})[_0x71f1('0x23')](respondWithFilteredResult(_0x1aea12,_0x338b51))[_0x71f1('0x42')](handleError(_0x1aea12,null));};exports[_0x71f1('0x43')]=function(_0x425013,_0x1856d7){var _0x5c1c48={'raw':![],'where':{'id':_0x425013[_0x71f1('0x44')]['id']}},_0x4d591a={};_0x4d591a[_0x71f1('0x2d')]=_['keys'](db['SalesforceConfiguration'][_0x71f1('0x45')]);_0x4d591a['query']=_[_0x71f1('0x30')](_0x425013[_0x71f1('0x2f')]);_0x4d591a[_0x71f1('0x31')]=_[_0x71f1('0x32')](_0x4d591a['model'],_0x4d591a[_0x71f1('0x2f')]);_0x5c1c48[_0x71f1('0x35')]=_['intersection'](_0x4d591a[_0x71f1('0x2d')],qs['fields'](_0x425013[_0x71f1('0x2f')][_0x71f1('0x33')]));_0x5c1c48[_0x71f1('0x35')]=_0x5c1c48[_0x71f1('0x35')][_0x71f1('0x34')]?_0x5c1c48[_0x71f1('0x35')]:_0x4d591a[_0x71f1('0x2d')];if(_0x425013['query']['includeAll']){_0x5c1c48[_0x71f1('0x3f')]=[{'all':!![]}];}_0x5c1c48=_[_0x71f1('0x3a')]({},_0x5c1c48,_0x425013['options']);return db['SalesforceConfiguration'][_0x71f1('0x46')](_0x5c1c48)[_0x71f1('0x23')](handleEntityNotFound(_0x1856d7,null))[_0x71f1('0x23')](respondWithResult(_0x1856d7,null))[_0x71f1('0x42')](handleError(_0x1856d7,null));};exports[_0x71f1('0x47')]=function(_0x1237ea,_0x1416a8){return db['SalesforceConfiguration'][_0x71f1('0x47')](_0x1237ea[_0x71f1('0x48')],{})[_0x71f1('0x23')](respondWithResult(_0x1416a8,0xc9))[_0x71f1('0x42')](handleError(_0x1416a8,null));};exports[_0x71f1('0x22')]=function(_0x2739bf,_0x5ba03a){if(_0x2739bf[_0x71f1('0x48')]['id']){delete _0x2739bf[_0x71f1('0x48')]['id'];}return db[_0x71f1('0x2a')][_0x71f1('0x46')]({'where':{'id':_0x2739bf[_0x71f1('0x44')]['id']}})[_0x71f1('0x23')](handleEntityNotFound(_0x5ba03a,null))[_0x71f1('0x23')](saveUpdates(_0x2739bf[_0x71f1('0x48')],null))['then'](respondWithResult(_0x5ba03a,null))['catch'](handleError(_0x5ba03a,null));};exports[_0x71f1('0x24')]=function(_0x5b4e8,_0x24b64b){return db['SalesforceConfiguration'][_0x71f1('0x46')]({'where':{'id':_0x5b4e8[_0x71f1('0x44')]['id']}})['then'](handleEntityNotFound(_0x24b64b,null))[_0x71f1('0x23')](removeEntity(_0x24b64b,null))[_0x71f1('0x42')](handleError(_0x24b64b,null));};exports['getFields']=function(_0x4e1cb6,_0x2154fa,_0x1c0d83){var _0x424a14={};var _0x3e6957={};var _0x15177b;var _0x21f9da;return db[_0x71f1('0x2a')][_0x71f1('0x49')]({'where':{'id':_0x4e1cb6[_0x71f1('0x44')]['id']}})[_0x71f1('0x23')](handleEntityNotFound(_0x2154fa,null))[_0x71f1('0x23')](function(_0x20cd9d){if(_0x20cd9d){_0x15177b=_0x20cd9d;_0x3e6957['model']=_['keys'](db['SalesforceField'][_0x71f1('0x45')]);_0x3e6957[_0x71f1('0x2f')]=_[_0x71f1('0x30')](_0x4e1cb6[_0x71f1('0x2f')]);_0x3e6957['filters']=_['intersection'](_0x3e6957['model'],_0x3e6957[_0x71f1('0x2f')]);_0x424a14[_0x71f1('0x35')]=_[_0x71f1('0x32')](_0x3e6957[_0x71f1('0x2d')],qs[_0x71f1('0x33')](_0x4e1cb6[_0x71f1('0x2f')][_0x71f1('0x33')]));_0x424a14['attributes']=_0x424a14[_0x71f1('0x35')][_0x71f1('0x34')]?_0x424a14[_0x71f1('0x35')]:_0x3e6957[_0x71f1('0x2d')];_0x424a14[_0x71f1('0x4a')]=qs['sort'](_0x4e1cb6[_0x71f1('0x2f')][_0x71f1('0x37')]);_0x424a14[_0x71f1('0x3b')]=qs['filters'](_['pick'](_0x4e1cb6[_0x71f1('0x2f')],_0x3e6957[_0x71f1('0x31')]));if(_0x4e1cb6['query'][_0x71f1('0x39')]){_0x424a14[_0x71f1('0x3b')]=_['merge'](_0x424a14[_0x71f1('0x3b')],{'$or':_[_0x71f1('0x2e')](_0x424a14['attributes'],function(_0x25932d){var _0x2f4150={};_0x2f4150[_0x25932d]={'$like':'%'+_0x4e1cb6['query'][_0x71f1('0x39')]+'%'};return _0x2f4150;})});}_0x424a14=_[_0x71f1('0x3a')]({},_0x424a14,_0x4e1cb6[_0x71f1('0x3d')]);return _0x15177b['getFields'](_0x424a14);}})['then'](function(_0x5c72bf){if(_0x5c72bf){_0x21f9da=_0x5c72bf[_0x71f1('0x34')];if(!_0x4e1cb6[_0x71f1('0x2f')][_0x71f1('0x36')](_0x71f1('0x4b'))){_0x424a14[_0x71f1('0x1b')]=qs[_0x71f1('0x1b')](_0x4e1cb6['query'][_0x71f1('0x1b')]);_0x424a14['offset']=qs['offset'](_0x4e1cb6[_0x71f1('0x2f')][_0x71f1('0x19')]);}return _0x15177b[_0x71f1('0x4c')](_0x424a14);}})[_0x71f1('0x23')](function(_0x5abd12){if(_0x5abd12){return _0x5abd12?{'count':_0x21f9da,'rows':_0x5abd12}:null;}})[_0x71f1('0x23')](respondWithResult(_0x2154fa,null))[_0x71f1('0x42')](handleError(_0x2154fa,null));};exports['getSubjects']=function(_0x270366,_0x534b2a,_0x2ef98b){var _0xc2bf6c={};var _0x5c955e={};var _0x45d161;var _0x1545ae;return db[_0x71f1('0x2a')]['findOne']({'where':{'id':_0x270366[_0x71f1('0x44')]['id']}})[_0x71f1('0x23')](handleEntityNotFound(_0x534b2a,null))[_0x71f1('0x23')](function(_0x3f778b){if(_0x3f778b){_0x45d161=_0x3f778b;_0x5c955e[_0x71f1('0x2d')]=_[_0x71f1('0x30')](db['SalesforceField']['rawAttributes']);_0x5c955e[_0x71f1('0x2f')]=_[_0x71f1('0x30')](_0x270366['query']);_0x5c955e[_0x71f1('0x31')]=_[_0x71f1('0x32')](_0x5c955e[_0x71f1('0x2d')],_0x5c955e['query']);_0xc2bf6c[_0x71f1('0x35')]=_['intersection'](_0x5c955e['model'],qs[_0x71f1('0x33')](_0x270366[_0x71f1('0x2f')][_0x71f1('0x33')]));_0xc2bf6c[_0x71f1('0x35')]=_0xc2bf6c[_0x71f1('0x35')][_0x71f1('0x34')]?_0xc2bf6c[_0x71f1('0x35')]:_0x5c955e[_0x71f1('0x2d')];_0xc2bf6c[_0x71f1('0x4a')]=qs[_0x71f1('0x37')](_0x270366[_0x71f1('0x2f')][_0x71f1('0x37')]);_0xc2bf6c[_0x71f1('0x3b')]=qs['filters'](_['pick'](_0x270366[_0x71f1('0x2f')],_0x5c955e[_0x71f1('0x31')]));if(_0x270366[_0x71f1('0x2f')][_0x71f1('0x39')]){_0xc2bf6c['where']=_[_0x71f1('0x3a')](_0xc2bf6c[_0x71f1('0x3b')],{'$or':_[_0x71f1('0x2e')](_0xc2bf6c[_0x71f1('0x35')],function(_0x128f7e){var _0x3b2e61={};_0x3b2e61[_0x128f7e]={'$like':'%'+_0x270366[_0x71f1('0x2f')][_0x71f1('0x39')]+'%'};return _0x3b2e61;})});}_0xc2bf6c=_[_0x71f1('0x3a')]({},_0xc2bf6c,_0x270366[_0x71f1('0x3d')]);return _0x45d161[_0x71f1('0x4d')](_0xc2bf6c);}})['then'](function(_0x1857cf){if(_0x1857cf){_0x1545ae=_0x1857cf[_0x71f1('0x34')];if(!_0x270366['query'][_0x71f1('0x36')](_0x71f1('0x4b'))){_0xc2bf6c[_0x71f1('0x1b')]=qs[_0x71f1('0x1b')](_0x270366[_0x71f1('0x2f')][_0x71f1('0x1b')]);_0xc2bf6c['offset']=qs['offset'](_0x270366[_0x71f1('0x2f')][_0x71f1('0x19')]);}return _0x45d161['getSubjects'](_0xc2bf6c);}})[_0x71f1('0x23')](function(_0x2790cb){if(_0x2790cb){return _0x2790cb?{'count':_0x1545ae,'rows':_0x2790cb}:null;}})[_0x71f1('0x23')](respondWithResult(_0x534b2a,null))[_0x71f1('0x42')](handleError(_0x534b2a,null));};exports[_0x71f1('0x4e')]=function(_0x1478c6,_0x9ce385,_0x466130){var _0x4788ed={};var _0x54bff4={};var _0x5efbfb;var _0x537adb;return db[_0x71f1('0x2a')]['findOne']({'where':{'id':_0x1478c6[_0x71f1('0x44')]['id']}})[_0x71f1('0x23')](handleEntityNotFound(_0x9ce385,null))[_0x71f1('0x23')](function(_0x397337){if(_0x397337){_0x5efbfb=_0x397337;_0x54bff4['model']=_[_0x71f1('0x30')](db[_0x71f1('0x4f')][_0x71f1('0x45')]);_0x54bff4['query']=_[_0x71f1('0x30')](_0x1478c6[_0x71f1('0x2f')]);_0x54bff4[_0x71f1('0x31')]=_[_0x71f1('0x32')](_0x54bff4[_0x71f1('0x2d')],_0x54bff4['query']);_0x4788ed['attributes']=_[_0x71f1('0x32')](_0x54bff4[_0x71f1('0x2d')],qs['fields'](_0x1478c6[_0x71f1('0x2f')][_0x71f1('0x33')]));_0x4788ed['attributes']=_0x4788ed[_0x71f1('0x35')][_0x71f1('0x34')]?_0x4788ed['attributes']:_0x54bff4[_0x71f1('0x2d')];_0x4788ed[_0x71f1('0x4a')]=qs['sort'](_0x1478c6[_0x71f1('0x2f')][_0x71f1('0x37')]);_0x4788ed[_0x71f1('0x3b')]=qs[_0x71f1('0x31')](_[_0x71f1('0x38')](_0x1478c6[_0x71f1('0x2f')],_0x54bff4[_0x71f1('0x31')]));if(_0x1478c6[_0x71f1('0x2f')][_0x71f1('0x39')]){_0x4788ed[_0x71f1('0x3b')]=_[_0x71f1('0x3a')](_0x4788ed[_0x71f1('0x3b')],{'$or':_['map'](_0x4788ed[_0x71f1('0x35')],function(_0x207bd1){var _0x273897={};_0x273897[_0x207bd1]={'$like':'%'+_0x1478c6['query']['filter']+'%'};return _0x273897;})});}_0x4788ed=_[_0x71f1('0x3a')]({},_0x4788ed,_0x1478c6['options']);return _0x5efbfb[_0x71f1('0x4e')](_0x4788ed);}})['then'](function(_0xe47ca4){if(_0xe47ca4){_0x537adb=_0xe47ca4[_0x71f1('0x34')];if(!_0x1478c6[_0x71f1('0x2f')][_0x71f1('0x36')](_0x71f1('0x4b'))){_0x4788ed[_0x71f1('0x1b')]=qs[_0x71f1('0x1b')](_0x1478c6['query'][_0x71f1('0x1b')]);_0x4788ed[_0x71f1('0x19')]=qs[_0x71f1('0x19')](_0x1478c6['query']['offset']);}return _0x5efbfb['getDescriptions'](_0x4788ed);}})['then'](function(_0x225e1a){if(_0x225e1a){return _0x225e1a?{'count':_0x537adb,'rows':_0x225e1a}:null;}})['then'](respondWithResult(_0x9ce385,null))[_0x71f1('0x42')](handleError(_0x9ce385,null));};