Built motion from commit d5e4af8c.|2.6.23
[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 _0x5f9f=['fs-extra','lodash','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../components/integrations/configuration','sendStatus','status','end','offset','limit','undefined','count','set','Content-Range','json','apply','save','update','then','destroy','name','send','map','SugarcrmAccount','rawAttributes','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','type','VIRTUAL','merge','options','includeAll','findAll','rows','catch','params','find','create','body','getConfigurations','findOne','SugarcrmConfiguration','AccountId','channel','Descriptions','getDescriptions','SugarcrmField','Subjects','assigned_user_id','created_by','description','date_start','parent_id','parent_type','direction','username','password','remoteUri','substring','lastIndexOf','POST','%s%s','/service/v4_1/rest.php','login','JSON','stringify','PLAIN','xCALLY\x20Motion\x20Integration','Invalid\x20Login','Password\x20Expired','format','get_module_fields','Calls','module_fields','remove','includes','label','endsWith','values','value','test','statusCode','error','getFields,\x20%s,\x20%s,\x20%s','getFields','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs'];(function(_0x122ef9,_0x451d2a){var _0x42fba0=function(_0x17c9d3){while(--_0x17c9d3){_0x122ef9['push'](_0x122ef9['shift']());}};_0x42fba0(++_0x451d2a);}(_0x5f9f,0x6d));var _0xf5f9=function(_0x6c993a,_0x5ea3ab){_0x6c993a=_0x6c993a-0x0;var _0x27f547=_0x5f9f[_0x6c993a];return _0x27f547;};'use strict';var emlformat=require(_0xf5f9('0x0'));var rimraf=require(_0xf5f9('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xf5f9('0x2'));var rp=require(_0xf5f9('0x3'));var moment=require(_0xf5f9('0x4'));var BPromise=require(_0xf5f9('0x5'));var Mustache=require(_0xf5f9('0x6'));var util=require(_0xf5f9('0x7'));var path=require(_0xf5f9('0x8'));var sox=require('sox');var csv=require(_0xf5f9('0x9'));var ejs=require(_0xf5f9('0xa'));var fs=require('fs');var fs_extra=require(_0xf5f9('0xb'));var _=require(_0xf5f9('0xc'));var squel=require('squel');var crypto=require(_0xf5f9('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xf5f9('0xe'));var toCsv=require(_0xf5f9('0x9'));var querystring=require(_0xf5f9('0xf'));var Papa=require('papaparse');var Redis=require(_0xf5f9('0x10'));var authService=require(_0xf5f9('0x11'));var qs=require(_0xf5f9('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf5f9('0x13'));var logger=require(_0xf5f9('0x14'))(_0xf5f9('0x15'));var utils=require(_0xf5f9('0x16'));var config=require(_0xf5f9('0x17'));var licenseUtil=require(_0xf5f9('0x18'));var db=require('../../mysqldb')['db'];var integrations=require(_0xf5f9('0x19'));function respondWithStatusCode(_0x37f1a4,_0x3e9e9c){_0x3e9e9c=_0x3e9e9c||0xcc;return function(_0x5cdcd7){if(_0x5cdcd7){return _0x37f1a4[_0xf5f9('0x1a')](_0x3e9e9c);}return _0x37f1a4[_0xf5f9('0x1b')](_0x3e9e9c)[_0xf5f9('0x1c')]();};}function respondWithResult(_0x48bcad,_0x2e945d){_0x2e945d=_0x2e945d||0xc8;return function(_0x24f524){if(_0x24f524){return _0x48bcad[_0xf5f9('0x1b')](_0x2e945d)['json'](_0x24f524);}};}function respondWithFilteredResult(_0x5ca60e,_0x1252d9){return function(_0x138058){if(_0x138058){var _0x19ffc9=typeof _0x1252d9[_0xf5f9('0x1d')]==='undefined'&&typeof _0x1252d9[_0xf5f9('0x1e')]===_0xf5f9('0x1f');var _0x5c1391=_0x138058['count'];var _0x367175=_0x19ffc9?0x0:_0x1252d9[_0xf5f9('0x1d')];var _0x5ab03c=_0x19ffc9?_0x138058[_0xf5f9('0x20')]:_0x1252d9[_0xf5f9('0x1d')]+_0x1252d9['limit'];var _0x25919a;if(_0x5ab03c>=_0x5c1391){_0x5ab03c=_0x5c1391;_0x25919a=0xc8;}else{_0x25919a=0xce;}_0x5ca60e[_0xf5f9('0x1b')](_0x25919a);return _0x5ca60e[_0xf5f9('0x21')](_0xf5f9('0x22'),_0x367175+'-'+_0x5ab03c+'/'+_0x5c1391)[_0xf5f9('0x23')](_0x138058);}return null;};}function patchUpdates(_0x357ed8){return function(_0x3e6827){try{jsonpatch[_0xf5f9('0x24')](_0x3e6827,_0x357ed8,!![]);}catch(_0x4fc687){return BPromise['reject'](_0x4fc687);}return _0x3e6827[_0xf5f9('0x25')]();};}function saveUpdates(_0x2b4913,_0x43202f){return function(_0x24b2f1){if(_0x24b2f1){return _0x24b2f1[_0xf5f9('0x26')](_0x2b4913)[_0xf5f9('0x27')](function(_0x5ea36c){return _0x5ea36c;});}return null;};}function removeEntity(_0x1c99b2,_0x5b13eb){return function(_0x524c1a){if(_0x524c1a){return _0x524c1a[_0xf5f9('0x28')]()[_0xf5f9('0x27')](function(){_0x1c99b2[_0xf5f9('0x1b')](0xcc)[_0xf5f9('0x1c')]();});}};}function handleEntityNotFound(_0x3a5eff,_0x4ed4b6){return function(_0xf654aa){if(!_0xf654aa){_0x3a5eff['sendStatus'](0x194);}return _0xf654aa;};}function handleError(_0x5c4b36,_0x1c91d1){_0x1c91d1=_0x1c91d1||0x1f4;return function(_0x31134d){logger['error'](_0x31134d['stack']);if(_0x31134d[_0xf5f9('0x29')]){delete _0x31134d[_0xf5f9('0x29')];}_0x5c4b36['status'](_0x1c91d1)[_0xf5f9('0x2a')](_0x31134d);};}exports['index']=function(_0x3e3d49,_0x45260f){var _0x1776d0={},_0x4aeb5b={},_0x367bc5={'count':0x0,'rows':[]};var _0x270e84=_[_0xf5f9('0x2b')](db[_0xf5f9('0x2c')][_0xf5f9('0x2d')],function(_0x4ae810){return{'name':_0x4ae810['fieldName'],'type':_0x4ae810['type'][_0xf5f9('0x2e')]};});_0x4aeb5b[_0xf5f9('0x2f')]=_[_0xf5f9('0x2b')](_0x270e84,_0xf5f9('0x29'));_0x4aeb5b[_0xf5f9('0x30')]=_[_0xf5f9('0x31')](_0x3e3d49[_0xf5f9('0x30')]);_0x4aeb5b[_0xf5f9('0x32')]=_[_0xf5f9('0x33')](_0x4aeb5b[_0xf5f9('0x2f')],_0x4aeb5b[_0xf5f9('0x30')]);_0x1776d0[_0xf5f9('0x34')]=_[_0xf5f9('0x33')](_0x4aeb5b[_0xf5f9('0x2f')],qs['fields'](_0x3e3d49['query'][_0xf5f9('0x35')]));_0x1776d0['attributes']=_0x1776d0['attributes'][_0xf5f9('0x36')]?_0x1776d0[_0xf5f9('0x34')]:_0x4aeb5b[_0xf5f9('0x2f')];if(!_0x3e3d49[_0xf5f9('0x30')][_0xf5f9('0x37')](_0xf5f9('0x38'))){_0x1776d0[_0xf5f9('0x1e')]=qs[_0xf5f9('0x1e')](_0x3e3d49[_0xf5f9('0x30')][_0xf5f9('0x1e')]);_0x1776d0['offset']=qs[_0xf5f9('0x1d')](_0x3e3d49['query']['offset']);}_0x1776d0[_0xf5f9('0x39')]=qs[_0xf5f9('0x3a')](_0x3e3d49[_0xf5f9('0x30')][_0xf5f9('0x3a')]);_0x1776d0[_0xf5f9('0x3b')]=qs[_0xf5f9('0x32')](_[_0xf5f9('0x3c')](_0x3e3d49['query'],_0x4aeb5b[_0xf5f9('0x32')]),_0x270e84);if(_0x3e3d49['query'][_0xf5f9('0x3d')]){_0x1776d0[_0xf5f9('0x3b')]=_['merge'](_0x1776d0[_0xf5f9('0x3b')],{'$or':_[_0xf5f9('0x2b')](_0x270e84,function(_0x88f9c9){if(_0x88f9c9[_0xf5f9('0x3e')]!==_0xf5f9('0x3f')){var _0x5f2fbe={};_0x5f2fbe[_0x88f9c9[_0xf5f9('0x29')]]={'$like':'%'+_0x3e3d49[_0xf5f9('0x30')][_0xf5f9('0x3d')]+'%'};return _0x5f2fbe;}})});}_0x1776d0=_[_0xf5f9('0x40')]({},_0x1776d0,_0x3e3d49[_0xf5f9('0x41')]);var _0x37e111={'where':_0x1776d0[_0xf5f9('0x3b')]};return db['SugarcrmAccount']['count'](_0x37e111)['then'](function(_0xaef729){_0x367bc5[_0xf5f9('0x20')]=_0xaef729;if(_0x3e3d49['query'][_0xf5f9('0x42')]){_0x1776d0['include']=[{'all':!![]}];}return db[_0xf5f9('0x2c')][_0xf5f9('0x43')](_0x1776d0);})[_0xf5f9('0x27')](function(_0x41193d){_0x367bc5[_0xf5f9('0x44')]=_0x41193d;return _0x367bc5;})[_0xf5f9('0x27')](respondWithFilteredResult(_0x45260f,_0x1776d0))[_0xf5f9('0x45')](handleError(_0x45260f,null));};exports['show']=function(_0x29978f,_0x2c58c2){var _0x585105={'raw':![],'where':{'id':_0x29978f[_0xf5f9('0x46')]['id']}},_0x150f7c={};_0x150f7c['model']=_[_0xf5f9('0x31')](db['SugarcrmAccount'][_0xf5f9('0x2d')]);_0x150f7c[_0xf5f9('0x30')]=_['keys'](_0x29978f[_0xf5f9('0x30')]);_0x150f7c['filters']=_[_0xf5f9('0x33')](_0x150f7c[_0xf5f9('0x2f')],_0x150f7c[_0xf5f9('0x30')]);_0x585105['attributes']=_[_0xf5f9('0x33')](_0x150f7c[_0xf5f9('0x2f')],qs['fields'](_0x29978f[_0xf5f9('0x30')][_0xf5f9('0x35')]));_0x585105[_0xf5f9('0x34')]=_0x585105[_0xf5f9('0x34')][_0xf5f9('0x36')]?_0x585105[_0xf5f9('0x34')]:_0x150f7c[_0xf5f9('0x2f')];if(_0x29978f[_0xf5f9('0x30')]['includeAll']){_0x585105['include']=[{'all':!![]}];}_0x585105=_[_0xf5f9('0x40')]({},_0x585105,_0x29978f['options']);return db[_0xf5f9('0x2c')][_0xf5f9('0x47')](_0x585105)[_0xf5f9('0x27')](handleEntityNotFound(_0x2c58c2,null))[_0xf5f9('0x27')](respondWithResult(_0x2c58c2,null))[_0xf5f9('0x45')](handleError(_0x2c58c2,null));};exports[_0xf5f9('0x48')]=function(_0x3e4f48,_0x1743e9){return db[_0xf5f9('0x2c')][_0xf5f9('0x48')](_0x3e4f48[_0xf5f9('0x49')],{})[_0xf5f9('0x27')](respondWithResult(_0x1743e9,0xc9))[_0xf5f9('0x45')](handleError(_0x1743e9,null));};exports['update']=function(_0x3b9a15,_0x5f9a9){if(_0x3b9a15[_0xf5f9('0x49')]['id']){delete _0x3b9a15[_0xf5f9('0x49')]['id'];}return db[_0xf5f9('0x2c')][_0xf5f9('0x47')]({'where':{'id':_0x3b9a15[_0xf5f9('0x46')]['id']}})[_0xf5f9('0x27')](handleEntityNotFound(_0x5f9a9,null))[_0xf5f9('0x27')](saveUpdates(_0x3b9a15[_0xf5f9('0x49')],null))[_0xf5f9('0x27')](respondWithResult(_0x5f9a9,null))['catch'](handleError(_0x5f9a9,null));};exports['destroy']=function(_0x3caed6,_0x5b1c46){return db[_0xf5f9('0x2c')][_0xf5f9('0x47')]({'where':{'id':_0x3caed6[_0xf5f9('0x46')]['id']}})[_0xf5f9('0x27')](handleEntityNotFound(_0x5b1c46,null))[_0xf5f9('0x27')](removeEntity(_0x5b1c46,null))['catch'](handleError(_0x5b1c46,null));};exports[_0xf5f9('0x4a')]=function(_0x2dab9f,_0x345996,_0x22896c){var _0x4c9431={};var _0x117eda={};var _0x20e4de;var _0x5f7016;return db[_0xf5f9('0x2c')][_0xf5f9('0x4b')]({'where':{'id':_0x2dab9f['params']['id']}})[_0xf5f9('0x27')](handleEntityNotFound(_0x345996,null))[_0xf5f9('0x27')](function(_0x207060){if(_0x207060){_0x20e4de=_0x207060;_0x117eda[_0xf5f9('0x2f')]=_[_0xf5f9('0x31')](db[_0xf5f9('0x4c')][_0xf5f9('0x2d')]);_0x117eda['query']=_[_0xf5f9('0x31')](_0x2dab9f[_0xf5f9('0x30')]);_0x117eda[_0xf5f9('0x32')]=_[_0xf5f9('0x33')](_0x117eda['model'],_0x117eda[_0xf5f9('0x30')]);_0x4c9431[_0xf5f9('0x34')]=_[_0xf5f9('0x33')](_0x117eda[_0xf5f9('0x2f')],qs['fields'](_0x2dab9f['query'][_0xf5f9('0x35')]));_0x4c9431[_0xf5f9('0x34')]=_0x4c9431[_0xf5f9('0x34')][_0xf5f9('0x36')]?_0x4c9431[_0xf5f9('0x34')]:_0x117eda[_0xf5f9('0x2f')];_0x4c9431[_0xf5f9('0x39')]=qs[_0xf5f9('0x3a')](_0x2dab9f[_0xf5f9('0x30')][_0xf5f9('0x3a')]);_0x4c9431[_0xf5f9('0x3b')]=qs[_0xf5f9('0x32')](_[_0xf5f9('0x3c')](_0x2dab9f[_0xf5f9('0x30')],_0x117eda[_0xf5f9('0x32')]));if(_0x2dab9f[_0xf5f9('0x30')]['filter']){_0x4c9431['where']=_[_0xf5f9('0x40')](_0x4c9431[_0xf5f9('0x3b')],{'$or':_[_0xf5f9('0x2b')](_0x4c9431[_0xf5f9('0x34')],function(_0x26941e){var _0x4d3acd={};_0x4d3acd[_0x26941e]={'$like':'%'+_0x2dab9f['query'][_0xf5f9('0x3d')]+'%'};return _0x4d3acd;})});}_0x4c9431=_['merge']({},_0x4c9431,_0x2dab9f[_0xf5f9('0x41')]);return _0x20e4de[_0xf5f9('0x4a')](_0x4c9431);}})['then'](function(_0x323035){if(_0x323035){_0x5f7016=_0x323035[_0xf5f9('0x36')];if(!_0x2dab9f[_0xf5f9('0x30')][_0xf5f9('0x37')](_0xf5f9('0x38'))){_0x4c9431[_0xf5f9('0x1e')]=qs[_0xf5f9('0x1e')](_0x2dab9f['query'][_0xf5f9('0x1e')]);_0x4c9431[_0xf5f9('0x1d')]=qs['offset'](_0x2dab9f['query'][_0xf5f9('0x1d')]);}return _0x20e4de['getConfigurations'](_0x4c9431);}})[_0xf5f9('0x27')](function(_0x187fa2){if(_0x187fa2){return _0x187fa2?{'count':_0x5f7016,'rows':_0x187fa2}:null;}})[_0xf5f9('0x27')](respondWithResult(_0x345996,null))['catch'](handleError(_0x345996,null));};exports['addConfiguration']=function(_0x5a5232,_0x37c2ea,_0x4bf57d){if(_0x5a5232[_0xf5f9('0x49')]['id']){delete _0x5a5232[_0xf5f9('0x49')]['id'];}return db['SugarcrmAccount'][_0xf5f9('0x4b')]({'where':{'id':_0x5a5232['params']['id']}})['then'](handleEntityNotFound(_0x37c2ea,null))['then'](function(_0x2a99d9){if(_0x2a99d9){_0x5a5232['body'][_0xf5f9('0x4d')]=_0x2a99d9['id'];_0x5a5232[_0xf5f9('0x49')]['Subjects']=integrations['getSubjects'](_0x5a5232[_0xf5f9('0x49')][_0xf5f9('0x4e')],_0x5a5232[_0xf5f9('0x49')][_0xf5f9('0x3e')]);_0x5a5232[_0xf5f9('0x49')][_0xf5f9('0x4f')]=integrations[_0xf5f9('0x50')](_0x5a5232[_0xf5f9('0x49')][_0xf5f9('0x4e')],_0x5a5232[_0xf5f9('0x49')][_0xf5f9('0x3e')]);return db[_0xf5f9('0x4c')][_0xf5f9('0x48')](_0x5a5232[_0xf5f9('0x49')],{'include':[{'model':db[_0xf5f9('0x51')],'as':_0xf5f9('0x52')},{'model':db[_0xf5f9('0x51')],'as':_0xf5f9('0x4f')}]});}return null;})[_0xf5f9('0x27')](respondWithResult(_0x37c2ea,null))[_0xf5f9('0x45')](handleError(_0x37c2ea,null));};exports['getFields']=function(_0x141a30,_0x1ea089,_0x5e6908){var _0x303ae5='';var _0x5b7b7a=['id',_0xf5f9('0x29'),_0xf5f9('0x53'),_0xf5f9('0x54'),_0xf5f9('0x55'),_0xf5f9('0x56'),'duration_hours','duration_minutes',_0xf5f9('0x57'),_0xf5f9('0x58'),'parent_name',_0xf5f9('0x59')];return db[_0xf5f9('0x2c')][_0xf5f9('0x4b')]({'where':{'id':_0x141a30[_0xf5f9('0x46')]['id']},'attributes':['id',_0xf5f9('0x5a'),_0xf5f9('0x5b'),_0xf5f9('0x5c')]})[_0xf5f9('0x27')](handleEntityNotFound(_0x1ea089,null))['then'](function(_0x2d6ecb){if(_0x2d6ecb){_0x303ae5=_0x2d6ecb[_0xf5f9('0x5c')];var _0xb0773=_0x303ae5['slice'](-0x1);if(_0xb0773==='/'){_0x303ae5=_0x303ae5[_0xf5f9('0x5d')](0x0,_0x303ae5[_0xf5f9('0x5e')](_0xb0773));}return rp({'method':_0xf5f9('0x5f'),'uri':util['format'](_0xf5f9('0x60'),_0x303ae5,_0xf5f9('0x61')),'json':!![],'form':{'method':_0xf5f9('0x62'),'input_type':_0xf5f9('0x63'),'response_type':_0xf5f9('0x63'),'rest_data':JSON[_0xf5f9('0x64')]({'user_auth':{'user_name':_0x2d6ecb[_0xf5f9('0x5a')],'password':_0x2d6ecb[_0xf5f9('0x5b')],'encryption':_0xf5f9('0x65')},'application':_0xf5f9('0x66')})}});}})[_0xf5f9('0x27')](function(_0xbc21d4){if(_0xbc21d4&&_0xbc21d4[_0xf5f9('0x29')]!==_0xf5f9('0x67')&&_0xbc21d4[_0xf5f9('0x29')]!==_0xf5f9('0x68')){return rp({'method':'POST','uri':util[_0xf5f9('0x69')]('%s%s',_0x303ae5,'/service/v4_1/rest.php'),'json':!![],'form':{'method':_0xf5f9('0x6a'),'input_type':_0xf5f9('0x63'),'response_type':_0xf5f9('0x63'),'rest_data':JSON['stringify']({'session':_0xbc21d4['id'],'module_name':_0xf5f9('0x6b')})}})[_0xf5f9('0x27')](function(_0x26135b){if(_0x26135b){var _0x546180=_['values'](_0x26135b[_0xf5f9('0x6c')]);_[_0xf5f9('0x6d')](_0x546180,function(_0x18da47){return _0x5b7b7a[_0xf5f9('0x6e')](_0x18da47[_0xf5f9('0x29')]);});return{'count':_0x546180['length'],'rows':_[_0xf5f9('0x2b')](_0x546180,function(_0x17e7b4){return{'id':_0x17e7b4['name'],'name':_0x17e7b4[_0xf5f9('0x6f')],'custom':_[_0xf5f9('0x70')](_0x17e7b4[_0xf5f9('0x29')],'_c')?!![]:![],'options':_0x17e7b4[_0xf5f9('0x41')]?_['map'](_[_0xf5f9('0x71')](_0x17e7b4['options']),function(_0x21db11){return{'name':_0x21db11[_0xf5f9('0x72')],'value':_0x21db11[_0xf5f9('0x29')]};}):[]};})};}})[_0xf5f9('0x27')](respondWithResult(_0x1ea089,null))[_0xf5f9('0x45')](function(_0x584e4f){var _0x154d7e=_0x141a30[_0xf5f9('0x30')][_0xf5f9('0x73')]?0x1f4:_0x584e4f[_0xf5f9('0x74')]||0x1f4;logger[_0xf5f9('0x75')](_0xf5f9('0x76'),_0xf5f9('0x77'),_0x154d7e,JSON['stringify'](_0x584e4f));delete _0x584e4f['name'];if(_0x154d7e===0x191){_0x154d7e=0x190;}_0x1ea089[_0xf5f9('0x1b')](_0x154d7e)[_0xf5f9('0x2a')](_0x141a30[_0xf5f9('0x30')][_0xf5f9('0x73')]?{'message':'Wrong\x20credentials','statusCode':_0x584e4f[_0xf5f9('0x74')]}:_0x584e4f);});}else{var _0x570824=0x1f4;_0x1ea089['status'](_0x570824)[_0xf5f9('0x2a')]({'message':'Wrong\x20credentials','statusCode':0x190});}});};