Built motion from commit 1fd437e9.|2.6.19
[motion2.git] / server / api / intDeskAccount / intDeskAccount.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 _0x2bcf=['Basic\x20%s','%s:%s','base64','concat','_embedded','entries','active','label','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../config/license/hardware','api','../../config/environment','../../components/integrations/configuration','sendStatus','status','end','json','offset','limit','undefined','count','set','reject','save','update','destroy','error','name','send','index','DeskAccount','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','VIRTUAL','options','then','rows','catch','show','params','includeAll','find','create','body','getConfigurations','findOne','DeskConfiguration','AccountId','Descriptions','getDescriptions','channel','Subjects','DeskField','getFields','priority','remoteUri','authType','username','password','consumerKey','consumerSecret','token','slice','substring','lastIndexOf','GET','format','%s/custom_fields','application/json','basic','Authorization'];(function(_0x5872f4,_0x399432){var _0xcb6124=function(_0x591aba){while(--_0x591aba){_0x5872f4['push'](_0x5872f4['shift']());}};_0xcb6124(++_0x399432);}(_0x2bcf,0x1d7));var _0xf2bc=function(_0x529f79,_0x5b4c85){_0x529f79=_0x529f79-0x0;var _0x21e1d5=_0x2bcf[_0x529f79];return _0x21e1d5;};'use strict';var emlformat=require(_0xf2bc('0x0'));var rimraf=require(_0xf2bc('0x1'));var zipdir=require(_0xf2bc('0x2'));var jsonpatch=require(_0xf2bc('0x3'));var rp=require(_0xf2bc('0x4'));var moment=require(_0xf2bc('0x5'));var BPromise=require(_0xf2bc('0x6'));var Mustache=require(_0xf2bc('0x7'));var util=require(_0xf2bc('0x8'));var path=require(_0xf2bc('0x9'));var sox=require(_0xf2bc('0xa'));var csv=require(_0xf2bc('0xb'));var ejs=require(_0xf2bc('0xc'));var fs=require('fs');var fs_extra=require(_0xf2bc('0xd'));var _=require(_0xf2bc('0xe'));var squel=require(_0xf2bc('0xf'));var crypto=require(_0xf2bc('0x10'));var jsforce=require(_0xf2bc('0x11'));var deskjs=require(_0xf2bc('0x12'));var toCsv=require(_0xf2bc('0xb'));var querystring=require(_0xf2bc('0x13'));var Papa=require(_0xf2bc('0x14'));var Redis=require(_0xf2bc('0x15'));var authService=require('../../components/auth/service');var qs=require(_0xf2bc('0x16'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf2bc('0x17'));var logger=require('../../config/logger')(_0xf2bc('0x18'));var utils=require('../../config/utils');var config=require(_0xf2bc('0x19'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];var integrations=require(_0xf2bc('0x1a'));function respondWithStatusCode(_0x371733,_0x18e8e9){_0x18e8e9=_0x18e8e9||0xcc;return function(_0x3829e2){if(_0x3829e2){return _0x371733[_0xf2bc('0x1b')](_0x18e8e9);}return _0x371733[_0xf2bc('0x1c')](_0x18e8e9)[_0xf2bc('0x1d')]();};}function respondWithResult(_0x163966,_0x4b488c){_0x4b488c=_0x4b488c||0xc8;return function(_0x16a728){if(_0x16a728){return _0x163966[_0xf2bc('0x1c')](_0x4b488c)[_0xf2bc('0x1e')](_0x16a728);}};}function respondWithFilteredResult(_0x6203c5,_0x474223){return function(_0x1f2aac){if(_0x1f2aac){var _0x45191c=typeof _0x474223[_0xf2bc('0x1f')]==='undefined'&&typeof _0x474223[_0xf2bc('0x20')]===_0xf2bc('0x21');var _0x105826=_0x1f2aac[_0xf2bc('0x22')];var _0x43957f=_0x45191c?0x0:_0x474223[_0xf2bc('0x1f')];var _0x5c8705=_0x45191c?_0x1f2aac[_0xf2bc('0x22')]:_0x474223[_0xf2bc('0x1f')]+_0x474223[_0xf2bc('0x20')];var _0x43bbd5;if(_0x5c8705>=_0x105826){_0x5c8705=_0x105826;_0x43bbd5=0xc8;}else{_0x43bbd5=0xce;}_0x6203c5[_0xf2bc('0x1c')](_0x43bbd5);return _0x6203c5[_0xf2bc('0x23')]('Content-Range',_0x43957f+'-'+_0x5c8705+'/'+_0x105826)['json'](_0x1f2aac);}return null;};}function patchUpdates(_0x4a36f0){return function(_0x2bd6c0){try{jsonpatch['apply'](_0x2bd6c0,_0x4a36f0,!![]);}catch(_0x2c76ca){return BPromise[_0xf2bc('0x24')](_0x2c76ca);}return _0x2bd6c0[_0xf2bc('0x25')]();};}function saveUpdates(_0x294b0e,_0x453129){return function(_0x1b08b0){if(_0x1b08b0){return _0x1b08b0[_0xf2bc('0x26')](_0x294b0e)['then'](function(_0x20417e){return _0x20417e;});}return null;};}function removeEntity(_0x255d00,_0x212d67){return function(_0x3a8506){if(_0x3a8506){return _0x3a8506[_0xf2bc('0x27')]()['then'](function(){_0x255d00['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x598468,_0x4466fd){return function(_0x6d1ca){if(!_0x6d1ca){_0x598468['sendStatus'](0x194);}return _0x6d1ca;};}function handleError(_0x3b5cbb,_0x2ab897){_0x2ab897=_0x2ab897||0x1f4;return function(_0x1fefd6){logger[_0xf2bc('0x28')](_0x1fefd6['stack']);if(_0x1fefd6[_0xf2bc('0x29')]){delete _0x1fefd6['name'];}_0x3b5cbb['status'](_0x2ab897)[_0xf2bc('0x2a')](_0x1fefd6);};}exports[_0xf2bc('0x2b')]=function(_0x357095,_0x437343){var _0x298a7b={},_0x5d1a25={},_0x4714df={'count':0x0,'rows':[]};var _0x14dde3=_['map'](db[_0xf2bc('0x2c')][_0xf2bc('0x2d')],function(_0x3cbd08){return{'name':_0x3cbd08[_0xf2bc('0x2e')],'type':_0x3cbd08[_0xf2bc('0x2f')][_0xf2bc('0x30')]};});_0x5d1a25[_0xf2bc('0x31')]=_[_0xf2bc('0x32')](_0x14dde3,_0xf2bc('0x29'));_0x5d1a25[_0xf2bc('0x33')]=_[_0xf2bc('0x34')](_0x357095['query']);_0x5d1a25[_0xf2bc('0x35')]=_[_0xf2bc('0x36')](_0x5d1a25[_0xf2bc('0x31')],_0x5d1a25[_0xf2bc('0x33')]);_0x298a7b['attributes']=_[_0xf2bc('0x36')](_0x5d1a25[_0xf2bc('0x31')],qs[_0xf2bc('0x37')](_0x357095[_0xf2bc('0x33')][_0xf2bc('0x37')]));_0x298a7b['attributes']=_0x298a7b[_0xf2bc('0x38')][_0xf2bc('0x39')]?_0x298a7b['attributes']:_0x5d1a25['model'];if(!_0x357095[_0xf2bc('0x33')][_0xf2bc('0x3a')](_0xf2bc('0x3b'))){_0x298a7b[_0xf2bc('0x20')]=qs[_0xf2bc('0x20')](_0x357095[_0xf2bc('0x33')]['limit']);_0x298a7b[_0xf2bc('0x1f')]=qs[_0xf2bc('0x1f')](_0x357095[_0xf2bc('0x33')][_0xf2bc('0x1f')]);}_0x298a7b[_0xf2bc('0x3c')]=qs[_0xf2bc('0x3d')](_0x357095[_0xf2bc('0x33')][_0xf2bc('0x3d')]);_0x298a7b['where']=qs[_0xf2bc('0x35')](_[_0xf2bc('0x3e')](_0x357095[_0xf2bc('0x33')],_0x5d1a25[_0xf2bc('0x35')]),_0x14dde3);if(_0x357095['query'][_0xf2bc('0x3f')]){_0x298a7b[_0xf2bc('0x40')]=_[_0xf2bc('0x41')](_0x298a7b[_0xf2bc('0x40')],{'$or':_[_0xf2bc('0x32')](_0x14dde3,function(_0x34b06e){if(_0x34b06e[_0xf2bc('0x2f')]!==_0xf2bc('0x42')){var _0x178d4b={};_0x178d4b[_0x34b06e[_0xf2bc('0x29')]]={'$like':'%'+_0x357095[_0xf2bc('0x33')][_0xf2bc('0x3f')]+'%'};return _0x178d4b;}})});}_0x298a7b=_[_0xf2bc('0x41')]({},_0x298a7b,_0x357095[_0xf2bc('0x43')]);var _0x4ec48f={'where':_0x298a7b[_0xf2bc('0x40')]};return db[_0xf2bc('0x2c')]['count'](_0x4ec48f)[_0xf2bc('0x44')](function(_0x4c642b){_0x4714df['count']=_0x4c642b;if(_0x357095[_0xf2bc('0x33')]['includeAll']){_0x298a7b['include']=[{'all':!![]}];}return db[_0xf2bc('0x2c')]['findAll'](_0x298a7b);})[_0xf2bc('0x44')](function(_0x456dae){_0x4714df[_0xf2bc('0x45')]=_0x456dae;return _0x4714df;})[_0xf2bc('0x44')](respondWithFilteredResult(_0x437343,_0x298a7b))[_0xf2bc('0x46')](handleError(_0x437343,null));};exports[_0xf2bc('0x47')]=function(_0x2ce06c,_0x281215){var _0x573847={'raw':![],'where':{'id':_0x2ce06c[_0xf2bc('0x48')]['id']}},_0x1e94c7={};_0x1e94c7[_0xf2bc('0x31')]=_[_0xf2bc('0x34')](db['DeskAccount']['rawAttributes']);_0x1e94c7[_0xf2bc('0x33')]=_[_0xf2bc('0x34')](_0x2ce06c[_0xf2bc('0x33')]);_0x1e94c7[_0xf2bc('0x35')]=_[_0xf2bc('0x36')](_0x1e94c7[_0xf2bc('0x31')],_0x1e94c7[_0xf2bc('0x33')]);_0x573847[_0xf2bc('0x38')]=_[_0xf2bc('0x36')](_0x1e94c7[_0xf2bc('0x31')],qs['fields'](_0x2ce06c[_0xf2bc('0x33')][_0xf2bc('0x37')]));_0x573847[_0xf2bc('0x38')]=_0x573847[_0xf2bc('0x38')][_0xf2bc('0x39')]?_0x573847['attributes']:_0x1e94c7[_0xf2bc('0x31')];if(_0x2ce06c[_0xf2bc('0x33')][_0xf2bc('0x49')]){_0x573847['include']=[{'all':!![]}];}_0x573847=_[_0xf2bc('0x41')]({},_0x573847,_0x2ce06c[_0xf2bc('0x43')]);return db['DeskAccount'][_0xf2bc('0x4a')](_0x573847)[_0xf2bc('0x44')](handleEntityNotFound(_0x281215,null))[_0xf2bc('0x44')](respondWithResult(_0x281215,null))[_0xf2bc('0x46')](handleError(_0x281215,null));};exports['create']=function(_0x938628,_0xdfe0c6){return db[_0xf2bc('0x2c')][_0xf2bc('0x4b')](_0x938628[_0xf2bc('0x4c')],{})[_0xf2bc('0x44')](respondWithResult(_0xdfe0c6,0xc9))[_0xf2bc('0x46')](handleError(_0xdfe0c6,null));};exports[_0xf2bc('0x26')]=function(_0x380371,_0x51b641){if(_0x380371[_0xf2bc('0x4c')]['id']){delete _0x380371[_0xf2bc('0x4c')]['id'];}return db[_0xf2bc('0x2c')][_0xf2bc('0x4a')]({'where':{'id':_0x380371[_0xf2bc('0x48')]['id']}})[_0xf2bc('0x44')](handleEntityNotFound(_0x51b641,null))[_0xf2bc('0x44')](saveUpdates(_0x380371['body'],null))['then'](respondWithResult(_0x51b641,null))[_0xf2bc('0x46')](handleError(_0x51b641,null));};exports[_0xf2bc('0x27')]=function(_0x3cbf64,_0x308765){return db[_0xf2bc('0x2c')][_0xf2bc('0x4a')]({'where':{'id':_0x3cbf64[_0xf2bc('0x48')]['id']}})['then'](handleEntityNotFound(_0x308765,null))['then'](removeEntity(_0x308765,null))[_0xf2bc('0x46')](handleError(_0x308765,null));};exports[_0xf2bc('0x4d')]=function(_0x321f8c,_0x5ef482,_0x39075b){var _0x4bb49a={};var _0x301c88={};var _0x2fb607;var _0x1a8215;return db[_0xf2bc('0x2c')][_0xf2bc('0x4e')]({'where':{'id':_0x321f8c[_0xf2bc('0x48')]['id']}})[_0xf2bc('0x44')](handleEntityNotFound(_0x5ef482,null))['then'](function(_0x272cb3){if(_0x272cb3){_0x2fb607=_0x272cb3;_0x301c88[_0xf2bc('0x31')]=_['keys'](db[_0xf2bc('0x4f')][_0xf2bc('0x2d')]);_0x301c88[_0xf2bc('0x33')]=_[_0xf2bc('0x34')](_0x321f8c[_0xf2bc('0x33')]);_0x301c88[_0xf2bc('0x35')]=_[_0xf2bc('0x36')](_0x301c88[_0xf2bc('0x31')],_0x301c88['query']);_0x4bb49a[_0xf2bc('0x38')]=_[_0xf2bc('0x36')](_0x301c88['model'],qs[_0xf2bc('0x37')](_0x321f8c['query'][_0xf2bc('0x37')]));_0x4bb49a[_0xf2bc('0x38')]=_0x4bb49a[_0xf2bc('0x38')][_0xf2bc('0x39')]?_0x4bb49a['attributes']:_0x301c88[_0xf2bc('0x31')];_0x4bb49a[_0xf2bc('0x3c')]=qs[_0xf2bc('0x3d')](_0x321f8c[_0xf2bc('0x33')]['sort']);_0x4bb49a['where']=qs['filters'](_[_0xf2bc('0x3e')](_0x321f8c['query'],_0x301c88[_0xf2bc('0x35')]));if(_0x321f8c[_0xf2bc('0x33')][_0xf2bc('0x3f')]){_0x4bb49a[_0xf2bc('0x40')]=_['merge'](_0x4bb49a['where'],{'$or':_[_0xf2bc('0x32')](_0x4bb49a[_0xf2bc('0x38')],function(_0x3ef723){var _0x50010f={};_0x50010f[_0x3ef723]={'$like':'%'+_0x321f8c[_0xf2bc('0x33')]['filter']+'%'};return _0x50010f;})});}_0x4bb49a=_[_0xf2bc('0x41')]({},_0x4bb49a,_0x321f8c['options']);return _0x2fb607['getConfigurations'](_0x4bb49a);}})[_0xf2bc('0x44')](function(_0x2c973f){if(_0x2c973f){_0x1a8215=_0x2c973f['length'];if(!_0x321f8c[_0xf2bc('0x33')][_0xf2bc('0x3a')]('nolimit')){_0x4bb49a['limit']=qs[_0xf2bc('0x20')](_0x321f8c[_0xf2bc('0x33')]['limit']);_0x4bb49a['offset']=qs[_0xf2bc('0x1f')](_0x321f8c[_0xf2bc('0x33')][_0xf2bc('0x1f')]);}return _0x2fb607[_0xf2bc('0x4d')](_0x4bb49a);}})['then'](function(_0x42a1de){if(_0x42a1de){return _0x42a1de?{'count':_0x1a8215,'rows':_0x42a1de}:null;}})['then'](respondWithResult(_0x5ef482,null))[_0xf2bc('0x46')](handleError(_0x5ef482,null));};exports['addConfiguration']=function(_0x5285d6,_0x584f8b,_0x2be745){if(_0x5285d6[_0xf2bc('0x4c')]['id']){delete _0x5285d6[_0xf2bc('0x4c')]['id'];}return db[_0xf2bc('0x2c')][_0xf2bc('0x4e')]({'where':{'id':_0x5285d6[_0xf2bc('0x48')]['id']}})[_0xf2bc('0x44')](handleEntityNotFound(_0x584f8b,null))[_0xf2bc('0x44')](function(_0x4796d8){if(_0x4796d8){_0x5285d6[_0xf2bc('0x4c')][_0xf2bc('0x50')]=_0x4796d8['id'];_0x5285d6[_0xf2bc('0x4c')]['Subjects']=integrations['getSubjects'](_0x5285d6[_0xf2bc('0x4c')]['channel'],_0x5285d6[_0xf2bc('0x4c')][_0xf2bc('0x2f')]);_0x5285d6[_0xf2bc('0x4c')][_0xf2bc('0x51')]=integrations[_0xf2bc('0x52')](_0x5285d6[_0xf2bc('0x4c')][_0xf2bc('0x53')],_0x5285d6['body'][_0xf2bc('0x2f')]);return db['DeskConfiguration']['create'](_0x5285d6[_0xf2bc('0x4c')],{'include':[{'model':db['DeskField'],'as':_0xf2bc('0x54')},{'model':db[_0xf2bc('0x55')],'as':'Descriptions'}]});}return null;})[_0xf2bc('0x44')](respondWithResult(_0x584f8b,null))[_0xf2bc('0x46')](handleError(_0x584f8b,null));};exports[_0xf2bc('0x56')]=function(_0x1bca07,_0x19179c,_0x1a3a63){var _0x100597=[{'name':_0xf2bc('0x57'),'label':'priority','custom':![],'options':[{'name':0x1,'value':0x1},{'name':0x2,'value':0x2},{'name':0x3,'value':0x3},{'name':0x4,'value':0x4},{'name':0x5,'value':0x5},{'name':0x6,'value':0x6},{'name':0x7,'value':0x7},{'name':0x8,'value':0x8},{'name':0x9,'value':0x9},{'name':0xa,'value':0xa}]}];var _0x52d02d='';return db[_0xf2bc('0x2c')][_0xf2bc('0x4e')]({'where':{'id':_0x1bca07['params']['id']},'attributes':['id',_0xf2bc('0x58'),_0xf2bc('0x59'),_0xf2bc('0x5a'),_0xf2bc('0x5b'),_0xf2bc('0x5c'),_0xf2bc('0x5d'),_0xf2bc('0x5e'),'tokenSecret']})[_0xf2bc('0x44')](handleEntityNotFound(_0x19179c,null))[_0xf2bc('0x44')](function(_0x206a1d){if(_0x206a1d){_0x52d02d=_0x206a1d[_0xf2bc('0x58')];var _0x360601=_0x52d02d[_0xf2bc('0x5f')](-0x1);if(_0x360601==='/'){_0x52d02d=_0x52d02d[_0xf2bc('0x60')](0x0,_0x52d02d[_0xf2bc('0x61')](_0x360601));}var _0x1c864e={'method':_0xf2bc('0x62'),'uri':util[_0xf2bc('0x63')](_0xf2bc('0x64'),_0x52d02d),'headers':{'Content-Type':_0xf2bc('0x65'),'Accept':'application/json'},'json':!![]};if(_0x206a1d[_0xf2bc('0x59')]===_0xf2bc('0x66')){_0x1c864e['headers'][_0xf2bc('0x67')]=util['format'](_0xf2bc('0x68'),new Buffer(util[_0xf2bc('0x63')](_0xf2bc('0x69'),_0x206a1d[_0xf2bc('0x5a')],_0x206a1d[_0xf2bc('0x5b')]))['toString'](_0xf2bc('0x6a')));}return rp(_0x1c864e)[_0xf2bc('0x44')](function(_0x5e200f){return _0x100597[_0xf2bc('0x6b')](_[_0xf2bc('0x32')](_0x5e200f[_0xf2bc('0x6c')][_0xf2bc('0x6d')],function(_0x5ec0e5){return{'id':_0x5ec0e5[_0xf2bc('0x29')],'name':_0x5ec0e5[_0xf2bc('0x29')],'type':_0x5ec0e5['type'],'active':_0x5ec0e5[_0xf2bc('0x6e')],'label':_0x5ec0e5[_0xf2bc('0x6f')],'custom':!![]};}));});}})[_0xf2bc('0x44')](function(_0x2bd746){return{'count':_0x2bd746[_0xf2bc('0x39')],'rows':_0x2bd746};})[_0xf2bc('0x44')](respondWithResult(_0x19179c,null))[_0xf2bc('0x46')](function(_0x295a75){var _0x3d1e71=_0x1bca07[_0xf2bc('0x33')][_0xf2bc('0x70')]?0x1f4:_0x295a75[_0xf2bc('0x71')]||0x1f4;logger[_0xf2bc('0x28')](_0xf2bc('0x72'),_0xf2bc('0x56'),_0x3d1e71,JSON['stringify'](_0x295a75));delete _0x295a75[_0xf2bc('0x29')];if(_0x3d1e71===0x191){_0x3d1e71=0x190;}_0x19179c[_0xf2bc('0x1c')](_0x3d1e71)['send'](_0x1bca07['query'][_0xf2bc('0x70')]?{'message':'Wrong\x20credentials','statusCode':_0x295a75[_0xf2bc('0x71')]}:_0x295a75);});};