Built motion from commit f8768ebe.|2.6.15
[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 _0xabb7=['to-csv','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../components/integrations/configuration','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','save','update','then','destroy','error','stack','name','send','index','map','FreshsalesAccount','rawAttributes','fieldName','key','model','query','keys','filters','attributes','intersection','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','options','include','findAll','rows','catch','params','fields','find','create','body','getConfigurations','findOne','FreshsalesConfiguration','addConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','FreshsalesField','getFields','username','apiKey','remoteUri','slice','substring','lastIndexOf','GET','format','%s%s','/api/selector/owners','Token\x20token=','users','email','Requested\x20Resource\x20not\x20Found','test','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','sox'];(function(_0x252c2b,_0x2fd4ed){var _0x131bb7=function(_0x3d3347){while(--_0x3d3347){_0x252c2b['push'](_0x252c2b['shift']());}};_0x131bb7(++_0x2fd4ed);}(_0xabb7,0x190));var _0x7abb=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xabb7[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x7abb('0x0'));var jsonpatch=require(_0x7abb('0x1'));var rp=require('request-promise');var moment=require(_0x7abb('0x2'));var BPromise=require(_0x7abb('0x3'));var Mustache=require(_0x7abb('0x4'));var util=require(_0x7abb('0x5'));var path=require(_0x7abb('0x6'));var sox=require(_0x7abb('0x7'));var csv=require(_0x7abb('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x7abb('0x9'));var squel=require(_0x7abb('0xa'));var crypto=require(_0x7abb('0xb'));var jsforce=require(_0x7abb('0xc'));var deskjs=require(_0x7abb('0xd'));var toCsv=require(_0x7abb('0x8'));var querystring=require(_0x7abb('0xe'));var Papa=require(_0x7abb('0xf'));var Redis=require(_0x7abb('0x10'));var authService=require(_0x7abb('0x11'));var qs=require(_0x7abb('0x12'));var as=require(_0x7abb('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x7abb('0x14'))(_0x7abb('0x15'));var utils=require(_0x7abb('0x16'));var config=require(_0x7abb('0x17'));var licenseUtil=require(_0x7abb('0x18'));var db=require('../../mysqldb')['db'];var integrations=require(_0x7abb('0x19'));function respondWithStatusCode(_0x14c8c9,_0x1d7905){_0x1d7905=_0x1d7905||0xcc;return function(_0x1d0348){if(_0x1d0348){return _0x14c8c9[_0x7abb('0x1a')](_0x1d7905);}return _0x14c8c9[_0x7abb('0x1b')](_0x1d7905)['end']();};}function respondWithResult(_0x3c0829,_0x348f51){_0x348f51=_0x348f51||0xc8;return function(_0x352003){if(_0x352003){return _0x3c0829[_0x7abb('0x1b')](_0x348f51)[_0x7abb('0x1c')](_0x352003);}};}function respondWithFilteredResult(_0x40ef82,_0x44c3f3){return function(_0x11eb78){if(_0x11eb78){var _0x562421=typeof _0x44c3f3[_0x7abb('0x1d')]===_0x7abb('0x1e')&&typeof _0x44c3f3[_0x7abb('0x1f')]==='undefined';var _0x21665e=_0x11eb78[_0x7abb('0x20')];var _0x4bff40=_0x562421?0x0:_0x44c3f3[_0x7abb('0x1d')];var _0x5ebfc3=_0x562421?_0x11eb78[_0x7abb('0x20')]:_0x44c3f3[_0x7abb('0x1d')]+_0x44c3f3['limit'];var _0x1ef8fd;if(_0x5ebfc3>=_0x21665e){_0x5ebfc3=_0x21665e;_0x1ef8fd=0xc8;}else{_0x1ef8fd=0xce;}_0x40ef82[_0x7abb('0x1b')](_0x1ef8fd);return _0x40ef82[_0x7abb('0x21')](_0x7abb('0x22'),_0x4bff40+'-'+_0x5ebfc3+'/'+_0x21665e)[_0x7abb('0x1c')](_0x11eb78);}return null;};}function patchUpdates(_0x27460f){return function(_0x192f35){try{jsonpatch[_0x7abb('0x23')](_0x192f35,_0x27460f,!![]);}catch(_0x14f97e){return BPromise['reject'](_0x14f97e);}return _0x192f35[_0x7abb('0x24')]();};}function saveUpdates(_0x3ce411,_0x56f9d4){return function(_0x2e5f4a){if(_0x2e5f4a){return _0x2e5f4a[_0x7abb('0x25')](_0x3ce411)[_0x7abb('0x26')](function(_0x336227){return _0x336227;});}return null;};}function removeEntity(_0x47244c,_0x2f041f){return function(_0x21cd4a){if(_0x21cd4a){return _0x21cd4a[_0x7abb('0x27')]()['then'](function(){_0x47244c[_0x7abb('0x1b')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4d7cc7,_0x55414b){return function(_0x2b4a30){if(!_0x2b4a30){_0x4d7cc7[_0x7abb('0x1a')](0x194);}return _0x2b4a30;};}function handleError(_0x5691bb,_0x550ec9){_0x550ec9=_0x550ec9||0x1f4;return function(_0x37bcef){logger[_0x7abb('0x28')](_0x37bcef[_0x7abb('0x29')]);if(_0x37bcef['name']){delete _0x37bcef[_0x7abb('0x2a')];}_0x5691bb[_0x7abb('0x1b')](_0x550ec9)[_0x7abb('0x2b')](_0x37bcef);};}exports[_0x7abb('0x2c')]=function(_0x41dd71,_0x3848ef){var _0x597cec={},_0x1a5e1e={},_0x5a058e={'count':0x0,'rows':[]};var _0xb4333=_[_0x7abb('0x2d')](db[_0x7abb('0x2e')][_0x7abb('0x2f')],function(_0x53f9ed){return{'name':_0x53f9ed[_0x7abb('0x30')],'type':_0x53f9ed['type'][_0x7abb('0x31')]};});_0x1a5e1e[_0x7abb('0x32')]=_[_0x7abb('0x2d')](_0xb4333,_0x7abb('0x2a'));_0x1a5e1e[_0x7abb('0x33')]=_[_0x7abb('0x34')](_0x41dd71[_0x7abb('0x33')]);_0x1a5e1e[_0x7abb('0x35')]=_['intersection'](_0x1a5e1e[_0x7abb('0x32')],_0x1a5e1e['query']);_0x597cec[_0x7abb('0x36')]=_[_0x7abb('0x37')](_0x1a5e1e['model'],qs['fields'](_0x41dd71['query']['fields']));_0x597cec[_0x7abb('0x36')]=_0x597cec[_0x7abb('0x36')][_0x7abb('0x38')]?_0x597cec['attributes']:_0x1a5e1e[_0x7abb('0x32')];if(!_0x41dd71[_0x7abb('0x33')][_0x7abb('0x39')](_0x7abb('0x3a'))){_0x597cec[_0x7abb('0x1f')]=qs[_0x7abb('0x1f')](_0x41dd71[_0x7abb('0x33')]['limit']);_0x597cec[_0x7abb('0x1d')]=qs[_0x7abb('0x1d')](_0x41dd71['query'][_0x7abb('0x1d')]);}_0x597cec[_0x7abb('0x3b')]=qs[_0x7abb('0x3c')](_0x41dd71['query'][_0x7abb('0x3c')]);_0x597cec[_0x7abb('0x3d')]=qs[_0x7abb('0x35')](_[_0x7abb('0x3e')](_0x41dd71[_0x7abb('0x33')],_0x1a5e1e[_0x7abb('0x35')]),_0xb4333);if(_0x41dd71['query'][_0x7abb('0x3f')]){_0x597cec[_0x7abb('0x3d')]=_[_0x7abb('0x40')](_0x597cec[_0x7abb('0x3d')],{'$or':_['map'](_0xb4333,function(_0x26566c){if(_0x26566c[_0x7abb('0x41')]!=='VIRTUAL'){var _0x30a9ce={};_0x30a9ce[_0x26566c[_0x7abb('0x2a')]]={'$like':'%'+_0x41dd71[_0x7abb('0x33')][_0x7abb('0x3f')]+'%'};return _0x30a9ce;}})});}_0x597cec=_[_0x7abb('0x40')]({},_0x597cec,_0x41dd71[_0x7abb('0x42')]);var _0x1b4105={'where':_0x597cec[_0x7abb('0x3d')]};return db[_0x7abb('0x2e')][_0x7abb('0x20')](_0x1b4105)['then'](function(_0x475064){_0x5a058e[_0x7abb('0x20')]=_0x475064;if(_0x41dd71[_0x7abb('0x33')]['includeAll']){_0x597cec[_0x7abb('0x43')]=[{'all':!![]}];}return db[_0x7abb('0x2e')][_0x7abb('0x44')](_0x597cec);})['then'](function(_0x46c62d){_0x5a058e[_0x7abb('0x45')]=_0x46c62d;return _0x5a058e;})[_0x7abb('0x26')](respondWithFilteredResult(_0x3848ef,_0x597cec))[_0x7abb('0x46')](handleError(_0x3848ef,null));};exports['show']=function(_0x518cb1,_0x2258ca){var _0x305100={'raw':![],'where':{'id':_0x518cb1[_0x7abb('0x47')]['id']}},_0x173348={};_0x173348[_0x7abb('0x32')]=_[_0x7abb('0x34')](db['FreshsalesAccount'][_0x7abb('0x2f')]);_0x173348[_0x7abb('0x33')]=_['keys'](_0x518cb1[_0x7abb('0x33')]);_0x173348[_0x7abb('0x35')]=_[_0x7abb('0x37')](_0x173348[_0x7abb('0x32')],_0x173348[_0x7abb('0x33')]);_0x305100[_0x7abb('0x36')]=_['intersection'](_0x173348['model'],qs[_0x7abb('0x48')](_0x518cb1['query']['fields']));_0x305100[_0x7abb('0x36')]=_0x305100[_0x7abb('0x36')][_0x7abb('0x38')]?_0x305100['attributes']:_0x173348['model'];if(_0x518cb1[_0x7abb('0x33')]['includeAll']){_0x305100['include']=[{'all':!![]}];}_0x305100=_[_0x7abb('0x40')]({},_0x305100,_0x518cb1[_0x7abb('0x42')]);return db[_0x7abb('0x2e')][_0x7abb('0x49')](_0x305100)[_0x7abb('0x26')](handleEntityNotFound(_0x2258ca,null))[_0x7abb('0x26')](respondWithResult(_0x2258ca,null))[_0x7abb('0x46')](handleError(_0x2258ca,null));};exports[_0x7abb('0x4a')]=function(_0x5dd0bb,_0x24e012){return db['FreshsalesAccount'][_0x7abb('0x4a')](_0x5dd0bb[_0x7abb('0x4b')],{})[_0x7abb('0x26')](respondWithResult(_0x24e012,0xc9))[_0x7abb('0x46')](handleError(_0x24e012,null));};exports['update']=function(_0x2b7054,_0x54bf46){if(_0x2b7054['body']['id']){delete _0x2b7054[_0x7abb('0x4b')]['id'];}return db[_0x7abb('0x2e')][_0x7abb('0x49')]({'where':{'id':_0x2b7054[_0x7abb('0x47')]['id']}})[_0x7abb('0x26')](handleEntityNotFound(_0x54bf46,null))[_0x7abb('0x26')](saveUpdates(_0x2b7054[_0x7abb('0x4b')],null))[_0x7abb('0x26')](respondWithResult(_0x54bf46,null))[_0x7abb('0x46')](handleError(_0x54bf46,null));};exports[_0x7abb('0x27')]=function(_0xfb6272,_0x43c533){return db['FreshsalesAccount'][_0x7abb('0x49')]({'where':{'id':_0xfb6272[_0x7abb('0x47')]['id']}})[_0x7abb('0x26')](handleEntityNotFound(_0x43c533,null))[_0x7abb('0x26')](removeEntity(_0x43c533,null))[_0x7abb('0x46')](handleError(_0x43c533,null));};exports[_0x7abb('0x4c')]=function(_0x508df9,_0x2f90fc,_0x100ff9){var _0x3a343a={};var _0x34f3da={};var _0x639247;var _0x2eecbc;return db[_0x7abb('0x2e')][_0x7abb('0x4d')]({'where':{'id':_0x508df9[_0x7abb('0x47')]['id']}})[_0x7abb('0x26')](handleEntityNotFound(_0x2f90fc,null))[_0x7abb('0x26')](function(_0x3b8d66){if(_0x3b8d66){_0x639247=_0x3b8d66;_0x34f3da[_0x7abb('0x32')]=_[_0x7abb('0x34')](db[_0x7abb('0x4e')][_0x7abb('0x2f')]);_0x34f3da[_0x7abb('0x33')]=_[_0x7abb('0x34')](_0x508df9['query']);_0x34f3da[_0x7abb('0x35')]=_[_0x7abb('0x37')](_0x34f3da[_0x7abb('0x32')],_0x34f3da[_0x7abb('0x33')]);_0x3a343a[_0x7abb('0x36')]=_[_0x7abb('0x37')](_0x34f3da['model'],qs[_0x7abb('0x48')](_0x508df9[_0x7abb('0x33')]['fields']));_0x3a343a[_0x7abb('0x36')]=_0x3a343a['attributes'][_0x7abb('0x38')]?_0x3a343a[_0x7abb('0x36')]:_0x34f3da['model'];_0x3a343a[_0x7abb('0x3b')]=qs[_0x7abb('0x3c')](_0x508df9['query']['sort']);_0x3a343a[_0x7abb('0x3d')]=qs[_0x7abb('0x35')](_[_0x7abb('0x3e')](_0x508df9['query'],_0x34f3da[_0x7abb('0x35')]));if(_0x508df9[_0x7abb('0x33')][_0x7abb('0x3f')]){_0x3a343a[_0x7abb('0x3d')]=_['merge'](_0x3a343a['where'],{'$or':_[_0x7abb('0x2d')](_0x3a343a[_0x7abb('0x36')],function(_0x47fe25){var _0x5a18c0={};_0x5a18c0[_0x47fe25]={'$like':'%'+_0x508df9['query'][_0x7abb('0x3f')]+'%'};return _0x5a18c0;})});}_0x3a343a=_[_0x7abb('0x40')]({},_0x3a343a,_0x508df9['options']);return _0x639247[_0x7abb('0x4c')](_0x3a343a);}})[_0x7abb('0x26')](function(_0x2413f6){if(_0x2413f6){_0x2eecbc=_0x2413f6[_0x7abb('0x38')];if(!_0x508df9[_0x7abb('0x33')]['hasOwnProperty']('nolimit')){_0x3a343a[_0x7abb('0x1f')]=qs[_0x7abb('0x1f')](_0x508df9['query']['limit']);_0x3a343a[_0x7abb('0x1d')]=qs[_0x7abb('0x1d')](_0x508df9[_0x7abb('0x33')][_0x7abb('0x1d')]);}return _0x639247['getConfigurations'](_0x3a343a);}})[_0x7abb('0x26')](function(_0x260998){if(_0x260998){return _0x260998?{'count':_0x2eecbc,'rows':_0x260998}:null;}})[_0x7abb('0x26')](respondWithResult(_0x2f90fc,null))['catch'](handleError(_0x2f90fc,null));};exports[_0x7abb('0x4f')]=function(_0x139304,_0x1e5606,_0x103d68){if(_0x139304[_0x7abb('0x4b')]['id']){delete _0x139304[_0x7abb('0x4b')]['id'];}return db[_0x7abb('0x2e')]['findOne']({'where':{'id':_0x139304['params']['id']}})['then'](handleEntityNotFound(_0x1e5606,null))[_0x7abb('0x26')](function(_0x6e2eae){if(_0x6e2eae){_0x139304['body'][_0x7abb('0x50')]=_0x6e2eae['id'];_0x139304[_0x7abb('0x4b')][_0x7abb('0x51')]=integrations[_0x7abb('0x52')](_0x139304['body'][_0x7abb('0x53')],_0x139304[_0x7abb('0x4b')][_0x7abb('0x41')]);_0x139304[_0x7abb('0x4b')][_0x7abb('0x54')]=integrations['getDescriptions'](_0x139304[_0x7abb('0x4b')]['channel'],_0x139304[_0x7abb('0x4b')][_0x7abb('0x41')]);return db[_0x7abb('0x4e')][_0x7abb('0x4a')](_0x139304['body'],{'include':[{'model':db['FreshsalesField'],'as':_0x7abb('0x51')},{'model':db[_0x7abb('0x55')],'as':'Descriptions'}]});}return null;})[_0x7abb('0x26')](respondWithResult(_0x1e5606,null))[_0x7abb('0x46')](handleError(_0x1e5606,null));};exports[_0x7abb('0x56')]=function(_0x299097,_0x1d3f8a,_0x54bd99){var _0x22e7b8='';var _0x322b68='';return db[_0x7abb('0x2e')][_0x7abb('0x4d')]({'where':{'id':_0x299097['params']['id']},'attributes':['id',_0x7abb('0x57'),'remoteUri',_0x7abb('0x58')]})[_0x7abb('0x26')](handleEntityNotFound(_0x1d3f8a,null))[_0x7abb('0x26')](function(_0x190e40){if(_0x190e40){_0x322b68=_0x190e40[_0x7abb('0x57')];_0x22e7b8=_0x190e40[_0x7abb('0x59')];var _0x3fdca7=_0x190e40['remoteUri'][_0x7abb('0x5a')](-0x1);if(_0x3fdca7==='/'){_0x22e7b8=_0x22e7b8[_0x7abb('0x5b')](0x0,_0x22e7b8[_0x7abb('0x5c')](_0x3fdca7));}return rp({'method':_0x7abb('0x5d'),'uri':util[_0x7abb('0x5e')](_0x7abb('0x5f'),_0x22e7b8,_0x7abb('0x60')),'headers':{'Authorization':_0x7abb('0x61')+_0x190e40[_0x7abb('0x58')]},'json':!![]});}})['then'](function(_0x3fd6b9){if(_0x3fd6b9){var _0x28ca32=_0x3fd6b9[_0x7abb('0x62')];var _0x859cd3=![];var _0x38d3c9=null;for(_0x38d3c9=0x0;_0x38d3c9<_0x28ca32['length'];_0x38d3c9++){if(_0x28ca32[_0x38d3c9][_0x7abb('0x63')]===_0x322b68){_0x859cd3=!![];}}if(_0x859cd3){return!![];}else{throw new Error({'statusCode':0x194,'message':_0x7abb('0x64')});}}})[_0x7abb('0x26')](respondWithResult(_0x1d3f8a,null))[_0x7abb('0x46')](function(_0x46b156){var _0x4b860c=_0x299097[_0x7abb('0x33')][_0x7abb('0x65')]?0x1f4:_0x46b156['statusCode']||0x1f4;logger[_0x7abb('0x28')]('getFields,\x20%s,\x20%s,\x20%s',_0x7abb('0x56'),_0x4b860c,JSON['stringify'](_0x46b156));delete _0x46b156['name'];if(_0x4b860c===0x191){_0x4b860c=0x190;}_0x1d3f8a[_0x7abb('0x1b')](_0x4b860c)[_0x7abb('0x2b')](_0x299097['query'][_0x7abb('0x65')]?{'message':'Wrong\x20credentials','statusCode':_0x46b156['statusCode']}:_0x46b156);});};