3d117604b14b8bde8b27d454cd8dab28499c1c2e
[motion2.git] / server / api / intFreshsalesAccount / intFreshsalesAccount.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 _0x79b8=['api','../../config/environment','../../components/integrations/configuration','sendStatus','status','end','offset','undefined','limit','count','set','json','apply','reject','save','then','destroy','error','name','send','map','FreshsalesAccount','key','query','keys','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','type','VIRTUAL','options','includeAll','findAll','rows','catch','show','params','rawAttributes','find','create','body','getConfigurations','FreshsalesConfiguration','addConfiguration','findOne','Subjects','getSubjects','channel','Descriptions','getDescriptions','FreshsalesField','getFields','username','remoteUri','apiKey','slice','substring','format','%s%s','/api/selector/owners','Token\x20token=','users','email','Requested\x20Resource\x20not\x20Found','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger'];(function(_0x4ecc1e,_0x3ae534){var _0x3f90e2=function(_0x42de1a){while(--_0x42de1a){_0x4ecc1e['push'](_0x4ecc1e['shift']());}};_0x3f90e2(++_0x3ae534);}(_0x79b8,0x11e));var _0x879b=function(_0x7c305b,_0x5512fd){_0x7c305b=_0x7c305b-0x0;var _0x33260f=_0x79b8[_0x7c305b];return _0x33260f;};'use strict';var emlformat=require(_0x879b('0x0'));var rimraf=require(_0x879b('0x1'));var zipdir=require(_0x879b('0x2'));var jsonpatch=require(_0x879b('0x3'));var rp=require(_0x879b('0x4'));var moment=require(_0x879b('0x5'));var BPromise=require(_0x879b('0x6'));var Mustache=require(_0x879b('0x7'));var util=require('util');var path=require(_0x879b('0x8'));var sox=require('sox');var csv=require(_0x879b('0x9'));var ejs=require(_0x879b('0xa'));var fs=require('fs');var fs_extra=require(_0x879b('0xb'));var _=require(_0x879b('0xc'));var squel=require(_0x879b('0xd'));var crypto=require('crypto');var jsforce=require(_0x879b('0xe'));var deskjs=require(_0x879b('0xf'));var toCsv=require(_0x879b('0x9'));var querystring=require(_0x879b('0x10'));var Papa=require(_0x879b('0x11'));var Redis=require(_0x879b('0x12'));var authService=require(_0x879b('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x879b('0x14'));var hardwareService=require(_0x879b('0x15'));var logger=require(_0x879b('0x16'))(_0x879b('0x17'));var utils=require('../../config/utils');var config=require(_0x879b('0x18'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];var integrations=require(_0x879b('0x19'));function respondWithStatusCode(_0x404d13,_0xa0296d){_0xa0296d=_0xa0296d||0xcc;return function(_0x1305ee){if(_0x1305ee){return _0x404d13[_0x879b('0x1a')](_0xa0296d);}return _0x404d13[_0x879b('0x1b')](_0xa0296d)[_0x879b('0x1c')]();};}function respondWithResult(_0x567c27,_0x203588){_0x203588=_0x203588||0xc8;return function(_0x55f2bb){if(_0x55f2bb){return _0x567c27[_0x879b('0x1b')](_0x203588)['json'](_0x55f2bb);}};}function respondWithFilteredResult(_0x86d51e,_0x4f5dab){return function(_0x38b957){if(_0x38b957){var _0x3d541e=typeof _0x4f5dab[_0x879b('0x1d')]===_0x879b('0x1e')&&typeof _0x4f5dab[_0x879b('0x1f')]==='undefined';var _0xfa631e=_0x38b957[_0x879b('0x20')];var _0x3cb5ec=_0x3d541e?0x0:_0x4f5dab[_0x879b('0x1d')];var _0x407aab=_0x3d541e?_0x38b957[_0x879b('0x20')]:_0x4f5dab[_0x879b('0x1d')]+_0x4f5dab[_0x879b('0x1f')];var _0x505f01;if(_0x407aab>=_0xfa631e){_0x407aab=_0xfa631e;_0x505f01=0xc8;}else{_0x505f01=0xce;}_0x86d51e['status'](_0x505f01);return _0x86d51e[_0x879b('0x21')]('Content-Range',_0x3cb5ec+'-'+_0x407aab+'/'+_0xfa631e)[_0x879b('0x22')](_0x38b957);}return null;};}function patchUpdates(_0x506611){return function(_0x4b6570){try{jsonpatch[_0x879b('0x23')](_0x4b6570,_0x506611,!![]);}catch(_0x38bcc7){return BPromise[_0x879b('0x24')](_0x38bcc7);}return _0x4b6570[_0x879b('0x25')]();};}function saveUpdates(_0x105817,_0x4a67db){return function(_0x63e638){if(_0x63e638){return _0x63e638['update'](_0x105817)[_0x879b('0x26')](function(_0x41aabe){return _0x41aabe;});}return null;};}function removeEntity(_0x59e795,_0x4ed23d){return function(_0x29e8d2){if(_0x29e8d2){return _0x29e8d2[_0x879b('0x27')]()[_0x879b('0x26')](function(){_0x59e795[_0x879b('0x1b')](0xcc)[_0x879b('0x1c')]();});}};}function handleEntityNotFound(_0xa7af23,_0x597131){return function(_0x1d64cc){if(!_0x1d64cc){_0xa7af23[_0x879b('0x1a')](0x194);}return _0x1d64cc;};}function handleError(_0x59150c,_0x5bde16){_0x5bde16=_0x5bde16||0x1f4;return function(_0x7b85e8){logger[_0x879b('0x28')](_0x7b85e8['stack']);if(_0x7b85e8['name']){delete _0x7b85e8[_0x879b('0x29')];}_0x59150c['status'](_0x5bde16)[_0x879b('0x2a')](_0x7b85e8);};}exports['index']=function(_0x1914a7,_0x234b53){var _0x3762ab={},_0x3e647a={},_0x1e4a56={'count':0x0,'rows':[]};var _0x466ecc=_[_0x879b('0x2b')](db[_0x879b('0x2c')]['rawAttributes'],function(_0x5b77dc){return{'name':_0x5b77dc['fieldName'],'type':_0x5b77dc['type'][_0x879b('0x2d')]};});_0x3e647a['model']=_[_0x879b('0x2b')](_0x466ecc,_0x879b('0x29'));_0x3e647a[_0x879b('0x2e')]=_[_0x879b('0x2f')](_0x1914a7[_0x879b('0x2e')]);_0x3e647a[_0x879b('0x30')]=_[_0x879b('0x31')](_0x3e647a['model'],_0x3e647a['query']);_0x3762ab['attributes']=_[_0x879b('0x31')](_0x3e647a[_0x879b('0x32')],qs['fields'](_0x1914a7[_0x879b('0x2e')][_0x879b('0x33')]));_0x3762ab[_0x879b('0x34')]=_0x3762ab['attributes'][_0x879b('0x35')]?_0x3762ab[_0x879b('0x34')]:_0x3e647a[_0x879b('0x32')];if(!_0x1914a7[_0x879b('0x2e')][_0x879b('0x36')](_0x879b('0x37'))){_0x3762ab[_0x879b('0x1f')]=qs[_0x879b('0x1f')](_0x1914a7[_0x879b('0x2e')][_0x879b('0x1f')]);_0x3762ab[_0x879b('0x1d')]=qs[_0x879b('0x1d')](_0x1914a7[_0x879b('0x2e')][_0x879b('0x1d')]);}_0x3762ab[_0x879b('0x38')]=qs[_0x879b('0x39')](_0x1914a7[_0x879b('0x2e')][_0x879b('0x39')]);_0x3762ab['where']=qs[_0x879b('0x30')](_[_0x879b('0x3a')](_0x1914a7[_0x879b('0x2e')],_0x3e647a[_0x879b('0x30')]),_0x466ecc);if(_0x1914a7[_0x879b('0x2e')][_0x879b('0x3b')]){_0x3762ab[_0x879b('0x3c')]=_[_0x879b('0x3d')](_0x3762ab[_0x879b('0x3c')],{'$or':_[_0x879b('0x2b')](_0x466ecc,function(_0xca7076){if(_0xca7076[_0x879b('0x3e')]!==_0x879b('0x3f')){var _0x5648e8={};_0x5648e8[_0xca7076[_0x879b('0x29')]]={'$like':'%'+_0x1914a7[_0x879b('0x2e')]['filter']+'%'};return _0x5648e8;}})});}_0x3762ab=_[_0x879b('0x3d')]({},_0x3762ab,_0x1914a7[_0x879b('0x40')]);var _0x5dc3ce={'where':_0x3762ab['where']};return db[_0x879b('0x2c')][_0x879b('0x20')](_0x5dc3ce)[_0x879b('0x26')](function(_0x61eef2){_0x1e4a56[_0x879b('0x20')]=_0x61eef2;if(_0x1914a7[_0x879b('0x2e')][_0x879b('0x41')]){_0x3762ab['include']=[{'all':!![]}];}return db['FreshsalesAccount'][_0x879b('0x42')](_0x3762ab);})[_0x879b('0x26')](function(_0x73d06a){_0x1e4a56[_0x879b('0x43')]=_0x73d06a;return _0x1e4a56;})[_0x879b('0x26')](respondWithFilteredResult(_0x234b53,_0x3762ab))[_0x879b('0x44')](handleError(_0x234b53,null));};exports[_0x879b('0x45')]=function(_0x2aa243,_0x5e3832){var _0x4b6b37={'raw':![],'where':{'id':_0x2aa243[_0x879b('0x46')]['id']}},_0x265d0b={};_0x265d0b[_0x879b('0x32')]=_[_0x879b('0x2f')](db[_0x879b('0x2c')][_0x879b('0x47')]);_0x265d0b[_0x879b('0x2e')]=_['keys'](_0x2aa243['query']);_0x265d0b['filters']=_[_0x879b('0x31')](_0x265d0b[_0x879b('0x32')],_0x265d0b[_0x879b('0x2e')]);_0x4b6b37[_0x879b('0x34')]=_['intersection'](_0x265d0b['model'],qs[_0x879b('0x33')](_0x2aa243[_0x879b('0x2e')][_0x879b('0x33')]));_0x4b6b37[_0x879b('0x34')]=_0x4b6b37[_0x879b('0x34')][_0x879b('0x35')]?_0x4b6b37[_0x879b('0x34')]:_0x265d0b['model'];if(_0x2aa243[_0x879b('0x2e')][_0x879b('0x41')]){_0x4b6b37['include']=[{'all':!![]}];}_0x4b6b37=_[_0x879b('0x3d')]({},_0x4b6b37,_0x2aa243[_0x879b('0x40')]);return db[_0x879b('0x2c')][_0x879b('0x48')](_0x4b6b37)[_0x879b('0x26')](handleEntityNotFound(_0x5e3832,null))[_0x879b('0x26')](respondWithResult(_0x5e3832,null))['catch'](handleError(_0x5e3832,null));};exports[_0x879b('0x49')]=function(_0x545d92,_0x6d1bfe){return db[_0x879b('0x2c')][_0x879b('0x49')](_0x545d92[_0x879b('0x4a')],{})[_0x879b('0x26')](respondWithResult(_0x6d1bfe,0xc9))[_0x879b('0x44')](handleError(_0x6d1bfe,null));};exports['update']=function(_0x4d5b1e,_0x114a64){if(_0x4d5b1e[_0x879b('0x4a')]['id']){delete _0x4d5b1e[_0x879b('0x4a')]['id'];}return db[_0x879b('0x2c')]['find']({'where':{'id':_0x4d5b1e[_0x879b('0x46')]['id']}})['then'](handleEntityNotFound(_0x114a64,null))[_0x879b('0x26')](saveUpdates(_0x4d5b1e[_0x879b('0x4a')],null))[_0x879b('0x26')](respondWithResult(_0x114a64,null))[_0x879b('0x44')](handleError(_0x114a64,null));};exports[_0x879b('0x27')]=function(_0x5b8990,_0x463a0d){return db['FreshsalesAccount'][_0x879b('0x48')]({'where':{'id':_0x5b8990['params']['id']}})[_0x879b('0x26')](handleEntityNotFound(_0x463a0d,null))['then'](removeEntity(_0x463a0d,null))['catch'](handleError(_0x463a0d,null));};exports[_0x879b('0x4b')]=function(_0x3cf76d,_0x248f10,_0x1b0082){var _0x202cce={};var _0x3ac971={};var _0x4356c0;var _0x323f48;return db['FreshsalesAccount']['findOne']({'where':{'id':_0x3cf76d[_0x879b('0x46')]['id']}})['then'](handleEntityNotFound(_0x248f10,null))[_0x879b('0x26')](function(_0x535f64){if(_0x535f64){_0x4356c0=_0x535f64;_0x3ac971[_0x879b('0x32')]=_[_0x879b('0x2f')](db[_0x879b('0x4c')]['rawAttributes']);_0x3ac971[_0x879b('0x2e')]=_[_0x879b('0x2f')](_0x3cf76d['query']);_0x3ac971[_0x879b('0x30')]=_[_0x879b('0x31')](_0x3ac971[_0x879b('0x32')],_0x3ac971[_0x879b('0x2e')]);_0x202cce[_0x879b('0x34')]=_[_0x879b('0x31')](_0x3ac971[_0x879b('0x32')],qs['fields'](_0x3cf76d[_0x879b('0x2e')]['fields']));_0x202cce[_0x879b('0x34')]=_0x202cce[_0x879b('0x34')][_0x879b('0x35')]?_0x202cce['attributes']:_0x3ac971[_0x879b('0x32')];_0x202cce['order']=qs[_0x879b('0x39')](_0x3cf76d[_0x879b('0x2e')][_0x879b('0x39')]);_0x202cce['where']=qs[_0x879b('0x30')](_[_0x879b('0x3a')](_0x3cf76d[_0x879b('0x2e')],_0x3ac971[_0x879b('0x30')]));if(_0x3cf76d[_0x879b('0x2e')]['filter']){_0x202cce[_0x879b('0x3c')]=_[_0x879b('0x3d')](_0x202cce[_0x879b('0x3c')],{'$or':_['map'](_0x202cce[_0x879b('0x34')],function(_0x2e1159){var _0x3783e8={};_0x3783e8[_0x2e1159]={'$like':'%'+_0x3cf76d[_0x879b('0x2e')][_0x879b('0x3b')]+'%'};return _0x3783e8;})});}_0x202cce=_['merge']({},_0x202cce,_0x3cf76d[_0x879b('0x40')]);return _0x4356c0['getConfigurations'](_0x202cce);}})[_0x879b('0x26')](function(_0x8b82ad){if(_0x8b82ad){_0x323f48=_0x8b82ad[_0x879b('0x35')];if(!_0x3cf76d[_0x879b('0x2e')][_0x879b('0x36')](_0x879b('0x37'))){_0x202cce['limit']=qs['limit'](_0x3cf76d[_0x879b('0x2e')][_0x879b('0x1f')]);_0x202cce['offset']=qs[_0x879b('0x1d')](_0x3cf76d[_0x879b('0x2e')]['offset']);}return _0x4356c0[_0x879b('0x4b')](_0x202cce);}})[_0x879b('0x26')](function(_0x5aacfc){if(_0x5aacfc){return _0x5aacfc?{'count':_0x323f48,'rows':_0x5aacfc}:null;}})[_0x879b('0x26')](respondWithResult(_0x248f10,null))[_0x879b('0x44')](handleError(_0x248f10,null));};exports[_0x879b('0x4d')]=function(_0x1fc6e6,_0x199852,_0x3cc4d8){if(_0x1fc6e6['body']['id']){delete _0x1fc6e6[_0x879b('0x4a')]['id'];}return db[_0x879b('0x2c')][_0x879b('0x4e')]({'where':{'id':_0x1fc6e6[_0x879b('0x46')]['id']}})['then'](handleEntityNotFound(_0x199852,null))[_0x879b('0x26')](function(_0xbb953e){if(_0xbb953e){_0x1fc6e6[_0x879b('0x4a')]['AccountId']=_0xbb953e['id'];_0x1fc6e6[_0x879b('0x4a')][_0x879b('0x4f')]=integrations[_0x879b('0x50')](_0x1fc6e6[_0x879b('0x4a')][_0x879b('0x51')],_0x1fc6e6[_0x879b('0x4a')]['type']);_0x1fc6e6[_0x879b('0x4a')][_0x879b('0x52')]=integrations[_0x879b('0x53')](_0x1fc6e6[_0x879b('0x4a')][_0x879b('0x51')],_0x1fc6e6[_0x879b('0x4a')][_0x879b('0x3e')]);return db['FreshsalesConfiguration']['create'](_0x1fc6e6[_0x879b('0x4a')],{'include':[{'model':db[_0x879b('0x54')],'as':_0x879b('0x4f')},{'model':db['FreshsalesField'],'as':_0x879b('0x52')}]});}return null;})[_0x879b('0x26')](respondWithResult(_0x199852,null))[_0x879b('0x44')](handleError(_0x199852,null));};exports[_0x879b('0x55')]=function(_0x4d1d3b,_0x550a35,_0x5c4b10){var _0x2b3e00='';var _0x49c552='';return db['FreshsalesAccount'][_0x879b('0x4e')]({'where':{'id':_0x4d1d3b['params']['id']},'attributes':['id',_0x879b('0x56'),_0x879b('0x57'),_0x879b('0x58')]})[_0x879b('0x26')](handleEntityNotFound(_0x550a35,null))['then'](function(_0x51c433){if(_0x51c433){_0x49c552=_0x51c433[_0x879b('0x56')];_0x2b3e00=_0x51c433[_0x879b('0x57')];var _0x2e4d26=_0x51c433['remoteUri'][_0x879b('0x59')](-0x1);if(_0x2e4d26==='/'){_0x2b3e00=_0x2b3e00[_0x879b('0x5a')](0x0,_0x2b3e00['lastIndexOf'](_0x2e4d26));}return rp({'method':'GET','uri':util[_0x879b('0x5b')](_0x879b('0x5c'),_0x2b3e00,_0x879b('0x5d')),'headers':{'Authorization':_0x879b('0x5e')+_0x51c433[_0x879b('0x58')]},'json':!![]});}})[_0x879b('0x26')](function(_0x37c464){if(_0x37c464){var _0xf93ed7=_0x37c464[_0x879b('0x5f')];var _0x1e5d84=![];var _0x12147e=null;for(_0x12147e=0x0;_0x12147e<_0xf93ed7[_0x879b('0x35')];_0x12147e++){if(_0xf93ed7[_0x12147e][_0x879b('0x60')]===_0x49c552){_0x1e5d84=!![];}}if(_0x1e5d84){return!![];}else{throw new Error({'statusCode':0x194,'message':_0x879b('0x61')});}}})['then'](respondWithResult(_0x550a35,null))[_0x879b('0x44')](function(_0x4fba17){var _0xf4281=_0x4d1d3b['query'][_0x879b('0x62')]?0x1f4:_0x4fba17[_0x879b('0x63')]||0x1f4;logger[_0x879b('0x28')](_0x879b('0x64'),'getFields',_0xf4281,JSON[_0x879b('0x65')](_0x4fba17));delete _0x4fba17[_0x879b('0x29')];if(_0xf4281===0x191){_0xf4281=0x190;}_0x550a35[_0x879b('0x1b')](_0xf4281)['send'](_0x4d1d3b[_0x879b('0x2e')]['test']?{'message':_0x879b('0x66'),'statusCode':_0x4fba17[_0x879b('0x63')]}:_0x4fba17);});};