Built motion from commit 00bf23f6.|2.6.16
[motion2.git] / server / api / intZohoConfiguration / intZohoConfiguration.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 _0x0f02=['ZohoConfiguration','includeAll','include','findAll','rows','catch','show','params','keys','find','create','body','update','getFields','findOne','rawAttributes','getSubjects','ZohoField','getDescriptions','rimraf','zip-dir','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','reject','save','then','destroy','name','index','type','map','query','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options'];(function(_0x1f0bba,_0x430c51){var _0x258c06=function(_0x4b905e){while(--_0x4b905e){_0x1f0bba['push'](_0x1f0bba['shift']());}};_0x258c06(++_0x430c51);}(_0x0f02,0x19e));var _0x20f0=function(_0xbde95a,_0x2d2c76){_0xbde95a=_0xbde95a-0x0;var _0xa36c7b=_0x0f02[_0xbde95a];return _0xa36c7b;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x20f0('0x0'));var zipdir=require(_0x20f0('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x20f0('0x2'));var BPromise=require(_0x20f0('0x3'));var Mustache=require(_0x20f0('0x4'));var util=require(_0x20f0('0x5'));var path=require(_0x20f0('0x6'));var sox=require(_0x20f0('0x7'));var csv=require(_0x20f0('0x8'));var ejs=require(_0x20f0('0x9'));var fs=require('fs');var fs_extra=require(_0x20f0('0xa'));var _=require(_0x20f0('0xb'));var squel=require(_0x20f0('0xc'));var crypto=require(_0x20f0('0xd'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x20f0('0x8'));var querystring=require(_0x20f0('0xe'));var Papa=require(_0x20f0('0xf'));var Redis=require(_0x20f0('0x10'));var authService=require(_0x20f0('0x11'));var qs=require(_0x20f0('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x20f0('0x13'));var logger=require(_0x20f0('0x14'))(_0x20f0('0x15'));var utils=require(_0x20f0('0x16'));var config=require(_0x20f0('0x17'));var licenseUtil=require(_0x20f0('0x18'));var db=require(_0x20f0('0x19'))['db'];function respondWithStatusCode(_0x370582,_0x4b0ea8){_0x4b0ea8=_0x4b0ea8||0xcc;return function(_0x4655c7){if(_0x4655c7){return _0x370582[_0x20f0('0x1a')](_0x4b0ea8);}return _0x370582[_0x20f0('0x1b')](_0x4b0ea8)[_0x20f0('0x1c')]();};}function respondWithResult(_0x186703,_0x5eccec){_0x5eccec=_0x5eccec||0xc8;return function(_0xd46f0c){if(_0xd46f0c){return _0x186703[_0x20f0('0x1b')](_0x5eccec)[_0x20f0('0x1d')](_0xd46f0c);}};}function respondWithFilteredResult(_0x406edb,_0x2780ec){return function(_0x4e12a4){if(_0x4e12a4){var _0x1d1968=typeof _0x2780ec[_0x20f0('0x1e')]===_0x20f0('0x1f')&&typeof _0x2780ec[_0x20f0('0x20')]===_0x20f0('0x1f');var _0xa4feed=_0x4e12a4[_0x20f0('0x21')];var _0x5178bc=_0x1d1968?0x0:_0x2780ec[_0x20f0('0x1e')];var _0x5b23f9=_0x1d1968?_0x4e12a4['count']:_0x2780ec[_0x20f0('0x1e')]+_0x2780ec[_0x20f0('0x20')];var _0x3fbfd4;if(_0x5b23f9>=_0xa4feed){_0x5b23f9=_0xa4feed;_0x3fbfd4=0xc8;}else{_0x3fbfd4=0xce;}_0x406edb[_0x20f0('0x1b')](_0x3fbfd4);return _0x406edb[_0x20f0('0x22')]('Content-Range',_0x5178bc+'-'+_0x5b23f9+'/'+_0xa4feed)[_0x20f0('0x1d')](_0x4e12a4);}return null;};}function patchUpdates(_0x108d62){return function(_0x4d1325){try{jsonpatch['apply'](_0x4d1325,_0x108d62,!![]);}catch(_0xd3be3b){return BPromise[_0x20f0('0x23')](_0xd3be3b);}return _0x4d1325[_0x20f0('0x24')]();};}function saveUpdates(_0x3b7cc6,_0x52c4b3){return function(_0x3545a2){if(_0x3545a2){return _0x3545a2['update'](_0x3b7cc6)[_0x20f0('0x25')](function(_0x4cb2e8){return _0x4cb2e8;});}return null;};}function removeEntity(_0x162c0c,_0x4a04bf){return function(_0x31842d){if(_0x31842d){return _0x31842d[_0x20f0('0x26')]()[_0x20f0('0x25')](function(){_0x162c0c[_0x20f0('0x1b')](0xcc)[_0x20f0('0x1c')]();});}};}function handleEntityNotFound(_0x151a82,_0x46a585){return function(_0x1baca1){if(!_0x1baca1){_0x151a82[_0x20f0('0x1a')](0x194);}return _0x1baca1;};}function handleError(_0x11e490,_0x1186d1){_0x1186d1=_0x1186d1||0x1f4;return function(_0x44888d){logger['error'](_0x44888d['stack']);if(_0x44888d[_0x20f0('0x27')]){delete _0x44888d['name'];}_0x11e490[_0x20f0('0x1b')](_0x1186d1)['send'](_0x44888d);};}exports[_0x20f0('0x28')]=function(_0x56c8d1,_0x5a1a71){var _0x287764={},_0x175974={},_0x268b6b={'count':0x0,'rows':[]};var _0x1e4f15=_['map'](db['ZohoConfiguration']['rawAttributes'],function(_0x4f8fad){return{'name':_0x4f8fad['fieldName'],'type':_0x4f8fad[_0x20f0('0x29')]['key']};});_0x175974['model']=_[_0x20f0('0x2a')](_0x1e4f15,'name');_0x175974[_0x20f0('0x2b')]=_['keys'](_0x56c8d1['query']);_0x175974[_0x20f0('0x2c')]=_[_0x20f0('0x2d')](_0x175974[_0x20f0('0x2e')],_0x175974[_0x20f0('0x2b')]);_0x287764['attributes']=_[_0x20f0('0x2d')](_0x175974[_0x20f0('0x2e')],qs[_0x20f0('0x2f')](_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x2f')]));_0x287764[_0x20f0('0x30')]=_0x287764[_0x20f0('0x30')][_0x20f0('0x31')]?_0x287764[_0x20f0('0x30')]:_0x175974[_0x20f0('0x2e')];if(!_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x32')](_0x20f0('0x33'))){_0x287764[_0x20f0('0x20')]=qs[_0x20f0('0x20')](_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x20')]);_0x287764['offset']=qs[_0x20f0('0x1e')](_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x1e')]);}_0x287764[_0x20f0('0x34')]=qs[_0x20f0('0x35')](_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x35')]);_0x287764[_0x20f0('0x36')]=qs['filters'](_[_0x20f0('0x37')](_0x56c8d1[_0x20f0('0x2b')],_0x175974[_0x20f0('0x2c')]),_0x1e4f15);if(_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x38')]){_0x287764[_0x20f0('0x36')]=_['merge'](_0x287764[_0x20f0('0x36')],{'$or':_[_0x20f0('0x2a')](_0x1e4f15,function(_0x3b8bf1){if(_0x3b8bf1[_0x20f0('0x29')]!==_0x20f0('0x39')){var _0xaa5b0c={};_0xaa5b0c[_0x3b8bf1[_0x20f0('0x27')]]={'$like':'%'+_0x56c8d1[_0x20f0('0x2b')][_0x20f0('0x38')]+'%'};return _0xaa5b0c;}})});}_0x287764=_[_0x20f0('0x3a')]({},_0x287764,_0x56c8d1[_0x20f0('0x3b')]);var _0x482ed5={'where':_0x287764[_0x20f0('0x36')]};return db[_0x20f0('0x3c')][_0x20f0('0x21')](_0x482ed5)[_0x20f0('0x25')](function(_0x266a2c){_0x268b6b[_0x20f0('0x21')]=_0x266a2c;if(_0x56c8d1['query'][_0x20f0('0x3d')]){_0x287764[_0x20f0('0x3e')]=[{'all':!![]}];}return db[_0x20f0('0x3c')][_0x20f0('0x3f')](_0x287764);})[_0x20f0('0x25')](function(_0x23fac8){_0x268b6b[_0x20f0('0x40')]=_0x23fac8;return _0x268b6b;})[_0x20f0('0x25')](respondWithFilteredResult(_0x5a1a71,_0x287764))[_0x20f0('0x41')](handleError(_0x5a1a71,null));};exports[_0x20f0('0x42')]=function(_0x2dce51,_0x1b9a77){var _0x12908d={'raw':![],'where':{'id':_0x2dce51[_0x20f0('0x43')]['id']}},_0x423c78={};_0x423c78[_0x20f0('0x2e')]=_['keys'](db[_0x20f0('0x3c')]['rawAttributes']);_0x423c78[_0x20f0('0x2b')]=_[_0x20f0('0x44')](_0x2dce51[_0x20f0('0x2b')]);_0x423c78[_0x20f0('0x2c')]=_[_0x20f0('0x2d')](_0x423c78[_0x20f0('0x2e')],_0x423c78[_0x20f0('0x2b')]);_0x12908d['attributes']=_[_0x20f0('0x2d')](_0x423c78[_0x20f0('0x2e')],qs[_0x20f0('0x2f')](_0x2dce51[_0x20f0('0x2b')][_0x20f0('0x2f')]));_0x12908d['attributes']=_0x12908d[_0x20f0('0x30')]['length']?_0x12908d[_0x20f0('0x30')]:_0x423c78[_0x20f0('0x2e')];if(_0x2dce51[_0x20f0('0x2b')][_0x20f0('0x3d')]){_0x12908d['include']=[{'all':!![]}];}_0x12908d=_[_0x20f0('0x3a')]({},_0x12908d,_0x2dce51[_0x20f0('0x3b')]);return db[_0x20f0('0x3c')][_0x20f0('0x45')](_0x12908d)['then'](handleEntityNotFound(_0x1b9a77,null))[_0x20f0('0x25')](respondWithResult(_0x1b9a77,null))[_0x20f0('0x41')](handleError(_0x1b9a77,null));};exports[_0x20f0('0x46')]=function(_0x3a1596,_0x217552){return db[_0x20f0('0x3c')][_0x20f0('0x46')](_0x3a1596[_0x20f0('0x47')],{})['then'](respondWithResult(_0x217552,0xc9))[_0x20f0('0x41')](handleError(_0x217552,null));};exports[_0x20f0('0x48')]=function(_0x117b70,_0x3fcfb4){if(_0x117b70[_0x20f0('0x47')]['id']){delete _0x117b70[_0x20f0('0x47')]['id'];}return db[_0x20f0('0x3c')][_0x20f0('0x45')]({'where':{'id':_0x117b70[_0x20f0('0x43')]['id']}})['then'](handleEntityNotFound(_0x3fcfb4,null))['then'](saveUpdates(_0x117b70['body'],null))['then'](respondWithResult(_0x3fcfb4,null))[_0x20f0('0x41')](handleError(_0x3fcfb4,null));};exports['destroy']=function(_0x589416,_0x3f63f9){return db[_0x20f0('0x3c')]['find']({'where':{'id':_0x589416['params']['id']}})[_0x20f0('0x25')](handleEntityNotFound(_0x3f63f9,null))[_0x20f0('0x25')](removeEntity(_0x3f63f9,null))[_0x20f0('0x41')](handleError(_0x3f63f9,null));};exports[_0x20f0('0x49')]=function(_0x529902,_0x4b30dc,_0x5862de){var _0x4de8a5={};var _0x1b7dc2={};var _0x82ebc2;var _0x1b30d2;return db['ZohoConfiguration'][_0x20f0('0x4a')]({'where':{'id':_0x529902['params']['id']}})['then'](handleEntityNotFound(_0x4b30dc,null))[_0x20f0('0x25')](function(_0xcc6788){if(_0xcc6788){_0x82ebc2=_0xcc6788;_0x1b7dc2['model']=_[_0x20f0('0x44')](db['ZohoField'][_0x20f0('0x4b')]);_0x1b7dc2[_0x20f0('0x2b')]=_[_0x20f0('0x44')](_0x529902['query']);_0x1b7dc2[_0x20f0('0x2c')]=_[_0x20f0('0x2d')](_0x1b7dc2[_0x20f0('0x2e')],_0x1b7dc2['query']);_0x4de8a5[_0x20f0('0x30')]=_[_0x20f0('0x2d')](_0x1b7dc2[_0x20f0('0x2e')],qs['fields'](_0x529902[_0x20f0('0x2b')]['fields']));_0x4de8a5['attributes']=_0x4de8a5[_0x20f0('0x30')][_0x20f0('0x31')]?_0x4de8a5[_0x20f0('0x30')]:_0x1b7dc2[_0x20f0('0x2e')];_0x4de8a5[_0x20f0('0x34')]=qs[_0x20f0('0x35')](_0x529902[_0x20f0('0x2b')]['sort']);_0x4de8a5[_0x20f0('0x36')]=qs['filters'](_[_0x20f0('0x37')](_0x529902[_0x20f0('0x2b')],_0x1b7dc2[_0x20f0('0x2c')]));if(_0x529902[_0x20f0('0x2b')][_0x20f0('0x38')]){_0x4de8a5['where']=_[_0x20f0('0x3a')](_0x4de8a5[_0x20f0('0x36')],{'$or':_[_0x20f0('0x2a')](_0x4de8a5['attributes'],function(_0x47d084){var _0x4a3551={};_0x4a3551[_0x47d084]={'$like':'%'+_0x529902[_0x20f0('0x2b')][_0x20f0('0x38')]+'%'};return _0x4a3551;})});}_0x4de8a5=_[_0x20f0('0x3a')]({},_0x4de8a5,_0x529902['options']);return _0x82ebc2[_0x20f0('0x49')](_0x4de8a5);}})[_0x20f0('0x25')](function(_0x97a926){if(_0x97a926){_0x1b30d2=_0x97a926['length'];if(!_0x529902[_0x20f0('0x2b')][_0x20f0('0x32')](_0x20f0('0x33'))){_0x4de8a5[_0x20f0('0x20')]=qs['limit'](_0x529902[_0x20f0('0x2b')][_0x20f0('0x20')]);_0x4de8a5['offset']=qs[_0x20f0('0x1e')](_0x529902['query']['offset']);}return _0x82ebc2['getFields'](_0x4de8a5);}})[_0x20f0('0x25')](function(_0x2a3e7a){if(_0x2a3e7a){return _0x2a3e7a?{'count':_0x1b30d2,'rows':_0x2a3e7a}:null;}})[_0x20f0('0x25')](respondWithResult(_0x4b30dc,null))[_0x20f0('0x41')](handleError(_0x4b30dc,null));};exports[_0x20f0('0x4c')]=function(_0x35ec5a,_0x3eed3,_0x1ddf20){var _0x592eb9={};var _0x5c7fa6={};var _0x246bc6;var _0x31f754;return db[_0x20f0('0x3c')][_0x20f0('0x4a')]({'where':{'id':_0x35ec5a['params']['id']}})['then'](handleEntityNotFound(_0x3eed3,null))[_0x20f0('0x25')](function(_0x590cf2){if(_0x590cf2){_0x246bc6=_0x590cf2;_0x5c7fa6[_0x20f0('0x2e')]=_[_0x20f0('0x44')](db[_0x20f0('0x4d')][_0x20f0('0x4b')]);_0x5c7fa6[_0x20f0('0x2b')]=_[_0x20f0('0x44')](_0x35ec5a[_0x20f0('0x2b')]);_0x5c7fa6[_0x20f0('0x2c')]=_[_0x20f0('0x2d')](_0x5c7fa6['model'],_0x5c7fa6[_0x20f0('0x2b')]);_0x592eb9[_0x20f0('0x30')]=_[_0x20f0('0x2d')](_0x5c7fa6[_0x20f0('0x2e')],qs[_0x20f0('0x2f')](_0x35ec5a[_0x20f0('0x2b')][_0x20f0('0x2f')]));_0x592eb9[_0x20f0('0x30')]=_0x592eb9[_0x20f0('0x30')][_0x20f0('0x31')]?_0x592eb9[_0x20f0('0x30')]:_0x5c7fa6[_0x20f0('0x2e')];_0x592eb9['order']=qs['sort'](_0x35ec5a['query'][_0x20f0('0x35')]);_0x592eb9[_0x20f0('0x36')]=qs[_0x20f0('0x2c')](_[_0x20f0('0x37')](_0x35ec5a[_0x20f0('0x2b')],_0x5c7fa6[_0x20f0('0x2c')]));if(_0x35ec5a['query'][_0x20f0('0x38')]){_0x592eb9[_0x20f0('0x36')]=_[_0x20f0('0x3a')](_0x592eb9[_0x20f0('0x36')],{'$or':_['map'](_0x592eb9['attributes'],function(_0x33b2ac){var _0x4f33d4={};_0x4f33d4[_0x33b2ac]={'$like':'%'+_0x35ec5a[_0x20f0('0x2b')]['filter']+'%'};return _0x4f33d4;})});}_0x592eb9=_[_0x20f0('0x3a')]({},_0x592eb9,_0x35ec5a['options']);return _0x246bc6[_0x20f0('0x4c')](_0x592eb9);}})[_0x20f0('0x25')](function(_0x2a0f0d){if(_0x2a0f0d){_0x31f754=_0x2a0f0d[_0x20f0('0x31')];if(!_0x35ec5a[_0x20f0('0x2b')]['hasOwnProperty'](_0x20f0('0x33'))){_0x592eb9[_0x20f0('0x20')]=qs[_0x20f0('0x20')](_0x35ec5a[_0x20f0('0x2b')][_0x20f0('0x20')]);_0x592eb9[_0x20f0('0x1e')]=qs[_0x20f0('0x1e')](_0x35ec5a[_0x20f0('0x2b')][_0x20f0('0x1e')]);}return _0x246bc6[_0x20f0('0x4c')](_0x592eb9);}})['then'](function(_0x54e76e){if(_0x54e76e){return _0x54e76e?{'count':_0x31f754,'rows':_0x54e76e}:null;}})[_0x20f0('0x25')](respondWithResult(_0x3eed3,null))['catch'](handleError(_0x3eed3,null));};exports[_0x20f0('0x4e')]=function(_0x1ab085,_0x30c6bd,_0x16308f){var _0x227017={};var _0x5bb855={};var _0x3b793d;var _0x3360d4;return db['ZohoConfiguration'][_0x20f0('0x4a')]({'where':{'id':_0x1ab085[_0x20f0('0x43')]['id']}})['then'](handleEntityNotFound(_0x30c6bd,null))[_0x20f0('0x25')](function(_0x5493b5){if(_0x5493b5){_0x3b793d=_0x5493b5;_0x5bb855['model']=_[_0x20f0('0x44')](db[_0x20f0('0x4d')][_0x20f0('0x4b')]);_0x5bb855['query']=_['keys'](_0x1ab085['query']);_0x5bb855['filters']=_[_0x20f0('0x2d')](_0x5bb855['model'],_0x5bb855[_0x20f0('0x2b')]);_0x227017[_0x20f0('0x30')]=_[_0x20f0('0x2d')](_0x5bb855['model'],qs[_0x20f0('0x2f')](_0x1ab085['query'][_0x20f0('0x2f')]));_0x227017[_0x20f0('0x30')]=_0x227017[_0x20f0('0x30')]['length']?_0x227017['attributes']:_0x5bb855['model'];_0x227017['order']=qs['sort'](_0x1ab085[_0x20f0('0x2b')]['sort']);_0x227017[_0x20f0('0x36')]=qs[_0x20f0('0x2c')](_[_0x20f0('0x37')](_0x1ab085['query'],_0x5bb855[_0x20f0('0x2c')]));if(_0x1ab085[_0x20f0('0x2b')][_0x20f0('0x38')]){_0x227017[_0x20f0('0x36')]=_[_0x20f0('0x3a')](_0x227017[_0x20f0('0x36')],{'$or':_['map'](_0x227017[_0x20f0('0x30')],function(_0x496e54){var _0x2e147d={};_0x2e147d[_0x496e54]={'$like':'%'+_0x1ab085[_0x20f0('0x2b')][_0x20f0('0x38')]+'%'};return _0x2e147d;})});}_0x227017=_[_0x20f0('0x3a')]({},_0x227017,_0x1ab085[_0x20f0('0x3b')]);return _0x3b793d[_0x20f0('0x4e')](_0x227017);}})[_0x20f0('0x25')](function(_0x30d07c){if(_0x30d07c){_0x3360d4=_0x30d07c[_0x20f0('0x31')];if(!_0x1ab085[_0x20f0('0x2b')][_0x20f0('0x32')]('nolimit')){_0x227017[_0x20f0('0x20')]=qs[_0x20f0('0x20')](_0x1ab085[_0x20f0('0x2b')][_0x20f0('0x20')]);_0x227017[_0x20f0('0x1e')]=qs['offset'](_0x1ab085['query'][_0x20f0('0x1e')]);}return _0x3b793d[_0x20f0('0x4e')](_0x227017);}})['then'](function(_0x25a55f){if(_0x25a55f){return _0x25a55f?{'count':_0x3360d4,'rows':_0x25a55f}:null;}})[_0x20f0('0x25')](respondWithResult(_0x30c6bd,null))[_0x20f0('0x41')](handleError(_0x30c6bd,null));};