Built motion from commit 0900f080.|2.5.45
[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 _0xbf4c=['mustache','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','undefined','limit','count','offset','set','Content-Range','apply','reject','then','destroy','stack','name','send','index','map','rawAttributes','fieldName','model','query','keys','filters','attributes','intersection','fields','length','nolimit','order','sort','where','merge','type','includeAll','FreshsalesConfiguration','findAll','rows','catch','show','params','find','create','body','getSubjects','findOne','FreshsalesField','filter','options','hasOwnProperty','pick','getDescriptions','getFields','eml-format','fast-json-patch','request-promise','moment','bluebird'];(function(_0x18fa61,_0x1d2fb3){var _0x45c0e9=function(_0x93678a){while(--_0x93678a){_0x18fa61['push'](_0x18fa61['shift']());}};_0x45c0e9(++_0x1d2fb3);}(_0xbf4c,0x1d5));var _0xcbf4=function(_0x78e2a4,_0x50bf51){_0x78e2a4=_0x78e2a4-0x0;var _0x307426=_0xbf4c[_0x78e2a4];return _0x307426;};'use strict';var emlformat=require(_0xcbf4('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xcbf4('0x1'));var rp=require(_0xcbf4('0x2'));var moment=require(_0xcbf4('0x3'));var BPromise=require(_0xcbf4('0x4'));var Mustache=require(_0xcbf4('0x5'));var util=require('util');var path=require('path');var sox=require(_0xcbf4('0x6'));var csv=require(_0xcbf4('0x7'));var ejs=require(_0xcbf4('0x8'));var fs=require('fs');var fs_extra=require(_0xcbf4('0x9'));var _=require(_0xcbf4('0xa'));var squel=require('squel');var crypto=require(_0xcbf4('0xb'));var jsforce=require(_0xcbf4('0xc'));var deskjs=require(_0xcbf4('0xd'));var toCsv=require(_0xcbf4('0x7'));var querystring=require(_0xcbf4('0xe'));var Papa=require(_0xcbf4('0xf'));var Redis=require('ioredis');var authService=require(_0xcbf4('0x10'));var qs=require(_0xcbf4('0x11'));var as=require(_0xcbf4('0x12'));var hardwareService=require(_0xcbf4('0x13'));var logger=require(_0xcbf4('0x14'))('api');var utils=require(_0xcbf4('0x15'));var config=require(_0xcbf4('0x16'));var licenseUtil=require(_0xcbf4('0x17'));var db=require(_0xcbf4('0x18'))['db'];function respondWithStatusCode(_0x29ecb8,_0x419c88){_0x419c88=_0x419c88||0xcc;return function(_0x111ac0){if(_0x111ac0){return _0x29ecb8[_0xcbf4('0x19')](_0x419c88);}return _0x29ecb8['status'](_0x419c88)[_0xcbf4('0x1a')]();};}function respondWithResult(_0x9ba6da,_0x52f5a5){_0x52f5a5=_0x52f5a5||0xc8;return function(_0x97b6bc){if(_0x97b6bc){return _0x9ba6da[_0xcbf4('0x1b')](_0x52f5a5)[_0xcbf4('0x1c')](_0x97b6bc);}};}function respondWithFilteredResult(_0x5cbeac,_0x26021d){return function(_0x53c429){if(_0x53c429){var _0x3a1ad8=typeof _0x26021d['offset']===_0xcbf4('0x1d')&&typeof _0x26021d[_0xcbf4('0x1e')]===_0xcbf4('0x1d');var _0x50c4d4=_0x53c429[_0xcbf4('0x1f')];var _0x21f0b1=_0x3a1ad8?0x0:_0x26021d[_0xcbf4('0x20')];var _0x283338=_0x3a1ad8?_0x53c429[_0xcbf4('0x1f')]:_0x26021d['offset']+_0x26021d[_0xcbf4('0x1e')];var _0x15f4d5;if(_0x283338>=_0x50c4d4){_0x283338=_0x50c4d4;_0x15f4d5=0xc8;}else{_0x15f4d5=0xce;}_0x5cbeac[_0xcbf4('0x1b')](_0x15f4d5);return _0x5cbeac[_0xcbf4('0x21')](_0xcbf4('0x22'),_0x21f0b1+'-'+_0x283338+'/'+_0x50c4d4)[_0xcbf4('0x1c')](_0x53c429);}return null;};}function patchUpdates(_0x251945){return function(_0x2327e5){try{jsonpatch[_0xcbf4('0x23')](_0x2327e5,_0x251945,!![]);}catch(_0x3dc938){return BPromise[_0xcbf4('0x24')](_0x3dc938);}return _0x2327e5['save']();};}function saveUpdates(_0x403eb5,_0x5efc4c){return function(_0x30b3fe){if(_0x30b3fe){return _0x30b3fe['update'](_0x403eb5)[_0xcbf4('0x25')](function(_0x2d75b2){return _0x2d75b2;});}return null;};}function removeEntity(_0xd4ced9,_0x235c08){return function(_0x1611c9){if(_0x1611c9){return _0x1611c9[_0xcbf4('0x26')]()[_0xcbf4('0x25')](function(){_0xd4ced9['status'](0xcc)[_0xcbf4('0x1a')]();});}};}function handleEntityNotFound(_0x5af3a6,_0x81520e){return function(_0x4cda92){if(!_0x4cda92){_0x5af3a6[_0xcbf4('0x19')](0x194);}return _0x4cda92;};}function handleError(_0x4f8542,_0x1e5431){_0x1e5431=_0x1e5431||0x1f4;return function(_0x5d0292){logger['error'](_0x5d0292[_0xcbf4('0x27')]);if(_0x5d0292[_0xcbf4('0x28')]){delete _0x5d0292['name'];}_0x4f8542[_0xcbf4('0x1b')](_0x1e5431)[_0xcbf4('0x29')](_0x5d0292);};}exports[_0xcbf4('0x2a')]=function(_0xaa5414,_0xbca956){var _0x558a75={},_0x2a4f6b={},_0x359ec6={'count':0x0,'rows':[]};var _0x5780b7=_[_0xcbf4('0x2b')](db['FreshsalesConfiguration'][_0xcbf4('0x2c')],function(_0xacb2e5){return{'name':_0xacb2e5[_0xcbf4('0x2d')],'type':_0xacb2e5['type']['key']};});_0x2a4f6b[_0xcbf4('0x2e')]=_[_0xcbf4('0x2b')](_0x5780b7,'name');_0x2a4f6b[_0xcbf4('0x2f')]=_[_0xcbf4('0x30')](_0xaa5414[_0xcbf4('0x2f')]);_0x2a4f6b[_0xcbf4('0x31')]=_['intersection'](_0x2a4f6b[_0xcbf4('0x2e')],_0x2a4f6b[_0xcbf4('0x2f')]);_0x558a75[_0xcbf4('0x32')]=_[_0xcbf4('0x33')](_0x2a4f6b[_0xcbf4('0x2e')],qs['fields'](_0xaa5414[_0xcbf4('0x2f')][_0xcbf4('0x34')]));_0x558a75['attributes']=_0x558a75[_0xcbf4('0x32')][_0xcbf4('0x35')]?_0x558a75['attributes']:_0x2a4f6b[_0xcbf4('0x2e')];if(!_0xaa5414[_0xcbf4('0x2f')]['hasOwnProperty'](_0xcbf4('0x36'))){_0x558a75[_0xcbf4('0x1e')]=qs[_0xcbf4('0x1e')](_0xaa5414[_0xcbf4('0x2f')][_0xcbf4('0x1e')]);_0x558a75['offset']=qs[_0xcbf4('0x20')](_0xaa5414[_0xcbf4('0x2f')][_0xcbf4('0x20')]);}_0x558a75[_0xcbf4('0x37')]=qs[_0xcbf4('0x38')](_0xaa5414[_0xcbf4('0x2f')][_0xcbf4('0x38')]);_0x558a75[_0xcbf4('0x39')]=qs['filters'](_['pick'](_0xaa5414['query'],_0x2a4f6b[_0xcbf4('0x31')]),_0x5780b7);if(_0xaa5414[_0xcbf4('0x2f')]['filter']){_0x558a75[_0xcbf4('0x39')]=_[_0xcbf4('0x3a')](_0x558a75['where'],{'$or':_['map'](_0x5780b7,function(_0x47d5c2){if(_0x47d5c2[_0xcbf4('0x3b')]!=='VIRTUAL'){var _0x171e85={};_0x171e85[_0x47d5c2[_0xcbf4('0x28')]]={'$like':'%'+_0xaa5414[_0xcbf4('0x2f')]['filter']+'%'};return _0x171e85;}})});}_0x558a75=_[_0xcbf4('0x3a')]({},_0x558a75,_0xaa5414['options']);var _0x48e4f3={'where':_0x558a75[_0xcbf4('0x39')]};return db['FreshsalesConfiguration'][_0xcbf4('0x1f')](_0x48e4f3)[_0xcbf4('0x25')](function(_0x57d8b5){_0x359ec6[_0xcbf4('0x1f')]=_0x57d8b5;if(_0xaa5414[_0xcbf4('0x2f')][_0xcbf4('0x3c')]){_0x558a75['include']=[{'all':!![]}];}return db[_0xcbf4('0x3d')][_0xcbf4('0x3e')](_0x558a75);})[_0xcbf4('0x25')](function(_0x3c895e){_0x359ec6[_0xcbf4('0x3f')]=_0x3c895e;return _0x359ec6;})[_0xcbf4('0x25')](respondWithFilteredResult(_0xbca956,_0x558a75))[_0xcbf4('0x40')](handleError(_0xbca956,null));};exports[_0xcbf4('0x41')]=function(_0x4427ca,_0xbc26e1){var _0x4707c0={'raw':![],'where':{'id':_0x4427ca[_0xcbf4('0x42')]['id']}},_0x161c5b={};_0x161c5b[_0xcbf4('0x2e')]=_[_0xcbf4('0x30')](db[_0xcbf4('0x3d')][_0xcbf4('0x2c')]);_0x161c5b[_0xcbf4('0x2f')]=_[_0xcbf4('0x30')](_0x4427ca[_0xcbf4('0x2f')]);_0x161c5b[_0xcbf4('0x31')]=_['intersection'](_0x161c5b['model'],_0x161c5b[_0xcbf4('0x2f')]);_0x4707c0[_0xcbf4('0x32')]=_[_0xcbf4('0x33')](_0x161c5b[_0xcbf4('0x2e')],qs[_0xcbf4('0x34')](_0x4427ca[_0xcbf4('0x2f')][_0xcbf4('0x34')]));_0x4707c0[_0xcbf4('0x32')]=_0x4707c0[_0xcbf4('0x32')]['length']?_0x4707c0[_0xcbf4('0x32')]:_0x161c5b[_0xcbf4('0x2e')];if(_0x4427ca['query'][_0xcbf4('0x3c')]){_0x4707c0['include']=[{'all':!![]}];}_0x4707c0=_[_0xcbf4('0x3a')]({},_0x4707c0,_0x4427ca['options']);return db[_0xcbf4('0x3d')][_0xcbf4('0x43')](_0x4707c0)[_0xcbf4('0x25')](handleEntityNotFound(_0xbc26e1,null))[_0xcbf4('0x25')](respondWithResult(_0xbc26e1,null))[_0xcbf4('0x40')](handleError(_0xbc26e1,null));};exports[_0xcbf4('0x44')]=function(_0x55fe01,_0x4742e9){return db[_0xcbf4('0x3d')][_0xcbf4('0x44')](_0x55fe01[_0xcbf4('0x45')],{})[_0xcbf4('0x25')](respondWithResult(_0x4742e9,0xc9))[_0xcbf4('0x40')](handleError(_0x4742e9,null));};exports['update']=function(_0x31d256,_0x22b689){if(_0x31d256[_0xcbf4('0x45')]['id']){delete _0x31d256[_0xcbf4('0x45')]['id'];}return db[_0xcbf4('0x3d')][_0xcbf4('0x43')]({'where':{'id':_0x31d256[_0xcbf4('0x42')]['id']}})['then'](handleEntityNotFound(_0x22b689,null))[_0xcbf4('0x25')](saveUpdates(_0x31d256[_0xcbf4('0x45')],null))['then'](respondWithResult(_0x22b689,null))[_0xcbf4('0x40')](handleError(_0x22b689,null));};exports[_0xcbf4('0x26')]=function(_0x304701,_0x1d3271){return db[_0xcbf4('0x3d')][_0xcbf4('0x43')]({'where':{'id':_0x304701[_0xcbf4('0x42')]['id']}})['then'](handleEntityNotFound(_0x1d3271,null))['then'](removeEntity(_0x1d3271,null))[_0xcbf4('0x40')](handleError(_0x1d3271,null));};exports[_0xcbf4('0x46')]=function(_0xbf64b8,_0x253a87,_0x216d3d){var _0xec9c15={};var _0x4e18f8={};var _0x2de778;var _0x1e5d84;return db[_0xcbf4('0x3d')][_0xcbf4('0x47')]({'where':{'id':_0xbf64b8[_0xcbf4('0x42')]['id']}})[_0xcbf4('0x25')](handleEntityNotFound(_0x253a87,null))['then'](function(_0x449554){if(_0x449554){_0x2de778=_0x449554;_0x4e18f8[_0xcbf4('0x2e')]=_[_0xcbf4('0x30')](db[_0xcbf4('0x48')][_0xcbf4('0x2c')]);_0x4e18f8[_0xcbf4('0x2f')]=_[_0xcbf4('0x30')](_0xbf64b8[_0xcbf4('0x2f')]);_0x4e18f8['filters']=_['intersection'](_0x4e18f8[_0xcbf4('0x2e')],_0x4e18f8[_0xcbf4('0x2f')]);_0xec9c15[_0xcbf4('0x32')]=_[_0xcbf4('0x33')](_0x4e18f8[_0xcbf4('0x2e')],qs['fields'](_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x34')]));_0xec9c15[_0xcbf4('0x32')]=_0xec9c15[_0xcbf4('0x32')][_0xcbf4('0x35')]?_0xec9c15[_0xcbf4('0x32')]:_0x4e18f8[_0xcbf4('0x2e')];_0xec9c15[_0xcbf4('0x37')]=qs[_0xcbf4('0x38')](_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x38')]);_0xec9c15[_0xcbf4('0x39')]=qs['filters'](_['pick'](_0xbf64b8[_0xcbf4('0x2f')],_0x4e18f8[_0xcbf4('0x31')]));if(_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x49')]){_0xec9c15[_0xcbf4('0x39')]=_['merge'](_0xec9c15[_0xcbf4('0x39')],{'$or':_[_0xcbf4('0x2b')](_0xec9c15[_0xcbf4('0x32')],function(_0x36894b){var _0x1e4554={};_0x1e4554[_0x36894b]={'$like':'%'+_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x49')]+'%'};return _0x1e4554;})});}_0xec9c15=_['merge']({},_0xec9c15,_0xbf64b8[_0xcbf4('0x4a')]);return _0x2de778['getSubjects'](_0xec9c15);}})['then'](function(_0x3f0d57){if(_0x3f0d57){_0x1e5d84=_0x3f0d57['length'];if(!_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x4b')](_0xcbf4('0x36'))){_0xec9c15[_0xcbf4('0x1e')]=qs['limit'](_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x1e')]);_0xec9c15[_0xcbf4('0x20')]=qs[_0xcbf4('0x20')](_0xbf64b8[_0xcbf4('0x2f')][_0xcbf4('0x20')]);}return _0x2de778[_0xcbf4('0x46')](_0xec9c15);}})[_0xcbf4('0x25')](function(_0xf8da79){if(_0xf8da79){return _0xf8da79?{'count':_0x1e5d84,'rows':_0xf8da79}:null;}})[_0xcbf4('0x25')](respondWithResult(_0x253a87,null))[_0xcbf4('0x40')](handleError(_0x253a87,null));};exports['getDescriptions']=function(_0x81d527,_0x40bde1,_0x404619){var _0x3d75dd={};var _0x3fc596={};var _0x374dd5;var _0x439385;return db[_0xcbf4('0x3d')][_0xcbf4('0x47')]({'where':{'id':_0x81d527['params']['id']}})[_0xcbf4('0x25')](handleEntityNotFound(_0x40bde1,null))['then'](function(_0x542fbb){if(_0x542fbb){_0x374dd5=_0x542fbb;_0x3fc596[_0xcbf4('0x2e')]=_['keys'](db['FreshsalesField'][_0xcbf4('0x2c')]);_0x3fc596[_0xcbf4('0x2f')]=_[_0xcbf4('0x30')](_0x81d527[_0xcbf4('0x2f')]);_0x3fc596[_0xcbf4('0x31')]=_['intersection'](_0x3fc596[_0xcbf4('0x2e')],_0x3fc596[_0xcbf4('0x2f')]);_0x3d75dd[_0xcbf4('0x32')]=_[_0xcbf4('0x33')](_0x3fc596[_0xcbf4('0x2e')],qs[_0xcbf4('0x34')](_0x81d527['query'][_0xcbf4('0x34')]));_0x3d75dd[_0xcbf4('0x32')]=_0x3d75dd['attributes'][_0xcbf4('0x35')]?_0x3d75dd['attributes']:_0x3fc596[_0xcbf4('0x2e')];_0x3d75dd['order']=qs[_0xcbf4('0x38')](_0x81d527[_0xcbf4('0x2f')]['sort']);_0x3d75dd[_0xcbf4('0x39')]=qs['filters'](_[_0xcbf4('0x4c')](_0x81d527[_0xcbf4('0x2f')],_0x3fc596[_0xcbf4('0x31')]));if(_0x81d527['query']['filter']){_0x3d75dd[_0xcbf4('0x39')]=_[_0xcbf4('0x3a')](_0x3d75dd[_0xcbf4('0x39')],{'$or':_['map'](_0x3d75dd['attributes'],function(_0x3a8bfc){var _0x5d4d30={};_0x5d4d30[_0x3a8bfc]={'$like':'%'+_0x81d527[_0xcbf4('0x2f')][_0xcbf4('0x49')]+'%'};return _0x5d4d30;})});}_0x3d75dd=_[_0xcbf4('0x3a')]({},_0x3d75dd,_0x81d527['options']);return _0x374dd5[_0xcbf4('0x4d')](_0x3d75dd);}})[_0xcbf4('0x25')](function(_0x4a96db){if(_0x4a96db){_0x439385=_0x4a96db['length'];if(!_0x81d527[_0xcbf4('0x2f')][_0xcbf4('0x4b')]('nolimit')){_0x3d75dd[_0xcbf4('0x1e')]=qs[_0xcbf4('0x1e')](_0x81d527['query'][_0xcbf4('0x1e')]);_0x3d75dd[_0xcbf4('0x20')]=qs['offset'](_0x81d527[_0xcbf4('0x2f')][_0xcbf4('0x20')]);}return _0x374dd5[_0xcbf4('0x4d')](_0x3d75dd);}})['then'](function(_0x446b99){if(_0x446b99){return _0x446b99?{'count':_0x439385,'rows':_0x446b99}:null;}})['then'](respondWithResult(_0x40bde1,null))['catch'](handleError(_0x40bde1,null));};exports[_0xcbf4('0x4e')]=function(_0x13dd18,_0x50f4b2,_0x4d119a){var _0xb8c439={};var _0x345f45={};var _0x2101a6;var _0x403978;return db[_0xcbf4('0x3d')][_0xcbf4('0x47')]({'where':{'id':_0x13dd18[_0xcbf4('0x42')]['id']}})[_0xcbf4('0x25')](handleEntityNotFound(_0x50f4b2,null))[_0xcbf4('0x25')](function(_0x5d008e){if(_0x5d008e){_0x2101a6=_0x5d008e;_0x345f45[_0xcbf4('0x2e')]=_[_0xcbf4('0x30')](db['FreshsalesField'][_0xcbf4('0x2c')]);_0x345f45[_0xcbf4('0x2f')]=_['keys'](_0x13dd18[_0xcbf4('0x2f')]);_0x345f45[_0xcbf4('0x31')]=_['intersection'](_0x345f45[_0xcbf4('0x2e')],_0x345f45[_0xcbf4('0x2f')]);_0xb8c439[_0xcbf4('0x32')]=_[_0xcbf4('0x33')](_0x345f45['model'],qs['fields'](_0x13dd18[_0xcbf4('0x2f')][_0xcbf4('0x34')]));_0xb8c439[_0xcbf4('0x32')]=_0xb8c439[_0xcbf4('0x32')][_0xcbf4('0x35')]?_0xb8c439[_0xcbf4('0x32')]:_0x345f45[_0xcbf4('0x2e')];_0xb8c439['order']=qs['sort'](_0x13dd18['query']['sort']);_0xb8c439[_0xcbf4('0x39')]=qs['filters'](_[_0xcbf4('0x4c')](_0x13dd18[_0xcbf4('0x2f')],_0x345f45[_0xcbf4('0x31')]));if(_0x13dd18[_0xcbf4('0x2f')][_0xcbf4('0x49')]){_0xb8c439['where']=_[_0xcbf4('0x3a')](_0xb8c439['where'],{'$or':_['map'](_0xb8c439['attributes'],function(_0x213a82){var _0x3457cc={};_0x3457cc[_0x213a82]={'$like':'%'+_0x13dd18[_0xcbf4('0x2f')][_0xcbf4('0x49')]+'%'};return _0x3457cc;})});}_0xb8c439=_[_0xcbf4('0x3a')]({},_0xb8c439,_0x13dd18[_0xcbf4('0x4a')]);return _0x2101a6[_0xcbf4('0x4e')](_0xb8c439);}})['then'](function(_0x121b43){if(_0x121b43){_0x403978=_0x121b43[_0xcbf4('0x35')];if(!_0x13dd18['query'][_0xcbf4('0x4b')]('nolimit')){_0xb8c439[_0xcbf4('0x1e')]=qs[_0xcbf4('0x1e')](_0x13dd18[_0xcbf4('0x2f')][_0xcbf4('0x1e')]);_0xb8c439[_0xcbf4('0x20')]=qs['offset'](_0x13dd18[_0xcbf4('0x2f')][_0xcbf4('0x20')]);}return _0x2101a6[_0xcbf4('0x4e')](_0xb8c439);}})[_0xcbf4('0x25')](function(_0x586db6){if(_0x586db6){return _0x586db6?{'count':_0x403978,'rows':_0x586db6}:null;}})['then'](respondWithResult(_0x50f4b2,null))[_0xcbf4('0x40')](handleError(_0x50f4b2,null));};