Built motion from commit 497d3d92.|2.5.43
[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 _0x11bb=['attributes','model','fields','length','hasOwnProperty','order','sort','where','filters','pick','filter','merge','FreshsalesConfiguration','includeAll','include','findAll','rows','catch','show','params','options','find','create','body','destroy','getSubjects','findOne','nolimit','getDescriptions','FreshsalesField','getFields','rimraf','zip-dir','fast-json-patch','moment','bluebird','util','ejs','fs-extra','lodash','squel','crypto','jsforce','to-csv','querystring','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','json','offset','undefined','count','limit','status','set','Content-Range','save','update','then','end','error','stack','name','send','index','map','rawAttributes','fieldName','type','query','keys','intersection'];(function(_0x3bf83f,_0x181cac){var _0x22c4a9=function(_0xb3ff0d){while(--_0xb3ff0d){_0x3bf83f['push'](_0x3bf83f['shift']());}};_0x22c4a9(++_0x181cac);}(_0x11bb,0xbb));var _0xb11b=function(_0x492d48,_0x56e986){_0x492d48=_0x492d48-0x0;var _0x6a0be0=_0x11bb[_0x492d48];return _0x6a0be0;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xb11b('0x0'));var zipdir=require(_0xb11b('0x1'));var jsonpatch=require(_0xb11b('0x2'));var rp=require('request-promise');var moment=require(_0xb11b('0x3'));var BPromise=require(_0xb11b('0x4'));var Mustache=require('mustache');var util=require(_0xb11b('0x5'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xb11b('0x6'));var fs=require('fs');var fs_extra=require(_0xb11b('0x7'));var _=require(_0xb11b('0x8'));var squel=require(_0xb11b('0x9'));var crypto=require(_0xb11b('0xa'));var jsforce=require(_0xb11b('0xb'));var deskjs=require('desk.js');var toCsv=require(_0xb11b('0xc'));var querystring=require(_0xb11b('0xd'));var Papa=require(_0xb11b('0xe'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xb11b('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xb11b('0x10'));var logger=require(_0xb11b('0x11'))(_0xb11b('0x12'));var utils=require('../../config/utils');var config=require(_0xb11b('0x13'));var licenseUtil=require(_0xb11b('0x14'));var db=require(_0xb11b('0x15'))['db'];function respondWithStatusCode(_0x427d06,_0x23d110){_0x23d110=_0x23d110||0xcc;return function(_0x450a2f){if(_0x450a2f){return _0x427d06[_0xb11b('0x16')](_0x23d110);}return _0x427d06['status'](_0x23d110)['end']();};}function respondWithResult(_0x10a974,_0x12a1c1){_0x12a1c1=_0x12a1c1||0xc8;return function(_0x26a9c6){if(_0x26a9c6){return _0x10a974['status'](_0x12a1c1)[_0xb11b('0x17')](_0x26a9c6);}};}function respondWithFilteredResult(_0x14af77,_0x8257f1){return function(_0x58f7c9){if(_0x58f7c9){var _0x5121ae=typeof _0x8257f1[_0xb11b('0x18')]===_0xb11b('0x19')&&typeof _0x8257f1['limit']===_0xb11b('0x19');var _0x1d0419=_0x58f7c9[_0xb11b('0x1a')];var _0xac4a1d=_0x5121ae?0x0:_0x8257f1[_0xb11b('0x18')];var _0x29f30f=_0x5121ae?_0x58f7c9['count']:_0x8257f1['offset']+_0x8257f1[_0xb11b('0x1b')];var _0x308e9e;if(_0x29f30f>=_0x1d0419){_0x29f30f=_0x1d0419;_0x308e9e=0xc8;}else{_0x308e9e=0xce;}_0x14af77[_0xb11b('0x1c')](_0x308e9e);return _0x14af77[_0xb11b('0x1d')](_0xb11b('0x1e'),_0xac4a1d+'-'+_0x29f30f+'/'+_0x1d0419)[_0xb11b('0x17')](_0x58f7c9);}return null;};}function patchUpdates(_0x14bb53){return function(_0x2e0559){try{jsonpatch['apply'](_0x2e0559,_0x14bb53,!![]);}catch(_0xa4dcaf){return BPromise['reject'](_0xa4dcaf);}return _0x2e0559[_0xb11b('0x1f')]();};}function saveUpdates(_0x2fe080,_0x205e53){return function(_0x2c0584){if(_0x2c0584){return _0x2c0584[_0xb11b('0x20')](_0x2fe080)[_0xb11b('0x21')](function(_0x545414){return _0x545414;});}return null;};}function removeEntity(_0x3e06ce,_0xcd8ce8){return function(_0x2b233e){if(_0x2b233e){return _0x2b233e['destroy']()['then'](function(){_0x3e06ce[_0xb11b('0x1c')](0xcc)[_0xb11b('0x22')]();});}};}function handleEntityNotFound(_0x415469,_0x48daf2){return function(_0x3060ef){if(!_0x3060ef){_0x415469[_0xb11b('0x16')](0x194);}return _0x3060ef;};}function handleError(_0x38ac0d,_0x23f39c){_0x23f39c=_0x23f39c||0x1f4;return function(_0x372be6){logger[_0xb11b('0x23')](_0x372be6[_0xb11b('0x24')]);if(_0x372be6['name']){delete _0x372be6[_0xb11b('0x25')];}_0x38ac0d[_0xb11b('0x1c')](_0x23f39c)[_0xb11b('0x26')](_0x372be6);};}exports[_0xb11b('0x27')]=function(_0x3e3638,_0x2f389e){var _0x31b492={},_0x14fe30={},_0x29ff2d={'count':0x0,'rows':[]};var _0x797399=_[_0xb11b('0x28')](db['FreshsalesConfiguration'][_0xb11b('0x29')],function(_0x3ffb4f){return{'name':_0x3ffb4f[_0xb11b('0x2a')],'type':_0x3ffb4f[_0xb11b('0x2b')]['key']};});_0x14fe30['model']=_[_0xb11b('0x28')](_0x797399,_0xb11b('0x25'));_0x14fe30[_0xb11b('0x2c')]=_[_0xb11b('0x2d')](_0x3e3638[_0xb11b('0x2c')]);_0x14fe30['filters']=_[_0xb11b('0x2e')](_0x14fe30['model'],_0x14fe30[_0xb11b('0x2c')]);_0x31b492[_0xb11b('0x2f')]=_['intersection'](_0x14fe30[_0xb11b('0x30')],qs[_0xb11b('0x31')](_0x3e3638[_0xb11b('0x2c')][_0xb11b('0x31')]));_0x31b492[_0xb11b('0x2f')]=_0x31b492['attributes'][_0xb11b('0x32')]?_0x31b492[_0xb11b('0x2f')]:_0x14fe30['model'];if(!_0x3e3638[_0xb11b('0x2c')][_0xb11b('0x33')]('nolimit')){_0x31b492[_0xb11b('0x1b')]=qs['limit'](_0x3e3638['query'][_0xb11b('0x1b')]);_0x31b492[_0xb11b('0x18')]=qs[_0xb11b('0x18')](_0x3e3638[_0xb11b('0x2c')][_0xb11b('0x18')]);}_0x31b492[_0xb11b('0x34')]=qs[_0xb11b('0x35')](_0x3e3638[_0xb11b('0x2c')][_0xb11b('0x35')]);_0x31b492[_0xb11b('0x36')]=qs[_0xb11b('0x37')](_[_0xb11b('0x38')](_0x3e3638['query'],_0x14fe30[_0xb11b('0x37')]),_0x797399);if(_0x3e3638['query']['filter']){_0x31b492['where']=_['merge'](_0x31b492[_0xb11b('0x36')],{'$or':_[_0xb11b('0x28')](_0x797399,function(_0x5f3390){if(_0x5f3390[_0xb11b('0x2b')]!=='VIRTUAL'){var _0x23b7da={};_0x23b7da[_0x5f3390[_0xb11b('0x25')]]={'$like':'%'+_0x3e3638['query'][_0xb11b('0x39')]+'%'};return _0x23b7da;}})});}_0x31b492=_[_0xb11b('0x3a')]({},_0x31b492,_0x3e3638['options']);var _0x27a840={'where':_0x31b492[_0xb11b('0x36')]};return db[_0xb11b('0x3b')]['count'](_0x27a840)[_0xb11b('0x21')](function(_0x132e61){_0x29ff2d[_0xb11b('0x1a')]=_0x132e61;if(_0x3e3638[_0xb11b('0x2c')][_0xb11b('0x3c')]){_0x31b492[_0xb11b('0x3d')]=[{'all':!![]}];}return db[_0xb11b('0x3b')][_0xb11b('0x3e')](_0x31b492);})[_0xb11b('0x21')](function(_0x2281e9){_0x29ff2d[_0xb11b('0x3f')]=_0x2281e9;return _0x29ff2d;})[_0xb11b('0x21')](respondWithFilteredResult(_0x2f389e,_0x31b492))[_0xb11b('0x40')](handleError(_0x2f389e,null));};exports[_0xb11b('0x41')]=function(_0x564b56,_0x3f037c){var _0xd86cc4={'raw':![],'where':{'id':_0x564b56[_0xb11b('0x42')]['id']}},_0x3dc72b={};_0x3dc72b[_0xb11b('0x30')]=_[_0xb11b('0x2d')](db[_0xb11b('0x3b')]['rawAttributes']);_0x3dc72b[_0xb11b('0x2c')]=_[_0xb11b('0x2d')](_0x564b56[_0xb11b('0x2c')]);_0x3dc72b[_0xb11b('0x37')]=_[_0xb11b('0x2e')](_0x3dc72b[_0xb11b('0x30')],_0x3dc72b[_0xb11b('0x2c')]);_0xd86cc4[_0xb11b('0x2f')]=_[_0xb11b('0x2e')](_0x3dc72b[_0xb11b('0x30')],qs[_0xb11b('0x31')](_0x564b56[_0xb11b('0x2c')][_0xb11b('0x31')]));_0xd86cc4[_0xb11b('0x2f')]=_0xd86cc4['attributes']['length']?_0xd86cc4[_0xb11b('0x2f')]:_0x3dc72b[_0xb11b('0x30')];if(_0x564b56[_0xb11b('0x2c')][_0xb11b('0x3c')]){_0xd86cc4[_0xb11b('0x3d')]=[{'all':!![]}];}_0xd86cc4=_[_0xb11b('0x3a')]({},_0xd86cc4,_0x564b56[_0xb11b('0x43')]);return db['FreshsalesConfiguration'][_0xb11b('0x44')](_0xd86cc4)['then'](handleEntityNotFound(_0x3f037c,null))[_0xb11b('0x21')](respondWithResult(_0x3f037c,null))[_0xb11b('0x40')](handleError(_0x3f037c,null));};exports[_0xb11b('0x45')]=function(_0x524bd9,_0xe407cb){return db[_0xb11b('0x3b')][_0xb11b('0x45')](_0x524bd9[_0xb11b('0x46')],{})[_0xb11b('0x21')](respondWithResult(_0xe407cb,0xc9))['catch'](handleError(_0xe407cb,null));};exports[_0xb11b('0x20')]=function(_0x2647f3,_0x11b9d4){if(_0x2647f3['body']['id']){delete _0x2647f3[_0xb11b('0x46')]['id'];}return db[_0xb11b('0x3b')]['find']({'where':{'id':_0x2647f3['params']['id']}})[_0xb11b('0x21')](handleEntityNotFound(_0x11b9d4,null))[_0xb11b('0x21')](saveUpdates(_0x2647f3[_0xb11b('0x46')],null))[_0xb11b('0x21')](respondWithResult(_0x11b9d4,null))[_0xb11b('0x40')](handleError(_0x11b9d4,null));};exports[_0xb11b('0x47')]=function(_0x4dae13,_0x5a14e7){return db[_0xb11b('0x3b')]['find']({'where':{'id':_0x4dae13[_0xb11b('0x42')]['id']}})[_0xb11b('0x21')](handleEntityNotFound(_0x5a14e7,null))['then'](removeEntity(_0x5a14e7,null))[_0xb11b('0x40')](handleError(_0x5a14e7,null));};exports[_0xb11b('0x48')]=function(_0x158583,_0x508708,_0x4620c7){var _0x10049a={};var _0x4ee8b3={};var _0x15c82c;var _0x4ef6f7;return db['FreshsalesConfiguration'][_0xb11b('0x49')]({'where':{'id':_0x158583['params']['id']}})['then'](handleEntityNotFound(_0x508708,null))[_0xb11b('0x21')](function(_0x2e5ee5){if(_0x2e5ee5){_0x15c82c=_0x2e5ee5;_0x4ee8b3[_0xb11b('0x30')]=_['keys'](db['FreshsalesField'][_0xb11b('0x29')]);_0x4ee8b3['query']=_[_0xb11b('0x2d')](_0x158583[_0xb11b('0x2c')]);_0x4ee8b3[_0xb11b('0x37')]=_[_0xb11b('0x2e')](_0x4ee8b3[_0xb11b('0x30')],_0x4ee8b3['query']);_0x10049a[_0xb11b('0x2f')]=_[_0xb11b('0x2e')](_0x4ee8b3['model'],qs[_0xb11b('0x31')](_0x158583[_0xb11b('0x2c')][_0xb11b('0x31')]));_0x10049a[_0xb11b('0x2f')]=_0x10049a[_0xb11b('0x2f')][_0xb11b('0x32')]?_0x10049a[_0xb11b('0x2f')]:_0x4ee8b3[_0xb11b('0x30')];_0x10049a[_0xb11b('0x34')]=qs['sort'](_0x158583[_0xb11b('0x2c')]['sort']);_0x10049a[_0xb11b('0x36')]=qs[_0xb11b('0x37')](_[_0xb11b('0x38')](_0x158583['query'],_0x4ee8b3['filters']));if(_0x158583[_0xb11b('0x2c')][_0xb11b('0x39')]){_0x10049a[_0xb11b('0x36')]=_[_0xb11b('0x3a')](_0x10049a['where'],{'$or':_[_0xb11b('0x28')](_0x10049a['attributes'],function(_0x4cdd56){var _0x4e0e5b={};_0x4e0e5b[_0x4cdd56]={'$like':'%'+_0x158583['query'][_0xb11b('0x39')]+'%'};return _0x4e0e5b;})});}_0x10049a=_[_0xb11b('0x3a')]({},_0x10049a,_0x158583[_0xb11b('0x43')]);return _0x15c82c[_0xb11b('0x48')](_0x10049a);}})['then'](function(_0xd9996d){if(_0xd9996d){_0x4ef6f7=_0xd9996d[_0xb11b('0x32')];if(!_0x158583[_0xb11b('0x2c')]['hasOwnProperty'](_0xb11b('0x4a'))){_0x10049a[_0xb11b('0x1b')]=qs['limit'](_0x158583[_0xb11b('0x2c')]['limit']);_0x10049a[_0xb11b('0x18')]=qs[_0xb11b('0x18')](_0x158583[_0xb11b('0x2c')][_0xb11b('0x18')]);}return _0x15c82c['getSubjects'](_0x10049a);}})[_0xb11b('0x21')](function(_0xbe13fc){if(_0xbe13fc){return _0xbe13fc?{'count':_0x4ef6f7,'rows':_0xbe13fc}:null;}})[_0xb11b('0x21')](respondWithResult(_0x508708,null))[_0xb11b('0x40')](handleError(_0x508708,null));};exports[_0xb11b('0x4b')]=function(_0x3c853b,_0x30637b,_0x2adc2e){var _0x144085={};var _0x5a3e1c={};var _0x48c2ae;var _0x3e2a05;return db[_0xb11b('0x3b')][_0xb11b('0x49')]({'where':{'id':_0x3c853b[_0xb11b('0x42')]['id']}})[_0xb11b('0x21')](handleEntityNotFound(_0x30637b,null))[_0xb11b('0x21')](function(_0x256c6e){if(_0x256c6e){_0x48c2ae=_0x256c6e;_0x5a3e1c[_0xb11b('0x30')]=_['keys'](db[_0xb11b('0x4c')][_0xb11b('0x29')]);_0x5a3e1c[_0xb11b('0x2c')]=_[_0xb11b('0x2d')](_0x3c853b[_0xb11b('0x2c')]);_0x5a3e1c[_0xb11b('0x37')]=_[_0xb11b('0x2e')](_0x5a3e1c[_0xb11b('0x30')],_0x5a3e1c[_0xb11b('0x2c')]);_0x144085[_0xb11b('0x2f')]=_[_0xb11b('0x2e')](_0x5a3e1c[_0xb11b('0x30')],qs[_0xb11b('0x31')](_0x3c853b[_0xb11b('0x2c')][_0xb11b('0x31')]));_0x144085['attributes']=_0x144085[_0xb11b('0x2f')][_0xb11b('0x32')]?_0x144085['attributes']:_0x5a3e1c[_0xb11b('0x30')];_0x144085[_0xb11b('0x34')]=qs[_0xb11b('0x35')](_0x3c853b[_0xb11b('0x2c')][_0xb11b('0x35')]);_0x144085[_0xb11b('0x36')]=qs[_0xb11b('0x37')](_[_0xb11b('0x38')](_0x3c853b[_0xb11b('0x2c')],_0x5a3e1c[_0xb11b('0x37')]));if(_0x3c853b['query'][_0xb11b('0x39')]){_0x144085[_0xb11b('0x36')]=_[_0xb11b('0x3a')](_0x144085['where'],{'$or':_['map'](_0x144085[_0xb11b('0x2f')],function(_0x38be60){var _0x28b0b4={};_0x28b0b4[_0x38be60]={'$like':'%'+_0x3c853b[_0xb11b('0x2c')][_0xb11b('0x39')]+'%'};return _0x28b0b4;})});}_0x144085=_[_0xb11b('0x3a')]({},_0x144085,_0x3c853b[_0xb11b('0x43')]);return _0x48c2ae[_0xb11b('0x4b')](_0x144085);}})[_0xb11b('0x21')](function(_0x4cc3b2){if(_0x4cc3b2){_0x3e2a05=_0x4cc3b2['length'];if(!_0x3c853b['query'][_0xb11b('0x33')](_0xb11b('0x4a'))){_0x144085[_0xb11b('0x1b')]=qs[_0xb11b('0x1b')](_0x3c853b['query'][_0xb11b('0x1b')]);_0x144085[_0xb11b('0x18')]=qs[_0xb11b('0x18')](_0x3c853b['query'][_0xb11b('0x18')]);}return _0x48c2ae[_0xb11b('0x4b')](_0x144085);}})[_0xb11b('0x21')](function(_0x5cc4ce){if(_0x5cc4ce){return _0x5cc4ce?{'count':_0x3e2a05,'rows':_0x5cc4ce}:null;}})[_0xb11b('0x21')](respondWithResult(_0x30637b,null))['catch'](handleError(_0x30637b,null));};exports[_0xb11b('0x4d')]=function(_0x55d5c9,_0x1546de,_0x15b1f3){var _0x33a7af={};var _0x4340c6={};var _0xfe8431;var _0x568fa2;return db[_0xb11b('0x3b')]['findOne']({'where':{'id':_0x55d5c9['params']['id']}})['then'](handleEntityNotFound(_0x1546de,null))[_0xb11b('0x21')](function(_0x52cfe5){if(_0x52cfe5){_0xfe8431=_0x52cfe5;_0x4340c6[_0xb11b('0x30')]=_[_0xb11b('0x2d')](db[_0xb11b('0x4c')][_0xb11b('0x29')]);_0x4340c6[_0xb11b('0x2c')]=_[_0xb11b('0x2d')](_0x55d5c9[_0xb11b('0x2c')]);_0x4340c6[_0xb11b('0x37')]=_[_0xb11b('0x2e')](_0x4340c6['model'],_0x4340c6[_0xb11b('0x2c')]);_0x33a7af[_0xb11b('0x2f')]=_[_0xb11b('0x2e')](_0x4340c6[_0xb11b('0x30')],qs[_0xb11b('0x31')](_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x31')]));_0x33a7af[_0xb11b('0x2f')]=_0x33a7af['attributes']['length']?_0x33a7af[_0xb11b('0x2f')]:_0x4340c6[_0xb11b('0x30')];_0x33a7af[_0xb11b('0x34')]=qs['sort'](_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x35')]);_0x33a7af['where']=qs['filters'](_[_0xb11b('0x38')](_0x55d5c9[_0xb11b('0x2c')],_0x4340c6[_0xb11b('0x37')]));if(_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x39')]){_0x33a7af['where']=_[_0xb11b('0x3a')](_0x33a7af[_0xb11b('0x36')],{'$or':_[_0xb11b('0x28')](_0x33a7af[_0xb11b('0x2f')],function(_0x4ca2bd){var _0x363d2c={};_0x363d2c[_0x4ca2bd]={'$like':'%'+_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x39')]+'%'};return _0x363d2c;})});}_0x33a7af=_[_0xb11b('0x3a')]({},_0x33a7af,_0x55d5c9[_0xb11b('0x43')]);return _0xfe8431[_0xb11b('0x4d')](_0x33a7af);}})['then'](function(_0x4ddf3c){if(_0x4ddf3c){_0x568fa2=_0x4ddf3c[_0xb11b('0x32')];if(!_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x33')](_0xb11b('0x4a'))){_0x33a7af[_0xb11b('0x1b')]=qs['limit'](_0x55d5c9[_0xb11b('0x2c')][_0xb11b('0x1b')]);_0x33a7af['offset']=qs[_0xb11b('0x18')](_0x55d5c9['query']['offset']);}return _0xfe8431[_0xb11b('0x4d')](_0x33a7af);}})['then'](function(_0x23d5ee){if(_0x23d5ee){return _0x23d5ee?{'count':_0x568fa2,'rows':_0x23d5ee}:null;}})[_0xb11b('0x21')](respondWithResult(_0x1546de,null))[_0xb11b('0x40')](handleError(_0x1546de,null));};