0abc920e19f96ad77bec6768384b129802335529
[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 _0xfc72=['query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','findAll','rows','catch','show','params','includeAll','include','find','create','body','destroy','getFields','ZohoField','getSubjects','findOne','options','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','moment','mustache','util','path','sox','fs-extra','lodash','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','error','stack','name','send','index','ZohoConfiguration','rawAttributes','fieldName','type','key','model','map'];(function(_0x13711f,_0x51006b){var _0x58a2c9=function(_0x238487){while(--_0x238487){_0x13711f['push'](_0x13711f['shift']());}};_0x58a2c9(++_0x51006b);}(_0xfc72,0x16c));var _0x2fc7=function(_0x437abe,_0x3e33a8){_0x437abe=_0x437abe-0x0;var _0x51189b=_0xfc72[_0x437abe];return _0x51189b;};'use strict';var emlformat=require(_0x2fc7('0x0'));var rimraf=require(_0x2fc7('0x1'));var zipdir=require(_0x2fc7('0x2'));var jsonpatch=require(_0x2fc7('0x3'));var rp=require('request-promise');var moment=require(_0x2fc7('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x2fc7('0x5'));var util=require(_0x2fc7('0x6'));var path=require(_0x2fc7('0x7'));var sox=require(_0x2fc7('0x8'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x2fc7('0x9'));var _=require(_0x2fc7('0xa'));var squel=require('squel');var crypto=require(_0x2fc7('0xb'));var jsforce=require(_0x2fc7('0xc'));var deskjs=require(_0x2fc7('0xd'));var toCsv=require(_0x2fc7('0xe'));var querystring=require(_0x2fc7('0xf'));var Papa=require(_0x2fc7('0x10'));var Redis=require(_0x2fc7('0x11'));var authService=require('../../components/auth/service');var qs=require(_0x2fc7('0x12'));var as=require(_0x2fc7('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x2fc7('0x14'))('api');var utils=require(_0x2fc7('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x2fc7('0x16'));var db=require(_0x2fc7('0x17'))['db'];function respondWithStatusCode(_0x243a65,_0x120fba){_0x120fba=_0x120fba||0xcc;return function(_0x2f87e4){if(_0x2f87e4){return _0x243a65[_0x2fc7('0x18')](_0x120fba);}return _0x243a65[_0x2fc7('0x19')](_0x120fba)[_0x2fc7('0x1a')]();};}function respondWithResult(_0x148851,_0x35a628){_0x35a628=_0x35a628||0xc8;return function(_0x2c6fe7){if(_0x2c6fe7){return _0x148851['status'](_0x35a628)[_0x2fc7('0x1b')](_0x2c6fe7);}};}function respondWithFilteredResult(_0x321586,_0x51442a){return function(_0xf2578){if(_0xf2578){var _0x5ce77c=typeof _0x51442a[_0x2fc7('0x1c')]===_0x2fc7('0x1d')&&typeof _0x51442a[_0x2fc7('0x1e')]==='undefined';var _0x1edba6=_0xf2578[_0x2fc7('0x1f')];var _0x284486=_0x5ce77c?0x0:_0x51442a['offset'];var _0x310a2c=_0x5ce77c?_0xf2578['count']:_0x51442a['offset']+_0x51442a['limit'];var _0x3b2359;if(_0x310a2c>=_0x1edba6){_0x310a2c=_0x1edba6;_0x3b2359=0xc8;}else{_0x3b2359=0xce;}_0x321586[_0x2fc7('0x19')](_0x3b2359);return _0x321586[_0x2fc7('0x20')](_0x2fc7('0x21'),_0x284486+'-'+_0x310a2c+'/'+_0x1edba6)[_0x2fc7('0x1b')](_0xf2578);}return null;};}function patchUpdates(_0x26e0bc){return function(_0x4632be){try{jsonpatch[_0x2fc7('0x22')](_0x4632be,_0x26e0bc,!![]);}catch(_0x4572dc){return BPromise[_0x2fc7('0x23')](_0x4572dc);}return _0x4632be[_0x2fc7('0x24')]();};}function saveUpdates(_0x361e31,_0x4dfb4a){return function(_0x263047){if(_0x263047){return _0x263047[_0x2fc7('0x25')](_0x361e31)[_0x2fc7('0x26')](function(_0x45fc06){return _0x45fc06;});}return null;};}function removeEntity(_0x3f003d,_0x32f7ac){return function(_0x3ac8d1){if(_0x3ac8d1){return _0x3ac8d1['destroy']()[_0x2fc7('0x26')](function(){_0x3f003d[_0x2fc7('0x19')](0xcc)[_0x2fc7('0x1a')]();});}};}function handleEntityNotFound(_0x48d59e,_0x4dacd2){return function(_0x25e98e){if(!_0x25e98e){_0x48d59e[_0x2fc7('0x18')](0x194);}return _0x25e98e;};}function handleError(_0x3840fd,_0x1c9d5f){_0x1c9d5f=_0x1c9d5f||0x1f4;return function(_0x594b96){logger[_0x2fc7('0x27')](_0x594b96[_0x2fc7('0x28')]);if(_0x594b96['name']){delete _0x594b96[_0x2fc7('0x29')];}_0x3840fd['status'](_0x1c9d5f)[_0x2fc7('0x2a')](_0x594b96);};}exports[_0x2fc7('0x2b')]=function(_0x10cc1e,_0x5e3664){var _0x3e422a={},_0x46ea5c={},_0x436d2c={'count':0x0,'rows':[]};var _0x1872ce=_['map'](db[_0x2fc7('0x2c')][_0x2fc7('0x2d')],function(_0xd3f5d2){return{'name':_0xd3f5d2[_0x2fc7('0x2e')],'type':_0xd3f5d2[_0x2fc7('0x2f')][_0x2fc7('0x30')]};});_0x46ea5c[_0x2fc7('0x31')]=_[_0x2fc7('0x32')](_0x1872ce,_0x2fc7('0x29'));_0x46ea5c[_0x2fc7('0x33')]=_[_0x2fc7('0x34')](_0x10cc1e['query']);_0x46ea5c[_0x2fc7('0x35')]=_[_0x2fc7('0x36')](_0x46ea5c[_0x2fc7('0x31')],_0x46ea5c[_0x2fc7('0x33')]);_0x3e422a[_0x2fc7('0x37')]=_[_0x2fc7('0x36')](_0x46ea5c[_0x2fc7('0x31')],qs[_0x2fc7('0x38')](_0x10cc1e['query']['fields']));_0x3e422a[_0x2fc7('0x37')]=_0x3e422a['attributes'][_0x2fc7('0x39')]?_0x3e422a[_0x2fc7('0x37')]:_0x46ea5c['model'];if(!_0x10cc1e[_0x2fc7('0x33')][_0x2fc7('0x3a')](_0x2fc7('0x3b'))){_0x3e422a[_0x2fc7('0x1e')]=qs['limit'](_0x10cc1e[_0x2fc7('0x33')][_0x2fc7('0x1e')]);_0x3e422a[_0x2fc7('0x1c')]=qs[_0x2fc7('0x1c')](_0x10cc1e[_0x2fc7('0x33')][_0x2fc7('0x1c')]);}_0x3e422a[_0x2fc7('0x3c')]=qs['sort'](_0x10cc1e[_0x2fc7('0x33')][_0x2fc7('0x3d')]);_0x3e422a[_0x2fc7('0x3e')]=qs[_0x2fc7('0x35')](_[_0x2fc7('0x3f')](_0x10cc1e[_0x2fc7('0x33')],_0x46ea5c['filters']),_0x1872ce);if(_0x10cc1e[_0x2fc7('0x33')][_0x2fc7('0x40')]){_0x3e422a[_0x2fc7('0x3e')]=_[_0x2fc7('0x41')](_0x3e422a[_0x2fc7('0x3e')],{'$or':_[_0x2fc7('0x32')](_0x1872ce,function(_0x405239){if(_0x405239['type']!=='VIRTUAL'){var _0x282981={};_0x282981[_0x405239['name']]={'$like':'%'+_0x10cc1e[_0x2fc7('0x33')]['filter']+'%'};return _0x282981;}})});}_0x3e422a=_[_0x2fc7('0x41')]({},_0x3e422a,_0x10cc1e['options']);var _0x56d5b5={'where':_0x3e422a['where']};return db[_0x2fc7('0x2c')][_0x2fc7('0x1f')](_0x56d5b5)[_0x2fc7('0x26')](function(_0x449eea){_0x436d2c[_0x2fc7('0x1f')]=_0x449eea;if(_0x10cc1e[_0x2fc7('0x33')]['includeAll']){_0x3e422a['include']=[{'all':!![]}];}return db[_0x2fc7('0x2c')][_0x2fc7('0x42')](_0x3e422a);})[_0x2fc7('0x26')](function(_0x116324){_0x436d2c[_0x2fc7('0x43')]=_0x116324;return _0x436d2c;})[_0x2fc7('0x26')](respondWithFilteredResult(_0x5e3664,_0x3e422a))[_0x2fc7('0x44')](handleError(_0x5e3664,null));};exports[_0x2fc7('0x45')]=function(_0x15ac17,_0x14f974){var _0x111c73={'raw':![],'where':{'id':_0x15ac17[_0x2fc7('0x46')]['id']}},_0x544487={};_0x544487[_0x2fc7('0x31')]=_[_0x2fc7('0x34')](db['ZohoConfiguration'][_0x2fc7('0x2d')]);_0x544487[_0x2fc7('0x33')]=_['keys'](_0x15ac17[_0x2fc7('0x33')]);_0x544487['filters']=_[_0x2fc7('0x36')](_0x544487['model'],_0x544487['query']);_0x111c73[_0x2fc7('0x37')]=_['intersection'](_0x544487[_0x2fc7('0x31')],qs['fields'](_0x15ac17[_0x2fc7('0x33')][_0x2fc7('0x38')]));_0x111c73[_0x2fc7('0x37')]=_0x111c73[_0x2fc7('0x37')][_0x2fc7('0x39')]?_0x111c73[_0x2fc7('0x37')]:_0x544487['model'];if(_0x15ac17[_0x2fc7('0x33')][_0x2fc7('0x47')]){_0x111c73[_0x2fc7('0x48')]=[{'all':!![]}];}_0x111c73=_[_0x2fc7('0x41')]({},_0x111c73,_0x15ac17['options']);return db[_0x2fc7('0x2c')][_0x2fc7('0x49')](_0x111c73)['then'](handleEntityNotFound(_0x14f974,null))['then'](respondWithResult(_0x14f974,null))[_0x2fc7('0x44')](handleError(_0x14f974,null));};exports[_0x2fc7('0x4a')]=function(_0x5374ec,_0x5c1a42){return db[_0x2fc7('0x2c')]['create'](_0x5374ec[_0x2fc7('0x4b')],{})[_0x2fc7('0x26')](respondWithResult(_0x5c1a42,0xc9))[_0x2fc7('0x44')](handleError(_0x5c1a42,null));};exports[_0x2fc7('0x25')]=function(_0x9a6701,_0x30277a){if(_0x9a6701['body']['id']){delete _0x9a6701['body']['id'];}return db[_0x2fc7('0x2c')]['find']({'where':{'id':_0x9a6701['params']['id']}})[_0x2fc7('0x26')](handleEntityNotFound(_0x30277a,null))[_0x2fc7('0x26')](saveUpdates(_0x9a6701['body'],null))[_0x2fc7('0x26')](respondWithResult(_0x30277a,null))[_0x2fc7('0x44')](handleError(_0x30277a,null));};exports[_0x2fc7('0x4c')]=function(_0x3f5a03,_0xec418){return db[_0x2fc7('0x2c')][_0x2fc7('0x49')]({'where':{'id':_0x3f5a03[_0x2fc7('0x46')]['id']}})[_0x2fc7('0x26')](handleEntityNotFound(_0xec418,null))[_0x2fc7('0x26')](removeEntity(_0xec418,null))[_0x2fc7('0x44')](handleError(_0xec418,null));};exports[_0x2fc7('0x4d')]=function(_0x32fc27,_0x1734b8,_0x400ca3){var _0x201251={};var _0x2a1c18={};var _0x5a7fcb;var _0x3ee7a6;return db[_0x2fc7('0x2c')]['findOne']({'where':{'id':_0x32fc27['params']['id']}})[_0x2fc7('0x26')](handleEntityNotFound(_0x1734b8,null))['then'](function(_0x220fe2){if(_0x220fe2){_0x5a7fcb=_0x220fe2;_0x2a1c18['model']=_[_0x2fc7('0x34')](db[_0x2fc7('0x4e')]['rawAttributes']);_0x2a1c18[_0x2fc7('0x33')]=_['keys'](_0x32fc27[_0x2fc7('0x33')]);_0x2a1c18[_0x2fc7('0x35')]=_[_0x2fc7('0x36')](_0x2a1c18[_0x2fc7('0x31')],_0x2a1c18[_0x2fc7('0x33')]);_0x201251[_0x2fc7('0x37')]=_[_0x2fc7('0x36')](_0x2a1c18[_0x2fc7('0x31')],qs[_0x2fc7('0x38')](_0x32fc27[_0x2fc7('0x33')][_0x2fc7('0x38')]));_0x201251[_0x2fc7('0x37')]=_0x201251[_0x2fc7('0x37')][_0x2fc7('0x39')]?_0x201251[_0x2fc7('0x37')]:_0x2a1c18[_0x2fc7('0x31')];_0x201251[_0x2fc7('0x3c')]=qs[_0x2fc7('0x3d')](_0x32fc27[_0x2fc7('0x33')][_0x2fc7('0x3d')]);_0x201251['where']=qs[_0x2fc7('0x35')](_[_0x2fc7('0x3f')](_0x32fc27[_0x2fc7('0x33')],_0x2a1c18[_0x2fc7('0x35')]));if(_0x32fc27[_0x2fc7('0x33')][_0x2fc7('0x40')]){_0x201251[_0x2fc7('0x3e')]=_[_0x2fc7('0x41')](_0x201251[_0x2fc7('0x3e')],{'$or':_[_0x2fc7('0x32')](_0x201251[_0x2fc7('0x37')],function(_0x2f7084){var _0x969c2a={};_0x969c2a[_0x2f7084]={'$like':'%'+_0x32fc27[_0x2fc7('0x33')]['filter']+'%'};return _0x969c2a;})});}_0x201251=_[_0x2fc7('0x41')]({},_0x201251,_0x32fc27['options']);return _0x5a7fcb[_0x2fc7('0x4d')](_0x201251);}})[_0x2fc7('0x26')](function(_0x4744a6){if(_0x4744a6){_0x3ee7a6=_0x4744a6['length'];if(!_0x32fc27[_0x2fc7('0x33')]['hasOwnProperty']('nolimit')){_0x201251[_0x2fc7('0x1e')]=qs[_0x2fc7('0x1e')](_0x32fc27['query'][_0x2fc7('0x1e')]);_0x201251[_0x2fc7('0x1c')]=qs[_0x2fc7('0x1c')](_0x32fc27['query'][_0x2fc7('0x1c')]);}return _0x5a7fcb['getFields'](_0x201251);}})['then'](function(_0x6ca354){if(_0x6ca354){return _0x6ca354?{'count':_0x3ee7a6,'rows':_0x6ca354}:null;}})['then'](respondWithResult(_0x1734b8,null))[_0x2fc7('0x44')](handleError(_0x1734b8,null));};exports[_0x2fc7('0x4f')]=function(_0x5b7fc1,_0xc1ffc7,_0x475948){var _0x28659b={};var _0x33319f={};var _0x5bb6de;var _0xe7d9e4;return db[_0x2fc7('0x2c')][_0x2fc7('0x50')]({'where':{'id':_0x5b7fc1['params']['id']}})[_0x2fc7('0x26')](handleEntityNotFound(_0xc1ffc7,null))[_0x2fc7('0x26')](function(_0x40d63a){if(_0x40d63a){_0x5bb6de=_0x40d63a;_0x33319f[_0x2fc7('0x31')]=_[_0x2fc7('0x34')](db['ZohoField'][_0x2fc7('0x2d')]);_0x33319f['query']=_[_0x2fc7('0x34')](_0x5b7fc1['query']);_0x33319f[_0x2fc7('0x35')]=_[_0x2fc7('0x36')](_0x33319f[_0x2fc7('0x31')],_0x33319f[_0x2fc7('0x33')]);_0x28659b[_0x2fc7('0x37')]=_[_0x2fc7('0x36')](_0x33319f['model'],qs[_0x2fc7('0x38')](_0x5b7fc1[_0x2fc7('0x33')][_0x2fc7('0x38')]));_0x28659b[_0x2fc7('0x37')]=_0x28659b[_0x2fc7('0x37')][_0x2fc7('0x39')]?_0x28659b[_0x2fc7('0x37')]:_0x33319f[_0x2fc7('0x31')];_0x28659b['order']=qs[_0x2fc7('0x3d')](_0x5b7fc1[_0x2fc7('0x33')][_0x2fc7('0x3d')]);_0x28659b['where']=qs['filters'](_[_0x2fc7('0x3f')](_0x5b7fc1[_0x2fc7('0x33')],_0x33319f[_0x2fc7('0x35')]));if(_0x5b7fc1['query'][_0x2fc7('0x40')]){_0x28659b[_0x2fc7('0x3e')]=_['merge'](_0x28659b['where'],{'$or':_[_0x2fc7('0x32')](_0x28659b[_0x2fc7('0x37')],function(_0x237b6c){var _0x397eb9={};_0x397eb9[_0x237b6c]={'$like':'%'+_0x5b7fc1['query'][_0x2fc7('0x40')]+'%'};return _0x397eb9;})});}_0x28659b=_[_0x2fc7('0x41')]({},_0x28659b,_0x5b7fc1[_0x2fc7('0x51')]);return _0x5bb6de[_0x2fc7('0x4f')](_0x28659b);}})['then'](function(_0x1a0cc3){if(_0x1a0cc3){_0xe7d9e4=_0x1a0cc3['length'];if(!_0x5b7fc1['query'][_0x2fc7('0x3a')](_0x2fc7('0x3b'))){_0x28659b[_0x2fc7('0x1e')]=qs[_0x2fc7('0x1e')](_0x5b7fc1[_0x2fc7('0x33')]['limit']);_0x28659b['offset']=qs[_0x2fc7('0x1c')](_0x5b7fc1[_0x2fc7('0x33')][_0x2fc7('0x1c')]);}return _0x5bb6de['getSubjects'](_0x28659b);}})[_0x2fc7('0x26')](function(_0x44e4f5){if(_0x44e4f5){return _0x44e4f5?{'count':_0xe7d9e4,'rows':_0x44e4f5}:null;}})['then'](respondWithResult(_0xc1ffc7,null))['catch'](handleError(_0xc1ffc7,null));};exports[_0x2fc7('0x52')]=function(_0xa00dfc,_0x3f237a,_0x530c82){var _0x1e7b5b={};var _0x45ac4b={};var _0x54851a;var _0x560340;return db[_0x2fc7('0x2c')]['findOne']({'where':{'id':_0xa00dfc[_0x2fc7('0x46')]['id']}})[_0x2fc7('0x26')](handleEntityNotFound(_0x3f237a,null))[_0x2fc7('0x26')](function(_0x1ec146){if(_0x1ec146){_0x54851a=_0x1ec146;_0x45ac4b['model']=_[_0x2fc7('0x34')](db[_0x2fc7('0x4e')]['rawAttributes']);_0x45ac4b[_0x2fc7('0x33')]=_[_0x2fc7('0x34')](_0xa00dfc[_0x2fc7('0x33')]);_0x45ac4b[_0x2fc7('0x35')]=_['intersection'](_0x45ac4b['model'],_0x45ac4b[_0x2fc7('0x33')]);_0x1e7b5b[_0x2fc7('0x37')]=_[_0x2fc7('0x36')](_0x45ac4b['model'],qs[_0x2fc7('0x38')](_0xa00dfc[_0x2fc7('0x33')]['fields']));_0x1e7b5b[_0x2fc7('0x37')]=_0x1e7b5b[_0x2fc7('0x37')]['length']?_0x1e7b5b[_0x2fc7('0x37')]:_0x45ac4b['model'];_0x1e7b5b[_0x2fc7('0x3c')]=qs['sort'](_0xa00dfc[_0x2fc7('0x33')][_0x2fc7('0x3d')]);_0x1e7b5b['where']=qs[_0x2fc7('0x35')](_[_0x2fc7('0x3f')](_0xa00dfc[_0x2fc7('0x33')],_0x45ac4b[_0x2fc7('0x35')]));if(_0xa00dfc[_0x2fc7('0x33')]['filter']){_0x1e7b5b[_0x2fc7('0x3e')]=_[_0x2fc7('0x41')](_0x1e7b5b[_0x2fc7('0x3e')],{'$or':_['map'](_0x1e7b5b[_0x2fc7('0x37')],function(_0x1b7212){var _0x1e08ae={};_0x1e08ae[_0x1b7212]={'$like':'%'+_0xa00dfc[_0x2fc7('0x33')][_0x2fc7('0x40')]+'%'};return _0x1e08ae;})});}_0x1e7b5b=_[_0x2fc7('0x41')]({},_0x1e7b5b,_0xa00dfc[_0x2fc7('0x51')]);return _0x54851a[_0x2fc7('0x52')](_0x1e7b5b);}})[_0x2fc7('0x26')](function(_0x3b0527){if(_0x3b0527){_0x560340=_0x3b0527['length'];if(!_0xa00dfc[_0x2fc7('0x33')][_0x2fc7('0x3a')](_0x2fc7('0x3b'))){_0x1e7b5b[_0x2fc7('0x1e')]=qs[_0x2fc7('0x1e')](_0xa00dfc[_0x2fc7('0x33')][_0x2fc7('0x1e')]);_0x1e7b5b[_0x2fc7('0x1c')]=qs['offset'](_0xa00dfc[_0x2fc7('0x33')][_0x2fc7('0x1c')]);}return _0x54851a[_0x2fc7('0x52')](_0x1e7b5b);}})[_0x2fc7('0x26')](function(_0x21af5a){if(_0x21af5a){return _0x21af5a?{'count':_0x560340,'rows':_0x21af5a}:null;}})[_0x2fc7('0x26')](respondWithResult(_0x3f237a,null))['catch'](handleError(_0x3f237a,null));};