Built motion from commit (unavailable).|2.3.1
[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 _0x0f54=['info','SugarcrmAccount,\x20%s,\x20%s','request\x20sent','debug','SugarcrmAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','status','end','json','count','offset','apply','save','destroy','then','sendStatus','stack','name','send','index','SugarcrmAccount','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','key','VIRTUAL','filter','merge','findAll','rows','show','params','keys','rawAttributes','include','create','body','update','find','getConfigurations','findOne','SugarcrmConfiguration','map','options','AccountId','Subjects','Descriptions','getDescriptions','channel','type','getFields','assigned_user_id','created_by','date_start','duration_hours','duration_minutes','parent_id','parent_type','parent_name','direction','password','remoteUri','format','%s%s','JSON','username','PLAIN','Invalid\x20Login','POST','/service/v4_1/rest.php','Calls','values','module_fields','remove','includes','label','value','statusCode','getFields,\x20%s,\x20%s,\x20%s','test','Wrong\x20credentials','html-pdf','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../config/license/hardware','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../mysqldb','../../components/integrations/configuration','jayson/promise','request'];(function(_0x26462c,_0xd20a35){var _0x509a42=function(_0x1b22c9){while(--_0x1b22c9){_0x26462c['push'](_0x26462c['shift']());}};_0x509a42(++_0xd20a35);}(_0x0f54,0x1db));var _0x40f5=function(_0x5d516b,_0x347b84){_0x5d516b=_0x5d516b-0x0;var _0x3cede5=_0x0f54[_0x5d516b];return _0x3cede5;};'use strict';var pdf=require(_0x40f5('0x0'));var emlformat=require(_0x40f5('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x40f5('0x2'));var jsonpatch=require(_0x40f5('0x3'));var rp=require(_0x40f5('0x4'));var moment=require(_0x40f5('0x5'));var BPromise=require(_0x40f5('0x6'));var Mustache=require(_0x40f5('0x7'));var util=require(_0x40f5('0x8'));var path=require(_0x40f5('0x9'));var sox=require(_0x40f5('0xa'));var csv=require(_0x40f5('0xb'));var ejs=require(_0x40f5('0xc'));var fs=require('fs');var _=require('lodash');var squel=require(_0x40f5('0xd'));var crypto=require(_0x40f5('0xe'));var jsforce=require(_0x40f5('0xf'));var deskjs=require(_0x40f5('0x10'));var toCsv=require(_0x40f5('0xb'));var querystring=require(_0x40f5('0x11'));var Papa=require(_0x40f5('0x12'));var authService=require(_0x40f5('0x13'));var hardwareService=require(_0x40f5('0x14'));var Redis=require('ioredis');var qs=require(_0x40f5('0x15'));var logger=require(_0x40f5('0x16'))(_0x40f5('0x17'));var utils=require(_0x40f5('0x18'));var config=require('../../config/environment');var db=require(_0x40f5('0x19'))['db'];var integrations=require(_0x40f5('0x1a'));var jayson=require(_0x40f5('0x1b'));var client=jayson['client']['http']({'port':0x232a});function respondWithRpcPromise(_0x320763,_0x3e690b,_0x526bd1,_0x52489a){return new BPromise(function(_0x5c12ed,_0x10ae08){var _0x3c4913=_0x52489a||client;return _0x3c4913[_0x40f5('0x1c')](_0x320763,_0x526bd1)['then'](function(_0x462cb1){logger[_0x40f5('0x1d')](_0x40f5('0x1e'),_0x3e690b,_0x40f5('0x1f'));logger[_0x40f5('0x20')](_0x40f5('0x21'),_0x3e690b,_0x40f5('0x1f'),JSON[_0x40f5('0x22')](_0x462cb1));if(_0x462cb1[_0x40f5('0x23')]){if(_0x462cb1['error'][_0x40f5('0x24')]===0x1f4){logger[_0x40f5('0x23')](_0x40f5('0x1e'),_0x3e690b,_0x462cb1['error'][_0x40f5('0x25')]);return _0x10ae08(_0x462cb1['error']['message']);}logger[_0x40f5('0x23')](_0x40f5('0x1e'),_0x3e690b,_0x462cb1[_0x40f5('0x23')][_0x40f5('0x25')]);return _0x5c12ed(_0x462cb1['error']['message']);}else{logger[_0x40f5('0x1d')]('SugarcrmAccount,\x20%s,\x20%s',_0x3e690b,'request\x20sent');_0x5c12ed(_0x462cb1[_0x40f5('0x26')][_0x40f5('0x25')]);}})[_0x40f5('0x27')](function(_0x30e8a2){logger[_0x40f5('0x23')](_0x40f5('0x1e'),_0x3e690b,_0x30e8a2);_0x10ae08(_0x30e8a2);});});}function respondWithStatusCode(_0x3a4cc7,_0x337aa4){_0x337aa4=_0x337aa4||0xcc;return function(_0x113fc8){if(_0x113fc8){return _0x3a4cc7['sendStatus'](_0x337aa4);}return _0x3a4cc7[_0x40f5('0x28')](_0x337aa4)[_0x40f5('0x29')]();};}function respondWithResult(_0x199241,_0x309262){_0x309262=_0x309262||0xc8;return function(_0x3db099){if(_0x3db099){return _0x199241['status'](_0x309262)[_0x40f5('0x2a')](_0x3db099);}};}function respondWithFilteredResult(_0x212e98,_0x5eb00d){return function(_0x1161e2){if(_0x1161e2){var _0x50d07d=_0x1161e2[_0x40f5('0x2b')],_0x50d38e=_0x5eb00d[_0x40f5('0x2c')],_0xa36029=_0x5eb00d['offset']+_0x5eb00d['limit'],_0x1a1a22;if(_0xa36029>=_0x50d07d){_0xa36029=_0x50d07d;_0x1a1a22=0xc8;}else{_0x1a1a22=0xce;}_0x212e98[_0x40f5('0x28')](_0x1a1a22);return _0x212e98['set']('Content-Range',_0x50d38e+'-'+_0xa36029+'/'+_0x50d07d)[_0x40f5('0x2a')](_0x1161e2);}return null;};}function patchUpdates(_0x199204){return function(_0x4cf548){try{jsonpatch[_0x40f5('0x2d')](_0x4cf548,_0x199204,!![]);}catch(_0x4a4d9e){return BPromise['reject'](_0x4a4d9e);}return _0x4cf548[_0x40f5('0x2e')]();};}function saveUpdates(_0x1e3c21,_0x201552){return function(_0x57f30f){if(_0x57f30f){return _0x57f30f['update'](_0x1e3c21)['then'](function(_0x277b0f){return _0x277b0f;});}return null;};}function removeEntity(_0x29873f,_0x4d1b66){return function(_0x4d2725){if(_0x4d2725){return _0x4d2725[_0x40f5('0x2f')]()[_0x40f5('0x30')](function(){_0x29873f[_0x40f5('0x28')](0xcc)[_0x40f5('0x29')]();});}};}function handleEntityNotFound(_0x1e41ab,_0x956b6){return function(_0x5d2bf6){if(!_0x5d2bf6){_0x1e41ab[_0x40f5('0x31')](0x194);}return _0x5d2bf6;};}function handleError(_0x2508f6,_0x436c40){_0x436c40=_0x436c40||0x1f4;return function(_0x1b97de){logger['error'](_0x1b97de[_0x40f5('0x32')]);if(_0x1b97de[_0x40f5('0x33')]){delete _0x1b97de[_0x40f5('0x33')];}_0x2508f6[_0x40f5('0x28')](_0x436c40)[_0x40f5('0x34')](_0x1b97de);};}exports[_0x40f5('0x35')]=function(_0x2075c5,_0x5b3458){var _0x2a0d90={},_0xb8947e={},_0x4af8fc={'count':0x0,'rows':[]};var _0x171d8f=db[_0x40f5('0x36')]['rawAttributes'];_0xb8947e[_0x40f5('0x37')]=_['keys'](_0x171d8f);_0xb8947e['query']=_['keys'](_0x2075c5[_0x40f5('0x38')]);_0xb8947e[_0x40f5('0x39')]=_[_0x40f5('0x3a')](_0xb8947e['model'],_0xb8947e['query']);_0x2a0d90[_0x40f5('0x3b')]=_['intersection'](_0xb8947e[_0x40f5('0x37')],qs[_0x40f5('0x3c')](_0x2075c5['query'][_0x40f5('0x3c')]));_0x2a0d90[_0x40f5('0x3b')]=_0x2a0d90[_0x40f5('0x3b')][_0x40f5('0x3d')]?_0x2a0d90[_0x40f5('0x3b')]:_0xb8947e['model'];if(!_0x2075c5[_0x40f5('0x38')][_0x40f5('0x3e')](_0x40f5('0x3f'))){_0x2a0d90[_0x40f5('0x40')]=qs[_0x40f5('0x40')](_0x2075c5['query'][_0x40f5('0x40')]);_0x2a0d90[_0x40f5('0x2c')]=qs[_0x40f5('0x2c')](_0x2075c5[_0x40f5('0x38')][_0x40f5('0x2c')]);}_0x2a0d90[_0x40f5('0x41')]=qs[_0x40f5('0x42')](_0x2075c5[_0x40f5('0x38')][_0x40f5('0x42')]);_0x2a0d90[_0x40f5('0x43')]=qs[_0x40f5('0x39')](_[_0x40f5('0x44')](_0x2075c5[_0x40f5('0x38')],_0xb8947e[_0x40f5('0x39')]));if(_0x2075c5[_0x40f5('0x38')]['filter']){_0x2a0d90[_0x40f5('0x43')]=_['merge'](_0x2a0d90['where'],{'$or':_['map'](_0x171d8f,function(_0x2ee41b){if(_0x2ee41b['type'][_0x40f5('0x45')]!==_0x40f5('0x46')){var _0x3a8e96={};_0x3a8e96[_0x2ee41b['field']]={'$like':'%'+_0x2075c5[_0x40f5('0x38')][_0x40f5('0x47')]+'%'};return _0x3a8e96;}})});}_0x2a0d90=_[_0x40f5('0x48')]({},_0x2a0d90,_0x2075c5['options']);var _0x6aee2d={'where':_0x2a0d90['where']};return db[_0x40f5('0x36')][_0x40f5('0x2b')](_0x6aee2d)['then'](function(_0x560942){_0x4af8fc[_0x40f5('0x2b')]=_0x560942;if(_0x2075c5[_0x40f5('0x38')]['includeAll']){_0x2a0d90['include']=[{'all':!![]}];}return db[_0x40f5('0x36')][_0x40f5('0x49')](_0x2a0d90);})[_0x40f5('0x30')](function(_0x16fec9){_0x4af8fc[_0x40f5('0x4a')]=_0x16fec9;return _0x4af8fc;})['then'](respondWithFilteredResult(_0x5b3458,_0x2a0d90))[_0x40f5('0x27')](handleError(_0x5b3458,null));};exports[_0x40f5('0x4b')]=function(_0x422225,_0x5a1c8f){var _0x20a71c={'raw':![],'where':{'id':_0x422225[_0x40f5('0x4c')]['id']}},_0x2ab929={};_0x2ab929[_0x40f5('0x37')]=_[_0x40f5('0x4d')](db['SugarcrmAccount'][_0x40f5('0x4e')]);_0x2ab929[_0x40f5('0x38')]=_['keys'](_0x422225[_0x40f5('0x38')]);_0x2ab929[_0x40f5('0x39')]=_[_0x40f5('0x3a')](_0x2ab929[_0x40f5('0x37')],_0x2ab929[_0x40f5('0x38')]);_0x20a71c[_0x40f5('0x3b')]=_['intersection'](_0x2ab929['model'],qs[_0x40f5('0x3c')](_0x422225['query'][_0x40f5('0x3c')]));_0x20a71c[_0x40f5('0x3b')]=_0x20a71c['attributes'][_0x40f5('0x3d')]?_0x20a71c[_0x40f5('0x3b')]:_0x2ab929[_0x40f5('0x37')];if(_0x422225['query']['includeAll']){_0x20a71c[_0x40f5('0x4f')]=[{'all':!![]}];}_0x20a71c=_[_0x40f5('0x48')]({},_0x20a71c,_0x422225['options']);return db[_0x40f5('0x36')]['find'](_0x20a71c)[_0x40f5('0x30')](handleEntityNotFound(_0x5a1c8f,null))[_0x40f5('0x30')](respondWithResult(_0x5a1c8f,null))[_0x40f5('0x27')](handleError(_0x5a1c8f,null));};exports[_0x40f5('0x50')]=function(_0x191a86,_0x26e0c3){return db[_0x40f5('0x36')][_0x40f5('0x50')](_0x191a86[_0x40f5('0x51')],{})['then'](respondWithResult(_0x26e0c3,0xc9))['catch'](handleError(_0x26e0c3,null));};exports[_0x40f5('0x52')]=function(_0x21c3ae,_0x404a2d){if(_0x21c3ae[_0x40f5('0x51')]['id']){delete _0x21c3ae[_0x40f5('0x51')]['id'];}return db[_0x40f5('0x36')][_0x40f5('0x53')]({'where':{'id':_0x21c3ae[_0x40f5('0x4c')]['id']}})['then'](handleEntityNotFound(_0x404a2d,null))[_0x40f5('0x30')](saveUpdates(_0x21c3ae[_0x40f5('0x51')],null))[_0x40f5('0x30')](respondWithResult(_0x404a2d,null))[_0x40f5('0x27')](handleError(_0x404a2d,null));};exports['destroy']=function(_0x4352c9,_0x4c2e2c){return db[_0x40f5('0x36')][_0x40f5('0x53')]({'where':{'id':_0x4352c9[_0x40f5('0x4c')]['id']}})[_0x40f5('0x30')](handleEntityNotFound(_0x4c2e2c,null))[_0x40f5('0x30')](removeEntity(_0x4c2e2c,null))['catch'](handleError(_0x4c2e2c,null));};exports[_0x40f5('0x54')]=function(_0x1a9154,_0x3c2cff,_0x3d6cfd){var _0x531a2d={};var _0x4e46cc={};var _0x541870;var _0x55851e;return db['SugarcrmAccount'][_0x40f5('0x55')]({'where':{'id':_0x1a9154[_0x40f5('0x4c')]['id']}})[_0x40f5('0x30')](handleEntityNotFound(_0x3c2cff,null))[_0x40f5('0x30')](function(_0x41a95a){if(_0x41a95a){_0x541870=_0x41a95a;_0x4e46cc['model']=_[_0x40f5('0x4d')](db[_0x40f5('0x56')][_0x40f5('0x4e')]);_0x4e46cc[_0x40f5('0x38')]=_[_0x40f5('0x4d')](_0x1a9154[_0x40f5('0x38')]);_0x4e46cc[_0x40f5('0x39')]=_['intersection'](_0x4e46cc[_0x40f5('0x37')],_0x4e46cc[_0x40f5('0x38')]);_0x531a2d[_0x40f5('0x3b')]=_[_0x40f5('0x3a')](_0x4e46cc[_0x40f5('0x37')],qs[_0x40f5('0x3c')](_0x1a9154[_0x40f5('0x38')]['fields']));_0x531a2d[_0x40f5('0x3b')]=_0x531a2d['attributes'][_0x40f5('0x3d')]?_0x531a2d['attributes']:_0x4e46cc[_0x40f5('0x37')];_0x531a2d[_0x40f5('0x41')]=qs[_0x40f5('0x42')](_0x1a9154[_0x40f5('0x38')][_0x40f5('0x42')]);_0x531a2d[_0x40f5('0x43')]=qs[_0x40f5('0x39')](_['pick'](_0x1a9154['query'],_0x4e46cc[_0x40f5('0x39')]));if(_0x1a9154['query'][_0x40f5('0x47')]){_0x531a2d[_0x40f5('0x43')]=_['merge'](_0x531a2d[_0x40f5('0x43')],{'$or':_[_0x40f5('0x57')](_0x531a2d[_0x40f5('0x3b')],function(_0x220761){var _0x45d33b={};_0x45d33b[_0x220761]={'$like':'%'+_0x1a9154['query']['filter']+'%'};return _0x45d33b;})});}_0x531a2d=_[_0x40f5('0x48')]({},_0x531a2d,_0x1a9154[_0x40f5('0x58')]);return _0x541870[_0x40f5('0x54')](_0x531a2d);}})[_0x40f5('0x30')](function(_0x59b907){if(_0x59b907){_0x55851e=_0x59b907[_0x40f5('0x3d')];if(!_0x1a9154['query'][_0x40f5('0x3e')]('nolimit')){_0x531a2d[_0x40f5('0x40')]=qs[_0x40f5('0x40')](_0x1a9154[_0x40f5('0x38')]['limit']);_0x531a2d[_0x40f5('0x2c')]=qs[_0x40f5('0x2c')](_0x1a9154[_0x40f5('0x38')][_0x40f5('0x2c')]);}return _0x541870[_0x40f5('0x54')](_0x531a2d);}})[_0x40f5('0x30')](function(_0x14b7c2){if(_0x14b7c2){return _0x14b7c2?{'count':_0x55851e,'rows':_0x14b7c2}:null;}})[_0x40f5('0x30')](respondWithResult(_0x3c2cff,null))[_0x40f5('0x27')](handleError(_0x3c2cff,null));};exports['addConfiguration']=function(_0x546726,_0x28a88e,_0x3b2171){if(_0x546726[_0x40f5('0x51')]['id']){delete _0x546726['body']['id'];}return db['SugarcrmAccount'][_0x40f5('0x55')]({'where':{'id':_0x546726['params']['id']}})[_0x40f5('0x30')](handleEntityNotFound(_0x28a88e,null))['then'](function(_0x42f3cb){if(_0x42f3cb){_0x546726['body'][_0x40f5('0x59')]=_0x42f3cb['id'];_0x546726[_0x40f5('0x51')][_0x40f5('0x5a')]=integrations['getSubjects'](_0x546726[_0x40f5('0x51')]['channel'],_0x546726[_0x40f5('0x51')]['type']);_0x546726[_0x40f5('0x51')][_0x40f5('0x5b')]=integrations[_0x40f5('0x5c')](_0x546726[_0x40f5('0x51')][_0x40f5('0x5d')],_0x546726[_0x40f5('0x51')][_0x40f5('0x5e')]);return db['SugarcrmConfiguration'][_0x40f5('0x50')](_0x546726[_0x40f5('0x51')],{'include':[{'model':db['SugarcrmField'],'as':_0x40f5('0x5a')},{'model':db['SugarcrmField'],'as':_0x40f5('0x5b')}]});}return null;})[_0x40f5('0x30')](respondWithResult(_0x28a88e,null))['catch'](handleError(_0x28a88e,null));};exports[_0x40f5('0x5f')]=function(_0x1d9cba,_0x40d673,_0x443044){var _0x2e3602;var _0x24d954=['id',_0x40f5('0x33'),_0x40f5('0x60'),_0x40f5('0x61'),'description',_0x40f5('0x62'),_0x40f5('0x63'),_0x40f5('0x64'),_0x40f5('0x65'),_0x40f5('0x66'),_0x40f5('0x67'),_0x40f5('0x68')];return db[_0x40f5('0x36')][_0x40f5('0x55')]({'where':{'id':_0x1d9cba[_0x40f5('0x4c')]['id']},'attributes':['id','username',_0x40f5('0x69'),'remoteUri']})[_0x40f5('0x30')](handleEntityNotFound(_0x40d673,null))['then'](function(_0x2f23b6){if(_0x2f23b6){_0x2e3602=_0x2f23b6[_0x40f5('0x6a')];return rp({'method':'POST','uri':util[_0x40f5('0x6b')](_0x40f5('0x6c'),_0x2e3602,'/service/v4_1/rest.php'),'json':!![],'form':{'method':'login','input_type':_0x40f5('0x6d'),'response_type':_0x40f5('0x6d'),'rest_data':JSON[_0x40f5('0x22')]({'user_auth':{'user_name':_0x2f23b6[_0x40f5('0x6e')],'password':_0x2f23b6[_0x40f5('0x69')],'encryption':_0x40f5('0x6f')},'application':'xCALLY\x20Motion\x20Integration'})}});}})[_0x40f5('0x30')](function(_0x158fd3){if(_0x158fd3&&_0x158fd3[_0x40f5('0x33')]!==_0x40f5('0x70')&&_0x158fd3[_0x40f5('0x33')]!=='Password\x20Expired'){return rp({'method':_0x40f5('0x71'),'uri':util['format'](_0x40f5('0x6c'),_0x2e3602,_0x40f5('0x72')),'json':!![],'form':{'method':'get_module_fields','input_type':'JSON','response_type':'JSON','rest_data':JSON[_0x40f5('0x22')]({'session':_0x158fd3['id'],'module_name':_0x40f5('0x73')})}})[_0x40f5('0x30')](function(_0x3e029f){if(_0x3e029f){var _0x473998=_[_0x40f5('0x74')](_0x3e029f[_0x40f5('0x75')]);_[_0x40f5('0x76')](_0x473998,function(_0xdcdf83){return _0x24d954[_0x40f5('0x77')](_0xdcdf83[_0x40f5('0x33')]);});return{'count':_0x473998['length'],'rows':_[_0x40f5('0x57')](_0x473998,function(_0xd79031){return{'id':_0xd79031['name'],'name':_0xd79031[_0x40f5('0x78')],'custom':_['endsWith'](_0xd79031[_0x40f5('0x33')],'_c')?!![]:![],'options':_0xd79031[_0x40f5('0x58')]?_[_0x40f5('0x57')](_[_0x40f5('0x74')](_0xd79031['options']),function(_0x30748f){return{'name':_0x30748f[_0x40f5('0x79')],'value':_0x30748f[_0x40f5('0x33')]};}):[]};})};}})[_0x40f5('0x30')](respondWithResult(_0x40d673,null))['catch'](function(_0x150e08){var _0x41e78e=_0x1d9cba['query']['test']?0x1f4:_0x150e08[_0x40f5('0x7a')]||0x1f4;logger[_0x40f5('0x23')](_0x40f5('0x7b'),_0x40f5('0x5f'),_0x41e78e,JSON[_0x40f5('0x22')](_0x150e08));delete _0x150e08[_0x40f5('0x33')];if(_0x41e78e===0x191){_0x41e78e=0x190;}_0x40d673[_0x40f5('0x28')](_0x41e78e)[_0x40f5('0x34')](_0x1d9cba[_0x40f5('0x38')][_0x40f5('0x7c')]?{'message':_0x40f5('0x7d'),'statusCode':_0x150e08[_0x40f5('0x7a')]}:_0x150e08);});}else{var _0x3aea38=0x1f4;_0x40d673[_0x40f5('0x28')](_0x3aea38)[_0x40f5('0x34')]({'message':_0x40f5('0x7d'),'statusCode':0x190});}});};