Built motion from commit (unavailable).|2.5.14
[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 _0x878f=['map','FreshsalesAccount','rawAttributes','type','model','query','keys','filters','intersection','attributes','fields','length','nolimit','limit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','params','create','body','find','getConfigurations','FreshsalesConfiguration','hasOwnProperty','addConfiguration','findOne','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','FreshsalesField','getFields','username','remoteUri','substring','lastIndexOf','GET','format','/api/selector/owners','apiKey','users','test','getFields,\x20%s,\x20%s,\x20%s','statusCode','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','lodash','squel','to-csv','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','../../components/integrations/configuration','client','http','request','then','info','FreshsalesAccount,\x20%s,\x20%s','request\x20sent','debug','FreshsalesAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','status','end','count','offset','set','Content-Range','apply','reject','update','destroy','sendStatus','stack','name','send','index'];(function(_0x321bb2,_0x202794){var _0x9c12ed=function(_0x263dd3){while(--_0x263dd3){_0x321bb2['push'](_0x321bb2['shift']());}};_0x9c12ed(++_0x202794);}(_0x878f,0x173));var _0xf878=function(_0x462f38,_0x2c8054){_0x462f38=_0x462f38-0x0;var _0x3e7faf=_0x878f[_0x462f38];return _0x3e7faf;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xf878('0x0'));var rimraf=require(_0xf878('0x1'));var zipdir=require(_0xf878('0x2'));var jsonpatch=require(_0xf878('0x3'));var rp=require(_0xf878('0x4'));var moment=require(_0xf878('0x5'));var BPromise=require(_0xf878('0x6'));var Mustache=require(_0xf878('0x7'));var util=require(_0xf878('0x8'));var path=require(_0xf878('0x9'));var sox=require(_0xf878('0xa'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0xf878('0xb'));var squel=require(_0xf878('0xc'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xf878('0xd'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xf878('0xe'));var authService=require(_0xf878('0xf'));var qs=require(_0xf878('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf878('0x11'));var logger=require(_0xf878('0x12'))('api');var utils=require(_0xf878('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0xf878('0x14'));var db=require(_0xf878('0x15'))['db'];var integrations=require(_0xf878('0x16'));var jayson=require('jayson/promise');var client=jayson[_0xf878('0x17')][_0xf878('0x18')]({'port':0x232a});function respondWithRpcPromise(_0x39b1bb,_0x570dcc,_0xe2c2de,_0x3902f7){return new BPromise(function(_0x119e9e,_0x492260){var _0x568a3a=_0x3902f7||client;return _0x568a3a[_0xf878('0x19')](_0x39b1bb,_0xe2c2de)[_0xf878('0x1a')](function(_0x31d888){logger[_0xf878('0x1b')](_0xf878('0x1c'),_0x570dcc,_0xf878('0x1d'));logger[_0xf878('0x1e')](_0xf878('0x1f'),_0x570dcc,_0xf878('0x1d'),JSON[_0xf878('0x20')](_0x31d888));if(_0x31d888[_0xf878('0x21')]){if(_0x31d888['error'][_0xf878('0x22')]===0x1f4){logger[_0xf878('0x21')](_0xf878('0x1c'),_0x570dcc,_0x31d888[_0xf878('0x21')][_0xf878('0x23')]);return _0x492260(_0x31d888[_0xf878('0x21')][_0xf878('0x23')]);}logger['error'](_0xf878('0x1c'),_0x570dcc,_0x31d888[_0xf878('0x21')][_0xf878('0x23')]);return _0x119e9e(_0x31d888['error'][_0xf878('0x23')]);}else{logger[_0xf878('0x1b')](_0xf878('0x1c'),_0x570dcc,_0xf878('0x1d'));_0x119e9e(_0x31d888[_0xf878('0x24')][_0xf878('0x23')]);}})[_0xf878('0x25')](function(_0x466c6b){logger[_0xf878('0x21')](_0xf878('0x1c'),_0x570dcc,_0x466c6b);_0x492260(_0x466c6b);});});}function respondWithStatusCode(_0x45e61f,_0x566995){_0x566995=_0x566995||0xcc;return function(_0x2ac38b){if(_0x2ac38b){return _0x45e61f['sendStatus'](_0x566995);}return _0x45e61f[_0xf878('0x26')](_0x566995)[_0xf878('0x27')]();};}function respondWithResult(_0x35b3b4,_0x280036){_0x280036=_0x280036||0xc8;return function(_0x20449e){if(_0x20449e){return _0x35b3b4['status'](_0x280036)['json'](_0x20449e);}};}function respondWithFilteredResult(_0x59ba7b,_0x242926){return function(_0x3baba4){if(_0x3baba4){var _0x38fa87=_0x3baba4[_0xf878('0x28')],_0x5af87f=_0x242926[_0xf878('0x29')],_0xfa3e11=_0x242926[_0xf878('0x29')]+_0x242926['limit'],_0x2e2256;if(_0xfa3e11>=_0x38fa87){_0xfa3e11=_0x38fa87;_0x2e2256=0xc8;}else{_0x2e2256=0xce;}_0x59ba7b['status'](_0x2e2256);return _0x59ba7b[_0xf878('0x2a')](_0xf878('0x2b'),_0x5af87f+'-'+_0xfa3e11+'/'+_0x38fa87)['json'](_0x3baba4);}return null;};}function patchUpdates(_0x31e2e1){return function(_0x52da83){try{jsonpatch[_0xf878('0x2c')](_0x52da83,_0x31e2e1,!![]);}catch(_0x51afaf){return BPromise[_0xf878('0x2d')](_0x51afaf);}return _0x52da83['save']();};}function saveUpdates(_0x2630fb,_0x48b151){return function(_0x20f0e6){if(_0x20f0e6){return _0x20f0e6[_0xf878('0x2e')](_0x2630fb)[_0xf878('0x1a')](function(_0x1f5331){return _0x1f5331;});}return null;};}function removeEntity(_0x27c53b,_0x716782){return function(_0x5aed48){if(_0x5aed48){return _0x5aed48[_0xf878('0x2f')]()[_0xf878('0x1a')](function(){_0x27c53b[_0xf878('0x26')](0xcc)[_0xf878('0x27')]();});}};}function handleEntityNotFound(_0xd11e68,_0x487e39){return function(_0x2288cb){if(!_0x2288cb){_0xd11e68[_0xf878('0x30')](0x194);}return _0x2288cb;};}function handleError(_0x56938d,_0x2e2d93){_0x2e2d93=_0x2e2d93||0x1f4;return function(_0xd1ad51){logger[_0xf878('0x21')](_0xd1ad51[_0xf878('0x31')]);if(_0xd1ad51[_0xf878('0x32')]){delete _0xd1ad51['name'];}_0x56938d[_0xf878('0x26')](_0x2e2d93)[_0xf878('0x33')](_0xd1ad51);};}exports[_0xf878('0x34')]=function(_0x129805,_0x579c91){var _0x25f18f={},_0x5a6330={},_0x4ce1e7={'count':0x0,'rows':[]};var _0x1e1174=_[_0xf878('0x35')](db[_0xf878('0x36')][_0xf878('0x37')],function(_0x20f415){return{'name':_0x20f415['fieldName'],'type':_0x20f415[_0xf878('0x38')]['key']};});_0x5a6330[_0xf878('0x39')]=_[_0xf878('0x35')](_0x1e1174,'name');_0x5a6330[_0xf878('0x3a')]=_[_0xf878('0x3b')](_0x129805['query']);_0x5a6330[_0xf878('0x3c')]=_[_0xf878('0x3d')](_0x5a6330['model'],_0x5a6330[_0xf878('0x3a')]);_0x25f18f[_0xf878('0x3e')]=_[_0xf878('0x3d')](_0x5a6330[_0xf878('0x39')],qs[_0xf878('0x3f')](_0x129805[_0xf878('0x3a')][_0xf878('0x3f')]));_0x25f18f['attributes']=_0x25f18f[_0xf878('0x3e')][_0xf878('0x40')]?_0x25f18f['attributes']:_0x5a6330[_0xf878('0x39')];if(!_0x129805['query']['hasOwnProperty'](_0xf878('0x41'))){_0x25f18f[_0xf878('0x42')]=qs[_0xf878('0x42')](_0x129805[_0xf878('0x3a')][_0xf878('0x42')]);_0x25f18f['offset']=qs[_0xf878('0x29')](_0x129805['query'][_0xf878('0x29')]);}_0x25f18f[_0xf878('0x43')]=qs['sort'](_0x129805['query'][_0xf878('0x44')]);_0x25f18f[_0xf878('0x45')]=qs[_0xf878('0x3c')](_[_0xf878('0x46')](_0x129805[_0xf878('0x3a')],_0x5a6330[_0xf878('0x3c')]),_0x1e1174);if(_0x129805[_0xf878('0x3a')][_0xf878('0x47')]){_0x25f18f[_0xf878('0x45')]=_['merge'](_0x25f18f[_0xf878('0x45')],{'$or':_[_0xf878('0x35')](_0x1e1174,function(_0xe2c096){if(_0xe2c096[_0xf878('0x38')]!==_0xf878('0x48')){var _0x319f29={};_0x319f29[_0xe2c096['name']]={'$like':'%'+_0x129805[_0xf878('0x3a')][_0xf878('0x47')]+'%'};return _0x319f29;}})});}_0x25f18f=_[_0xf878('0x49')]({},_0x25f18f,_0x129805[_0xf878('0x4a')]);var _0xe8b571={'where':_0x25f18f['where']};return db[_0xf878('0x36')][_0xf878('0x28')](_0xe8b571)[_0xf878('0x1a')](function(_0x1ec3aa){_0x4ce1e7['count']=_0x1ec3aa;if(_0x129805[_0xf878('0x3a')][_0xf878('0x4b')]){_0x25f18f[_0xf878('0x4c')]=[{'all':!![]}];}return db['FreshsalesAccount']['findAll'](_0x25f18f);})[_0xf878('0x1a')](function(_0x5bf396){_0x4ce1e7['rows']=_0x5bf396;return _0x4ce1e7;})['then'](respondWithFilteredResult(_0x579c91,_0x25f18f))[_0xf878('0x25')](handleError(_0x579c91,null));};exports['show']=function(_0x156fdb,_0x450a10){var _0x1dcb19={'raw':![],'where':{'id':_0x156fdb[_0xf878('0x4d')]['id']}},_0x1ff60b={};_0x1ff60b[_0xf878('0x39')]=_[_0xf878('0x3b')](db['FreshsalesAccount'][_0xf878('0x37')]);_0x1ff60b[_0xf878('0x3a')]=_[_0xf878('0x3b')](_0x156fdb[_0xf878('0x3a')]);_0x1ff60b[_0xf878('0x3c')]=_['intersection'](_0x1ff60b['model'],_0x1ff60b[_0xf878('0x3a')]);_0x1dcb19[_0xf878('0x3e')]=_['intersection'](_0x1ff60b[_0xf878('0x39')],qs['fields'](_0x156fdb[_0xf878('0x3a')][_0xf878('0x3f')]));_0x1dcb19[_0xf878('0x3e')]=_0x1dcb19[_0xf878('0x3e')]['length']?_0x1dcb19[_0xf878('0x3e')]:_0x1ff60b[_0xf878('0x39')];if(_0x156fdb[_0xf878('0x3a')]['includeAll']){_0x1dcb19[_0xf878('0x4c')]=[{'all':!![]}];}_0x1dcb19=_[_0xf878('0x49')]({},_0x1dcb19,_0x156fdb[_0xf878('0x4a')]);return db[_0xf878('0x36')]['find'](_0x1dcb19)[_0xf878('0x1a')](handleEntityNotFound(_0x450a10,null))[_0xf878('0x1a')](respondWithResult(_0x450a10,null))[_0xf878('0x25')](handleError(_0x450a10,null));};exports['create']=function(_0x426b6e,_0xdfa29b){return db[_0xf878('0x36')][_0xf878('0x4e')](_0x426b6e['body'],{})['then'](respondWithResult(_0xdfa29b,0xc9))['catch'](handleError(_0xdfa29b,null));};exports[_0xf878('0x2e')]=function(_0x5e2b56,_0x516345){if(_0x5e2b56['body']['id']){delete _0x5e2b56[_0xf878('0x4f')]['id'];}return db[_0xf878('0x36')]['find']({'where':{'id':_0x5e2b56[_0xf878('0x4d')]['id']}})[_0xf878('0x1a')](handleEntityNotFound(_0x516345,null))[_0xf878('0x1a')](saveUpdates(_0x5e2b56['body'],null))['then'](respondWithResult(_0x516345,null))[_0xf878('0x25')](handleError(_0x516345,null));};exports['destroy']=function(_0x203ea6,_0x25e982){return db[_0xf878('0x36')][_0xf878('0x50')]({'where':{'id':_0x203ea6[_0xf878('0x4d')]['id']}})[_0xf878('0x1a')](handleEntityNotFound(_0x25e982,null))['then'](removeEntity(_0x25e982,null))[_0xf878('0x25')](handleError(_0x25e982,null));};exports[_0xf878('0x51')]=function(_0x566c49,_0x1e0824,_0xab7e4a){var _0x192abd={};var _0x282985={};var _0x263f26;var _0x36b000;return db[_0xf878('0x36')]['findOne']({'where':{'id':_0x566c49[_0xf878('0x4d')]['id']}})[_0xf878('0x1a')](handleEntityNotFound(_0x1e0824,null))[_0xf878('0x1a')](function(_0x52b94e){if(_0x52b94e){_0x263f26=_0x52b94e;_0x282985[_0xf878('0x39')]=_['keys'](db[_0xf878('0x52')][_0xf878('0x37')]);_0x282985[_0xf878('0x3a')]=_[_0xf878('0x3b')](_0x566c49[_0xf878('0x3a')]);_0x282985[_0xf878('0x3c')]=_[_0xf878('0x3d')](_0x282985[_0xf878('0x39')],_0x282985[_0xf878('0x3a')]);_0x192abd[_0xf878('0x3e')]=_['intersection'](_0x282985['model'],qs['fields'](_0x566c49[_0xf878('0x3a')]['fields']));_0x192abd[_0xf878('0x3e')]=_0x192abd[_0xf878('0x3e')]['length']?_0x192abd['attributes']:_0x282985[_0xf878('0x39')];_0x192abd[_0xf878('0x43')]=qs[_0xf878('0x44')](_0x566c49[_0xf878('0x3a')][_0xf878('0x44')]);_0x192abd[_0xf878('0x45')]=qs[_0xf878('0x3c')](_[_0xf878('0x46')](_0x566c49[_0xf878('0x3a')],_0x282985[_0xf878('0x3c')]));if(_0x566c49[_0xf878('0x3a')][_0xf878('0x47')]){_0x192abd[_0xf878('0x45')]=_[_0xf878('0x49')](_0x192abd[_0xf878('0x45')],{'$or':_[_0xf878('0x35')](_0x192abd[_0xf878('0x3e')],function(_0x76ad7a){var _0x5c0479={};_0x5c0479[_0x76ad7a]={'$like':'%'+_0x566c49[_0xf878('0x3a')]['filter']+'%'};return _0x5c0479;})});}_0x192abd=_[_0xf878('0x49')]({},_0x192abd,_0x566c49[_0xf878('0x4a')]);return _0x263f26[_0xf878('0x51')](_0x192abd);}})[_0xf878('0x1a')](function(_0x392177){if(_0x392177){_0x36b000=_0x392177[_0xf878('0x40')];if(!_0x566c49[_0xf878('0x3a')][_0xf878('0x53')](_0xf878('0x41'))){_0x192abd['limit']=qs[_0xf878('0x42')](_0x566c49[_0xf878('0x3a')][_0xf878('0x42')]);_0x192abd[_0xf878('0x29')]=qs['offset'](_0x566c49[_0xf878('0x3a')][_0xf878('0x29')]);}return _0x263f26[_0xf878('0x51')](_0x192abd);}})[_0xf878('0x1a')](function(_0x3befe1){if(_0x3befe1){return _0x3befe1?{'count':_0x36b000,'rows':_0x3befe1}:null;}})['then'](respondWithResult(_0x1e0824,null))['catch'](handleError(_0x1e0824,null));};exports[_0xf878('0x54')]=function(_0x1b0230,_0x193f6c,_0x251613){if(_0x1b0230[_0xf878('0x4f')]['id']){delete _0x1b0230[_0xf878('0x4f')]['id'];}return db[_0xf878('0x36')][_0xf878('0x55')]({'where':{'id':_0x1b0230[_0xf878('0x4d')]['id']}})[_0xf878('0x1a')](handleEntityNotFound(_0x193f6c,null))[_0xf878('0x1a')](function(_0x147059){if(_0x147059){_0x1b0230[_0xf878('0x4f')][_0xf878('0x56')]=_0x147059['id'];_0x1b0230[_0xf878('0x4f')][_0xf878('0x57')]=integrations[_0xf878('0x58')](_0x1b0230['body'][_0xf878('0x59')],_0x1b0230[_0xf878('0x4f')][_0xf878('0x38')]);_0x1b0230[_0xf878('0x4f')][_0xf878('0x5a')]=integrations[_0xf878('0x5b')](_0x1b0230[_0xf878('0x4f')][_0xf878('0x59')],_0x1b0230[_0xf878('0x4f')]['type']);return db[_0xf878('0x52')][_0xf878('0x4e')](_0x1b0230[_0xf878('0x4f')],{'include':[{'model':db[_0xf878('0x5c')],'as':_0xf878('0x57')},{'model':db[_0xf878('0x5c')],'as':_0xf878('0x5a')}]});}return null;})['then'](respondWithResult(_0x193f6c,null))['catch'](handleError(_0x193f6c,null));};exports[_0xf878('0x5d')]=function(_0x54ecdd,_0x338e77,_0x33b243){var _0x3a1862='';var _0x122091='';return db['FreshsalesAccount'][_0xf878('0x55')]({'where':{'id':_0x54ecdd[_0xf878('0x4d')]['id']},'attributes':['id',_0xf878('0x5e'),_0xf878('0x5f'),'apiKey']})[_0xf878('0x1a')](handleEntityNotFound(_0x338e77,null))['then'](function(_0x21537f){if(_0x21537f){_0x122091=_0x21537f[_0xf878('0x5e')];_0x3a1862=_0x21537f[_0xf878('0x5f')];var _0x1d7c1f=_0x21537f[_0xf878('0x5f')]['slice'](-0x1);if(_0x1d7c1f==='/'){_0x3a1862=_0x3a1862[_0xf878('0x60')](0x0,_0x3a1862[_0xf878('0x61')](_0x1d7c1f));}return rp({'method':_0xf878('0x62'),'uri':util[_0xf878('0x63')]('%s%s',_0x3a1862,_0xf878('0x64')),'headers':{'Authorization':'Token\x20token='+_0x21537f[_0xf878('0x65')]},'json':!![]});}})[_0xf878('0x1a')](function(_0x9c9850){if(_0x9c9850){var _0x334d6f=_0x9c9850[_0xf878('0x66')];var _0x200cb5=![];var _0x2871bf=null;for(_0x2871bf=0x0;_0x2871bf<_0x334d6f[_0xf878('0x40')];_0x2871bf++){if(_0x334d6f[_0x2871bf]['email']===_0x122091){_0x200cb5=!![];}}if(_0x200cb5){return!![];}else{throw new Error({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}}})['then'](respondWithResult(_0x338e77,null))[_0xf878('0x25')](function(_0x1a9af7){var _0x497b7a=_0x54ecdd['query'][_0xf878('0x67')]?0x1f4:_0x1a9af7['statusCode']||0x1f4;logger[_0xf878('0x21')](_0xf878('0x68'),_0xf878('0x5d'),_0x497b7a,JSON[_0xf878('0x20')](_0x1a9af7));delete _0x1a9af7[_0xf878('0x32')];if(_0x497b7a===0x191){_0x497b7a=0x190;}_0x338e77[_0xf878('0x26')](_0x497b7a)[_0xf878('0x33')](_0x54ecdd[_0xf878('0x3a')][_0xf878('0x67')]?{'message':'Wrong\x20credentials','statusCode':_0x1a9af7[_0xf878('0x69')]}:_0x1a9af7);});};