f2b1e97de2b91c0a64389835c4b4b9e72b972535
[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 _0x253f=['value','test','statusCode','getFields','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../components/integrations/configuration','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','sendStatus','error','name','send','index','map','SugarcrmAccount','rawAttributes','fieldName','type','key','model','query','filters','intersection','attributes','fields','length','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','findAll','catch','show','params','keys','find','create','body','getConfigurations','findOne','AccountId','Subjects','getSubjects','channel','getDescriptions','SugarcrmConfiguration','SugarcrmField','assigned_user_id','created_by','description','duration_hours','duration_minutes','parent_id','parent_name','username','password','remoteUri','substring','lastIndexOf','POST','format','%s%s','/service/v4_1/rest.php','login','JSON','stringify','PLAIN','xCALLY\x20Motion\x20Integration','Invalid\x20Login','Password\x20Expired','Calls','values','module_fields','remove','includes','label','endsWith'];(function(_0x26cf7a,_0x1f8cc5){var _0x3e1bc7=function(_0x4aff6f){while(--_0x4aff6f){_0x26cf7a['push'](_0x26cf7a['shift']());}};_0x3e1bc7(++_0x1f8cc5);}(_0x253f,0x7e));var _0xf253=function(_0x50007c,_0x1cbf11){_0x50007c=_0x50007c-0x0;var _0x5c1ec7=_0x253f[_0x50007c];return _0x5c1ec7;};'use strict';var emlformat=require(_0xf253('0x0'));var rimraf=require(_0xf253('0x1'));var zipdir=require(_0xf253('0x2'));var jsonpatch=require(_0xf253('0x3'));var rp=require(_0xf253('0x4'));var moment=require('moment');var BPromise=require(_0xf253('0x5'));var Mustache=require(_0xf253('0x6'));var util=require('util');var path=require(_0xf253('0x7'));var sox=require(_0xf253('0x8'));var csv=require(_0xf253('0x9'));var ejs=require(_0xf253('0xa'));var fs=require('fs');var fs_extra=require(_0xf253('0xb'));var _=require(_0xf253('0xc'));var squel=require(_0xf253('0xd'));var crypto=require('crypto');var jsforce=require(_0xf253('0xe'));var deskjs=require('desk.js');var toCsv=require(_0xf253('0x9'));var querystring=require('querystring');var Papa=require(_0xf253('0xf'));var Redis=require(_0xf253('0x10'));var authService=require(_0xf253('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0xf253('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf253('0x13'))(_0xf253('0x14'));var utils=require(_0xf253('0x15'));var config=require(_0xf253('0x16'));var licenseUtil=require(_0xf253('0x17'));var db=require('../../mysqldb')['db'];var integrations=require(_0xf253('0x18'));function respondWithStatusCode(_0x39136b,_0x45c03e){_0x45c03e=_0x45c03e||0xcc;return function(_0x1632fe){if(_0x1632fe){return _0x39136b['sendStatus'](_0x45c03e);}return _0x39136b[_0xf253('0x19')](_0x45c03e)[_0xf253('0x1a')]();};}function respondWithResult(_0x56e163,_0x3fac02){_0x3fac02=_0x3fac02||0xc8;return function(_0x508626){if(_0x508626){return _0x56e163['status'](_0x3fac02)[_0xf253('0x1b')](_0x508626);}};}function respondWithFilteredResult(_0x6f36b4,_0x5b2924){return function(_0x5e9b2e){if(_0x5e9b2e){var _0x3fb07f=typeof _0x5b2924[_0xf253('0x1c')]===_0xf253('0x1d')&&typeof _0x5b2924[_0xf253('0x1e')]===_0xf253('0x1d');var _0x1bbab2=_0x5e9b2e[_0xf253('0x1f')];var _0x20e5ef=_0x3fb07f?0x0:_0x5b2924[_0xf253('0x1c')];var _0x26ea9a=_0x3fb07f?_0x5e9b2e[_0xf253('0x1f')]:_0x5b2924[_0xf253('0x1c')]+_0x5b2924[_0xf253('0x1e')];var _0x413ad7;if(_0x26ea9a>=_0x1bbab2){_0x26ea9a=_0x1bbab2;_0x413ad7=0xc8;}else{_0x413ad7=0xce;}_0x6f36b4[_0xf253('0x19')](_0x413ad7);return _0x6f36b4[_0xf253('0x20')](_0xf253('0x21'),_0x20e5ef+'-'+_0x26ea9a+'/'+_0x1bbab2)[_0xf253('0x1b')](_0x5e9b2e);}return null;};}function patchUpdates(_0x59784c){return function(_0x2f84ef){try{jsonpatch[_0xf253('0x22')](_0x2f84ef,_0x59784c,!![]);}catch(_0x4182f2){return BPromise[_0xf253('0x23')](_0x4182f2);}return _0x2f84ef[_0xf253('0x24')]();};}function saveUpdates(_0x1c5e35,_0x40b625){return function(_0x956dff){if(_0x956dff){return _0x956dff[_0xf253('0x25')](_0x1c5e35)[_0xf253('0x26')](function(_0x78e030){return _0x78e030;});}return null;};}function removeEntity(_0x44d604,_0x1f68cc){return function(_0xbc8c62){if(_0xbc8c62){return _0xbc8c62[_0xf253('0x27')]()[_0xf253('0x26')](function(){_0x44d604[_0xf253('0x19')](0xcc)[_0xf253('0x1a')]();});}};}function handleEntityNotFound(_0x4a39e5,_0x4f7696){return function(_0x56f968){if(!_0x56f968){_0x4a39e5[_0xf253('0x28')](0x194);}return _0x56f968;};}function handleError(_0x504b3a,_0x56c551){_0x56c551=_0x56c551||0x1f4;return function(_0x101b45){logger[_0xf253('0x29')](_0x101b45['stack']);if(_0x101b45[_0xf253('0x2a')]){delete _0x101b45[_0xf253('0x2a')];}_0x504b3a['status'](_0x56c551)[_0xf253('0x2b')](_0x101b45);};}exports[_0xf253('0x2c')]=function(_0xc81491,_0x41c3b7){var _0x238a81={},_0x52359e={},_0x14404a={'count':0x0,'rows':[]};var _0x270c28=_[_0xf253('0x2d')](db[_0xf253('0x2e')][_0xf253('0x2f')],function(_0x5e360c){return{'name':_0x5e360c[_0xf253('0x30')],'type':_0x5e360c[_0xf253('0x31')][_0xf253('0x32')]};});_0x52359e[_0xf253('0x33')]=_['map'](_0x270c28,_0xf253('0x2a'));_0x52359e[_0xf253('0x34')]=_['keys'](_0xc81491[_0xf253('0x34')]);_0x52359e[_0xf253('0x35')]=_[_0xf253('0x36')](_0x52359e[_0xf253('0x33')],_0x52359e[_0xf253('0x34')]);_0x238a81[_0xf253('0x37')]=_[_0xf253('0x36')](_0x52359e[_0xf253('0x33')],qs[_0xf253('0x38')](_0xc81491[_0xf253('0x34')][_0xf253('0x38')]));_0x238a81['attributes']=_0x238a81[_0xf253('0x37')][_0xf253('0x39')]?_0x238a81[_0xf253('0x37')]:_0x52359e[_0xf253('0x33')];if(!_0xc81491['query']['hasOwnProperty'](_0xf253('0x3a'))){_0x238a81['limit']=qs['limit'](_0xc81491[_0xf253('0x34')]['limit']);_0x238a81['offset']=qs[_0xf253('0x1c')](_0xc81491[_0xf253('0x34')][_0xf253('0x1c')]);}_0x238a81[_0xf253('0x3b')]=qs[_0xf253('0x3c')](_0xc81491[_0xf253('0x34')][_0xf253('0x3c')]);_0x238a81[_0xf253('0x3d')]=qs[_0xf253('0x35')](_[_0xf253('0x3e')](_0xc81491[_0xf253('0x34')],_0x52359e[_0xf253('0x35')]),_0x270c28);if(_0xc81491[_0xf253('0x34')][_0xf253('0x3f')]){_0x238a81[_0xf253('0x3d')]=_['merge'](_0x238a81[_0xf253('0x3d')],{'$or':_[_0xf253('0x2d')](_0x270c28,function(_0x36102d){if(_0x36102d['type']!==_0xf253('0x40')){var _0x38f5ed={};_0x38f5ed[_0x36102d[_0xf253('0x2a')]]={'$like':'%'+_0xc81491[_0xf253('0x34')][_0xf253('0x3f')]+'%'};return _0x38f5ed;}})});}_0x238a81=_[_0xf253('0x41')]({},_0x238a81,_0xc81491[_0xf253('0x42')]);var _0x5dc622={'where':_0x238a81[_0xf253('0x3d')]};return db[_0xf253('0x2e')]['count'](_0x5dc622)['then'](function(_0x32553e){_0x14404a[_0xf253('0x1f')]=_0x32553e;if(_0xc81491['query'][_0xf253('0x43')]){_0x238a81[_0xf253('0x44')]=[{'all':!![]}];}return db[_0xf253('0x2e')][_0xf253('0x45')](_0x238a81);})[_0xf253('0x26')](function(_0x49641b){_0x14404a['rows']=_0x49641b;return _0x14404a;})[_0xf253('0x26')](respondWithFilteredResult(_0x41c3b7,_0x238a81))[_0xf253('0x46')](handleError(_0x41c3b7,null));};exports[_0xf253('0x47')]=function(_0x5a0ded,_0x48870c){var _0x1fca22={'raw':![],'where':{'id':_0x5a0ded[_0xf253('0x48')]['id']}},_0xa2e8d2={};_0xa2e8d2[_0xf253('0x33')]=_[_0xf253('0x49')](db['SugarcrmAccount'][_0xf253('0x2f')]);_0xa2e8d2['query']=_[_0xf253('0x49')](_0x5a0ded[_0xf253('0x34')]);_0xa2e8d2[_0xf253('0x35')]=_[_0xf253('0x36')](_0xa2e8d2[_0xf253('0x33')],_0xa2e8d2[_0xf253('0x34')]);_0x1fca22[_0xf253('0x37')]=_[_0xf253('0x36')](_0xa2e8d2[_0xf253('0x33')],qs[_0xf253('0x38')](_0x5a0ded[_0xf253('0x34')][_0xf253('0x38')]));_0x1fca22[_0xf253('0x37')]=_0x1fca22[_0xf253('0x37')][_0xf253('0x39')]?_0x1fca22[_0xf253('0x37')]:_0xa2e8d2[_0xf253('0x33')];if(_0x5a0ded[_0xf253('0x34')]['includeAll']){_0x1fca22[_0xf253('0x44')]=[{'all':!![]}];}_0x1fca22=_[_0xf253('0x41')]({},_0x1fca22,_0x5a0ded[_0xf253('0x42')]);return db[_0xf253('0x2e')][_0xf253('0x4a')](_0x1fca22)[_0xf253('0x26')](handleEntityNotFound(_0x48870c,null))['then'](respondWithResult(_0x48870c,null))[_0xf253('0x46')](handleError(_0x48870c,null));};exports['create']=function(_0x2ea4f9,_0x399ce3){return db['SugarcrmAccount'][_0xf253('0x4b')](_0x2ea4f9[_0xf253('0x4c')],{})[_0xf253('0x26')](respondWithResult(_0x399ce3,0xc9))['catch'](handleError(_0x399ce3,null));};exports[_0xf253('0x25')]=function(_0xc8a817,_0x2ac9cd){if(_0xc8a817[_0xf253('0x4c')]['id']){delete _0xc8a817['body']['id'];}return db[_0xf253('0x2e')]['find']({'where':{'id':_0xc8a817[_0xf253('0x48')]['id']}})[_0xf253('0x26')](handleEntityNotFound(_0x2ac9cd,null))['then'](saveUpdates(_0xc8a817['body'],null))['then'](respondWithResult(_0x2ac9cd,null))[_0xf253('0x46')](handleError(_0x2ac9cd,null));};exports[_0xf253('0x27')]=function(_0x11082b,_0x3b5a24){return db['SugarcrmAccount']['find']({'where':{'id':_0x11082b[_0xf253('0x48')]['id']}})[_0xf253('0x26')](handleEntityNotFound(_0x3b5a24,null))[_0xf253('0x26')](removeEntity(_0x3b5a24,null))['catch'](handleError(_0x3b5a24,null));};exports[_0xf253('0x4d')]=function(_0x81b1c0,_0x28b728,_0x38f6ce){var _0x5deb29={};var _0xf64ee4={};var _0x8da8fa;var _0xada714;return db['SugarcrmAccount']['findOne']({'where':{'id':_0x81b1c0[_0xf253('0x48')]['id']}})[_0xf253('0x26')](handleEntityNotFound(_0x28b728,null))[_0xf253('0x26')](function(_0x4664e0){if(_0x4664e0){_0x8da8fa=_0x4664e0;_0xf64ee4[_0xf253('0x33')]=_['keys'](db['SugarcrmConfiguration']['rawAttributes']);_0xf64ee4[_0xf253('0x34')]=_[_0xf253('0x49')](_0x81b1c0['query']);_0xf64ee4[_0xf253('0x35')]=_[_0xf253('0x36')](_0xf64ee4[_0xf253('0x33')],_0xf64ee4[_0xf253('0x34')]);_0x5deb29[_0xf253('0x37')]=_[_0xf253('0x36')](_0xf64ee4['model'],qs['fields'](_0x81b1c0['query'][_0xf253('0x38')]));_0x5deb29['attributes']=_0x5deb29[_0xf253('0x37')][_0xf253('0x39')]?_0x5deb29[_0xf253('0x37')]:_0xf64ee4['model'];_0x5deb29[_0xf253('0x3b')]=qs[_0xf253('0x3c')](_0x81b1c0['query'][_0xf253('0x3c')]);_0x5deb29[_0xf253('0x3d')]=qs[_0xf253('0x35')](_[_0xf253('0x3e')](_0x81b1c0[_0xf253('0x34')],_0xf64ee4['filters']));if(_0x81b1c0['query'][_0xf253('0x3f')]){_0x5deb29[_0xf253('0x3d')]=_['merge'](_0x5deb29[_0xf253('0x3d')],{'$or':_[_0xf253('0x2d')](_0x5deb29[_0xf253('0x37')],function(_0x1f7aa0){var _0x4f75f1={};_0x4f75f1[_0x1f7aa0]={'$like':'%'+_0x81b1c0[_0xf253('0x34')][_0xf253('0x3f')]+'%'};return _0x4f75f1;})});}_0x5deb29=_[_0xf253('0x41')]({},_0x5deb29,_0x81b1c0[_0xf253('0x42')]);return _0x8da8fa[_0xf253('0x4d')](_0x5deb29);}})['then'](function(_0x143035){if(_0x143035){_0xada714=_0x143035[_0xf253('0x39')];if(!_0x81b1c0[_0xf253('0x34')]['hasOwnProperty'](_0xf253('0x3a'))){_0x5deb29[_0xf253('0x1e')]=qs[_0xf253('0x1e')](_0x81b1c0[_0xf253('0x34')][_0xf253('0x1e')]);_0x5deb29['offset']=qs[_0xf253('0x1c')](_0x81b1c0[_0xf253('0x34')][_0xf253('0x1c')]);}return _0x8da8fa[_0xf253('0x4d')](_0x5deb29);}})[_0xf253('0x26')](function(_0x23c0e3){if(_0x23c0e3){return _0x23c0e3?{'count':_0xada714,'rows':_0x23c0e3}:null;}})[_0xf253('0x26')](respondWithResult(_0x28b728,null))[_0xf253('0x46')](handleError(_0x28b728,null));};exports['addConfiguration']=function(_0x43c561,_0x321436,_0x1490bf){if(_0x43c561['body']['id']){delete _0x43c561[_0xf253('0x4c')]['id'];}return db[_0xf253('0x2e')][_0xf253('0x4e')]({'where':{'id':_0x43c561['params']['id']}})[_0xf253('0x26')](handleEntityNotFound(_0x321436,null))[_0xf253('0x26')](function(_0x58ec01){if(_0x58ec01){_0x43c561[_0xf253('0x4c')][_0xf253('0x4f')]=_0x58ec01['id'];_0x43c561[_0xf253('0x4c')][_0xf253('0x50')]=integrations[_0xf253('0x51')](_0x43c561[_0xf253('0x4c')][_0xf253('0x52')],_0x43c561[_0xf253('0x4c')][_0xf253('0x31')]);_0x43c561['body']['Descriptions']=integrations[_0xf253('0x53')](_0x43c561[_0xf253('0x4c')][_0xf253('0x52')],_0x43c561[_0xf253('0x4c')][_0xf253('0x31')]);return db[_0xf253('0x54')][_0xf253('0x4b')](_0x43c561['body'],{'include':[{'model':db[_0xf253('0x55')],'as':_0xf253('0x50')},{'model':db[_0xf253('0x55')],'as':'Descriptions'}]});}return null;})['then'](respondWithResult(_0x321436,null))['catch'](handleError(_0x321436,null));};exports['getFields']=function(_0x2fab17,_0xe4495b,_0x45136e){var _0x4e5248='';var _0x15e983=['id','name',_0xf253('0x56'),_0xf253('0x57'),_0xf253('0x58'),'date_start',_0xf253('0x59'),_0xf253('0x5a'),_0xf253('0x5b'),'parent_type',_0xf253('0x5c'),'direction'];return db[_0xf253('0x2e')][_0xf253('0x4e')]({'where':{'id':_0x2fab17['params']['id']},'attributes':['id',_0xf253('0x5d'),_0xf253('0x5e'),'remoteUri']})['then'](handleEntityNotFound(_0xe4495b,null))[_0xf253('0x26')](function(_0x4799b9){if(_0x4799b9){_0x4e5248=_0x4799b9[_0xf253('0x5f')];var _0x10b3a6=_0x4e5248['slice'](-0x1);if(_0x10b3a6==='/'){_0x4e5248=_0x4e5248[_0xf253('0x60')](0x0,_0x4e5248[_0xf253('0x61')](_0x10b3a6));}return rp({'method':_0xf253('0x62'),'uri':util[_0xf253('0x63')](_0xf253('0x64'),_0x4e5248,_0xf253('0x65')),'json':!![],'form':{'method':_0xf253('0x66'),'input_type':'JSON','response_type':_0xf253('0x67'),'rest_data':JSON[_0xf253('0x68')]({'user_auth':{'user_name':_0x4799b9['username'],'password':_0x4799b9[_0xf253('0x5e')],'encryption':_0xf253('0x69')},'application':_0xf253('0x6a')})}});}})[_0xf253('0x26')](function(_0x54276c){if(_0x54276c&&_0x54276c[_0xf253('0x2a')]!==_0xf253('0x6b')&&_0x54276c[_0xf253('0x2a')]!==_0xf253('0x6c')){return rp({'method':_0xf253('0x62'),'uri':util['format'](_0xf253('0x64'),_0x4e5248,_0xf253('0x65')),'json':!![],'form':{'method':'get_module_fields','input_type':_0xf253('0x67'),'response_type':_0xf253('0x67'),'rest_data':JSON[_0xf253('0x68')]({'session':_0x54276c['id'],'module_name':_0xf253('0x6d')})}})['then'](function(_0xcfd9fa){if(_0xcfd9fa){var _0x5601a4=_[_0xf253('0x6e')](_0xcfd9fa[_0xf253('0x6f')]);_[_0xf253('0x70')](_0x5601a4,function(_0x57d688){return _0x15e983[_0xf253('0x71')](_0x57d688[_0xf253('0x2a')]);});return{'count':_0x5601a4['length'],'rows':_[_0xf253('0x2d')](_0x5601a4,function(_0x45b2c4){return{'id':_0x45b2c4[_0xf253('0x2a')],'name':_0x45b2c4[_0xf253('0x72')],'custom':_[_0xf253('0x73')](_0x45b2c4[_0xf253('0x2a')],'_c')?!![]:![],'options':_0x45b2c4[_0xf253('0x42')]?_[_0xf253('0x2d')](_[_0xf253('0x6e')](_0x45b2c4['options']),function(_0x1fca0a){return{'name':_0x1fca0a[_0xf253('0x74')],'value':_0x1fca0a['name']};}):[]};})};}})[_0xf253('0x26')](respondWithResult(_0xe4495b,null))[_0xf253('0x46')](function(_0x599cb0){var _0x265faf=_0x2fab17[_0xf253('0x34')][_0xf253('0x75')]?0x1f4:_0x599cb0[_0xf253('0x76')]||0x1f4;logger[_0xf253('0x29')]('getFields,\x20%s,\x20%s,\x20%s',_0xf253('0x77'),_0x265faf,JSON[_0xf253('0x68')](_0x599cb0));delete _0x599cb0['name'];if(_0x265faf===0x191){_0x265faf=0x190;}_0xe4495b['status'](_0x265faf)[_0xf253('0x2b')](_0x2fab17[_0xf253('0x34')][_0xf253('0x75')]?{'message':'Wrong\x20credentials','statusCode':_0x599cb0['statusCode']}:_0x599cb0);});}else{var _0x4d6e5d=0x1f4;_0xe4495b[_0xf253('0x19')](_0x4d6e5d)[_0xf253('0x2b')]({'message':_0xf253('0x78'),'statusCode':0x190});}});};