4af2414517f73a5f2871c427f2c36f8584c4c3c0
[motion2.git] / server / api / intSugarcrmAccount / intSugarcrmAccount.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 _0x3ff8=['nolimit','order','where','pick','filter','merge','VIRTUAL','options','includeAll','include','rows','catch','length','find','create','body','params','getConfigurations','findOne','sort','addConfiguration','Subjects','channel','Descriptions','getDescriptions','SugarcrmConfiguration','SugarcrmField','assigned_user_id','description','duration_hours','duration_minutes','parent_id','parent_type','parent_name','direction','remoteUri','substring','lastIndexOf','POST','format','%s%s','login','JSON','stringify','password','Invalid\x20Login','Password\x20Expired','/service/v4_1/rest.php','get_module_fields','Calls','values','module_fields','remove','includes','label','endsWith','value','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','Wrong\x20credentials','eml-format','rimraf','zip-dir','request-promise','bluebird','mustache','util','path','sox','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/utils','../../components/integrations/configuration','sendStatus','end','status','json','offset','undefined','count','limit','Content-Range','save','update','then','destroy','error','name','send','index','map','SugarcrmAccount','rawAttributes','type','model','keys','query','filters','intersection','attributes','fields','hasOwnProperty'];(function(_0x1651e4,_0x49c4af){var _0x349e62=function(_0x1fddef){while(--_0x1fddef){_0x1651e4['push'](_0x1651e4['shift']());}};_0x349e62(++_0x49c4af);}(_0x3ff8,0x194));var _0x83ff=function(_0x4bf4a2,_0x15f64c){_0x4bf4a2=_0x4bf4a2-0x0;var _0x1eca49=_0x3ff8[_0x4bf4a2];return _0x1eca49;};'use strict';var emlformat=require(_0x83ff('0x0'));var rimraf=require(_0x83ff('0x1'));var zipdir=require(_0x83ff('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x83ff('0x3'));var moment=require('moment');var BPromise=require(_0x83ff('0x4'));var Mustache=require(_0x83ff('0x5'));var util=require(_0x83ff('0x6'));var path=require(_0x83ff('0x7'));var sox=require(_0x83ff('0x8'));var csv=require('to-csv');var ejs=require(_0x83ff('0x9'));var fs=require('fs');var fs_extra=require(_0x83ff('0xa'));var _=require('lodash');var squel=require(_0x83ff('0xb'));var crypto=require(_0x83ff('0xc'));var jsforce=require(_0x83ff('0xd'));var deskjs=require(_0x83ff('0xe'));var toCsv=require('to-csv');var querystring=require(_0x83ff('0xf'));var Papa=require(_0x83ff('0x10'));var Redis=require(_0x83ff('0x11'));var authService=require(_0x83ff('0x12'));var qs=require(_0x83ff('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x83ff('0x14'));var logger=require('../../config/logger')('api');var utils=require(_0x83ff('0x15'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];var integrations=require(_0x83ff('0x16'));function respondWithStatusCode(_0x2ca8f1,_0x428833){_0x428833=_0x428833||0xcc;return function(_0x1ac763){if(_0x1ac763){return _0x2ca8f1[_0x83ff('0x17')](_0x428833);}return _0x2ca8f1['status'](_0x428833)[_0x83ff('0x18')]();};}function respondWithResult(_0x5a4c22,_0x2e8fc9){_0x2e8fc9=_0x2e8fc9||0xc8;return function(_0x10b147){if(_0x10b147){return _0x5a4c22[_0x83ff('0x19')](_0x2e8fc9)[_0x83ff('0x1a')](_0x10b147);}};}function respondWithFilteredResult(_0x5b44e6,_0x4df331){return function(_0x1f8205){if(_0x1f8205){var _0x19f576=typeof _0x4df331[_0x83ff('0x1b')]===_0x83ff('0x1c')&&typeof _0x4df331['limit']===_0x83ff('0x1c');var _0x4fbf85=_0x1f8205[_0x83ff('0x1d')];var _0x149a11=_0x19f576?0x0:_0x4df331[_0x83ff('0x1b')];var _0x5f2188=_0x19f576?_0x1f8205[_0x83ff('0x1d')]:_0x4df331[_0x83ff('0x1b')]+_0x4df331[_0x83ff('0x1e')];var _0x50598e;if(_0x5f2188>=_0x4fbf85){_0x5f2188=_0x4fbf85;_0x50598e=0xc8;}else{_0x50598e=0xce;}_0x5b44e6[_0x83ff('0x19')](_0x50598e);return _0x5b44e6['set'](_0x83ff('0x1f'),_0x149a11+'-'+_0x5f2188+'/'+_0x4fbf85)[_0x83ff('0x1a')](_0x1f8205);}return null;};}function patchUpdates(_0x1e567d){return function(_0xcbd32e){try{jsonpatch['apply'](_0xcbd32e,_0x1e567d,!![]);}catch(_0x2b0279){return BPromise['reject'](_0x2b0279);}return _0xcbd32e[_0x83ff('0x20')]();};}function saveUpdates(_0x48dba2,_0x15d180){return function(_0x29b884){if(_0x29b884){return _0x29b884[_0x83ff('0x21')](_0x48dba2)[_0x83ff('0x22')](function(_0x121db7){return _0x121db7;});}return null;};}function removeEntity(_0x5ae45f,_0x51882c){return function(_0x200180){if(_0x200180){return _0x200180[_0x83ff('0x23')]()['then'](function(){_0x5ae45f[_0x83ff('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x30426d,_0x193f56){return function(_0x19086e){if(!_0x19086e){_0x30426d['sendStatus'](0x194);}return _0x19086e;};}function handleError(_0x546d87,_0x4bcb4a){_0x4bcb4a=_0x4bcb4a||0x1f4;return function(_0x526e93){logger[_0x83ff('0x24')](_0x526e93['stack']);if(_0x526e93[_0x83ff('0x25')]){delete _0x526e93[_0x83ff('0x25')];}_0x546d87[_0x83ff('0x19')](_0x4bcb4a)[_0x83ff('0x26')](_0x526e93);};}exports[_0x83ff('0x27')]=function(_0x2c7b56,_0x3911ec){var _0x1dc58b={},_0x500e16={},_0x3a9cda={'count':0x0,'rows':[]};var _0xbc19b2=_[_0x83ff('0x28')](db[_0x83ff('0x29')][_0x83ff('0x2a')],function(_0x311d6e){return{'name':_0x311d6e['fieldName'],'type':_0x311d6e[_0x83ff('0x2b')]['key']};});_0x500e16[_0x83ff('0x2c')]=_[_0x83ff('0x28')](_0xbc19b2,_0x83ff('0x25'));_0x500e16['query']=_[_0x83ff('0x2d')](_0x2c7b56[_0x83ff('0x2e')]);_0x500e16[_0x83ff('0x2f')]=_[_0x83ff('0x30')](_0x500e16[_0x83ff('0x2c')],_0x500e16[_0x83ff('0x2e')]);_0x1dc58b[_0x83ff('0x31')]=_[_0x83ff('0x30')](_0x500e16[_0x83ff('0x2c')],qs['fields'](_0x2c7b56['query'][_0x83ff('0x32')]));_0x1dc58b[_0x83ff('0x31')]=_0x1dc58b[_0x83ff('0x31')]['length']?_0x1dc58b['attributes']:_0x500e16['model'];if(!_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x33')](_0x83ff('0x34'))){_0x1dc58b['limit']=qs['limit'](_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x1e')]);_0x1dc58b[_0x83ff('0x1b')]=qs[_0x83ff('0x1b')](_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x1b')]);}_0x1dc58b[_0x83ff('0x35')]=qs['sort'](_0x2c7b56['query']['sort']);_0x1dc58b[_0x83ff('0x36')]=qs[_0x83ff('0x2f')](_[_0x83ff('0x37')](_0x2c7b56['query'],_0x500e16[_0x83ff('0x2f')]),_0xbc19b2);if(_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x38')]){_0x1dc58b[_0x83ff('0x36')]=_[_0x83ff('0x39')](_0x1dc58b[_0x83ff('0x36')],{'$or':_['map'](_0xbc19b2,function(_0x49fc68){if(_0x49fc68[_0x83ff('0x2b')]!==_0x83ff('0x3a')){var _0x432d88={};_0x432d88[_0x49fc68[_0x83ff('0x25')]]={'$like':'%'+_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x38')]+'%'};return _0x432d88;}})});}_0x1dc58b=_['merge']({},_0x1dc58b,_0x2c7b56[_0x83ff('0x3b')]);var _0x4cf5e7={'where':_0x1dc58b[_0x83ff('0x36')]};return db[_0x83ff('0x29')]['count'](_0x4cf5e7)['then'](function(_0x45eb4f){_0x3a9cda[_0x83ff('0x1d')]=_0x45eb4f;if(_0x2c7b56[_0x83ff('0x2e')][_0x83ff('0x3c')]){_0x1dc58b[_0x83ff('0x3d')]=[{'all':!![]}];}return db['SugarcrmAccount']['findAll'](_0x1dc58b);})[_0x83ff('0x22')](function(_0x1c2782){_0x3a9cda[_0x83ff('0x3e')]=_0x1c2782;return _0x3a9cda;})[_0x83ff('0x22')](respondWithFilteredResult(_0x3911ec,_0x1dc58b))[_0x83ff('0x3f')](handleError(_0x3911ec,null));};exports['show']=function(_0x481c40,_0x4400ec){var _0x5523a6={'raw':![],'where':{'id':_0x481c40['params']['id']}},_0x82478a={};_0x82478a[_0x83ff('0x2c')]=_[_0x83ff('0x2d')](db[_0x83ff('0x29')][_0x83ff('0x2a')]);_0x82478a[_0x83ff('0x2e')]=_[_0x83ff('0x2d')](_0x481c40[_0x83ff('0x2e')]);_0x82478a[_0x83ff('0x2f')]=_[_0x83ff('0x30')](_0x82478a[_0x83ff('0x2c')],_0x82478a[_0x83ff('0x2e')]);_0x5523a6['attributes']=_[_0x83ff('0x30')](_0x82478a['model'],qs['fields'](_0x481c40[_0x83ff('0x2e')][_0x83ff('0x32')]));_0x5523a6['attributes']=_0x5523a6[_0x83ff('0x31')][_0x83ff('0x40')]?_0x5523a6[_0x83ff('0x31')]:_0x82478a['model'];if(_0x481c40[_0x83ff('0x2e')]['includeAll']){_0x5523a6['include']=[{'all':!![]}];}_0x5523a6=_[_0x83ff('0x39')]({},_0x5523a6,_0x481c40[_0x83ff('0x3b')]);return db['SugarcrmAccount'][_0x83ff('0x41')](_0x5523a6)[_0x83ff('0x22')](handleEntityNotFound(_0x4400ec,null))[_0x83ff('0x22')](respondWithResult(_0x4400ec,null))['catch'](handleError(_0x4400ec,null));};exports[_0x83ff('0x42')]=function(_0x2bc4aa,_0x9e91ce){return db[_0x83ff('0x29')]['create'](_0x2bc4aa[_0x83ff('0x43')],{})[_0x83ff('0x22')](respondWithResult(_0x9e91ce,0xc9))[_0x83ff('0x3f')](handleError(_0x9e91ce,null));};exports[_0x83ff('0x21')]=function(_0xb2de24,_0x474987){if(_0xb2de24[_0x83ff('0x43')]['id']){delete _0xb2de24[_0x83ff('0x43')]['id'];}return db[_0x83ff('0x29')][_0x83ff('0x41')]({'where':{'id':_0xb2de24[_0x83ff('0x44')]['id']}})[_0x83ff('0x22')](handleEntityNotFound(_0x474987,null))[_0x83ff('0x22')](saveUpdates(_0xb2de24[_0x83ff('0x43')],null))[_0x83ff('0x22')](respondWithResult(_0x474987,null))[_0x83ff('0x3f')](handleError(_0x474987,null));};exports[_0x83ff('0x23')]=function(_0x1827b3,_0x326f41){return db['SugarcrmAccount']['find']({'where':{'id':_0x1827b3[_0x83ff('0x44')]['id']}})[_0x83ff('0x22')](handleEntityNotFound(_0x326f41,null))['then'](removeEntity(_0x326f41,null))[_0x83ff('0x3f')](handleError(_0x326f41,null));};exports[_0x83ff('0x45')]=function(_0x5738e7,_0x1e87a7,_0x9644d){var _0x42c771={};var _0x5dca5e={};var _0xd89752;var _0x25cc73;return db[_0x83ff('0x29')][_0x83ff('0x46')]({'where':{'id':_0x5738e7[_0x83ff('0x44')]['id']}})[_0x83ff('0x22')](handleEntityNotFound(_0x1e87a7,null))[_0x83ff('0x22')](function(_0x4c0281){if(_0x4c0281){_0xd89752=_0x4c0281;_0x5dca5e[_0x83ff('0x2c')]=_['keys'](db['SugarcrmConfiguration'][_0x83ff('0x2a')]);_0x5dca5e[_0x83ff('0x2e')]=_[_0x83ff('0x2d')](_0x5738e7[_0x83ff('0x2e')]);_0x5dca5e[_0x83ff('0x2f')]=_[_0x83ff('0x30')](_0x5dca5e[_0x83ff('0x2c')],_0x5dca5e['query']);_0x42c771[_0x83ff('0x31')]=_[_0x83ff('0x30')](_0x5dca5e['model'],qs['fields'](_0x5738e7[_0x83ff('0x2e')]['fields']));_0x42c771[_0x83ff('0x31')]=_0x42c771[_0x83ff('0x31')]['length']?_0x42c771['attributes']:_0x5dca5e['model'];_0x42c771[_0x83ff('0x35')]=qs[_0x83ff('0x47')](_0x5738e7[_0x83ff('0x2e')][_0x83ff('0x47')]);_0x42c771[_0x83ff('0x36')]=qs['filters'](_['pick'](_0x5738e7[_0x83ff('0x2e')],_0x5dca5e['filters']));if(_0x5738e7['query'][_0x83ff('0x38')]){_0x42c771['where']=_[_0x83ff('0x39')](_0x42c771[_0x83ff('0x36')],{'$or':_['map'](_0x42c771[_0x83ff('0x31')],function(_0x12095c){var _0x4cd922={};_0x4cd922[_0x12095c]={'$like':'%'+_0x5738e7[_0x83ff('0x2e')][_0x83ff('0x38')]+'%'};return _0x4cd922;})});}_0x42c771=_[_0x83ff('0x39')]({},_0x42c771,_0x5738e7[_0x83ff('0x3b')]);return _0xd89752[_0x83ff('0x45')](_0x42c771);}})[_0x83ff('0x22')](function(_0x2fdc50){if(_0x2fdc50){_0x25cc73=_0x2fdc50[_0x83ff('0x40')];if(!_0x5738e7['query']['hasOwnProperty'](_0x83ff('0x34'))){_0x42c771[_0x83ff('0x1e')]=qs[_0x83ff('0x1e')](_0x5738e7[_0x83ff('0x2e')][_0x83ff('0x1e')]);_0x42c771[_0x83ff('0x1b')]=qs[_0x83ff('0x1b')](_0x5738e7[_0x83ff('0x2e')][_0x83ff('0x1b')]);}return _0xd89752['getConfigurations'](_0x42c771);}})['then'](function(_0x331e54){if(_0x331e54){return _0x331e54?{'count':_0x25cc73,'rows':_0x331e54}:null;}})[_0x83ff('0x22')](respondWithResult(_0x1e87a7,null))[_0x83ff('0x3f')](handleError(_0x1e87a7,null));};exports[_0x83ff('0x48')]=function(_0x19b108,_0x471e8b,_0x436afd){if(_0x19b108[_0x83ff('0x43')]['id']){delete _0x19b108[_0x83ff('0x43')]['id'];}return db[_0x83ff('0x29')][_0x83ff('0x46')]({'where':{'id':_0x19b108['params']['id']}})[_0x83ff('0x22')](handleEntityNotFound(_0x471e8b,null))[_0x83ff('0x22')](function(_0x4da1ff){if(_0x4da1ff){_0x19b108['body']['AccountId']=_0x4da1ff['id'];_0x19b108[_0x83ff('0x43')][_0x83ff('0x49')]=integrations['getSubjects'](_0x19b108['body'][_0x83ff('0x4a')],_0x19b108[_0x83ff('0x43')][_0x83ff('0x2b')]);_0x19b108['body'][_0x83ff('0x4b')]=integrations[_0x83ff('0x4c')](_0x19b108[_0x83ff('0x43')][_0x83ff('0x4a')],_0x19b108[_0x83ff('0x43')][_0x83ff('0x2b')]);return db[_0x83ff('0x4d')][_0x83ff('0x42')](_0x19b108[_0x83ff('0x43')],{'include':[{'model':db['SugarcrmField'],'as':'Subjects'},{'model':db[_0x83ff('0x4e')],'as':_0x83ff('0x4b')}]});}return null;})['then'](respondWithResult(_0x471e8b,null))[_0x83ff('0x3f')](handleError(_0x471e8b,null));};exports['getFields']=function(_0x20fffb,_0x38b562,_0x18ef89){var _0x400b5c='';var _0x5e4add=['id',_0x83ff('0x25'),_0x83ff('0x4f'),'created_by',_0x83ff('0x50'),'date_start',_0x83ff('0x51'),_0x83ff('0x52'),_0x83ff('0x53'),_0x83ff('0x54'),_0x83ff('0x55'),_0x83ff('0x56')];return db[_0x83ff('0x29')]['findOne']({'where':{'id':_0x20fffb['params']['id']},'attributes':['id','username','password',_0x83ff('0x57')]})[_0x83ff('0x22')](handleEntityNotFound(_0x38b562,null))['then'](function(_0x300f08){if(_0x300f08){_0x400b5c=_0x300f08[_0x83ff('0x57')];var _0x256552=_0x400b5c['slice'](-0x1);if(_0x256552==='/'){_0x400b5c=_0x400b5c[_0x83ff('0x58')](0x0,_0x400b5c[_0x83ff('0x59')](_0x256552));}return rp({'method':_0x83ff('0x5a'),'uri':util[_0x83ff('0x5b')](_0x83ff('0x5c'),_0x400b5c,'/service/v4_1/rest.php'),'json':!![],'form':{'method':_0x83ff('0x5d'),'input_type':'JSON','response_type':_0x83ff('0x5e'),'rest_data':JSON[_0x83ff('0x5f')]({'user_auth':{'user_name':_0x300f08['username'],'password':_0x300f08[_0x83ff('0x60')],'encryption':'PLAIN'},'application':'xCALLY\x20Motion\x20Integration'})}});}})[_0x83ff('0x22')](function(_0xe54d1d){if(_0xe54d1d&&_0xe54d1d[_0x83ff('0x25')]!==_0x83ff('0x61')&&_0xe54d1d[_0x83ff('0x25')]!==_0x83ff('0x62')){return rp({'method':'POST','uri':util['format'](_0x83ff('0x5c'),_0x400b5c,_0x83ff('0x63')),'json':!![],'form':{'method':_0x83ff('0x64'),'input_type':_0x83ff('0x5e'),'response_type':_0x83ff('0x5e'),'rest_data':JSON[_0x83ff('0x5f')]({'session':_0xe54d1d['id'],'module_name':_0x83ff('0x65')})}})[_0x83ff('0x22')](function(_0x5905a7){if(_0x5905a7){var _0x26d7d4=_[_0x83ff('0x66')](_0x5905a7[_0x83ff('0x67')]);_[_0x83ff('0x68')](_0x26d7d4,function(_0x5d3dba){return _0x5e4add[_0x83ff('0x69')](_0x5d3dba[_0x83ff('0x25')]);});return{'count':_0x26d7d4[_0x83ff('0x40')],'rows':_[_0x83ff('0x28')](_0x26d7d4,function(_0x26efe6){return{'id':_0x26efe6['name'],'name':_0x26efe6[_0x83ff('0x6a')],'custom':_[_0x83ff('0x6b')](_0x26efe6[_0x83ff('0x25')],'_c')?!![]:![],'options':_0x26efe6['options']?_[_0x83ff('0x28')](_[_0x83ff('0x66')](_0x26efe6[_0x83ff('0x3b')]),function(_0x590101){return{'name':_0x590101[_0x83ff('0x6c')],'value':_0x590101[_0x83ff('0x25')]};}):[]};})};}})[_0x83ff('0x22')](respondWithResult(_0x38b562,null))[_0x83ff('0x3f')](function(_0x4bb662){var _0x18bbcc=_0x20fffb[_0x83ff('0x2e')][_0x83ff('0x6d')]?0x1f4:_0x4bb662[_0x83ff('0x6e')]||0x1f4;logger['error'](_0x83ff('0x6f'),_0x83ff('0x70'),_0x18bbcc,JSON['stringify'](_0x4bb662));delete _0x4bb662[_0x83ff('0x25')];if(_0x18bbcc===0x191){_0x18bbcc=0x190;}_0x38b562['status'](_0x18bbcc)[_0x83ff('0x26')](_0x20fffb[_0x83ff('0x2e')]['test']?{'message':'Wrong\x20credentials','statusCode':_0x4bb662[_0x83ff('0x6e')]}:_0x4bb662);});}else{var _0x47577c=0x1f4;_0x38b562['status'](_0x47577c)[_0x83ff('0x26')]({'message':_0x83ff('0x71'),'statusCode':0x190});}});};