872b6ce44854b6ff450a2554ffcabcf6bdbbf63d
[motion2.git] / server / api / intFreshsalesConfiguration / intFreshsalesConfiguration.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 _0x37f8=['json','reject','save','update','destroy','end','sendStatus','stack','name','send','index','FreshsalesConfiguration','rawAttributes','type','key','model','query','keys','intersection','attributes','fields','hasOwnProperty','nolimit','limit','order','sort','where','pick','filters','filter','merge','map','VIRTUAL','options','includeAll','include','rows','show','params','length','create','body','find','getSubjects','findOne','getDescriptions','FreshsalesField','getFields','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','sox','to-csv','squel','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','client','http','then','FreshsalesConfiguration,\x20%s,\x20%s','debug','FreshsalesConfiguration,\x20%s,\x20%s,\x20%s','request\x20sent','error','code','message','info','result','catch','status','count','offset','set','Content-Range'];(function(_0x52dc13,_0x5813c2){var _0x1143e7=function(_0x4c0a4c){while(--_0x4c0a4c){_0x52dc13['push'](_0x52dc13['shift']());}};_0x1143e7(++_0x5813c2);}(_0x37f8,0x13e));var _0x837f=function(_0x101d2d,_0x5963d9){_0x101d2d=_0x101d2d-0x0;var _0x5982d5=_0x37f8[_0x101d2d];return _0x5982d5;};'use strict';var pdf=require(_0x837f('0x0'));var emlformat=require(_0x837f('0x1'));var rimraf=require(_0x837f('0x2'));var zipdir=require(_0x837f('0x3'));var jsonpatch=require(_0x837f('0x4'));var rp=require(_0x837f('0x5'));var moment=require(_0x837f('0x6'));var BPromise=require(_0x837f('0x7'));var Mustache=require('mustache');var util=require(_0x837f('0x8'));var path=require('path');var sox=require(_0x837f('0x9'));var csv=require(_0x837f('0xa'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var squel=require(_0x837f('0xb'));var crypto=require('crypto');var jsforce=require(_0x837f('0xc'));var deskjs=require(_0x837f('0xd'));var toCsv=require(_0x837f('0xa'));var querystring=require(_0x837f('0xe'));var Papa=require(_0x837f('0xf'));var Redis=require('ioredis');var authService=require(_0x837f('0x10'));var qs=require(_0x837f('0x11'));var as=require(_0x837f('0x12'));var hardwareService=require(_0x837f('0x13'));var logger=require('../../config/logger')('api');var utils=require(_0x837f('0x14'));var config=require(_0x837f('0x15'));var licenseUtil=require(_0x837f('0x16'));var db=require(_0x837f('0x17'))['db'];var jayson=require('jayson/promise');var client=jayson[_0x837f('0x18')][_0x837f('0x19')]({'port':0x232a});function respondWithRpcPromise(_0x576e30,_0x4fa6de,_0x545cc1,_0x2dc0ac){return new BPromise(function(_0x31c25f,_0x335c70){var _0x33195a=_0x2dc0ac||client;return _0x33195a['request'](_0x576e30,_0x545cc1)[_0x837f('0x1a')](function(_0x327746){logger['info'](_0x837f('0x1b'),_0x4fa6de,'request\x20sent');logger[_0x837f('0x1c')](_0x837f('0x1d'),_0x4fa6de,_0x837f('0x1e'),JSON['stringify'](_0x327746));if(_0x327746[_0x837f('0x1f')]){if(_0x327746[_0x837f('0x1f')][_0x837f('0x20')]===0x1f4){logger[_0x837f('0x1f')](_0x837f('0x1b'),_0x4fa6de,_0x327746[_0x837f('0x1f')][_0x837f('0x21')]);return _0x335c70(_0x327746[_0x837f('0x1f')][_0x837f('0x21')]);}logger[_0x837f('0x1f')](_0x837f('0x1b'),_0x4fa6de,_0x327746[_0x837f('0x1f')][_0x837f('0x21')]);return _0x31c25f(_0x327746[_0x837f('0x1f')][_0x837f('0x21')]);}else{logger[_0x837f('0x22')](_0x837f('0x1b'),_0x4fa6de,_0x837f('0x1e'));_0x31c25f(_0x327746[_0x837f('0x23')][_0x837f('0x21')]);}})[_0x837f('0x24')](function(_0x374e6b){logger['error'](_0x837f('0x1b'),_0x4fa6de,_0x374e6b);_0x335c70(_0x374e6b);});});}function respondWithStatusCode(_0x58ed47,_0x361e5d){_0x361e5d=_0x361e5d||0xcc;return function(_0xa484c4){if(_0xa484c4){return _0x58ed47['sendStatus'](_0x361e5d);}return _0x58ed47[_0x837f('0x25')](_0x361e5d)['end']();};}function respondWithResult(_0x344bf8,_0x273410){_0x273410=_0x273410||0xc8;return function(_0x26d4a0){if(_0x26d4a0){return _0x344bf8['status'](_0x273410)['json'](_0x26d4a0);}};}function respondWithFilteredResult(_0x414c11,_0x8960cc){return function(_0x8c42ba){if(_0x8c42ba){var _0x1f0665=_0x8c42ba[_0x837f('0x26')],_0x1c5dc3=_0x8960cc['offset'],_0x2531b2=_0x8960cc[_0x837f('0x27')]+_0x8960cc['limit'],_0x3263df;if(_0x2531b2>=_0x1f0665){_0x2531b2=_0x1f0665;_0x3263df=0xc8;}else{_0x3263df=0xce;}_0x414c11['status'](_0x3263df);return _0x414c11[_0x837f('0x28')](_0x837f('0x29'),_0x1c5dc3+'-'+_0x2531b2+'/'+_0x1f0665)[_0x837f('0x2a')](_0x8c42ba);}return null;};}function patchUpdates(_0x2a637a){return function(_0xa5d632){try{jsonpatch['apply'](_0xa5d632,_0x2a637a,!![]);}catch(_0x87d1c4){return BPromise[_0x837f('0x2b')](_0x87d1c4);}return _0xa5d632[_0x837f('0x2c')]();};}function saveUpdates(_0x699bb0,_0x36076a){return function(_0x5497b1){if(_0x5497b1){return _0x5497b1[_0x837f('0x2d')](_0x699bb0)[_0x837f('0x1a')](function(_0x5762d4){return _0x5762d4;});}return null;};}function removeEntity(_0x26d6d3,_0x1fb17d){return function(_0x23b62c){if(_0x23b62c){return _0x23b62c[_0x837f('0x2e')]()[_0x837f('0x1a')](function(){_0x26d6d3['status'](0xcc)[_0x837f('0x2f')]();});}};}function handleEntityNotFound(_0x438ef9,_0x3611f8){return function(_0x21a7ef){if(!_0x21a7ef){_0x438ef9[_0x837f('0x30')](0x194);}return _0x21a7ef;};}function handleError(_0x2fda0c,_0x3c24d9){_0x3c24d9=_0x3c24d9||0x1f4;return function(_0x11f91a){logger['error'](_0x11f91a[_0x837f('0x31')]);if(_0x11f91a[_0x837f('0x32')]){delete _0x11f91a[_0x837f('0x32')];}_0x2fda0c['status'](_0x3c24d9)[_0x837f('0x33')](_0x11f91a);};}exports[_0x837f('0x34')]=function(_0x39249a,_0x31df0a){var _0x400b74={},_0x89abea={},_0xddbff7={'count':0x0,'rows':[]};var _0x52c606=_['map'](db[_0x837f('0x35')][_0x837f('0x36')],function(_0x4e65cd){return{'name':_0x4e65cd['fieldName'],'type':_0x4e65cd[_0x837f('0x37')][_0x837f('0x38')]};});_0x89abea[_0x837f('0x39')]=_['map'](_0x52c606,_0x837f('0x32'));_0x89abea[_0x837f('0x3a')]=_[_0x837f('0x3b')](_0x39249a[_0x837f('0x3a')]);_0x89abea['filters']=_[_0x837f('0x3c')](_0x89abea[_0x837f('0x39')],_0x89abea[_0x837f('0x3a')]);_0x400b74[_0x837f('0x3d')]=_[_0x837f('0x3c')](_0x89abea[_0x837f('0x39')],qs[_0x837f('0x3e')](_0x39249a[_0x837f('0x3a')][_0x837f('0x3e')]));_0x400b74[_0x837f('0x3d')]=_0x400b74['attributes']['length']?_0x400b74[_0x837f('0x3d')]:_0x89abea[_0x837f('0x39')];if(!_0x39249a[_0x837f('0x3a')][_0x837f('0x3f')](_0x837f('0x40'))){_0x400b74[_0x837f('0x41')]=qs[_0x837f('0x41')](_0x39249a[_0x837f('0x3a')][_0x837f('0x41')]);_0x400b74['offset']=qs[_0x837f('0x27')](_0x39249a[_0x837f('0x3a')][_0x837f('0x27')]);}_0x400b74[_0x837f('0x42')]=qs[_0x837f('0x43')](_0x39249a[_0x837f('0x3a')][_0x837f('0x43')]);_0x400b74[_0x837f('0x44')]=qs['filters'](_[_0x837f('0x45')](_0x39249a[_0x837f('0x3a')],_0x89abea[_0x837f('0x46')]),_0x52c606);if(_0x39249a[_0x837f('0x3a')][_0x837f('0x47')]){_0x400b74['where']=_[_0x837f('0x48')](_0x400b74[_0x837f('0x44')],{'$or':_[_0x837f('0x49')](_0x52c606,function(_0xf14bd7){if(_0xf14bd7[_0x837f('0x37')]!==_0x837f('0x4a')){var _0x5b8ca5={};_0x5b8ca5[_0xf14bd7['name']]={'$like':'%'+_0x39249a[_0x837f('0x3a')]['filter']+'%'};return _0x5b8ca5;}})});}_0x400b74=_[_0x837f('0x48')]({},_0x400b74,_0x39249a[_0x837f('0x4b')]);var _0x57ae21={'where':_0x400b74[_0x837f('0x44')]};return db[_0x837f('0x35')][_0x837f('0x26')](_0x57ae21)[_0x837f('0x1a')](function(_0x50da8b){_0xddbff7['count']=_0x50da8b;if(_0x39249a[_0x837f('0x3a')][_0x837f('0x4c')]){_0x400b74[_0x837f('0x4d')]=[{'all':!![]}];}return db[_0x837f('0x35')]['findAll'](_0x400b74);})[_0x837f('0x1a')](function(_0x23a7fb){_0xddbff7[_0x837f('0x4e')]=_0x23a7fb;return _0xddbff7;})[_0x837f('0x1a')](respondWithFilteredResult(_0x31df0a,_0x400b74))[_0x837f('0x24')](handleError(_0x31df0a,null));};exports[_0x837f('0x4f')]=function(_0x434eda,_0x59f9b2){var _0x151c2e={'raw':![],'where':{'id':_0x434eda[_0x837f('0x50')]['id']}},_0x57778e={};_0x57778e[_0x837f('0x39')]=_[_0x837f('0x3b')](db[_0x837f('0x35')][_0x837f('0x36')]);_0x57778e[_0x837f('0x3a')]=_[_0x837f('0x3b')](_0x434eda[_0x837f('0x3a')]);_0x57778e[_0x837f('0x46')]=_[_0x837f('0x3c')](_0x57778e['model'],_0x57778e[_0x837f('0x3a')]);_0x151c2e[_0x837f('0x3d')]=_[_0x837f('0x3c')](_0x57778e[_0x837f('0x39')],qs[_0x837f('0x3e')](_0x434eda[_0x837f('0x3a')][_0x837f('0x3e')]));_0x151c2e[_0x837f('0x3d')]=_0x151c2e['attributes'][_0x837f('0x51')]?_0x151c2e[_0x837f('0x3d')]:_0x57778e['model'];if(_0x434eda[_0x837f('0x3a')][_0x837f('0x4c')]){_0x151c2e[_0x837f('0x4d')]=[{'all':!![]}];}_0x151c2e=_[_0x837f('0x48')]({},_0x151c2e,_0x434eda[_0x837f('0x4b')]);return db[_0x837f('0x35')]['find'](_0x151c2e)[_0x837f('0x1a')](handleEntityNotFound(_0x59f9b2,null))[_0x837f('0x1a')](respondWithResult(_0x59f9b2,null))[_0x837f('0x24')](handleError(_0x59f9b2,null));};exports[_0x837f('0x52')]=function(_0xfa9bed,_0x438612){return db[_0x837f('0x35')][_0x837f('0x52')](_0xfa9bed['body'],{})['then'](respondWithResult(_0x438612,0xc9))['catch'](handleError(_0x438612,null));};exports[_0x837f('0x2d')]=function(_0x334ce1,_0x5f0d4c){if(_0x334ce1[_0x837f('0x53')]['id']){delete _0x334ce1['body']['id'];}return db['FreshsalesConfiguration']['find']({'where':{'id':_0x334ce1['params']['id']}})[_0x837f('0x1a')](handleEntityNotFound(_0x5f0d4c,null))[_0x837f('0x1a')](saveUpdates(_0x334ce1[_0x837f('0x53')],null))[_0x837f('0x1a')](respondWithResult(_0x5f0d4c,null))[_0x837f('0x24')](handleError(_0x5f0d4c,null));};exports[_0x837f('0x2e')]=function(_0x218abd,_0x240016){return db[_0x837f('0x35')][_0x837f('0x54')]({'where':{'id':_0x218abd[_0x837f('0x50')]['id']}})[_0x837f('0x1a')](handleEntityNotFound(_0x240016,null))['then'](removeEntity(_0x240016,null))[_0x837f('0x24')](handleError(_0x240016,null));};exports[_0x837f('0x55')]=function(_0x869984,_0x2d1fbf,_0xd68333){var _0x551bee={};var _0x48859f={};var _0x374ec2;var _0xd12cdc;return db[_0x837f('0x35')][_0x837f('0x56')]({'where':{'id':_0x869984[_0x837f('0x50')]['id']}})[_0x837f('0x1a')](handleEntityNotFound(_0x2d1fbf,null))[_0x837f('0x1a')](function(_0x5d5c8b){if(_0x5d5c8b){_0x374ec2=_0x5d5c8b;_0x48859f[_0x837f('0x39')]=_[_0x837f('0x3b')](db['FreshsalesField'][_0x837f('0x36')]);_0x48859f[_0x837f('0x3a')]=_[_0x837f('0x3b')](_0x869984[_0x837f('0x3a')]);_0x48859f['filters']=_[_0x837f('0x3c')](_0x48859f[_0x837f('0x39')],_0x48859f[_0x837f('0x3a')]);_0x551bee[_0x837f('0x3d')]=_['intersection'](_0x48859f['model'],qs['fields'](_0x869984[_0x837f('0x3a')]['fields']));_0x551bee[_0x837f('0x3d')]=_0x551bee['attributes'][_0x837f('0x51')]?_0x551bee[_0x837f('0x3d')]:_0x48859f[_0x837f('0x39')];_0x551bee[_0x837f('0x42')]=qs['sort'](_0x869984[_0x837f('0x3a')][_0x837f('0x43')]);_0x551bee[_0x837f('0x44')]=qs['filters'](_[_0x837f('0x45')](_0x869984['query'],_0x48859f['filters']));if(_0x869984['query']['filter']){_0x551bee[_0x837f('0x44')]=_[_0x837f('0x48')](_0x551bee[_0x837f('0x44')],{'$or':_[_0x837f('0x49')](_0x551bee[_0x837f('0x3d')],function(_0x4babf6){var _0x155346={};_0x155346[_0x4babf6]={'$like':'%'+_0x869984[_0x837f('0x3a')]['filter']+'%'};return _0x155346;})});}_0x551bee=_[_0x837f('0x48')]({},_0x551bee,_0x869984['options']);return _0x374ec2[_0x837f('0x55')](_0x551bee);}})['then'](function(_0xbc1281){if(_0xbc1281){_0xd12cdc=_0xbc1281[_0x837f('0x51')];if(!_0x869984[_0x837f('0x3a')][_0x837f('0x3f')](_0x837f('0x40'))){_0x551bee['limit']=qs['limit'](_0x869984[_0x837f('0x3a')][_0x837f('0x41')]);_0x551bee['offset']=qs[_0x837f('0x27')](_0x869984[_0x837f('0x3a')][_0x837f('0x27')]);}return _0x374ec2[_0x837f('0x55')](_0x551bee);}})['then'](function(_0x18eb01){if(_0x18eb01){return _0x18eb01?{'count':_0xd12cdc,'rows':_0x18eb01}:null;}})[_0x837f('0x1a')](respondWithResult(_0x2d1fbf,null))[_0x837f('0x24')](handleError(_0x2d1fbf,null));};exports[_0x837f('0x57')]=function(_0x1b2cd6,_0x21ad1d,_0x53d743){var _0x296897={};var _0x2e3062={};var _0x15fb3c;var _0x13e8ba;return db[_0x837f('0x35')][_0x837f('0x56')]({'where':{'id':_0x1b2cd6[_0x837f('0x50')]['id']}})[_0x837f('0x1a')](handleEntityNotFound(_0x21ad1d,null))['then'](function(_0x532753){if(_0x532753){_0x15fb3c=_0x532753;_0x2e3062['model']=_['keys'](db[_0x837f('0x58')][_0x837f('0x36')]);_0x2e3062[_0x837f('0x3a')]=_['keys'](_0x1b2cd6[_0x837f('0x3a')]);_0x2e3062[_0x837f('0x46')]=_[_0x837f('0x3c')](_0x2e3062[_0x837f('0x39')],_0x2e3062[_0x837f('0x3a')]);_0x296897[_0x837f('0x3d')]=_[_0x837f('0x3c')](_0x2e3062['model'],qs[_0x837f('0x3e')](_0x1b2cd6[_0x837f('0x3a')][_0x837f('0x3e')]));_0x296897[_0x837f('0x3d')]=_0x296897[_0x837f('0x3d')][_0x837f('0x51')]?_0x296897[_0x837f('0x3d')]:_0x2e3062['model'];_0x296897[_0x837f('0x42')]=qs[_0x837f('0x43')](_0x1b2cd6[_0x837f('0x3a')][_0x837f('0x43')]);_0x296897[_0x837f('0x44')]=qs['filters'](_[_0x837f('0x45')](_0x1b2cd6[_0x837f('0x3a')],_0x2e3062[_0x837f('0x46')]));if(_0x1b2cd6['query'][_0x837f('0x47')]){_0x296897['where']=_[_0x837f('0x48')](_0x296897['where'],{'$or':_[_0x837f('0x49')](_0x296897[_0x837f('0x3d')],function(_0x224cc4){var _0x146bb7={};_0x146bb7[_0x224cc4]={'$like':'%'+_0x1b2cd6[_0x837f('0x3a')][_0x837f('0x47')]+'%'};return _0x146bb7;})});}_0x296897=_[_0x837f('0x48')]({},_0x296897,_0x1b2cd6[_0x837f('0x4b')]);return _0x15fb3c[_0x837f('0x57')](_0x296897);}})[_0x837f('0x1a')](function(_0x466e3c){if(_0x466e3c){_0x13e8ba=_0x466e3c[_0x837f('0x51')];if(!_0x1b2cd6[_0x837f('0x3a')][_0x837f('0x3f')](_0x837f('0x40'))){_0x296897[_0x837f('0x41')]=qs[_0x837f('0x41')](_0x1b2cd6['query'][_0x837f('0x41')]);_0x296897[_0x837f('0x27')]=qs['offset'](_0x1b2cd6[_0x837f('0x3a')][_0x837f('0x27')]);}return _0x15fb3c[_0x837f('0x57')](_0x296897);}})[_0x837f('0x1a')](function(_0xe1374a){if(_0xe1374a){return _0xe1374a?{'count':_0x13e8ba,'rows':_0xe1374a}:null;}})[_0x837f('0x1a')](respondWithResult(_0x21ad1d,null))[_0x837f('0x24')](handleError(_0x21ad1d,null));};exports[_0x837f('0x59')]=function(_0x493baf,_0x26808f,_0x13307c){var _0x4387f4={};var _0x99108b={};var _0xae9ae5;var _0x33a57e;return db[_0x837f('0x35')][_0x837f('0x56')]({'where':{'id':_0x493baf['params']['id']}})[_0x837f('0x1a')](handleEntityNotFound(_0x26808f,null))[_0x837f('0x1a')](function(_0x3bc49d){if(_0x3bc49d){_0xae9ae5=_0x3bc49d;_0x99108b[_0x837f('0x39')]=_[_0x837f('0x3b')](db['FreshsalesField'][_0x837f('0x36')]);_0x99108b['query']=_[_0x837f('0x3b')](_0x493baf['query']);_0x99108b[_0x837f('0x46')]=_[_0x837f('0x3c')](_0x99108b[_0x837f('0x39')],_0x99108b['query']);_0x4387f4[_0x837f('0x3d')]=_['intersection'](_0x99108b[_0x837f('0x39')],qs[_0x837f('0x3e')](_0x493baf[_0x837f('0x3a')]['fields']));_0x4387f4[_0x837f('0x3d')]=_0x4387f4[_0x837f('0x3d')]['length']?_0x4387f4[_0x837f('0x3d')]:_0x99108b['model'];_0x4387f4[_0x837f('0x42')]=qs[_0x837f('0x43')](_0x493baf[_0x837f('0x3a')][_0x837f('0x43')]);_0x4387f4[_0x837f('0x44')]=qs[_0x837f('0x46')](_[_0x837f('0x45')](_0x493baf[_0x837f('0x3a')],_0x99108b['filters']));if(_0x493baf[_0x837f('0x3a')][_0x837f('0x47')]){_0x4387f4['where']=_[_0x837f('0x48')](_0x4387f4['where'],{'$or':_[_0x837f('0x49')](_0x4387f4['attributes'],function(_0x2f52b3){var _0x4ba3ba={};_0x4ba3ba[_0x2f52b3]={'$like':'%'+_0x493baf[_0x837f('0x3a')]['filter']+'%'};return _0x4ba3ba;})});}_0x4387f4=_['merge']({},_0x4387f4,_0x493baf[_0x837f('0x4b')]);return _0xae9ae5[_0x837f('0x59')](_0x4387f4);}})[_0x837f('0x1a')](function(_0x2cd50a){if(_0x2cd50a){_0x33a57e=_0x2cd50a[_0x837f('0x51')];if(!_0x493baf[_0x837f('0x3a')]['hasOwnProperty'](_0x837f('0x40'))){_0x4387f4['limit']=qs[_0x837f('0x41')](_0x493baf[_0x837f('0x3a')][_0x837f('0x41')]);_0x4387f4[_0x837f('0x27')]=qs[_0x837f('0x27')](_0x493baf['query'][_0x837f('0x27')]);}return _0xae9ae5[_0x837f('0x59')](_0x4387f4);}})[_0x837f('0x1a')](function(_0x15ea1b){if(_0x15ea1b){return _0x15ea1b?{'count':_0x33a57e,'rows':_0x15ea1b}:null;}})[_0x837f('0x1a')](respondWithResult(_0x26808f,null))['catch'](handleError(_0x26808f,null));};