Built motion from commit fda31434.|2.5.44
[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 _0xeb95=['where','pick','filters','filter','merge','options','includeAll','include','rows','show','rawAttributes','find','create','body','params','catch','getConfigurations','SugarcrmConfiguration','findOne','AccountId','Subjects','Descriptions','getDescriptions','SugarcrmField','getFields','duration_minutes','parent_type','parent_name','username','password','remoteUri','slice','lastIndexOf','POST','format','%s%s','/service/v4_1/rest.php','login','JSON','Invalid\x20Login','Password\x20Expired','get_module_fields','stringify','Calls','includes','label','values','value','statusCode','getFields,\x20%s,\x20%s,\x20%s','test','Wrong\x20credentials','send','eml-format','zip-dir','request-promise','util','sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','undefined','limit','count','offset','Content-Range','json','apply','reject','save','then','destroy','error','stack','name','SugarcrmAccount','type','key','model','map','query','keys','intersection','attributes','length','nolimit','order','sort'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0xeb95,0x173));var _0x5eb9=function(_0x496cf3,_0x5c8e9e){_0x496cf3=_0x496cf3-0x0;var _0x47f41f=_0xeb95[_0x496cf3];return _0x47f41f;};'use strict';var emlformat=require(_0x5eb9('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x5eb9('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x5eb9('0x2'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x5eb9('0x3'));var path=require('path');var sox=require(_0x5eb9('0x4'));var csv=require(_0x5eb9('0x5'));var ejs=require(_0x5eb9('0x6'));var fs=require('fs');var fs_extra=require(_0x5eb9('0x7'));var _=require('lodash');var squel=require(_0x5eb9('0x8'));var crypto=require(_0x5eb9('0x9'));var jsforce=require(_0x5eb9('0xa'));var deskjs=require(_0x5eb9('0xb'));var toCsv=require(_0x5eb9('0x5'));var querystring=require(_0x5eb9('0xc'));var Papa=require(_0x5eb9('0xd'));var Redis=require('ioredis');var authService=require(_0x5eb9('0xe'));var qs=require(_0x5eb9('0xf'));var as=require(_0x5eb9('0x10'));var hardwareService=require(_0x5eb9('0x11'));var logger=require(_0x5eb9('0x12'))(_0x5eb9('0x13'));var utils=require(_0x5eb9('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x5eb9('0x15'));var db=require(_0x5eb9('0x16'))['db'];var integrations=require('../../components/integrations/configuration');function respondWithStatusCode(_0x62d284,_0x19d092){_0x19d092=_0x19d092||0xcc;return function(_0x37ad9b){if(_0x37ad9b){return _0x62d284[_0x5eb9('0x17')](_0x19d092);}return _0x62d284[_0x5eb9('0x18')](_0x19d092)[_0x5eb9('0x19')]();};}function respondWithResult(_0x4cc922,_0x4f5ffc){_0x4f5ffc=_0x4f5ffc||0xc8;return function(_0x5d3c33){if(_0x5d3c33){return _0x4cc922[_0x5eb9('0x18')](_0x4f5ffc)['json'](_0x5d3c33);}};}function respondWithFilteredResult(_0x669caf,_0x324143){return function(_0x2a7129){if(_0x2a7129){var _0x57ad88=typeof _0x324143['offset']===_0x5eb9('0x1a')&&typeof _0x324143[_0x5eb9('0x1b')]===_0x5eb9('0x1a');var _0x31b05b=_0x2a7129[_0x5eb9('0x1c')];var _0x219406=_0x57ad88?0x0:_0x324143['offset'];var _0x27d109=_0x57ad88?_0x2a7129[_0x5eb9('0x1c')]:_0x324143[_0x5eb9('0x1d')]+_0x324143[_0x5eb9('0x1b')];var _0x335a44;if(_0x27d109>=_0x31b05b){_0x27d109=_0x31b05b;_0x335a44=0xc8;}else{_0x335a44=0xce;}_0x669caf['status'](_0x335a44);return _0x669caf['set'](_0x5eb9('0x1e'),_0x219406+'-'+_0x27d109+'/'+_0x31b05b)[_0x5eb9('0x1f')](_0x2a7129);}return null;};}function patchUpdates(_0x4028a2){return function(_0x1edff1){try{jsonpatch[_0x5eb9('0x20')](_0x1edff1,_0x4028a2,!![]);}catch(_0x241719){return BPromise[_0x5eb9('0x21')](_0x241719);}return _0x1edff1[_0x5eb9('0x22')]();};}function saveUpdates(_0x5ccf3e,_0x56feb4){return function(_0x5d0cce){if(_0x5d0cce){return _0x5d0cce['update'](_0x5ccf3e)[_0x5eb9('0x23')](function(_0x492119){return _0x492119;});}return null;};}function removeEntity(_0x217add,_0x93fed5){return function(_0x5da977){if(_0x5da977){return _0x5da977[_0x5eb9('0x24')]()[_0x5eb9('0x23')](function(){_0x217add[_0x5eb9('0x18')](0xcc)[_0x5eb9('0x19')]();});}};}function handleEntityNotFound(_0x1ddb9d,_0x47fc3c){return function(_0x2ff487){if(!_0x2ff487){_0x1ddb9d[_0x5eb9('0x17')](0x194);}return _0x2ff487;};}function handleError(_0x59360d,_0x7fe2a1){_0x7fe2a1=_0x7fe2a1||0x1f4;return function(_0x5dc3f9){logger[_0x5eb9('0x25')](_0x5dc3f9[_0x5eb9('0x26')]);if(_0x5dc3f9[_0x5eb9('0x27')]){delete _0x5dc3f9[_0x5eb9('0x27')];}_0x59360d['status'](_0x7fe2a1)['send'](_0x5dc3f9);};}exports['index']=function(_0x4092eb,_0x55b56b){var _0xa39fc1={},_0x4c9118={},_0x149a21={'count':0x0,'rows':[]};var _0x498fc7=_['map'](db[_0x5eb9('0x28')]['rawAttributes'],function(_0x14298b){return{'name':_0x14298b['fieldName'],'type':_0x14298b[_0x5eb9('0x29')][_0x5eb9('0x2a')]};});_0x4c9118[_0x5eb9('0x2b')]=_[_0x5eb9('0x2c')](_0x498fc7,_0x5eb9('0x27'));_0x4c9118[_0x5eb9('0x2d')]=_[_0x5eb9('0x2e')](_0x4092eb[_0x5eb9('0x2d')]);_0x4c9118['filters']=_[_0x5eb9('0x2f')](_0x4c9118['model'],_0x4c9118[_0x5eb9('0x2d')]);_0xa39fc1[_0x5eb9('0x30')]=_[_0x5eb9('0x2f')](_0x4c9118[_0x5eb9('0x2b')],qs['fields'](_0x4092eb[_0x5eb9('0x2d')]['fields']));_0xa39fc1[_0x5eb9('0x30')]=_0xa39fc1['attributes'][_0x5eb9('0x31')]?_0xa39fc1[_0x5eb9('0x30')]:_0x4c9118[_0x5eb9('0x2b')];if(!_0x4092eb[_0x5eb9('0x2d')]['hasOwnProperty'](_0x5eb9('0x32'))){_0xa39fc1[_0x5eb9('0x1b')]=qs[_0x5eb9('0x1b')](_0x4092eb[_0x5eb9('0x2d')]['limit']);_0xa39fc1[_0x5eb9('0x1d')]=qs['offset'](_0x4092eb[_0x5eb9('0x2d')][_0x5eb9('0x1d')]);}_0xa39fc1[_0x5eb9('0x33')]=qs[_0x5eb9('0x34')](_0x4092eb[_0x5eb9('0x2d')][_0x5eb9('0x34')]);_0xa39fc1[_0x5eb9('0x35')]=qs['filters'](_[_0x5eb9('0x36')](_0x4092eb[_0x5eb9('0x2d')],_0x4c9118[_0x5eb9('0x37')]),_0x498fc7);if(_0x4092eb[_0x5eb9('0x2d')][_0x5eb9('0x38')]){_0xa39fc1[_0x5eb9('0x35')]=_[_0x5eb9('0x39')](_0xa39fc1[_0x5eb9('0x35')],{'$or':_[_0x5eb9('0x2c')](_0x498fc7,function(_0x520625){if(_0x520625[_0x5eb9('0x29')]!=='VIRTUAL'){var _0x4257c0={};_0x4257c0[_0x520625[_0x5eb9('0x27')]]={'$like':'%'+_0x4092eb['query']['filter']+'%'};return _0x4257c0;}})});}_0xa39fc1=_[_0x5eb9('0x39')]({},_0xa39fc1,_0x4092eb[_0x5eb9('0x3a')]);var _0x574496={'where':_0xa39fc1[_0x5eb9('0x35')]};return db[_0x5eb9('0x28')][_0x5eb9('0x1c')](_0x574496)[_0x5eb9('0x23')](function(_0x2da5d0){_0x149a21[_0x5eb9('0x1c')]=_0x2da5d0;if(_0x4092eb[_0x5eb9('0x2d')][_0x5eb9('0x3b')]){_0xa39fc1[_0x5eb9('0x3c')]=[{'all':!![]}];}return db[_0x5eb9('0x28')]['findAll'](_0xa39fc1);})[_0x5eb9('0x23')](function(_0x3535f9){_0x149a21[_0x5eb9('0x3d')]=_0x3535f9;return _0x149a21;})[_0x5eb9('0x23')](respondWithFilteredResult(_0x55b56b,_0xa39fc1))['catch'](handleError(_0x55b56b,null));};exports[_0x5eb9('0x3e')]=function(_0x2dc682,_0xf09894){var _0x14644d={'raw':![],'where':{'id':_0x2dc682['params']['id']}},_0x21f622={};_0x21f622[_0x5eb9('0x2b')]=_['keys'](db[_0x5eb9('0x28')][_0x5eb9('0x3f')]);_0x21f622[_0x5eb9('0x2d')]=_[_0x5eb9('0x2e')](_0x2dc682['query']);_0x21f622[_0x5eb9('0x37')]=_[_0x5eb9('0x2f')](_0x21f622['model'],_0x21f622[_0x5eb9('0x2d')]);_0x14644d[_0x5eb9('0x30')]=_[_0x5eb9('0x2f')](_0x21f622[_0x5eb9('0x2b')],qs['fields'](_0x2dc682['query']['fields']));_0x14644d[_0x5eb9('0x30')]=_0x14644d['attributes'][_0x5eb9('0x31')]?_0x14644d[_0x5eb9('0x30')]:_0x21f622[_0x5eb9('0x2b')];if(_0x2dc682[_0x5eb9('0x2d')][_0x5eb9('0x3b')]){_0x14644d[_0x5eb9('0x3c')]=[{'all':!![]}];}_0x14644d=_[_0x5eb9('0x39')]({},_0x14644d,_0x2dc682['options']);return db[_0x5eb9('0x28')][_0x5eb9('0x40')](_0x14644d)[_0x5eb9('0x23')](handleEntityNotFound(_0xf09894,null))[_0x5eb9('0x23')](respondWithResult(_0xf09894,null))['catch'](handleError(_0xf09894,null));};exports[_0x5eb9('0x41')]=function(_0x3b56a4,_0xb82839){return db['SugarcrmAccount']['create'](_0x3b56a4[_0x5eb9('0x42')],{})['then'](respondWithResult(_0xb82839,0xc9))['catch'](handleError(_0xb82839,null));};exports['update']=function(_0x79b898,_0x5c31b1){if(_0x79b898[_0x5eb9('0x42')]['id']){delete _0x79b898[_0x5eb9('0x42')]['id'];}return db[_0x5eb9('0x28')][_0x5eb9('0x40')]({'where':{'id':_0x79b898[_0x5eb9('0x43')]['id']}})[_0x5eb9('0x23')](handleEntityNotFound(_0x5c31b1,null))[_0x5eb9('0x23')](saveUpdates(_0x79b898['body'],null))[_0x5eb9('0x23')](respondWithResult(_0x5c31b1,null))[_0x5eb9('0x44')](handleError(_0x5c31b1,null));};exports[_0x5eb9('0x24')]=function(_0x3b8240,_0x211899){return db[_0x5eb9('0x28')][_0x5eb9('0x40')]({'where':{'id':_0x3b8240[_0x5eb9('0x43')]['id']}})[_0x5eb9('0x23')](handleEntityNotFound(_0x211899,null))[_0x5eb9('0x23')](removeEntity(_0x211899,null))[_0x5eb9('0x44')](handleError(_0x211899,null));};exports[_0x5eb9('0x45')]=function(_0x48b4fa,_0x4c4594,_0x1bcd09){var _0x51e346={};var _0x4d3395={};var _0x43925b;var _0x2cda3e;return db['SugarcrmAccount']['findOne']({'where':{'id':_0x48b4fa[_0x5eb9('0x43')]['id']}})[_0x5eb9('0x23')](handleEntityNotFound(_0x4c4594,null))['then'](function(_0x37c54f){if(_0x37c54f){_0x43925b=_0x37c54f;_0x4d3395[_0x5eb9('0x2b')]=_['keys'](db[_0x5eb9('0x46')]['rawAttributes']);_0x4d3395['query']=_['keys'](_0x48b4fa[_0x5eb9('0x2d')]);_0x4d3395[_0x5eb9('0x37')]=_[_0x5eb9('0x2f')](_0x4d3395['model'],_0x4d3395['query']);_0x51e346['attributes']=_[_0x5eb9('0x2f')](_0x4d3395['model'],qs['fields'](_0x48b4fa[_0x5eb9('0x2d')]['fields']));_0x51e346[_0x5eb9('0x30')]=_0x51e346[_0x5eb9('0x30')][_0x5eb9('0x31')]?_0x51e346['attributes']:_0x4d3395[_0x5eb9('0x2b')];_0x51e346[_0x5eb9('0x33')]=qs[_0x5eb9('0x34')](_0x48b4fa['query'][_0x5eb9('0x34')]);_0x51e346[_0x5eb9('0x35')]=qs[_0x5eb9('0x37')](_[_0x5eb9('0x36')](_0x48b4fa['query'],_0x4d3395['filters']));if(_0x48b4fa['query'][_0x5eb9('0x38')]){_0x51e346[_0x5eb9('0x35')]=_[_0x5eb9('0x39')](_0x51e346[_0x5eb9('0x35')],{'$or':_[_0x5eb9('0x2c')](_0x51e346[_0x5eb9('0x30')],function(_0x1cc8bc){var _0x1a90b2={};_0x1a90b2[_0x1cc8bc]={'$like':'%'+_0x48b4fa[_0x5eb9('0x2d')][_0x5eb9('0x38')]+'%'};return _0x1a90b2;})});}_0x51e346=_[_0x5eb9('0x39')]({},_0x51e346,_0x48b4fa['options']);return _0x43925b[_0x5eb9('0x45')](_0x51e346);}})['then'](function(_0x3c64b1){if(_0x3c64b1){_0x2cda3e=_0x3c64b1[_0x5eb9('0x31')];if(!_0x48b4fa[_0x5eb9('0x2d')]['hasOwnProperty']('nolimit')){_0x51e346['limit']=qs[_0x5eb9('0x1b')](_0x48b4fa['query'][_0x5eb9('0x1b')]);_0x51e346['offset']=qs[_0x5eb9('0x1d')](_0x48b4fa[_0x5eb9('0x2d')][_0x5eb9('0x1d')]);}return _0x43925b[_0x5eb9('0x45')](_0x51e346);}})[_0x5eb9('0x23')](function(_0x3101e9){if(_0x3101e9){return _0x3101e9?{'count':_0x2cda3e,'rows':_0x3101e9}:null;}})[_0x5eb9('0x23')](respondWithResult(_0x4c4594,null))[_0x5eb9('0x44')](handleError(_0x4c4594,null));};exports['addConfiguration']=function(_0x304578,_0x2b59f7,_0x423da0){if(_0x304578[_0x5eb9('0x42')]['id']){delete _0x304578[_0x5eb9('0x42')]['id'];}return db['SugarcrmAccount'][_0x5eb9('0x47')]({'where':{'id':_0x304578['params']['id']}})['then'](handleEntityNotFound(_0x2b59f7,null))[_0x5eb9('0x23')](function(_0x2553d5){if(_0x2553d5){_0x304578[_0x5eb9('0x42')][_0x5eb9('0x48')]=_0x2553d5['id'];_0x304578[_0x5eb9('0x42')][_0x5eb9('0x49')]=integrations['getSubjects'](_0x304578[_0x5eb9('0x42')]['channel'],_0x304578[_0x5eb9('0x42')][_0x5eb9('0x29')]);_0x304578[_0x5eb9('0x42')][_0x5eb9('0x4a')]=integrations[_0x5eb9('0x4b')](_0x304578[_0x5eb9('0x42')]['channel'],_0x304578[_0x5eb9('0x42')][_0x5eb9('0x29')]);return db[_0x5eb9('0x46')][_0x5eb9('0x41')](_0x304578[_0x5eb9('0x42')],{'include':[{'model':db[_0x5eb9('0x4c')],'as':_0x5eb9('0x49')},{'model':db['SugarcrmField'],'as':_0x5eb9('0x4a')}]});}return null;})[_0x5eb9('0x23')](respondWithResult(_0x2b59f7,null))[_0x5eb9('0x44')](handleError(_0x2b59f7,null));};exports[_0x5eb9('0x4d')]=function(_0x3a383b,_0x3796ec,_0x2117d0){var _0x3e7ccc='';var _0x5bf5af=['id',_0x5eb9('0x27'),'assigned_user_id','created_by','description','date_start','duration_hours',_0x5eb9('0x4e'),'parent_id',_0x5eb9('0x4f'),_0x5eb9('0x50'),'direction'];return db[_0x5eb9('0x28')]['findOne']({'where':{'id':_0x3a383b[_0x5eb9('0x43')]['id']},'attributes':['id',_0x5eb9('0x51'),_0x5eb9('0x52'),'remoteUri']})[_0x5eb9('0x23')](handleEntityNotFound(_0x3796ec,null))[_0x5eb9('0x23')](function(_0x166a97){if(_0x166a97){_0x3e7ccc=_0x166a97[_0x5eb9('0x53')];var _0x2a9da8=_0x3e7ccc[_0x5eb9('0x54')](-0x1);if(_0x2a9da8==='/'){_0x3e7ccc=_0x3e7ccc['substring'](0x0,_0x3e7ccc[_0x5eb9('0x55')](_0x2a9da8));}return rp({'method':_0x5eb9('0x56'),'uri':util[_0x5eb9('0x57')](_0x5eb9('0x58'),_0x3e7ccc,_0x5eb9('0x59')),'json':!![],'form':{'method':_0x5eb9('0x5a'),'input_type':_0x5eb9('0x5b'),'response_type':_0x5eb9('0x5b'),'rest_data':JSON['stringify']({'user_auth':{'user_name':_0x166a97['username'],'password':_0x166a97['password'],'encryption':'PLAIN'},'application':'xCALLY\x20Motion\x20Integration'})}});}})[_0x5eb9('0x23')](function(_0x346947){if(_0x346947&&_0x346947[_0x5eb9('0x27')]!==_0x5eb9('0x5c')&&_0x346947[_0x5eb9('0x27')]!==_0x5eb9('0x5d')){return rp({'method':'POST','uri':util[_0x5eb9('0x57')]('%s%s',_0x3e7ccc,'/service/v4_1/rest.php'),'json':!![],'form':{'method':_0x5eb9('0x5e'),'input_type':_0x5eb9('0x5b'),'response_type':'JSON','rest_data':JSON[_0x5eb9('0x5f')]({'session':_0x346947['id'],'module_name':_0x5eb9('0x60')})}})['then'](function(_0x4e32c0){if(_0x4e32c0){var _0xe31c4d=_['values'](_0x4e32c0['module_fields']);_['remove'](_0xe31c4d,function(_0x542c44){return _0x5bf5af[_0x5eb9('0x61')](_0x542c44[_0x5eb9('0x27')]);});return{'count':_0xe31c4d['length'],'rows':_[_0x5eb9('0x2c')](_0xe31c4d,function(_0xf0357a){return{'id':_0xf0357a['name'],'name':_0xf0357a[_0x5eb9('0x62')],'custom':_['endsWith'](_0xf0357a[_0x5eb9('0x27')],'_c')?!![]:![],'options':_0xf0357a['options']?_[_0x5eb9('0x2c')](_[_0x5eb9('0x63')](_0xf0357a[_0x5eb9('0x3a')]),function(_0xcbc39){return{'name':_0xcbc39[_0x5eb9('0x64')],'value':_0xcbc39[_0x5eb9('0x27')]};}):[]};})};}})['then'](respondWithResult(_0x3796ec,null))['catch'](function(_0x3de200){var _0x2df0cb=_0x3a383b[_0x5eb9('0x2d')]['test']?0x1f4:_0x3de200[_0x5eb9('0x65')]||0x1f4;logger[_0x5eb9('0x25')](_0x5eb9('0x66'),_0x5eb9('0x4d'),_0x2df0cb,JSON[_0x5eb9('0x5f')](_0x3de200));delete _0x3de200['name'];if(_0x2df0cb===0x191){_0x2df0cb=0x190;}_0x3796ec[_0x5eb9('0x18')](_0x2df0cb)['send'](_0x3a383b[_0x5eb9('0x2d')][_0x5eb9('0x67')]?{'message':_0x5eb9('0x68'),'statusCode':_0x3de200['statusCode']}:_0x3de200);});}else{var _0x51cfc7=0x1f4;_0x3796ec[_0x5eb9('0x18')](_0x51cfc7)[_0x5eb9('0x69')]({'message':_0x5eb9('0x68'),'statusCode':0x190});}});};