5d2c0c610fc89af6ad6b99c5a4926604852e729a
[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 _0xa684=['hasOwnProperty','order','sort','where','pick','filter','merge','includeAll','include','rows','catch','show','params','options','find','body','findOne','ZohoField','getFields','nolimit','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','sox','ejs','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','send','ZohoConfiguration','rawAttributes','fieldName','type','key','model','map','name','query','keys','filters','intersection','fields','length','attributes'];(function(_0x8afe8a,_0xde88c9){var _0x13a2df=function(_0x4a15eb){while(--_0x4a15eb){_0x8afe8a['push'](_0x8afe8a['shift']());}};_0x13a2df(++_0xde88c9);}(_0xa684,0x1e4));var _0x4a68=function(_0x2d0589,_0x1bdd0f){_0x2d0589=_0x2d0589-0x0;var _0x464506=_0xa684[_0x2d0589];return _0x464506;};'use strict';var emlformat=require(_0x4a68('0x0'));var rimraf=require(_0x4a68('0x1'));var zipdir=require(_0x4a68('0x2'));var jsonpatch=require(_0x4a68('0x3'));var rp=require(_0x4a68('0x4'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require('mustache');var util=require('util');var path=require('path');var sox=require(_0x4a68('0x5'));var csv=require('to-csv');var ejs=require(_0x4a68('0x6'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x4a68('0x7'));var squel=require('squel');var crypto=require(_0x4a68('0x8'));var jsforce=require(_0x4a68('0x9'));var deskjs=require(_0x4a68('0xa'));var toCsv=require('to-csv');var querystring=require(_0x4a68('0xb'));var Papa=require(_0x4a68('0xc'));var Redis=require(_0x4a68('0xd'));var authService=require(_0x4a68('0xe'));var qs=require(_0x4a68('0xf'));var as=require(_0x4a68('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x4a68('0x11'));var utils=require(_0x4a68('0x12'));var config=require(_0x4a68('0x13'));var licenseUtil=require(_0x4a68('0x14'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x1b3b62,_0x594c38){_0x594c38=_0x594c38||0xcc;return function(_0x48685f){if(_0x48685f){return _0x1b3b62[_0x4a68('0x15')](_0x594c38);}return _0x1b3b62[_0x4a68('0x16')](_0x594c38)[_0x4a68('0x17')]();};}function respondWithResult(_0x193bed,_0xf832f2){_0xf832f2=_0xf832f2||0xc8;return function(_0x7dfb46){if(_0x7dfb46){return _0x193bed[_0x4a68('0x16')](_0xf832f2)[_0x4a68('0x18')](_0x7dfb46);}};}function respondWithFilteredResult(_0x28c84e,_0xb2cd84){return function(_0x569400){if(_0x569400){var _0x37fa1e=typeof _0xb2cd84[_0x4a68('0x19')]===_0x4a68('0x1a')&&typeof _0xb2cd84['limit']===_0x4a68('0x1a');var _0x2f4b01=_0x569400[_0x4a68('0x1b')];var _0xf02558=_0x37fa1e?0x0:_0xb2cd84['offset'];var _0x386924=_0x37fa1e?_0x569400[_0x4a68('0x1b')]:_0xb2cd84['offset']+_0xb2cd84[_0x4a68('0x1c')];var _0x1a691d;if(_0x386924>=_0x2f4b01){_0x386924=_0x2f4b01;_0x1a691d=0xc8;}else{_0x1a691d=0xce;}_0x28c84e[_0x4a68('0x16')](_0x1a691d);return _0x28c84e[_0x4a68('0x1d')](_0x4a68('0x1e'),_0xf02558+'-'+_0x386924+'/'+_0x2f4b01)[_0x4a68('0x18')](_0x569400);}return null;};}function patchUpdates(_0x91334f){return function(_0x247841){try{jsonpatch[_0x4a68('0x1f')](_0x247841,_0x91334f,!![]);}catch(_0x1ada18){return BPromise[_0x4a68('0x20')](_0x1ada18);}return _0x247841[_0x4a68('0x21')]();};}function saveUpdates(_0x4a3928,_0x293ce3){return function(_0x25c1de){if(_0x25c1de){return _0x25c1de[_0x4a68('0x22')](_0x4a3928)[_0x4a68('0x23')](function(_0xc53bf2){return _0xc53bf2;});}return null;};}function removeEntity(_0x4524ec,_0x2d1ebc){return function(_0x3c49b7){if(_0x3c49b7){return _0x3c49b7[_0x4a68('0x24')]()[_0x4a68('0x23')](function(){_0x4524ec[_0x4a68('0x16')](0xcc)['end']();});}};}function handleEntityNotFound(_0x288ee9,_0x244e07){return function(_0x140df1){if(!_0x140df1){_0x288ee9[_0x4a68('0x15')](0x194);}return _0x140df1;};}function handleError(_0x3c1f14,_0x32572a){_0x32572a=_0x32572a||0x1f4;return function(_0x1e21a0){logger[_0x4a68('0x25')](_0x1e21a0[_0x4a68('0x26')]);if(_0x1e21a0['name']){delete _0x1e21a0['name'];}_0x3c1f14[_0x4a68('0x16')](_0x32572a)[_0x4a68('0x27')](_0x1e21a0);};}exports['index']=function(_0x43a26d,_0x4e173a){var _0x42db52={},_0x414a8d={},_0x46ae68={'count':0x0,'rows':[]};var _0x4ec48e=_['map'](db[_0x4a68('0x28')][_0x4a68('0x29')],function(_0x18edbb){return{'name':_0x18edbb[_0x4a68('0x2a')],'type':_0x18edbb[_0x4a68('0x2b')][_0x4a68('0x2c')]};});_0x414a8d[_0x4a68('0x2d')]=_[_0x4a68('0x2e')](_0x4ec48e,_0x4a68('0x2f'));_0x414a8d[_0x4a68('0x30')]=_[_0x4a68('0x31')](_0x43a26d[_0x4a68('0x30')]);_0x414a8d[_0x4a68('0x32')]=_[_0x4a68('0x33')](_0x414a8d[_0x4a68('0x2d')],_0x414a8d[_0x4a68('0x30')]);_0x42db52['attributes']=_['intersection'](_0x414a8d[_0x4a68('0x2d')],qs[_0x4a68('0x34')](_0x43a26d[_0x4a68('0x30')][_0x4a68('0x34')]));_0x42db52['attributes']=_0x42db52['attributes'][_0x4a68('0x35')]?_0x42db52[_0x4a68('0x36')]:_0x414a8d[_0x4a68('0x2d')];if(!_0x43a26d[_0x4a68('0x30')][_0x4a68('0x37')]('nolimit')){_0x42db52[_0x4a68('0x1c')]=qs[_0x4a68('0x1c')](_0x43a26d[_0x4a68('0x30')]['limit']);_0x42db52['offset']=qs[_0x4a68('0x19')](_0x43a26d[_0x4a68('0x30')]['offset']);}_0x42db52[_0x4a68('0x38')]=qs[_0x4a68('0x39')](_0x43a26d[_0x4a68('0x30')][_0x4a68('0x39')]);_0x42db52[_0x4a68('0x3a')]=qs[_0x4a68('0x32')](_[_0x4a68('0x3b')](_0x43a26d[_0x4a68('0x30')],_0x414a8d[_0x4a68('0x32')]),_0x4ec48e);if(_0x43a26d['query'][_0x4a68('0x3c')]){_0x42db52[_0x4a68('0x3a')]=_['merge'](_0x42db52[_0x4a68('0x3a')],{'$or':_[_0x4a68('0x2e')](_0x4ec48e,function(_0x234c2c){if(_0x234c2c[_0x4a68('0x2b')]!=='VIRTUAL'){var _0x4287ca={};_0x4287ca[_0x234c2c[_0x4a68('0x2f')]]={'$like':'%'+_0x43a26d[_0x4a68('0x30')][_0x4a68('0x3c')]+'%'};return _0x4287ca;}})});}_0x42db52=_[_0x4a68('0x3d')]({},_0x42db52,_0x43a26d['options']);var _0x39ae11={'where':_0x42db52[_0x4a68('0x3a')]};return db['ZohoConfiguration'][_0x4a68('0x1b')](_0x39ae11)['then'](function(_0x4818bb){_0x46ae68[_0x4a68('0x1b')]=_0x4818bb;if(_0x43a26d[_0x4a68('0x30')][_0x4a68('0x3e')]){_0x42db52[_0x4a68('0x3f')]=[{'all':!![]}];}return db[_0x4a68('0x28')]['findAll'](_0x42db52);})[_0x4a68('0x23')](function(_0x9afb2){_0x46ae68[_0x4a68('0x40')]=_0x9afb2;return _0x46ae68;})[_0x4a68('0x23')](respondWithFilteredResult(_0x4e173a,_0x42db52))[_0x4a68('0x41')](handleError(_0x4e173a,null));};exports[_0x4a68('0x42')]=function(_0x3f6b06,_0x38c7b1){var _0x4120fe={'raw':![],'where':{'id':_0x3f6b06[_0x4a68('0x43')]['id']}},_0x18d583={};_0x18d583[_0x4a68('0x2d')]=_[_0x4a68('0x31')](db[_0x4a68('0x28')][_0x4a68('0x29')]);_0x18d583[_0x4a68('0x30')]=_[_0x4a68('0x31')](_0x3f6b06['query']);_0x18d583[_0x4a68('0x32')]=_[_0x4a68('0x33')](_0x18d583[_0x4a68('0x2d')],_0x18d583[_0x4a68('0x30')]);_0x4120fe[_0x4a68('0x36')]=_[_0x4a68('0x33')](_0x18d583[_0x4a68('0x2d')],qs[_0x4a68('0x34')](_0x3f6b06[_0x4a68('0x30')]['fields']));_0x4120fe[_0x4a68('0x36')]=_0x4120fe[_0x4a68('0x36')]['length']?_0x4120fe[_0x4a68('0x36')]:_0x18d583[_0x4a68('0x2d')];if(_0x3f6b06[_0x4a68('0x30')][_0x4a68('0x3e')]){_0x4120fe[_0x4a68('0x3f')]=[{'all':!![]}];}_0x4120fe=_[_0x4a68('0x3d')]({},_0x4120fe,_0x3f6b06[_0x4a68('0x44')]);return db[_0x4a68('0x28')][_0x4a68('0x45')](_0x4120fe)[_0x4a68('0x23')](handleEntityNotFound(_0x38c7b1,null))[_0x4a68('0x23')](respondWithResult(_0x38c7b1,null))[_0x4a68('0x41')](handleError(_0x38c7b1,null));};exports['create']=function(_0x4fcb5b,_0x404d46){return db['ZohoConfiguration']['create'](_0x4fcb5b[_0x4a68('0x46')],{})[_0x4a68('0x23')](respondWithResult(_0x404d46,0xc9))[_0x4a68('0x41')](handleError(_0x404d46,null));};exports[_0x4a68('0x22')]=function(_0x54aa6a,_0x1aeaea){if(_0x54aa6a['body']['id']){delete _0x54aa6a[_0x4a68('0x46')]['id'];}return db[_0x4a68('0x28')]['find']({'where':{'id':_0x54aa6a[_0x4a68('0x43')]['id']}})['then'](handleEntityNotFound(_0x1aeaea,null))[_0x4a68('0x23')](saveUpdates(_0x54aa6a['body'],null))[_0x4a68('0x23')](respondWithResult(_0x1aeaea,null))[_0x4a68('0x41')](handleError(_0x1aeaea,null));};exports[_0x4a68('0x24')]=function(_0x16bf70,_0xf72835){return db[_0x4a68('0x28')][_0x4a68('0x45')]({'where':{'id':_0x16bf70[_0x4a68('0x43')]['id']}})[_0x4a68('0x23')](handleEntityNotFound(_0xf72835,null))[_0x4a68('0x23')](removeEntity(_0xf72835,null))[_0x4a68('0x41')](handleError(_0xf72835,null));};exports['getFields']=function(_0x2c3ef2,_0x385350,_0x54be12){var _0x20278d={};var _0xba8834={};var _0x590b41;var _0x5bbc8f;return db[_0x4a68('0x28')][_0x4a68('0x47')]({'where':{'id':_0x2c3ef2[_0x4a68('0x43')]['id']}})[_0x4a68('0x23')](handleEntityNotFound(_0x385350,null))[_0x4a68('0x23')](function(_0x405e16){if(_0x405e16){_0x590b41=_0x405e16;_0xba8834[_0x4a68('0x2d')]=_[_0x4a68('0x31')](db[_0x4a68('0x48')]['rawAttributes']);_0xba8834[_0x4a68('0x30')]=_[_0x4a68('0x31')](_0x2c3ef2[_0x4a68('0x30')]);_0xba8834[_0x4a68('0x32')]=_['intersection'](_0xba8834['model'],_0xba8834[_0x4a68('0x30')]);_0x20278d[_0x4a68('0x36')]=_[_0x4a68('0x33')](_0xba8834[_0x4a68('0x2d')],qs[_0x4a68('0x34')](_0x2c3ef2[_0x4a68('0x30')][_0x4a68('0x34')]));_0x20278d[_0x4a68('0x36')]=_0x20278d[_0x4a68('0x36')][_0x4a68('0x35')]?_0x20278d['attributes']:_0xba8834['model'];_0x20278d[_0x4a68('0x38')]=qs[_0x4a68('0x39')](_0x2c3ef2['query'][_0x4a68('0x39')]);_0x20278d['where']=qs[_0x4a68('0x32')](_[_0x4a68('0x3b')](_0x2c3ef2['query'],_0xba8834[_0x4a68('0x32')]));if(_0x2c3ef2[_0x4a68('0x30')][_0x4a68('0x3c')]){_0x20278d['where']=_[_0x4a68('0x3d')](_0x20278d['where'],{'$or':_['map'](_0x20278d['attributes'],function(_0x15fd06){var _0x178d72={};_0x178d72[_0x15fd06]={'$like':'%'+_0x2c3ef2[_0x4a68('0x30')][_0x4a68('0x3c')]+'%'};return _0x178d72;})});}_0x20278d=_[_0x4a68('0x3d')]({},_0x20278d,_0x2c3ef2[_0x4a68('0x44')]);return _0x590b41[_0x4a68('0x49')](_0x20278d);}})[_0x4a68('0x23')](function(_0x1ae7f6){if(_0x1ae7f6){_0x5bbc8f=_0x1ae7f6['length'];if(!_0x2c3ef2[_0x4a68('0x30')][_0x4a68('0x37')](_0x4a68('0x4a'))){_0x20278d['limit']=qs[_0x4a68('0x1c')](_0x2c3ef2['query'][_0x4a68('0x1c')]);_0x20278d[_0x4a68('0x19')]=qs[_0x4a68('0x19')](_0x2c3ef2[_0x4a68('0x30')][_0x4a68('0x19')]);}return _0x590b41[_0x4a68('0x49')](_0x20278d);}})[_0x4a68('0x23')](function(_0x50571c){if(_0x50571c){return _0x50571c?{'count':_0x5bbc8f,'rows':_0x50571c}:null;}})[_0x4a68('0x23')](respondWithResult(_0x385350,null))[_0x4a68('0x41')](handleError(_0x385350,null));};exports[_0x4a68('0x4b')]=function(_0x5827f3,_0x171109,_0x5dfdfd){var _0x599387={};var _0x3b0969={};var _0x44553b;var _0x3ee534;return db['ZohoConfiguration'][_0x4a68('0x47')]({'where':{'id':_0x5827f3[_0x4a68('0x43')]['id']}})[_0x4a68('0x23')](handleEntityNotFound(_0x171109,null))[_0x4a68('0x23')](function(_0x366a7d){if(_0x366a7d){_0x44553b=_0x366a7d;_0x3b0969['model']=_[_0x4a68('0x31')](db[_0x4a68('0x48')][_0x4a68('0x29')]);_0x3b0969[_0x4a68('0x30')]=_['keys'](_0x5827f3[_0x4a68('0x30')]);_0x3b0969[_0x4a68('0x32')]=_[_0x4a68('0x33')](_0x3b0969['model'],_0x3b0969['query']);_0x599387[_0x4a68('0x36')]=_[_0x4a68('0x33')](_0x3b0969[_0x4a68('0x2d')],qs[_0x4a68('0x34')](_0x5827f3['query'][_0x4a68('0x34')]));_0x599387[_0x4a68('0x36')]=_0x599387[_0x4a68('0x36')][_0x4a68('0x35')]?_0x599387[_0x4a68('0x36')]:_0x3b0969['model'];_0x599387[_0x4a68('0x38')]=qs[_0x4a68('0x39')](_0x5827f3[_0x4a68('0x30')][_0x4a68('0x39')]);_0x599387[_0x4a68('0x3a')]=qs[_0x4a68('0x32')](_[_0x4a68('0x3b')](_0x5827f3[_0x4a68('0x30')],_0x3b0969[_0x4a68('0x32')]));if(_0x5827f3[_0x4a68('0x30')][_0x4a68('0x3c')]){_0x599387[_0x4a68('0x3a')]=_[_0x4a68('0x3d')](_0x599387['where'],{'$or':_[_0x4a68('0x2e')](_0x599387[_0x4a68('0x36')],function(_0x458e36){var _0x2d6966={};_0x2d6966[_0x458e36]={'$like':'%'+_0x5827f3[_0x4a68('0x30')]['filter']+'%'};return _0x2d6966;})});}_0x599387=_[_0x4a68('0x3d')]({},_0x599387,_0x5827f3[_0x4a68('0x44')]);return _0x44553b[_0x4a68('0x4b')](_0x599387);}})['then'](function(_0xdb86a6){if(_0xdb86a6){_0x3ee534=_0xdb86a6[_0x4a68('0x35')];if(!_0x5827f3[_0x4a68('0x30')][_0x4a68('0x37')](_0x4a68('0x4a'))){_0x599387[_0x4a68('0x1c')]=qs['limit'](_0x5827f3[_0x4a68('0x30')]['limit']);_0x599387[_0x4a68('0x19')]=qs[_0x4a68('0x19')](_0x5827f3[_0x4a68('0x30')][_0x4a68('0x19')]);}return _0x44553b[_0x4a68('0x4b')](_0x599387);}})[_0x4a68('0x23')](function(_0x365b16){if(_0x365b16){return _0x365b16?{'count':_0x3ee534,'rows':_0x365b16}:null;}})[_0x4a68('0x23')](respondWithResult(_0x171109,null))[_0x4a68('0x41')](handleError(_0x171109,null));};exports['getDescriptions']=function(_0x3dffd6,_0x256917,_0x3a4cb6){var _0x31305a={};var _0x9ebeca={};var _0x5646db;var _0x281340;return db[_0x4a68('0x28')]['findOne']({'where':{'id':_0x3dffd6[_0x4a68('0x43')]['id']}})[_0x4a68('0x23')](handleEntityNotFound(_0x256917,null))[_0x4a68('0x23')](function(_0x1715cc){if(_0x1715cc){_0x5646db=_0x1715cc;_0x9ebeca[_0x4a68('0x2d')]=_[_0x4a68('0x31')](db[_0x4a68('0x48')][_0x4a68('0x29')]);_0x9ebeca[_0x4a68('0x30')]=_[_0x4a68('0x31')](_0x3dffd6['query']);_0x9ebeca[_0x4a68('0x32')]=_['intersection'](_0x9ebeca[_0x4a68('0x2d')],_0x9ebeca[_0x4a68('0x30')]);_0x31305a['attributes']=_['intersection'](_0x9ebeca[_0x4a68('0x2d')],qs[_0x4a68('0x34')](_0x3dffd6[_0x4a68('0x30')]['fields']));_0x31305a[_0x4a68('0x36')]=_0x31305a[_0x4a68('0x36')]['length']?_0x31305a[_0x4a68('0x36')]:_0x9ebeca[_0x4a68('0x2d')];_0x31305a['order']=qs[_0x4a68('0x39')](_0x3dffd6[_0x4a68('0x30')]['sort']);_0x31305a['where']=qs[_0x4a68('0x32')](_[_0x4a68('0x3b')](_0x3dffd6[_0x4a68('0x30')],_0x9ebeca['filters']));if(_0x3dffd6['query'][_0x4a68('0x3c')]){_0x31305a[_0x4a68('0x3a')]=_[_0x4a68('0x3d')](_0x31305a['where'],{'$or':_[_0x4a68('0x2e')](_0x31305a['attributes'],function(_0x40b169){var _0x53e44b={};_0x53e44b[_0x40b169]={'$like':'%'+_0x3dffd6[_0x4a68('0x30')]['filter']+'%'};return _0x53e44b;})});}_0x31305a=_[_0x4a68('0x3d')]({},_0x31305a,_0x3dffd6[_0x4a68('0x44')]);return _0x5646db[_0x4a68('0x4c')](_0x31305a);}})[_0x4a68('0x23')](function(_0x42f234){if(_0x42f234){_0x281340=_0x42f234[_0x4a68('0x35')];if(!_0x3dffd6[_0x4a68('0x30')][_0x4a68('0x37')](_0x4a68('0x4a'))){_0x31305a[_0x4a68('0x1c')]=qs[_0x4a68('0x1c')](_0x3dffd6[_0x4a68('0x30')]['limit']);_0x31305a['offset']=qs[_0x4a68('0x19')](_0x3dffd6['query'][_0x4a68('0x19')]);}return _0x5646db[_0x4a68('0x4c')](_0x31305a);}})[_0x4a68('0x23')](function(_0x474282){if(_0x474282){return _0x474282?{'count':_0x281340,'rows':_0x474282}:null;}})[_0x4a68('0x23')](respondWithResult(_0x256917,null))[_0x4a68('0x41')](handleError(_0x256917,null));};