Built motion from commit e1760111.|2.6.18
[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 _0x203f=['json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','send','map','FreshsalesConfiguration','fieldName','type','key','model','query','filters','intersection','fields','attributes','hasOwnProperty','nolimit','order','sort','where','filter','merge','VIRTUAL','includeAll','findAll','catch','show','params','keys','length','include','options','find','create','body','getSubjects','findOne','FreshsalesField','rawAttributes','getDescriptions','pick','getFields','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../mysqldb','sendStatus','end','status'];(function(_0x18032f,_0x4f4e45){var _0xb0b2b8=function(_0x563262){while(--_0x563262){_0x18032f['push'](_0x18032f['shift']());}};_0xb0b2b8(++_0x4f4e45);}(_0x203f,0x187));var _0xf203=function(_0x386c3e,_0x44a375){_0x386c3e=_0x386c3e-0x0;var _0x4b8e13=_0x203f[_0x386c3e];return _0x4b8e13;};'use strict';var emlformat=require(_0xf203('0x0'));var rimraf=require(_0xf203('0x1'));var zipdir=require(_0xf203('0x2'));var jsonpatch=require(_0xf203('0x3'));var rp=require(_0xf203('0x4'));var moment=require(_0xf203('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xf203('0x6'));var util=require('util');var path=require(_0xf203('0x7'));var sox=require('sox');var csv=require(_0xf203('0x8'));var ejs=require(_0xf203('0x9'));var fs=require('fs');var fs_extra=require(_0xf203('0xa'));var _=require(_0xf203('0xb'));var squel=require(_0xf203('0xc'));var crypto=require(_0xf203('0xd'));var jsforce=require(_0xf203('0xe'));var deskjs=require(_0xf203('0xf'));var toCsv=require('to-csv');var querystring=require(_0xf203('0x10'));var Papa=require(_0xf203('0x11'));var Redis=require(_0xf203('0x12'));var authService=require('../../components/auth/service');var qs=require(_0xf203('0x13'));var as=require(_0xf203('0x14'));var hardwareService=require(_0xf203('0x15'));var logger=require(_0xf203('0x16'))(_0xf203('0x17'));var utils=require('../../config/utils');var config=require(_0xf203('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0xf203('0x19'))['db'];function respondWithStatusCode(_0xd102c0,_0x53333a){_0x53333a=_0x53333a||0xcc;return function(_0x20512e){if(_0x20512e){return _0xd102c0[_0xf203('0x1a')](_0x53333a);}return _0xd102c0['status'](_0x53333a)[_0xf203('0x1b')]();};}function respondWithResult(_0x37bd4e,_0x228dea){_0x228dea=_0x228dea||0xc8;return function(_0x222dc7){if(_0x222dc7){return _0x37bd4e[_0xf203('0x1c')](_0x228dea)[_0xf203('0x1d')](_0x222dc7);}};}function respondWithFilteredResult(_0x1f43fc,_0x237ad3){return function(_0x5e9a3c){if(_0x5e9a3c){var _0x1f61e8=typeof _0x237ad3[_0xf203('0x1e')]===_0xf203('0x1f')&&typeof _0x237ad3[_0xf203('0x20')]===_0xf203('0x1f');var _0x5e48d4=_0x5e9a3c[_0xf203('0x21')];var _0x244d3e=_0x1f61e8?0x0:_0x237ad3[_0xf203('0x1e')];var _0x390d4c=_0x1f61e8?_0x5e9a3c[_0xf203('0x21')]:_0x237ad3[_0xf203('0x1e')]+_0x237ad3[_0xf203('0x20')];var _0x10700f;if(_0x390d4c>=_0x5e48d4){_0x390d4c=_0x5e48d4;_0x10700f=0xc8;}else{_0x10700f=0xce;}_0x1f43fc[_0xf203('0x1c')](_0x10700f);return _0x1f43fc[_0xf203('0x22')](_0xf203('0x23'),_0x244d3e+'-'+_0x390d4c+'/'+_0x5e48d4)[_0xf203('0x1d')](_0x5e9a3c);}return null;};}function patchUpdates(_0xc4013a){return function(_0x191e04){try{jsonpatch[_0xf203('0x24')](_0x191e04,_0xc4013a,!![]);}catch(_0x1ee230){return BPromise[_0xf203('0x25')](_0x1ee230);}return _0x191e04[_0xf203('0x26')]();};}function saveUpdates(_0x4cd262,_0x10a8d8){return function(_0x4d98f2){if(_0x4d98f2){return _0x4d98f2[_0xf203('0x27')](_0x4cd262)[_0xf203('0x28')](function(_0x599b3c){return _0x599b3c;});}return null;};}function removeEntity(_0x31a13d,_0x3f3f12){return function(_0x3ead04){if(_0x3ead04){return _0x3ead04[_0xf203('0x29')]()[_0xf203('0x28')](function(){_0x31a13d['status'](0xcc)[_0xf203('0x1b')]();});}};}function handleEntityNotFound(_0x5ae2c0,_0xf61203){return function(_0x326f1b){if(!_0x326f1b){_0x5ae2c0[_0xf203('0x1a')](0x194);}return _0x326f1b;};}function handleError(_0x2ddf48,_0xbd8483){_0xbd8483=_0xbd8483||0x1f4;return function(_0x118aee){logger[_0xf203('0x2a')](_0x118aee[_0xf203('0x2b')]);if(_0x118aee['name']){delete _0x118aee[_0xf203('0x2c')];}_0x2ddf48['status'](_0xbd8483)[_0xf203('0x2d')](_0x118aee);};}exports['index']=function(_0x2bf5fa,_0x44fa0e){var _0x52a0b8={},_0xbd288c={},_0x1b4783={'count':0x0,'rows':[]};var _0x5ecd3f=_[_0xf203('0x2e')](db[_0xf203('0x2f')]['rawAttributes'],function(_0x42b257){return{'name':_0x42b257[_0xf203('0x30')],'type':_0x42b257[_0xf203('0x31')][_0xf203('0x32')]};});_0xbd288c[_0xf203('0x33')]=_['map'](_0x5ecd3f,_0xf203('0x2c'));_0xbd288c['query']=_['keys'](_0x2bf5fa[_0xf203('0x34')]);_0xbd288c[_0xf203('0x35')]=_['intersection'](_0xbd288c['model'],_0xbd288c['query']);_0x52a0b8['attributes']=_[_0xf203('0x36')](_0xbd288c['model'],qs[_0xf203('0x37')](_0x2bf5fa[_0xf203('0x34')][_0xf203('0x37')]));_0x52a0b8['attributes']=_0x52a0b8[_0xf203('0x38')]['length']?_0x52a0b8[_0xf203('0x38')]:_0xbd288c[_0xf203('0x33')];if(!_0x2bf5fa[_0xf203('0x34')][_0xf203('0x39')](_0xf203('0x3a'))){_0x52a0b8[_0xf203('0x20')]=qs[_0xf203('0x20')](_0x2bf5fa['query'][_0xf203('0x20')]);_0x52a0b8[_0xf203('0x1e')]=qs[_0xf203('0x1e')](_0x2bf5fa[_0xf203('0x34')][_0xf203('0x1e')]);}_0x52a0b8[_0xf203('0x3b')]=qs[_0xf203('0x3c')](_0x2bf5fa['query'][_0xf203('0x3c')]);_0x52a0b8[_0xf203('0x3d')]=qs[_0xf203('0x35')](_['pick'](_0x2bf5fa[_0xf203('0x34')],_0xbd288c[_0xf203('0x35')]),_0x5ecd3f);if(_0x2bf5fa[_0xf203('0x34')][_0xf203('0x3e')]){_0x52a0b8[_0xf203('0x3d')]=_[_0xf203('0x3f')](_0x52a0b8[_0xf203('0x3d')],{'$or':_[_0xf203('0x2e')](_0x5ecd3f,function(_0x332c0f){if(_0x332c0f[_0xf203('0x31')]!==_0xf203('0x40')){var _0x1271a1={};_0x1271a1[_0x332c0f[_0xf203('0x2c')]]={'$like':'%'+_0x2bf5fa[_0xf203('0x34')][_0xf203('0x3e')]+'%'};return _0x1271a1;}})});}_0x52a0b8=_[_0xf203('0x3f')]({},_0x52a0b8,_0x2bf5fa['options']);var _0x2e4ad0={'where':_0x52a0b8[_0xf203('0x3d')]};return db['FreshsalesConfiguration'][_0xf203('0x21')](_0x2e4ad0)['then'](function(_0x330bbd){_0x1b4783['count']=_0x330bbd;if(_0x2bf5fa[_0xf203('0x34')][_0xf203('0x41')]){_0x52a0b8['include']=[{'all':!![]}];}return db[_0xf203('0x2f')][_0xf203('0x42')](_0x52a0b8);})['then'](function(_0x1a2336){_0x1b4783['rows']=_0x1a2336;return _0x1b4783;})[_0xf203('0x28')](respondWithFilteredResult(_0x44fa0e,_0x52a0b8))[_0xf203('0x43')](handleError(_0x44fa0e,null));};exports[_0xf203('0x44')]=function(_0x252b9a,_0x2f21fb){var _0x3f880d={'raw':![],'where':{'id':_0x252b9a[_0xf203('0x45')]['id']}},_0x397827={};_0x397827[_0xf203('0x33')]=_[_0xf203('0x46')](db[_0xf203('0x2f')]['rawAttributes']);_0x397827['query']=_[_0xf203('0x46')](_0x252b9a['query']);_0x397827[_0xf203('0x35')]=_[_0xf203('0x36')](_0x397827[_0xf203('0x33')],_0x397827['query']);_0x3f880d[_0xf203('0x38')]=_[_0xf203('0x36')](_0x397827[_0xf203('0x33')],qs[_0xf203('0x37')](_0x252b9a[_0xf203('0x34')][_0xf203('0x37')]));_0x3f880d['attributes']=_0x3f880d[_0xf203('0x38')][_0xf203('0x47')]?_0x3f880d['attributes']:_0x397827[_0xf203('0x33')];if(_0x252b9a[_0xf203('0x34')]['includeAll']){_0x3f880d[_0xf203('0x48')]=[{'all':!![]}];}_0x3f880d=_[_0xf203('0x3f')]({},_0x3f880d,_0x252b9a[_0xf203('0x49')]);return db[_0xf203('0x2f')][_0xf203('0x4a')](_0x3f880d)[_0xf203('0x28')](handleEntityNotFound(_0x2f21fb,null))[_0xf203('0x28')](respondWithResult(_0x2f21fb,null))['catch'](handleError(_0x2f21fb,null));};exports[_0xf203('0x4b')]=function(_0x446294,_0x1c9c45){return db[_0xf203('0x2f')][_0xf203('0x4b')](_0x446294[_0xf203('0x4c')],{})['then'](respondWithResult(_0x1c9c45,0xc9))[_0xf203('0x43')](handleError(_0x1c9c45,null));};exports[_0xf203('0x27')]=function(_0x3443b9,_0x846b3d){if(_0x3443b9['body']['id']){delete _0x3443b9[_0xf203('0x4c')]['id'];}return db[_0xf203('0x2f')][_0xf203('0x4a')]({'where':{'id':_0x3443b9['params']['id']}})['then'](handleEntityNotFound(_0x846b3d,null))[_0xf203('0x28')](saveUpdates(_0x3443b9[_0xf203('0x4c')],null))[_0xf203('0x28')](respondWithResult(_0x846b3d,null))[_0xf203('0x43')](handleError(_0x846b3d,null));};exports[_0xf203('0x29')]=function(_0x4b61c2,_0xdd3833){return db['FreshsalesConfiguration']['find']({'where':{'id':_0x4b61c2['params']['id']}})[_0xf203('0x28')](handleEntityNotFound(_0xdd3833,null))[_0xf203('0x28')](removeEntity(_0xdd3833,null))[_0xf203('0x43')](handleError(_0xdd3833,null));};exports[_0xf203('0x4d')]=function(_0x2265a8,_0x4a7290,_0x4d44a2){var _0x5230a6={};var _0x3c5a36={};var _0x409968;var _0x2d42d2;return db[_0xf203('0x2f')][_0xf203('0x4e')]({'where':{'id':_0x2265a8[_0xf203('0x45')]['id']}})[_0xf203('0x28')](handleEntityNotFound(_0x4a7290,null))[_0xf203('0x28')](function(_0x5abf53){if(_0x5abf53){_0x409968=_0x5abf53;_0x3c5a36[_0xf203('0x33')]=_[_0xf203('0x46')](db[_0xf203('0x4f')][_0xf203('0x50')]);_0x3c5a36[_0xf203('0x34')]=_[_0xf203('0x46')](_0x2265a8[_0xf203('0x34')]);_0x3c5a36[_0xf203('0x35')]=_['intersection'](_0x3c5a36['model'],_0x3c5a36['query']);_0x5230a6[_0xf203('0x38')]=_[_0xf203('0x36')](_0x3c5a36[_0xf203('0x33')],qs[_0xf203('0x37')](_0x2265a8[_0xf203('0x34')][_0xf203('0x37')]));_0x5230a6['attributes']=_0x5230a6['attributes'][_0xf203('0x47')]?_0x5230a6['attributes']:_0x3c5a36['model'];_0x5230a6[_0xf203('0x3b')]=qs[_0xf203('0x3c')](_0x2265a8[_0xf203('0x34')][_0xf203('0x3c')]);_0x5230a6[_0xf203('0x3d')]=qs[_0xf203('0x35')](_['pick'](_0x2265a8[_0xf203('0x34')],_0x3c5a36[_0xf203('0x35')]));if(_0x2265a8[_0xf203('0x34')]['filter']){_0x5230a6['where']=_[_0xf203('0x3f')](_0x5230a6['where'],{'$or':_['map'](_0x5230a6['attributes'],function(_0x50eaee){var _0x186b65={};_0x186b65[_0x50eaee]={'$like':'%'+_0x2265a8[_0xf203('0x34')]['filter']+'%'};return _0x186b65;})});}_0x5230a6=_[_0xf203('0x3f')]({},_0x5230a6,_0x2265a8['options']);return _0x409968['getSubjects'](_0x5230a6);}})[_0xf203('0x28')](function(_0x22195f){if(_0x22195f){_0x2d42d2=_0x22195f['length'];if(!_0x2265a8[_0xf203('0x34')]['hasOwnProperty'](_0xf203('0x3a'))){_0x5230a6[_0xf203('0x20')]=qs[_0xf203('0x20')](_0x2265a8[_0xf203('0x34')][_0xf203('0x20')]);_0x5230a6['offset']=qs['offset'](_0x2265a8[_0xf203('0x34')][_0xf203('0x1e')]);}return _0x409968['getSubjects'](_0x5230a6);}})[_0xf203('0x28')](function(_0xca1c90){if(_0xca1c90){return _0xca1c90?{'count':_0x2d42d2,'rows':_0xca1c90}:null;}})[_0xf203('0x28')](respondWithResult(_0x4a7290,null))['catch'](handleError(_0x4a7290,null));};exports[_0xf203('0x51')]=function(_0x2fc732,_0x380eb4,_0x486f0c){var _0x1cf7a0={};var _0x48f70d={};var _0x53a14a;var _0x22ff54;return db[_0xf203('0x2f')][_0xf203('0x4e')]({'where':{'id':_0x2fc732[_0xf203('0x45')]['id']}})[_0xf203('0x28')](handleEntityNotFound(_0x380eb4,null))[_0xf203('0x28')](function(_0x137646){if(_0x137646){_0x53a14a=_0x137646;_0x48f70d[_0xf203('0x33')]=_[_0xf203('0x46')](db[_0xf203('0x4f')][_0xf203('0x50')]);_0x48f70d[_0xf203('0x34')]=_[_0xf203('0x46')](_0x2fc732[_0xf203('0x34')]);_0x48f70d['filters']=_[_0xf203('0x36')](_0x48f70d['model'],_0x48f70d['query']);_0x1cf7a0[_0xf203('0x38')]=_[_0xf203('0x36')](_0x48f70d['model'],qs[_0xf203('0x37')](_0x2fc732[_0xf203('0x34')][_0xf203('0x37')]));_0x1cf7a0[_0xf203('0x38')]=_0x1cf7a0[_0xf203('0x38')][_0xf203('0x47')]?_0x1cf7a0[_0xf203('0x38')]:_0x48f70d['model'];_0x1cf7a0[_0xf203('0x3b')]=qs['sort'](_0x2fc732[_0xf203('0x34')][_0xf203('0x3c')]);_0x1cf7a0[_0xf203('0x3d')]=qs['filters'](_[_0xf203('0x52')](_0x2fc732[_0xf203('0x34')],_0x48f70d[_0xf203('0x35')]));if(_0x2fc732['query'][_0xf203('0x3e')]){_0x1cf7a0[_0xf203('0x3d')]=_[_0xf203('0x3f')](_0x1cf7a0[_0xf203('0x3d')],{'$or':_['map'](_0x1cf7a0[_0xf203('0x38')],function(_0x136ca2){var _0x55fc0e={};_0x55fc0e[_0x136ca2]={'$like':'%'+_0x2fc732[_0xf203('0x34')][_0xf203('0x3e')]+'%'};return _0x55fc0e;})});}_0x1cf7a0=_[_0xf203('0x3f')]({},_0x1cf7a0,_0x2fc732[_0xf203('0x49')]);return _0x53a14a['getDescriptions'](_0x1cf7a0);}})[_0xf203('0x28')](function(_0x32289f){if(_0x32289f){_0x22ff54=_0x32289f[_0xf203('0x47')];if(!_0x2fc732[_0xf203('0x34')]['hasOwnProperty'](_0xf203('0x3a'))){_0x1cf7a0[_0xf203('0x20')]=qs[_0xf203('0x20')](_0x2fc732[_0xf203('0x34')][_0xf203('0x20')]);_0x1cf7a0['offset']=qs[_0xf203('0x1e')](_0x2fc732[_0xf203('0x34')]['offset']);}return _0x53a14a[_0xf203('0x51')](_0x1cf7a0);}})[_0xf203('0x28')](function(_0x382550){if(_0x382550){return _0x382550?{'count':_0x22ff54,'rows':_0x382550}:null;}})[_0xf203('0x28')](respondWithResult(_0x380eb4,null))[_0xf203('0x43')](handleError(_0x380eb4,null));};exports[_0xf203('0x53')]=function(_0x4eb441,_0x2ef3bd,_0x40c628){var _0x1ad5bc={};var _0x18b283={};var _0x112f46;var _0x388bc5;return db[_0xf203('0x2f')][_0xf203('0x4e')]({'where':{'id':_0x4eb441[_0xf203('0x45')]['id']}})['then'](handleEntityNotFound(_0x2ef3bd,null))[_0xf203('0x28')](function(_0x730521){if(_0x730521){_0x112f46=_0x730521;_0x18b283[_0xf203('0x33')]=_['keys'](db['FreshsalesField'][_0xf203('0x50')]);_0x18b283['query']=_[_0xf203('0x46')](_0x4eb441[_0xf203('0x34')]);_0x18b283['filters']=_[_0xf203('0x36')](_0x18b283['model'],_0x18b283[_0xf203('0x34')]);_0x1ad5bc[_0xf203('0x38')]=_['intersection'](_0x18b283['model'],qs[_0xf203('0x37')](_0x4eb441[_0xf203('0x34')]['fields']));_0x1ad5bc[_0xf203('0x38')]=_0x1ad5bc[_0xf203('0x38')][_0xf203('0x47')]?_0x1ad5bc[_0xf203('0x38')]:_0x18b283[_0xf203('0x33')];_0x1ad5bc[_0xf203('0x3b')]=qs[_0xf203('0x3c')](_0x4eb441[_0xf203('0x34')][_0xf203('0x3c')]);_0x1ad5bc[_0xf203('0x3d')]=qs[_0xf203('0x35')](_[_0xf203('0x52')](_0x4eb441['query'],_0x18b283[_0xf203('0x35')]));if(_0x4eb441[_0xf203('0x34')][_0xf203('0x3e')]){_0x1ad5bc[_0xf203('0x3d')]=_['merge'](_0x1ad5bc[_0xf203('0x3d')],{'$or':_[_0xf203('0x2e')](_0x1ad5bc[_0xf203('0x38')],function(_0x6f1146){var _0x426a54={};_0x426a54[_0x6f1146]={'$like':'%'+_0x4eb441[_0xf203('0x34')]['filter']+'%'};return _0x426a54;})});}_0x1ad5bc=_['merge']({},_0x1ad5bc,_0x4eb441[_0xf203('0x49')]);return _0x112f46[_0xf203('0x53')](_0x1ad5bc);}})[_0xf203('0x28')](function(_0x475cac){if(_0x475cac){_0x388bc5=_0x475cac['length'];if(!_0x4eb441[_0xf203('0x34')][_0xf203('0x39')](_0xf203('0x3a'))){_0x1ad5bc[_0xf203('0x20')]=qs[_0xf203('0x20')](_0x4eb441['query'][_0xf203('0x20')]);_0x1ad5bc['offset']=qs['offset'](_0x4eb441[_0xf203('0x34')][_0xf203('0x1e')]);}return _0x112f46[_0xf203('0x53')](_0x1ad5bc);}})['then'](function(_0xce3e89){if(_0xce3e89){return _0xce3e89?{'count':_0x388bc5,'rows':_0xce3e89}:null;}})[_0xf203('0x28')](respondWithResult(_0x2ef3bd,null))[_0xf203('0x43')](handleError(_0x2ef3bd,null));};