21c428d2349c2f3f0af23d4e11e0c6dd4ac82047
[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 _0x3f19=['path','to-csv','ejs','fs-extra','lodash','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','apply','reject','save','update','destroy','error','stack','name','send','index','map','ZohoConfiguration','fieldName','type','query','filters','intersection','model','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','VIRTUAL','filter','merge','options','then','include','findAll','rows','catch','show','params','rawAttributes','keys','length','includeAll','find','create','body','getFields','findOne','ZohoField','getSubjects','getDescriptions','eml-format','zip-dir','fast-json-patch','bluebird'];(function(_0x56a494,_0x2117fc){var _0x51fab2=function(_0x2c3c8f){while(--_0x2c3c8f){_0x56a494['push'](_0x56a494['shift']());}};_0x51fab2(++_0x2117fc);}(_0x3f19,0x182));var _0x93f1=function(_0x4aca28,_0x4ab42c){_0x4aca28=_0x4aca28-0x0;var _0x1946a0=_0x3f19[_0x4aca28];return _0x1946a0;};'use strict';var emlformat=require(_0x93f1('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x93f1('0x1'));var jsonpatch=require(_0x93f1('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x93f1('0x3'));var Mustache=require('mustache');var util=require('util');var path=require(_0x93f1('0x4'));var sox=require('sox');var csv=require(_0x93f1('0x5'));var ejs=require(_0x93f1('0x6'));var fs=require('fs');var fs_extra=require(_0x93f1('0x7'));var _=require(_0x93f1('0x8'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x93f1('0x9'));var deskjs=require(_0x93f1('0xa'));var toCsv=require(_0x93f1('0x5'));var querystring=require(_0x93f1('0xb'));var Papa=require(_0x93f1('0xc'));var Redis=require('ioredis');var authService=require(_0x93f1('0xd'));var qs=require(_0x93f1('0xe'));var as=require(_0x93f1('0xf'));var hardwareService=require(_0x93f1('0x10'));var logger=require(_0x93f1('0x11'))(_0x93f1('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x93f1('0x13'));var db=require(_0x93f1('0x14'))['db'];function respondWithStatusCode(_0xd80cfe,_0x559668){_0x559668=_0x559668||0xcc;return function(_0x29b60b){if(_0x29b60b){return _0xd80cfe['sendStatus'](_0x559668);}return _0xd80cfe[_0x93f1('0x15')](_0x559668)[_0x93f1('0x16')]();};}function respondWithResult(_0x3a00f0,_0x2f4bda){_0x2f4bda=_0x2f4bda||0xc8;return function(_0x9e4764){if(_0x9e4764){return _0x3a00f0[_0x93f1('0x15')](_0x2f4bda)[_0x93f1('0x17')](_0x9e4764);}};}function respondWithFilteredResult(_0x4e232e,_0x32de22){return function(_0x10e2f4){if(_0x10e2f4){var _0x5e82d8=typeof _0x32de22[_0x93f1('0x18')]===_0x93f1('0x19')&&typeof _0x32de22[_0x93f1('0x1a')]===_0x93f1('0x19');var _0x1c8450=_0x10e2f4[_0x93f1('0x1b')];var _0x5d1b77=_0x5e82d8?0x0:_0x32de22[_0x93f1('0x18')];var _0x1ed11e=_0x5e82d8?_0x10e2f4[_0x93f1('0x1b')]:_0x32de22['offset']+_0x32de22[_0x93f1('0x1a')];var _0x362a28;if(_0x1ed11e>=_0x1c8450){_0x1ed11e=_0x1c8450;_0x362a28=0xc8;}else{_0x362a28=0xce;}_0x4e232e[_0x93f1('0x15')](_0x362a28);return _0x4e232e[_0x93f1('0x1c')]('Content-Range',_0x5d1b77+'-'+_0x1ed11e+'/'+_0x1c8450)[_0x93f1('0x17')](_0x10e2f4);}return null;};}function patchUpdates(_0x4f7edc){return function(_0x4934a3){try{jsonpatch[_0x93f1('0x1d')](_0x4934a3,_0x4f7edc,!![]);}catch(_0xc99f30){return BPromise[_0x93f1('0x1e')](_0xc99f30);}return _0x4934a3[_0x93f1('0x1f')]();};}function saveUpdates(_0x1877d5,_0x4ad68a){return function(_0x19a650){if(_0x19a650){return _0x19a650[_0x93f1('0x20')](_0x1877d5)['then'](function(_0x9367e8){return _0x9367e8;});}return null;};}function removeEntity(_0xb07f18,_0x5329f9){return function(_0x1aadb7){if(_0x1aadb7){return _0x1aadb7[_0x93f1('0x21')]()['then'](function(){_0xb07f18[_0x93f1('0x15')](0xcc)['end']();});}};}function handleEntityNotFound(_0x47ebb8,_0xd8de6f){return function(_0x4f3d01){if(!_0x4f3d01){_0x47ebb8['sendStatus'](0x194);}return _0x4f3d01;};}function handleError(_0x248a83,_0x97f532){_0x97f532=_0x97f532||0x1f4;return function(_0x37cdf0){logger[_0x93f1('0x22')](_0x37cdf0[_0x93f1('0x23')]);if(_0x37cdf0['name']){delete _0x37cdf0[_0x93f1('0x24')];}_0x248a83[_0x93f1('0x15')](_0x97f532)[_0x93f1('0x25')](_0x37cdf0);};}exports[_0x93f1('0x26')]=function(_0x4cd947,_0x591270){var _0x4e6257={},_0x1ea982={},_0x309cfa={'count':0x0,'rows':[]};var _0x14980e=_[_0x93f1('0x27')](db[_0x93f1('0x28')]['rawAttributes'],function(_0x21038c){return{'name':_0x21038c[_0x93f1('0x29')],'type':_0x21038c[_0x93f1('0x2a')]['key']};});_0x1ea982['model']=_[_0x93f1('0x27')](_0x14980e,_0x93f1('0x24'));_0x1ea982[_0x93f1('0x2b')]=_['keys'](_0x4cd947[_0x93f1('0x2b')]);_0x1ea982[_0x93f1('0x2c')]=_[_0x93f1('0x2d')](_0x1ea982[_0x93f1('0x2e')],_0x1ea982['query']);_0x4e6257[_0x93f1('0x2f')]=_['intersection'](_0x1ea982['model'],qs['fields'](_0x4cd947[_0x93f1('0x2b')][_0x93f1('0x30')]));_0x4e6257['attributes']=_0x4e6257[_0x93f1('0x2f')]['length']?_0x4e6257[_0x93f1('0x2f')]:_0x1ea982[_0x93f1('0x2e')];if(!_0x4cd947[_0x93f1('0x2b')][_0x93f1('0x31')](_0x93f1('0x32'))){_0x4e6257['limit']=qs[_0x93f1('0x1a')](_0x4cd947['query'][_0x93f1('0x1a')]);_0x4e6257[_0x93f1('0x18')]=qs['offset'](_0x4cd947['query'][_0x93f1('0x18')]);}_0x4e6257[_0x93f1('0x33')]=qs[_0x93f1('0x34')](_0x4cd947[_0x93f1('0x2b')][_0x93f1('0x34')]);_0x4e6257[_0x93f1('0x35')]=qs[_0x93f1('0x2c')](_[_0x93f1('0x36')](_0x4cd947['query'],_0x1ea982['filters']),_0x14980e);if(_0x4cd947[_0x93f1('0x2b')]['filter']){_0x4e6257[_0x93f1('0x35')]=_['merge'](_0x4e6257[_0x93f1('0x35')],{'$or':_[_0x93f1('0x27')](_0x14980e,function(_0x15ca4e){if(_0x15ca4e[_0x93f1('0x2a')]!==_0x93f1('0x37')){var _0x1909fe={};_0x1909fe[_0x15ca4e[_0x93f1('0x24')]]={'$like':'%'+_0x4cd947['query'][_0x93f1('0x38')]+'%'};return _0x1909fe;}})});}_0x4e6257=_[_0x93f1('0x39')]({},_0x4e6257,_0x4cd947[_0x93f1('0x3a')]);var _0x233f96={'where':_0x4e6257[_0x93f1('0x35')]};return db[_0x93f1('0x28')][_0x93f1('0x1b')](_0x233f96)[_0x93f1('0x3b')](function(_0x250947){_0x309cfa['count']=_0x250947;if(_0x4cd947[_0x93f1('0x2b')]['includeAll']){_0x4e6257[_0x93f1('0x3c')]=[{'all':!![]}];}return db['ZohoConfiguration'][_0x93f1('0x3d')](_0x4e6257);})[_0x93f1('0x3b')](function(_0x5912e2){_0x309cfa[_0x93f1('0x3e')]=_0x5912e2;return _0x309cfa;})['then'](respondWithFilteredResult(_0x591270,_0x4e6257))[_0x93f1('0x3f')](handleError(_0x591270,null));};exports[_0x93f1('0x40')]=function(_0x18bc4a,_0xdf40b0){var _0x59d863={'raw':![],'where':{'id':_0x18bc4a[_0x93f1('0x41')]['id']}},_0x34a1d8={};_0x34a1d8[_0x93f1('0x2e')]=_['keys'](db[_0x93f1('0x28')][_0x93f1('0x42')]);_0x34a1d8[_0x93f1('0x2b')]=_[_0x93f1('0x43')](_0x18bc4a[_0x93f1('0x2b')]);_0x34a1d8[_0x93f1('0x2c')]=_[_0x93f1('0x2d')](_0x34a1d8['model'],_0x34a1d8[_0x93f1('0x2b')]);_0x59d863[_0x93f1('0x2f')]=_[_0x93f1('0x2d')](_0x34a1d8[_0x93f1('0x2e')],qs[_0x93f1('0x30')](_0x18bc4a[_0x93f1('0x2b')]['fields']));_0x59d863[_0x93f1('0x2f')]=_0x59d863[_0x93f1('0x2f')][_0x93f1('0x44')]?_0x59d863[_0x93f1('0x2f')]:_0x34a1d8[_0x93f1('0x2e')];if(_0x18bc4a[_0x93f1('0x2b')][_0x93f1('0x45')]){_0x59d863[_0x93f1('0x3c')]=[{'all':!![]}];}_0x59d863=_['merge']({},_0x59d863,_0x18bc4a[_0x93f1('0x3a')]);return db[_0x93f1('0x28')][_0x93f1('0x46')](_0x59d863)[_0x93f1('0x3b')](handleEntityNotFound(_0xdf40b0,null))['then'](respondWithResult(_0xdf40b0,null))['catch'](handleError(_0xdf40b0,null));};exports[_0x93f1('0x47')]=function(_0x791bc1,_0x473cfc){return db[_0x93f1('0x28')][_0x93f1('0x47')](_0x791bc1[_0x93f1('0x48')],{})[_0x93f1('0x3b')](respondWithResult(_0x473cfc,0xc9))[_0x93f1('0x3f')](handleError(_0x473cfc,null));};exports[_0x93f1('0x20')]=function(_0x1866b7,_0x5ac052){if(_0x1866b7[_0x93f1('0x48')]['id']){delete _0x1866b7[_0x93f1('0x48')]['id'];}return db[_0x93f1('0x28')][_0x93f1('0x46')]({'where':{'id':_0x1866b7[_0x93f1('0x41')]['id']}})[_0x93f1('0x3b')](handleEntityNotFound(_0x5ac052,null))[_0x93f1('0x3b')](saveUpdates(_0x1866b7[_0x93f1('0x48')],null))[_0x93f1('0x3b')](respondWithResult(_0x5ac052,null))[_0x93f1('0x3f')](handleError(_0x5ac052,null));};exports[_0x93f1('0x21')]=function(_0x11f93b,_0x2db81c){return db['ZohoConfiguration'][_0x93f1('0x46')]({'where':{'id':_0x11f93b['params']['id']}})[_0x93f1('0x3b')](handleEntityNotFound(_0x2db81c,null))[_0x93f1('0x3b')](removeEntity(_0x2db81c,null))[_0x93f1('0x3f')](handleError(_0x2db81c,null));};exports[_0x93f1('0x49')]=function(_0x3bd452,_0x348dc1,_0x291c1c){var _0x4761b2={};var _0x500a9c={};var _0x282e50;var _0xa38b81;return db[_0x93f1('0x28')][_0x93f1('0x4a')]({'where':{'id':_0x3bd452[_0x93f1('0x41')]['id']}})[_0x93f1('0x3b')](handleEntityNotFound(_0x348dc1,null))[_0x93f1('0x3b')](function(_0xe9e6bd){if(_0xe9e6bd){_0x282e50=_0xe9e6bd;_0x500a9c[_0x93f1('0x2e')]=_[_0x93f1('0x43')](db[_0x93f1('0x4b')]['rawAttributes']);_0x500a9c[_0x93f1('0x2b')]=_[_0x93f1('0x43')](_0x3bd452[_0x93f1('0x2b')]);_0x500a9c['filters']=_[_0x93f1('0x2d')](_0x500a9c[_0x93f1('0x2e')],_0x500a9c[_0x93f1('0x2b')]);_0x4761b2[_0x93f1('0x2f')]=_[_0x93f1('0x2d')](_0x500a9c[_0x93f1('0x2e')],qs[_0x93f1('0x30')](_0x3bd452['query'][_0x93f1('0x30')]));_0x4761b2[_0x93f1('0x2f')]=_0x4761b2[_0x93f1('0x2f')][_0x93f1('0x44')]?_0x4761b2['attributes']:_0x500a9c[_0x93f1('0x2e')];_0x4761b2[_0x93f1('0x33')]=qs[_0x93f1('0x34')](_0x3bd452[_0x93f1('0x2b')][_0x93f1('0x34')]);_0x4761b2[_0x93f1('0x35')]=qs[_0x93f1('0x2c')](_['pick'](_0x3bd452[_0x93f1('0x2b')],_0x500a9c[_0x93f1('0x2c')]));if(_0x3bd452[_0x93f1('0x2b')][_0x93f1('0x38')]){_0x4761b2[_0x93f1('0x35')]=_[_0x93f1('0x39')](_0x4761b2[_0x93f1('0x35')],{'$or':_[_0x93f1('0x27')](_0x4761b2[_0x93f1('0x2f')],function(_0xea8180){var _0x525d7f={};_0x525d7f[_0xea8180]={'$like':'%'+_0x3bd452['query'][_0x93f1('0x38')]+'%'};return _0x525d7f;})});}_0x4761b2=_[_0x93f1('0x39')]({},_0x4761b2,_0x3bd452[_0x93f1('0x3a')]);return _0x282e50[_0x93f1('0x49')](_0x4761b2);}})[_0x93f1('0x3b')](function(_0x12285d){if(_0x12285d){_0xa38b81=_0x12285d['length'];if(!_0x3bd452[_0x93f1('0x2b')][_0x93f1('0x31')]('nolimit')){_0x4761b2[_0x93f1('0x1a')]=qs[_0x93f1('0x1a')](_0x3bd452[_0x93f1('0x2b')][_0x93f1('0x1a')]);_0x4761b2[_0x93f1('0x18')]=qs[_0x93f1('0x18')](_0x3bd452[_0x93f1('0x2b')][_0x93f1('0x18')]);}return _0x282e50['getFields'](_0x4761b2);}})[_0x93f1('0x3b')](function(_0xc47301){if(_0xc47301){return _0xc47301?{'count':_0xa38b81,'rows':_0xc47301}:null;}})[_0x93f1('0x3b')](respondWithResult(_0x348dc1,null))[_0x93f1('0x3f')](handleError(_0x348dc1,null));};exports[_0x93f1('0x4c')]=function(_0x47ee7d,_0x4b8ef3,_0x569a93){var _0x58c47c={};var _0x2e344a={};var _0x4a2e68;var _0x3ed05c;return db['ZohoConfiguration'][_0x93f1('0x4a')]({'where':{'id':_0x47ee7d['params']['id']}})[_0x93f1('0x3b')](handleEntityNotFound(_0x4b8ef3,null))[_0x93f1('0x3b')](function(_0x40a377){if(_0x40a377){_0x4a2e68=_0x40a377;_0x2e344a['model']=_['keys'](db['ZohoField'][_0x93f1('0x42')]);_0x2e344a[_0x93f1('0x2b')]=_[_0x93f1('0x43')](_0x47ee7d['query']);_0x2e344a[_0x93f1('0x2c')]=_['intersection'](_0x2e344a[_0x93f1('0x2e')],_0x2e344a[_0x93f1('0x2b')]);_0x58c47c[_0x93f1('0x2f')]=_[_0x93f1('0x2d')](_0x2e344a[_0x93f1('0x2e')],qs[_0x93f1('0x30')](_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x30')]));_0x58c47c['attributes']=_0x58c47c['attributes'][_0x93f1('0x44')]?_0x58c47c[_0x93f1('0x2f')]:_0x2e344a[_0x93f1('0x2e')];_0x58c47c['order']=qs[_0x93f1('0x34')](_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x34')]);_0x58c47c[_0x93f1('0x35')]=qs[_0x93f1('0x2c')](_[_0x93f1('0x36')](_0x47ee7d[_0x93f1('0x2b')],_0x2e344a[_0x93f1('0x2c')]));if(_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x38')]){_0x58c47c[_0x93f1('0x35')]=_[_0x93f1('0x39')](_0x58c47c[_0x93f1('0x35')],{'$or':_[_0x93f1('0x27')](_0x58c47c[_0x93f1('0x2f')],function(_0x573fe6){var _0x3f16ae={};_0x3f16ae[_0x573fe6]={'$like':'%'+_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x38')]+'%'};return _0x3f16ae;})});}_0x58c47c=_[_0x93f1('0x39')]({},_0x58c47c,_0x47ee7d[_0x93f1('0x3a')]);return _0x4a2e68['getSubjects'](_0x58c47c);}})[_0x93f1('0x3b')](function(_0x57ad5f){if(_0x57ad5f){_0x3ed05c=_0x57ad5f[_0x93f1('0x44')];if(!_0x47ee7d['query'][_0x93f1('0x31')](_0x93f1('0x32'))){_0x58c47c[_0x93f1('0x1a')]=qs[_0x93f1('0x1a')](_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x1a')]);_0x58c47c[_0x93f1('0x18')]=qs[_0x93f1('0x18')](_0x47ee7d[_0x93f1('0x2b')][_0x93f1('0x18')]);}return _0x4a2e68['getSubjects'](_0x58c47c);}})[_0x93f1('0x3b')](function(_0x5dbcd0){if(_0x5dbcd0){return _0x5dbcd0?{'count':_0x3ed05c,'rows':_0x5dbcd0}:null;}})['then'](respondWithResult(_0x4b8ef3,null))['catch'](handleError(_0x4b8ef3,null));};exports[_0x93f1('0x4d')]=function(_0x4233ae,_0x2afc59,_0x501b83){var _0x501d08={};var _0x44b1e1={};var _0x2e24dd;var _0x4f2ab4;return db['ZohoConfiguration'][_0x93f1('0x4a')]({'where':{'id':_0x4233ae['params']['id']}})[_0x93f1('0x3b')](handleEntityNotFound(_0x2afc59,null))[_0x93f1('0x3b')](function(_0x2a85da){if(_0x2a85da){_0x2e24dd=_0x2a85da;_0x44b1e1[_0x93f1('0x2e')]=_[_0x93f1('0x43')](db['ZohoField']['rawAttributes']);_0x44b1e1[_0x93f1('0x2b')]=_[_0x93f1('0x43')](_0x4233ae[_0x93f1('0x2b')]);_0x44b1e1['filters']=_[_0x93f1('0x2d')](_0x44b1e1['model'],_0x44b1e1[_0x93f1('0x2b')]);_0x501d08[_0x93f1('0x2f')]=_[_0x93f1('0x2d')](_0x44b1e1[_0x93f1('0x2e')],qs[_0x93f1('0x30')](_0x4233ae[_0x93f1('0x2b')][_0x93f1('0x30')]));_0x501d08[_0x93f1('0x2f')]=_0x501d08[_0x93f1('0x2f')][_0x93f1('0x44')]?_0x501d08[_0x93f1('0x2f')]:_0x44b1e1[_0x93f1('0x2e')];_0x501d08[_0x93f1('0x33')]=qs['sort'](_0x4233ae[_0x93f1('0x2b')][_0x93f1('0x34')]);_0x501d08[_0x93f1('0x35')]=qs[_0x93f1('0x2c')](_[_0x93f1('0x36')](_0x4233ae[_0x93f1('0x2b')],_0x44b1e1[_0x93f1('0x2c')]));if(_0x4233ae[_0x93f1('0x2b')][_0x93f1('0x38')]){_0x501d08[_0x93f1('0x35')]=_[_0x93f1('0x39')](_0x501d08[_0x93f1('0x35')],{'$or':_[_0x93f1('0x27')](_0x501d08[_0x93f1('0x2f')],function(_0x36cde2){var _0x40ce11={};_0x40ce11[_0x36cde2]={'$like':'%'+_0x4233ae[_0x93f1('0x2b')][_0x93f1('0x38')]+'%'};return _0x40ce11;})});}_0x501d08=_[_0x93f1('0x39')]({},_0x501d08,_0x4233ae[_0x93f1('0x3a')]);return _0x2e24dd[_0x93f1('0x4d')](_0x501d08);}})[_0x93f1('0x3b')](function(_0x4f0c7a){if(_0x4f0c7a){_0x4f2ab4=_0x4f0c7a[_0x93f1('0x44')];if(!_0x4233ae['query'][_0x93f1('0x31')](_0x93f1('0x32'))){_0x501d08[_0x93f1('0x1a')]=qs[_0x93f1('0x1a')](_0x4233ae[_0x93f1('0x2b')]['limit']);_0x501d08[_0x93f1('0x18')]=qs['offset'](_0x4233ae['query']['offset']);}return _0x2e24dd[_0x93f1('0x4d')](_0x501d08);}})[_0x93f1('0x3b')](function(_0x347f78){if(_0x347f78){return _0x347f78?{'count':_0x4f2ab4,'rows':_0x347f78}:null;}})[_0x93f1('0x3b')](respondWithResult(_0x2afc59,null))[_0x93f1('0x3f')](handleError(_0x2afc59,null));};