Built motion from commit (unavailable).|2.2.0
[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 _0xf315=['message','catch','status','end','json','count','offset','limit','Content-Range','apply','reject','save','update','destroy','sendStatus','stack','name','SugarcrmAccount','rawAttributes','model','keys','dateStart','dateEnd','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','add','day','merge','filter','map','key','VIRTUAL','options','includeAll','include','rows','params','create','body','find','getConfigurations','findOne','SugarcrmConfiguration','addConfiguration','AccountId','Subjects','getSubjects','channel','type','Descriptions','getDescriptions','SugarcrmField','getFields','username','password','remoteUri','POST','%s%s','/service/v4_1/rest.php','login','JSON','xCALLY\x20Motion\x20Integration','format','get_module_fields','Calls','values','module_fields','label','endsWith','value','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','html-pdf','rimraf','zip-dir','moment','bluebird','mustache','util','path','to-csv','lodash','squel','crypto','jsforce','desk.js','querystring','../../components/parsers/qs','api','../../config/utils','../../config/environment','../../mysqldb','../../components/integrations/configuration','client','http','then','info','SugarcrmAccount,\x20%s,\x20%s','debug','SugarcrmAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code'];(function(_0x5381fb,_0x374431){var _0x293909=function(_0xa42161){while(--_0xa42161){_0x5381fb['push'](_0x5381fb['shift']());}};_0x293909(++_0x374431);}(_0xf315,0xc8));var _0x5f31=function(_0x14add3,_0x13ce8d){_0x14add3=_0x14add3-0x0;var _0x4a174f=_0xf315[_0x14add3];return _0x4a174f;};'use strict';var pdf=require(_0x5f31('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x5f31('0x1'));var zipdir=require(_0x5f31('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x5f31('0x3'));var BPromise=require(_0x5f31('0x4'));var Mustache=require(_0x5f31('0x5'));var util=require(_0x5f31('0x6'));var path=require(_0x5f31('0x7'));var sox=require('sox');var csv=require(_0x5f31('0x8'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x5f31('0x9'));var squel=require(_0x5f31('0xa'));var crypto=require(_0x5f31('0xb'));var jsforce=require(_0x5f31('0xc'));var deskjs=require(_0x5f31('0xd'));var toCsv=require('to-csv');var querystring=require(_0x5f31('0xe'));var Papa=require('papaparse');var Redis=require('ioredis');var qs=require(_0x5f31('0xf'));var logger=require('../../config/logger')(_0x5f31('0x10'));var utils=require(_0x5f31('0x11'));var config=require(_0x5f31('0x12'));var db=require(_0x5f31('0x13'))['db'];var integrations=require(_0x5f31('0x14'));var jayson=require('jayson/promise');var client=jayson[_0x5f31('0x15')][_0x5f31('0x16')]({'port':0x232a});function respondWithRpcPromise(_0x2f188f,_0x58615a,_0x4d12e0,_0x273d65){return new BPromise(function(_0x2c7948,_0x276eb5){var _0x2eaa00=_0x273d65||client;return _0x2eaa00['request'](_0x2f188f,_0x4d12e0)[_0x5f31('0x17')](function(_0x35d51e){logger[_0x5f31('0x18')](_0x5f31('0x19'),_0x58615a,'request\x20sent');logger[_0x5f31('0x1a')](_0x5f31('0x1b'),_0x58615a,_0x5f31('0x1c'),JSON[_0x5f31('0x1d')](_0x35d51e));if(_0x35d51e[_0x5f31('0x1e')]){if(_0x35d51e[_0x5f31('0x1e')][_0x5f31('0x1f')]===0x1f4){logger[_0x5f31('0x1e')]('SugarcrmAccount,\x20%s,\x20%s',_0x58615a,_0x35d51e[_0x5f31('0x1e')][_0x5f31('0x20')]);return _0x276eb5(_0x35d51e[_0x5f31('0x1e')][_0x5f31('0x20')]);}logger[_0x5f31('0x1e')](_0x5f31('0x19'),_0x58615a,_0x35d51e[_0x5f31('0x1e')][_0x5f31('0x20')]);return _0x2c7948(_0x35d51e['error']['message']);}else{logger['info'](_0x5f31('0x19'),_0x58615a,'request\x20sent');_0x2c7948(_0x35d51e['result'][_0x5f31('0x20')]);}})[_0x5f31('0x21')](function(_0x523314){logger[_0x5f31('0x1e')](_0x5f31('0x19'),_0x58615a,_0x523314);_0x276eb5(_0x523314);});});}function respondWithStatusCode(_0x418a45,_0x4185b1){_0x4185b1=_0x4185b1||0xcc;return function(_0x2ba7ff){if(_0x2ba7ff){return _0x418a45['sendStatus'](_0x4185b1);}return _0x418a45[_0x5f31('0x22')](_0x4185b1)[_0x5f31('0x23')]();};}function respondWithResult(_0x31c90b,_0x5c2394){_0x5c2394=_0x5c2394||0xc8;return function(_0x326cbf){if(_0x326cbf){return _0x31c90b[_0x5f31('0x22')](_0x5c2394)[_0x5f31('0x24')](_0x326cbf);}};}function respondWithFilteredResult(_0x745207,_0xa9647a){return function(_0x137c91){if(_0x137c91){var _0x4a0391=_0x137c91[_0x5f31('0x25')],_0x353459=_0xa9647a[_0x5f31('0x26')],_0x40f9be=_0xa9647a[_0x5f31('0x26')]+_0xa9647a[_0x5f31('0x27')],_0x565c24;if(_0x40f9be>=_0x4a0391){_0x40f9be=_0x4a0391;_0x565c24=0xc8;}else{_0x565c24=0xce;}_0x745207[_0x5f31('0x22')](_0x565c24);return _0x745207['set'](_0x5f31('0x28'),_0x353459+'-'+_0x40f9be+'/'+_0x4a0391)[_0x5f31('0x24')](_0x137c91);}return null;};}function patchUpdates(_0xa0f752){return function(_0x4f4402){try{jsonpatch[_0x5f31('0x29')](_0x4f4402,_0xa0f752,!![]);}catch(_0x2e4361){return BPromise[_0x5f31('0x2a')](_0x2e4361);}return _0x4f4402[_0x5f31('0x2b')]();};}function saveUpdates(_0x2c4931,_0x985529){return function(_0x2e138f){if(_0x2e138f){return _0x2e138f[_0x5f31('0x2c')](_0x2c4931)['then'](function(_0x4108e3){return _0x4108e3;});}return null;};}function removeEntity(_0x5a670a,_0x892f77){return function(_0xff150e){if(_0xff150e){return _0xff150e[_0x5f31('0x2d')]()['then'](function(){_0x5a670a['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x36ecec,_0xf45a3a){return function(_0x2152a6){if(!_0x2152a6){_0x36ecec[_0x5f31('0x2e')](0x194);}return _0x2152a6;};}function handleError(_0x3c99d1,_0x4ab03c){_0x4ab03c=_0x4ab03c||0x1f4;return function(_0x4d8e3a){logger[_0x5f31('0x1e')](_0x4d8e3a[_0x5f31('0x2f')]);if(_0x4d8e3a[_0x5f31('0x30')]){delete _0x4d8e3a[_0x5f31('0x30')];}_0x3c99d1[_0x5f31('0x22')](_0x4ab03c)['send'](_0x4d8e3a);};}exports['index']=function(_0x1b2896,_0x1992e3){var _0x248ba0={},_0x151073={},_0x38c058={'count':0x0,'rows':[]};var _0x36d279=db[_0x5f31('0x31')][_0x5f31('0x32')];_0x151073[_0x5f31('0x33')]=_[_0x5f31('0x34')](_0x36d279);var _0x4ee915={'dateStart':_0x1b2896['query'][_0x5f31('0x35')],'dateEnd':_0x1b2896['query'][_0x5f31('0x36')]};delete _0x1b2896['query']['dateStart'];delete _0x1b2896[_0x5f31('0x37')]['dateEnd'];_0x151073['query']=_[_0x5f31('0x34')](_0x1b2896['query']);_0x151073[_0x5f31('0x38')]=_[_0x5f31('0x39')](_0x151073[_0x5f31('0x33')],_0x151073[_0x5f31('0x37')]);_0x248ba0[_0x5f31('0x3a')]=_[_0x5f31('0x39')](_0x151073['model'],qs['fields'](_0x1b2896['query'][_0x5f31('0x3b')]));_0x248ba0[_0x5f31('0x3a')]=_0x248ba0[_0x5f31('0x3a')][_0x5f31('0x3c')]?_0x248ba0['attributes']:_0x151073['model'];if(!_0x1b2896[_0x5f31('0x37')][_0x5f31('0x3d')](_0x5f31('0x3e'))){_0x248ba0['limit']=qs['limit'](_0x1b2896[_0x5f31('0x37')]['limit']);_0x248ba0[_0x5f31('0x26')]=qs[_0x5f31('0x26')](_0x1b2896[_0x5f31('0x37')][_0x5f31('0x26')]);}_0x248ba0[_0x5f31('0x3f')]=qs[_0x5f31('0x40')](_0x1b2896[_0x5f31('0x37')]['sort']);_0x248ba0[_0x5f31('0x41')]=qs[_0x5f31('0x38')](_[_0x5f31('0x42')](_0x1b2896[_0x5f31('0x37')],_0x151073[_0x5f31('0x38')]));if(_0x4ee915[_0x5f31('0x35')]){if(_0x4ee915[_0x5f31('0x36')]){_0x4ee915[_0x5f31('0x36')]=moment(_0x4ee915[_0x5f31('0x36')])[_0x5f31('0x43')](0x1,_0x5f31('0x44'));}else{_0x4ee915['dateEnd']=moment(_0x4ee915['dateStart'])[_0x5f31('0x43')](0x1,'day');}_[_0x5f31('0x45')](_0x248ba0[_0x5f31('0x41')],{'createdAt':{'$gte':_0x4ee915[_0x5f31('0x35')],'$lte':_0x4ee915[_0x5f31('0x36')]}});}if(_0x1b2896[_0x5f31('0x37')][_0x5f31('0x46')]){_0x248ba0['where']=_[_0x5f31('0x45')](_0x248ba0['where'],{'$or':_[_0x5f31('0x47')](_0x36d279,function(_0xecefc0){if(_0xecefc0['type'][_0x5f31('0x48')]!==_0x5f31('0x49')){var _0x3f9c07={};_0x3f9c07[_0xecefc0['field']]={'$like':'%'+_0x1b2896[_0x5f31('0x37')][_0x5f31('0x46')]+'%'};return _0x3f9c07;}})});}_0x248ba0=_[_0x5f31('0x45')]({},_0x248ba0,_0x1b2896[_0x5f31('0x4a')]);var _0x697be6={'where':_0x248ba0[_0x5f31('0x41')]};return db[_0x5f31('0x31')]['count'](_0x697be6)[_0x5f31('0x17')](function(_0x5dc909){_0x38c058[_0x5f31('0x25')]=_0x5dc909;if(_0x1b2896[_0x5f31('0x37')][_0x5f31('0x4b')]){_0x248ba0[_0x5f31('0x4c')]=[{'all':!![]}];}return db[_0x5f31('0x31')]['findAll'](_0x248ba0);})[_0x5f31('0x17')](function(_0x4e36cb){_0x38c058[_0x5f31('0x4d')]=_0x4e36cb;return _0x38c058;})[_0x5f31('0x17')](respondWithFilteredResult(_0x1992e3,_0x248ba0))['catch'](handleError(_0x1992e3,null));};exports['show']=function(_0x3f7803,_0x5aa31a){var _0x28dc91={'raw':![],'where':{'id':_0x3f7803[_0x5f31('0x4e')]['id']}},_0x437dd7={};_0x437dd7[_0x5f31('0x33')]=_[_0x5f31('0x34')](db['SugarcrmAccount'][_0x5f31('0x32')]);_0x437dd7[_0x5f31('0x37')]=_['keys'](_0x3f7803[_0x5f31('0x37')]);_0x437dd7[_0x5f31('0x38')]=_[_0x5f31('0x39')](_0x437dd7[_0x5f31('0x33')],_0x437dd7['query']);_0x28dc91[_0x5f31('0x3a')]=_[_0x5f31('0x39')](_0x437dd7[_0x5f31('0x33')],qs[_0x5f31('0x3b')](_0x3f7803[_0x5f31('0x37')]['fields']));_0x28dc91[_0x5f31('0x3a')]=_0x28dc91[_0x5f31('0x3a')][_0x5f31('0x3c')]?_0x28dc91[_0x5f31('0x3a')]:_0x437dd7[_0x5f31('0x33')];if(_0x3f7803[_0x5f31('0x37')][_0x5f31('0x4b')]){_0x28dc91[_0x5f31('0x4c')]=[{'all':!![]}];}_0x28dc91=_['merge']({},_0x28dc91,_0x3f7803[_0x5f31('0x4a')]);return db[_0x5f31('0x31')]['find'](_0x28dc91)[_0x5f31('0x17')](handleEntityNotFound(_0x5aa31a,null))[_0x5f31('0x17')](respondWithResult(_0x5aa31a,null))[_0x5f31('0x21')](handleError(_0x5aa31a,null));};exports[_0x5f31('0x4f')]=function(_0x442caa,_0x3e4ad9){return db[_0x5f31('0x31')]['create'](_0x442caa['body'],{})[_0x5f31('0x17')](respondWithResult(_0x3e4ad9,0xc9))[_0x5f31('0x21')](handleError(_0x3e4ad9,null));};exports[_0x5f31('0x2c')]=function(_0x48e4b7,_0x465bf3){if(_0x48e4b7[_0x5f31('0x50')]['id']){delete _0x48e4b7[_0x5f31('0x50')]['id'];}return db['SugarcrmAccount']['find']({'where':{'id':_0x48e4b7[_0x5f31('0x4e')]['id']}})[_0x5f31('0x17')](handleEntityNotFound(_0x465bf3,null))[_0x5f31('0x17')](saveUpdates(_0x48e4b7['body'],null))[_0x5f31('0x17')](respondWithResult(_0x465bf3,null))['catch'](handleError(_0x465bf3,null));};exports[_0x5f31('0x2d')]=function(_0x301259,_0x585b76){return db['SugarcrmAccount'][_0x5f31('0x51')]({'where':{'id':_0x301259[_0x5f31('0x4e')]['id']}})[_0x5f31('0x17')](handleEntityNotFound(_0x585b76,null))['then'](removeEntity(_0x585b76,null))['catch'](handleError(_0x585b76,null));};exports[_0x5f31('0x52')]=function(_0x48a4a7,_0x3c612a,_0x28ebe2){var _0x26a948={};var _0x2a64e9={};var _0x7cc5ff;var _0x3793fa;return db['SugarcrmAccount'][_0x5f31('0x53')]({'where':{'id':_0x48a4a7[_0x5f31('0x4e')]['id']}})[_0x5f31('0x17')](handleEntityNotFound(_0x3c612a,null))[_0x5f31('0x17')](function(_0x2b5236){if(_0x2b5236){_0x7cc5ff=_0x2b5236;_0x2a64e9['model']=_['keys'](db[_0x5f31('0x54')]['rawAttributes']);_0x2a64e9[_0x5f31('0x37')]=_[_0x5f31('0x34')](_0x48a4a7['query']);_0x2a64e9[_0x5f31('0x38')]=_['intersection'](_0x2a64e9[_0x5f31('0x33')],_0x2a64e9['query']);_0x26a948[_0x5f31('0x3a')]=_['intersection'](_0x2a64e9[_0x5f31('0x33')],qs['fields'](_0x48a4a7[_0x5f31('0x37')][_0x5f31('0x3b')]));_0x26a948[_0x5f31('0x3a')]=_0x26a948[_0x5f31('0x3a')]['length']?_0x26a948['attributes']:_0x2a64e9[_0x5f31('0x33')];_0x26a948[_0x5f31('0x3f')]=qs[_0x5f31('0x40')](_0x48a4a7['query']['sort']);_0x26a948[_0x5f31('0x41')]=qs[_0x5f31('0x38')](_['pick'](_0x48a4a7[_0x5f31('0x37')],_0x2a64e9[_0x5f31('0x38')]));if(_0x48a4a7[_0x5f31('0x37')]['filter']){_0x26a948[_0x5f31('0x41')]=_[_0x5f31('0x45')](_0x26a948[_0x5f31('0x41')],{'$or':_[_0x5f31('0x47')](_0x26a948['attributes'],function(_0x55f3f6){var _0x148588={};_0x148588[_0x55f3f6]={'$like':'%'+_0x48a4a7['query'][_0x5f31('0x46')]+'%'};return _0x148588;})});}_0x26a948=_[_0x5f31('0x45')]({},_0x26a948,_0x48a4a7[_0x5f31('0x4a')]);return _0x7cc5ff[_0x5f31('0x52')](_0x26a948);}})[_0x5f31('0x17')](function(_0x1dd150){if(_0x1dd150){_0x3793fa=_0x1dd150[_0x5f31('0x3c')];if(!_0x48a4a7[_0x5f31('0x37')][_0x5f31('0x3d')](_0x5f31('0x3e'))){_0x26a948[_0x5f31('0x27')]=qs['limit'](_0x48a4a7[_0x5f31('0x37')][_0x5f31('0x27')]);_0x26a948[_0x5f31('0x26')]=qs[_0x5f31('0x26')](_0x48a4a7[_0x5f31('0x37')]['offset']);}return _0x7cc5ff[_0x5f31('0x52')](_0x26a948);}})[_0x5f31('0x17')](function(_0x4bdb5c){if(_0x4bdb5c){return _0x4bdb5c?{'count':_0x3793fa,'rows':_0x4bdb5c}:null;}})['then'](respondWithResult(_0x3c612a,null))[_0x5f31('0x21')](handleError(_0x3c612a,null));};exports[_0x5f31('0x55')]=function(_0x626cf0,_0x486277,_0x51cfff){if(_0x626cf0[_0x5f31('0x50')]['id']){delete _0x626cf0['body']['id'];}return db[_0x5f31('0x31')][_0x5f31('0x53')]({'where':{'id':_0x626cf0[_0x5f31('0x4e')]['id']}})[_0x5f31('0x17')](handleEntityNotFound(_0x486277,null))[_0x5f31('0x17')](function(_0x529dfb){if(_0x529dfb){_0x626cf0[_0x5f31('0x50')][_0x5f31('0x56')]=_0x529dfb['id'];_0x626cf0[_0x5f31('0x50')][_0x5f31('0x57')]=integrations[_0x5f31('0x58')](_0x626cf0['body'][_0x5f31('0x59')],_0x626cf0[_0x5f31('0x50')][_0x5f31('0x5a')]);_0x626cf0[_0x5f31('0x50')][_0x5f31('0x5b')]=integrations[_0x5f31('0x5c')](_0x626cf0[_0x5f31('0x50')][_0x5f31('0x59')],_0x626cf0[_0x5f31('0x50')]['type']);return db[_0x5f31('0x54')]['create'](_0x626cf0[_0x5f31('0x50')],{'include':[{'model':db['SugarcrmField'],'as':'Subjects'},{'model':db[_0x5f31('0x5d')],'as':_0x5f31('0x5b')}]});}return null;})[_0x5f31('0x17')](respondWithResult(_0x486277,null))['catch'](handleError(_0x486277,null));};exports[_0x5f31('0x5e')]=function(_0x4cd94a,_0x102704,_0x50ad63){var _0x32e298;return db[_0x5f31('0x31')]['findOne']({'where':{'id':_0x4cd94a[_0x5f31('0x4e')]['id']},'attributes':['id',_0x5f31('0x5f'),_0x5f31('0x60'),'remoteUri']})[_0x5f31('0x17')](handleEntityNotFound(_0x102704,null))[_0x5f31('0x17')](function(_0x4dc4e3){if(_0x4dc4e3){_0x32e298=_0x4dc4e3[_0x5f31('0x61')];return rp({'method':_0x5f31('0x62'),'uri':util['format'](_0x5f31('0x63'),_0x32e298,_0x5f31('0x64')),'json':!![],'form':{'method':_0x5f31('0x65'),'input_type':_0x5f31('0x66'),'response_type':'JSON','rest_data':JSON['stringify']({'user_auth':{'user_name':_0x4dc4e3['username'],'password':_0x4dc4e3[_0x5f31('0x60')],'encryption':'PLAIN'},'application':_0x5f31('0x67')})}});}})[_0x5f31('0x17')](function(_0x3661b0){if(_0x3661b0){return rp({'method':_0x5f31('0x62'),'uri':util[_0x5f31('0x68')](_0x5f31('0x63'),_0x32e298,_0x5f31('0x64')),'json':!![],'form':{'method':_0x5f31('0x69'),'input_type':_0x5f31('0x66'),'response_type':_0x5f31('0x66'),'rest_data':JSON[_0x5f31('0x1d')]({'session':_0x3661b0['id'],'module_name':_0x5f31('0x6a')})}});}})[_0x5f31('0x17')](function(_0x5e4641){if(_0x5e4641){var _0x42767e=_[_0x5f31('0x6b')](_0x5e4641[_0x5f31('0x6c')]);return{'count':_0x42767e[_0x5f31('0x3c')],'rows':_[_0x5f31('0x47')](_0x42767e,function(_0x39d7f7){return{'id':_0x39d7f7[_0x5f31('0x30')],'name':_0x39d7f7[_0x5f31('0x6d')],'custom':_[_0x5f31('0x6e')](_0x39d7f7['name'],'_c')?!![]:![],'options':_0x39d7f7[_0x5f31('0x4a')]?_[_0x5f31('0x47')](_[_0x5f31('0x6b')](_0x39d7f7[_0x5f31('0x4a')]),function(_0x1156a9){return{'name':_0x1156a9[_0x5f31('0x6f')],'value':_0x1156a9[_0x5f31('0x30')]};}):[]};})};}})[_0x5f31('0x17')](respondWithResult(_0x102704,null))[_0x5f31('0x21')](function(_0x18de70){var _0x1613e3=_0x4cd94a[_0x5f31('0x37')][_0x5f31('0x70')]?0x1f4:_0x18de70[_0x5f31('0x71')]||0x1f4;logger['error'](_0x5f31('0x72'),_0x5f31('0x5e'),_0x1613e3,JSON[_0x5f31('0x1d')](_0x18de70));delete _0x18de70[_0x5f31('0x30')];if(_0x1613e3===0x191){_0x1613e3=0x190;}_0x102704[_0x5f31('0x22')](_0x1613e3)['send'](_0x4cd94a[_0x5f31('0x37')]['test']?{'message':_0x5f31('0x73'),'statusCode':_0x18de70[_0x5f31('0x71')]}:_0x18de70);});};