838e974c4b1305f80bb7ad82ef318db759eb5ade
[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 _0xaf7b=['end','json','offset','undefined','count','set','apply','reject','save','update','then','destroy','stack','name','send','index','map','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','nolimit','limit','sort','where','pick','filter','VIRTUAL','merge','options','FreshsalesConfiguration','includeAll','findAll','rows','catch','show','params','rawAttributes','include','create','body','find','getSubjects','findOne','FreshsalesField','length','order','hasOwnProperty','getDescriptions','getFields','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','lodash','crypto','desk.js','../../components/parsers/qs','../../config/logger','../../config/environment','../../config/license/util','sendStatus','status'];(function(_0x1546eb,_0x1d1380){var _0x7b1d4f=function(_0x4fca24){while(--_0x4fca24){_0x1546eb['push'](_0x1546eb['shift']());}};_0x7b1d4f(++_0x1d1380);}(_0xaf7b,0x16c));var _0xbaf7=function(_0x14add3,_0x13ce8d){_0x14add3=_0x14add3-0x0;var _0x4a174f=_0xaf7b[_0x14add3];return _0x4a174f;};'use strict';var emlformat=require(_0xbaf7('0x0'));var rimraf=require(_0xbaf7('0x1'));var zipdir=require(_0xbaf7('0x2'));var jsonpatch=require(_0xbaf7('0x3'));var rp=require(_0xbaf7('0x4'));var moment=require(_0xbaf7('0x5'));var BPromise=require(_0xbaf7('0x6'));var Mustache=require(_0xbaf7('0x7'));var util=require(_0xbaf7('0x8'));var path=require(_0xbaf7('0x9'));var sox=require('sox');var csv=require(_0xbaf7('0xa'));var ejs=require(_0xbaf7('0xb'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbaf7('0xc'));var squel=require('squel');var crypto=require(_0xbaf7('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xbaf7('0xe'));var toCsv=require(_0xbaf7('0xa'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xbaf7('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0xbaf7('0x10'))('api');var utils=require('../../config/utils');var config=require(_0xbaf7('0x11'));var licenseUtil=require(_0xbaf7('0x12'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x104a60,_0x590e4d){_0x590e4d=_0x590e4d||0xcc;return function(_0x458ec1){if(_0x458ec1){return _0x104a60[_0xbaf7('0x13')](_0x590e4d);}return _0x104a60[_0xbaf7('0x14')](_0x590e4d)[_0xbaf7('0x15')]();};}function respondWithResult(_0x45cdd2,_0x1e679a){_0x1e679a=_0x1e679a||0xc8;return function(_0x437f39){if(_0x437f39){return _0x45cdd2[_0xbaf7('0x14')](_0x1e679a)[_0xbaf7('0x16')](_0x437f39);}};}function respondWithFilteredResult(_0x513cc7,_0x9b565e){return function(_0x326ac5){if(_0x326ac5){var _0x574b42=typeof _0x9b565e[_0xbaf7('0x17')]==='undefined'&&typeof _0x9b565e['limit']===_0xbaf7('0x18');var _0x476451=_0x326ac5[_0xbaf7('0x19')];var _0x3449e7=_0x574b42?0x0:_0x9b565e[_0xbaf7('0x17')];var _0x3ddffa=_0x574b42?_0x326ac5[_0xbaf7('0x19')]:_0x9b565e[_0xbaf7('0x17')]+_0x9b565e['limit'];var _0x441383;if(_0x3ddffa>=_0x476451){_0x3ddffa=_0x476451;_0x441383=0xc8;}else{_0x441383=0xce;}_0x513cc7['status'](_0x441383);return _0x513cc7[_0xbaf7('0x1a')]('Content-Range',_0x3449e7+'-'+_0x3ddffa+'/'+_0x476451)['json'](_0x326ac5);}return null;};}function patchUpdates(_0x55d7c1){return function(_0x30154b){try{jsonpatch[_0xbaf7('0x1b')](_0x30154b,_0x55d7c1,!![]);}catch(_0x5a7ce3){return BPromise[_0xbaf7('0x1c')](_0x5a7ce3);}return _0x30154b[_0xbaf7('0x1d')]();};}function saveUpdates(_0x359d45,_0x14d358){return function(_0x265244){if(_0x265244){return _0x265244[_0xbaf7('0x1e')](_0x359d45)[_0xbaf7('0x1f')](function(_0x30893c){return _0x30893c;});}return null;};}function removeEntity(_0x4eaa5d,_0x18a7a8){return function(_0x48157d){if(_0x48157d){return _0x48157d[_0xbaf7('0x20')]()['then'](function(){_0x4eaa5d[_0xbaf7('0x14')](0xcc)[_0xbaf7('0x15')]();});}};}function handleEntityNotFound(_0xecc152,_0x14e1e3){return function(_0x3f3f7a){if(!_0x3f3f7a){_0xecc152[_0xbaf7('0x13')](0x194);}return _0x3f3f7a;};}function handleError(_0x51e14b,_0x4bb914){_0x4bb914=_0x4bb914||0x1f4;return function(_0x262ba6){logger['error'](_0x262ba6[_0xbaf7('0x21')]);if(_0x262ba6[_0xbaf7('0x22')]){delete _0x262ba6[_0xbaf7('0x22')];}_0x51e14b['status'](_0x4bb914)[_0xbaf7('0x23')](_0x262ba6);};}exports[_0xbaf7('0x24')]=function(_0x568b0b,_0x464097){var _0x196cb5={},_0x5bdebf={},_0x267777={'count':0x0,'rows':[]};var _0x16cba9=_[_0xbaf7('0x25')](db['FreshsalesConfiguration']['rawAttributes'],function(_0x350463){return{'name':_0x350463[_0xbaf7('0x26')],'type':_0x350463[_0xbaf7('0x27')][_0xbaf7('0x28')]};});_0x5bdebf[_0xbaf7('0x29')]=_[_0xbaf7('0x25')](_0x16cba9,_0xbaf7('0x22'));_0x5bdebf[_0xbaf7('0x2a')]=_[_0xbaf7('0x2b')](_0x568b0b[_0xbaf7('0x2a')]);_0x5bdebf[_0xbaf7('0x2c')]=_[_0xbaf7('0x2d')](_0x5bdebf[_0xbaf7('0x29')],_0x5bdebf[_0xbaf7('0x2a')]);_0x196cb5[_0xbaf7('0x2e')]=_[_0xbaf7('0x2d')](_0x5bdebf[_0xbaf7('0x29')],qs[_0xbaf7('0x2f')](_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x2f')]));_0x196cb5[_0xbaf7('0x2e')]=_0x196cb5[_0xbaf7('0x2e')]['length']?_0x196cb5[_0xbaf7('0x2e')]:_0x5bdebf['model'];if(!_0x568b0b[_0xbaf7('0x2a')]['hasOwnProperty'](_0xbaf7('0x30'))){_0x196cb5[_0xbaf7('0x31')]=qs[_0xbaf7('0x31')](_0x568b0b[_0xbaf7('0x2a')]['limit']);_0x196cb5['offset']=qs[_0xbaf7('0x17')](_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x17')]);}_0x196cb5['order']=qs['sort'](_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x32')]);_0x196cb5[_0xbaf7('0x33')]=qs[_0xbaf7('0x2c')](_[_0xbaf7('0x34')](_0x568b0b[_0xbaf7('0x2a')],_0x5bdebf['filters']),_0x16cba9);if(_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x35')]){_0x196cb5['where']=_['merge'](_0x196cb5[_0xbaf7('0x33')],{'$or':_[_0xbaf7('0x25')](_0x16cba9,function(_0x2c3425){if(_0x2c3425[_0xbaf7('0x27')]!==_0xbaf7('0x36')){var _0x4fc0a6={};_0x4fc0a6[_0x2c3425[_0xbaf7('0x22')]]={'$like':'%'+_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x35')]+'%'};return _0x4fc0a6;}})});}_0x196cb5=_[_0xbaf7('0x37')]({},_0x196cb5,_0x568b0b[_0xbaf7('0x38')]);var _0x1aa920={'where':_0x196cb5[_0xbaf7('0x33')]};return db[_0xbaf7('0x39')]['count'](_0x1aa920)[_0xbaf7('0x1f')](function(_0x24240a){_0x267777['count']=_0x24240a;if(_0x568b0b[_0xbaf7('0x2a')][_0xbaf7('0x3a')]){_0x196cb5['include']=[{'all':!![]}];}return db[_0xbaf7('0x39')][_0xbaf7('0x3b')](_0x196cb5);})[_0xbaf7('0x1f')](function(_0x88db94){_0x267777[_0xbaf7('0x3c')]=_0x88db94;return _0x267777;})[_0xbaf7('0x1f')](respondWithFilteredResult(_0x464097,_0x196cb5))[_0xbaf7('0x3d')](handleError(_0x464097,null));};exports[_0xbaf7('0x3e')]=function(_0x585495,_0x2b3852){var _0x353f82={'raw':![],'where':{'id':_0x585495[_0xbaf7('0x3f')]['id']}},_0x555d09={};_0x555d09[_0xbaf7('0x29')]=_[_0xbaf7('0x2b')](db[_0xbaf7('0x39')][_0xbaf7('0x40')]);_0x555d09[_0xbaf7('0x2a')]=_[_0xbaf7('0x2b')](_0x585495[_0xbaf7('0x2a')]);_0x555d09[_0xbaf7('0x2c')]=_['intersection'](_0x555d09['model'],_0x555d09[_0xbaf7('0x2a')]);_0x353f82['attributes']=_[_0xbaf7('0x2d')](_0x555d09[_0xbaf7('0x29')],qs[_0xbaf7('0x2f')](_0x585495[_0xbaf7('0x2a')][_0xbaf7('0x2f')]));_0x353f82[_0xbaf7('0x2e')]=_0x353f82[_0xbaf7('0x2e')]['length']?_0x353f82[_0xbaf7('0x2e')]:_0x555d09[_0xbaf7('0x29')];if(_0x585495['query'][_0xbaf7('0x3a')]){_0x353f82[_0xbaf7('0x41')]=[{'all':!![]}];}_0x353f82=_[_0xbaf7('0x37')]({},_0x353f82,_0x585495[_0xbaf7('0x38')]);return db[_0xbaf7('0x39')]['find'](_0x353f82)[_0xbaf7('0x1f')](handleEntityNotFound(_0x2b3852,null))[_0xbaf7('0x1f')](respondWithResult(_0x2b3852,null))['catch'](handleError(_0x2b3852,null));};exports[_0xbaf7('0x42')]=function(_0x27d8bd,_0x343358){return db['FreshsalesConfiguration'][_0xbaf7('0x42')](_0x27d8bd[_0xbaf7('0x43')],{})[_0xbaf7('0x1f')](respondWithResult(_0x343358,0xc9))[_0xbaf7('0x3d')](handleError(_0x343358,null));};exports['update']=function(_0x2e52fd,_0x2dc369){if(_0x2e52fd[_0xbaf7('0x43')]['id']){delete _0x2e52fd[_0xbaf7('0x43')]['id'];}return db['FreshsalesConfiguration'][_0xbaf7('0x44')]({'where':{'id':_0x2e52fd[_0xbaf7('0x3f')]['id']}})[_0xbaf7('0x1f')](handleEntityNotFound(_0x2dc369,null))[_0xbaf7('0x1f')](saveUpdates(_0x2e52fd[_0xbaf7('0x43')],null))[_0xbaf7('0x1f')](respondWithResult(_0x2dc369,null))[_0xbaf7('0x3d')](handleError(_0x2dc369,null));};exports['destroy']=function(_0x1939b6,_0x1647e5){return db[_0xbaf7('0x39')][_0xbaf7('0x44')]({'where':{'id':_0x1939b6[_0xbaf7('0x3f')]['id']}})[_0xbaf7('0x1f')](handleEntityNotFound(_0x1647e5,null))[_0xbaf7('0x1f')](removeEntity(_0x1647e5,null))['catch'](handleError(_0x1647e5,null));};exports[_0xbaf7('0x45')]=function(_0xbe939,_0xe9de8a,_0x40d710){var _0x5b07f9={};var _0x557d23={};var _0x3ab997;var _0x554cda;return db[_0xbaf7('0x39')][_0xbaf7('0x46')]({'where':{'id':_0xbe939[_0xbaf7('0x3f')]['id']}})[_0xbaf7('0x1f')](handleEntityNotFound(_0xe9de8a,null))[_0xbaf7('0x1f')](function(_0x7e064c){if(_0x7e064c){_0x3ab997=_0x7e064c;_0x557d23[_0xbaf7('0x29')]=_['keys'](db[_0xbaf7('0x47')][_0xbaf7('0x40')]);_0x557d23[_0xbaf7('0x2a')]=_[_0xbaf7('0x2b')](_0xbe939[_0xbaf7('0x2a')]);_0x557d23[_0xbaf7('0x2c')]=_[_0xbaf7('0x2d')](_0x557d23['model'],_0x557d23[_0xbaf7('0x2a')]);_0x5b07f9['attributes']=_[_0xbaf7('0x2d')](_0x557d23[_0xbaf7('0x29')],qs[_0xbaf7('0x2f')](_0xbe939[_0xbaf7('0x2a')][_0xbaf7('0x2f')]));_0x5b07f9[_0xbaf7('0x2e')]=_0x5b07f9[_0xbaf7('0x2e')][_0xbaf7('0x48')]?_0x5b07f9[_0xbaf7('0x2e')]:_0x557d23[_0xbaf7('0x29')];_0x5b07f9[_0xbaf7('0x49')]=qs['sort'](_0xbe939[_0xbaf7('0x2a')][_0xbaf7('0x32')]);_0x5b07f9[_0xbaf7('0x33')]=qs[_0xbaf7('0x2c')](_[_0xbaf7('0x34')](_0xbe939[_0xbaf7('0x2a')],_0x557d23[_0xbaf7('0x2c')]));if(_0xbe939['query']['filter']){_0x5b07f9[_0xbaf7('0x33')]=_[_0xbaf7('0x37')](_0x5b07f9[_0xbaf7('0x33')],{'$or':_[_0xbaf7('0x25')](_0x5b07f9[_0xbaf7('0x2e')],function(_0x376c28){var _0x4dc6b3={};_0x4dc6b3[_0x376c28]={'$like':'%'+_0xbe939['query']['filter']+'%'};return _0x4dc6b3;})});}_0x5b07f9=_['merge']({},_0x5b07f9,_0xbe939[_0xbaf7('0x38')]);return _0x3ab997[_0xbaf7('0x45')](_0x5b07f9);}})['then'](function(_0x573d83){if(_0x573d83){_0x554cda=_0x573d83[_0xbaf7('0x48')];if(!_0xbe939['query'][_0xbaf7('0x4a')](_0xbaf7('0x30'))){_0x5b07f9[_0xbaf7('0x31')]=qs[_0xbaf7('0x31')](_0xbe939[_0xbaf7('0x2a')][_0xbaf7('0x31')]);_0x5b07f9[_0xbaf7('0x17')]=qs[_0xbaf7('0x17')](_0xbe939[_0xbaf7('0x2a')][_0xbaf7('0x17')]);}return _0x3ab997[_0xbaf7('0x45')](_0x5b07f9);}})[_0xbaf7('0x1f')](function(_0x2ab6f7){if(_0x2ab6f7){return _0x2ab6f7?{'count':_0x554cda,'rows':_0x2ab6f7}:null;}})['then'](respondWithResult(_0xe9de8a,null))[_0xbaf7('0x3d')](handleError(_0xe9de8a,null));};exports[_0xbaf7('0x4b')]=function(_0x1e33dc,_0x585799,_0x469eec){var _0x55371a={};var _0x52db7c={};var _0x201eea;var _0x4e85d8;return db[_0xbaf7('0x39')][_0xbaf7('0x46')]({'where':{'id':_0x1e33dc['params']['id']}})[_0xbaf7('0x1f')](handleEntityNotFound(_0x585799,null))[_0xbaf7('0x1f')](function(_0xc2bded){if(_0xc2bded){_0x201eea=_0xc2bded;_0x52db7c[_0xbaf7('0x29')]=_[_0xbaf7('0x2b')](db[_0xbaf7('0x47')]['rawAttributes']);_0x52db7c['query']=_[_0xbaf7('0x2b')](_0x1e33dc[_0xbaf7('0x2a')]);_0x52db7c['filters']=_['intersection'](_0x52db7c[_0xbaf7('0x29')],_0x52db7c[_0xbaf7('0x2a')]);_0x55371a['attributes']=_[_0xbaf7('0x2d')](_0x52db7c[_0xbaf7('0x29')],qs[_0xbaf7('0x2f')](_0x1e33dc[_0xbaf7('0x2a')][_0xbaf7('0x2f')]));_0x55371a[_0xbaf7('0x2e')]=_0x55371a[_0xbaf7('0x2e')][_0xbaf7('0x48')]?_0x55371a[_0xbaf7('0x2e')]:_0x52db7c[_0xbaf7('0x29')];_0x55371a[_0xbaf7('0x49')]=qs['sort'](_0x1e33dc[_0xbaf7('0x2a')]['sort']);_0x55371a['where']=qs[_0xbaf7('0x2c')](_['pick'](_0x1e33dc[_0xbaf7('0x2a')],_0x52db7c[_0xbaf7('0x2c')]));if(_0x1e33dc[_0xbaf7('0x2a')][_0xbaf7('0x35')]){_0x55371a[_0xbaf7('0x33')]=_[_0xbaf7('0x37')](_0x55371a[_0xbaf7('0x33')],{'$or':_[_0xbaf7('0x25')](_0x55371a[_0xbaf7('0x2e')],function(_0x258916){var _0x41e1fc={};_0x41e1fc[_0x258916]={'$like':'%'+_0x1e33dc[_0xbaf7('0x2a')][_0xbaf7('0x35')]+'%'};return _0x41e1fc;})});}_0x55371a=_['merge']({},_0x55371a,_0x1e33dc[_0xbaf7('0x38')]);return _0x201eea[_0xbaf7('0x4b')](_0x55371a);}})[_0xbaf7('0x1f')](function(_0x568002){if(_0x568002){_0x4e85d8=_0x568002['length'];if(!_0x1e33dc[_0xbaf7('0x2a')][_0xbaf7('0x4a')]('nolimit')){_0x55371a['limit']=qs[_0xbaf7('0x31')](_0x1e33dc['query'][_0xbaf7('0x31')]);_0x55371a[_0xbaf7('0x17')]=qs[_0xbaf7('0x17')](_0x1e33dc[_0xbaf7('0x2a')][_0xbaf7('0x17')]);}return _0x201eea[_0xbaf7('0x4b')](_0x55371a);}})[_0xbaf7('0x1f')](function(_0x5e7082){if(_0x5e7082){return _0x5e7082?{'count':_0x4e85d8,'rows':_0x5e7082}:null;}})['then'](respondWithResult(_0x585799,null))[_0xbaf7('0x3d')](handleError(_0x585799,null));};exports[_0xbaf7('0x4c')]=function(_0x425e54,_0x2a085c,_0x16a9b8){var _0x5d6cf1={};var _0x102013={};var _0x552c41;var _0x2d6060;return db[_0xbaf7('0x39')][_0xbaf7('0x46')]({'where':{'id':_0x425e54[_0xbaf7('0x3f')]['id']}})[_0xbaf7('0x1f')](handleEntityNotFound(_0x2a085c,null))['then'](function(_0x116f0d){if(_0x116f0d){_0x552c41=_0x116f0d;_0x102013[_0xbaf7('0x29')]=_[_0xbaf7('0x2b')](db[_0xbaf7('0x47')][_0xbaf7('0x40')]);_0x102013[_0xbaf7('0x2a')]=_[_0xbaf7('0x2b')](_0x425e54['query']);_0x102013[_0xbaf7('0x2c')]=_[_0xbaf7('0x2d')](_0x102013[_0xbaf7('0x29')],_0x102013[_0xbaf7('0x2a')]);_0x5d6cf1[_0xbaf7('0x2e')]=_['intersection'](_0x102013['model'],qs['fields'](_0x425e54[_0xbaf7('0x2a')]['fields']));_0x5d6cf1[_0xbaf7('0x2e')]=_0x5d6cf1['attributes'][_0xbaf7('0x48')]?_0x5d6cf1[_0xbaf7('0x2e')]:_0x102013[_0xbaf7('0x29')];_0x5d6cf1[_0xbaf7('0x49')]=qs['sort'](_0x425e54['query'][_0xbaf7('0x32')]);_0x5d6cf1[_0xbaf7('0x33')]=qs[_0xbaf7('0x2c')](_['pick'](_0x425e54[_0xbaf7('0x2a')],_0x102013[_0xbaf7('0x2c')]));if(_0x425e54[_0xbaf7('0x2a')]['filter']){_0x5d6cf1['where']=_[_0xbaf7('0x37')](_0x5d6cf1[_0xbaf7('0x33')],{'$or':_[_0xbaf7('0x25')](_0x5d6cf1[_0xbaf7('0x2e')],function(_0x4b45ce){var _0x219c6f={};_0x219c6f[_0x4b45ce]={'$like':'%'+_0x425e54[_0xbaf7('0x2a')][_0xbaf7('0x35')]+'%'};return _0x219c6f;})});}_0x5d6cf1=_[_0xbaf7('0x37')]({},_0x5d6cf1,_0x425e54['options']);return _0x552c41[_0xbaf7('0x4c')](_0x5d6cf1);}})[_0xbaf7('0x1f')](function(_0x3fbe9a){if(_0x3fbe9a){_0x2d6060=_0x3fbe9a[_0xbaf7('0x48')];if(!_0x425e54['query'][_0xbaf7('0x4a')](_0xbaf7('0x30'))){_0x5d6cf1[_0xbaf7('0x31')]=qs['limit'](_0x425e54[_0xbaf7('0x2a')][_0xbaf7('0x31')]);_0x5d6cf1[_0xbaf7('0x17')]=qs[_0xbaf7('0x17')](_0x425e54[_0xbaf7('0x2a')]['offset']);}return _0x552c41[_0xbaf7('0x4c')](_0x5d6cf1);}})[_0xbaf7('0x1f')](function(_0x57dc4d){if(_0x57dc4d){return _0x57dc4d?{'count':_0x2d6060,'rows':_0x57dc4d}:null;}})[_0xbaf7('0x1f')](respondWithResult(_0x2a085c,null))[_0xbaf7('0x3d')](handleError(_0x2a085c,null));};