4bf8d8992581fbfd836d66b6e977793ea91aad43
[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 _0x4c8f=['key','model','keys','query','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','type','options','include','findAll','catch','show','params','length','includeAll','create','body','update','find','DeskConfiguration','map','getConfigurations','addConfiguration','findOne','AccountId','getSubjects','channel','Descriptions','getDescriptions','DeskField','priority','remoteUri','authType','username','password','consumerKey','consumerSecret','token','tokenSecret','slice','GET','format','%s/custom_fields','application/json','basic','headers','Authorization','Basic\x20%s','%s:%s','toString','base64','concat','entries','active','label','test','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','Wrong\x20credentials','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','then','destroy','error','stack','name','send','DeskAccount','rawAttributes','fieldName'];(function(_0x2b6c5b,_0x5da881){var _0x139100=function(_0x334c06){while(--_0x334c06){_0x2b6c5b['push'](_0x2b6c5b['shift']());}};_0x139100(++_0x5da881);}(_0x4c8f,0x1ae));var _0xf4c8=function(_0xc244c3,_0x3cd9c9){_0xc244c3=_0xc244c3-0x0;var _0x34ba72=_0x4c8f[_0xc244c3];return _0x34ba72;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0xf4c8('0x0'));var jsonpatch=require(_0xf4c8('0x1'));var rp=require(_0xf4c8('0x2'));var moment=require(_0xf4c8('0x3'));var BPromise=require(_0xf4c8('0x4'));var Mustache=require(_0xf4c8('0x5'));var util=require(_0xf4c8('0x6'));var path=require(_0xf4c8('0x7'));var sox=require(_0xf4c8('0x8'));var csv=require(_0xf4c8('0x9'));var ejs=require(_0xf4c8('0xa'));var fs=require('fs');var fs_extra=require(_0xf4c8('0xb'));var _=require(_0xf4c8('0xc'));var squel=require(_0xf4c8('0xd'));var crypto=require(_0xf4c8('0xe'));var jsforce=require(_0xf4c8('0xf'));var deskjs=require(_0xf4c8('0x10'));var toCsv=require(_0xf4c8('0x9'));var querystring=require('querystring');var Papa=require(_0xf4c8('0x11'));var Redis=require('ioredis');var authService=require(_0xf4c8('0x12'));var qs=require(_0xf4c8('0x13'));var as=require(_0xf4c8('0x14'));var hardwareService=require(_0xf4c8('0x15'));var logger=require(_0xf4c8('0x16'))(_0xf4c8('0x17'));var utils=require('../../config/utils');var config=require(_0xf4c8('0x18'));var licenseUtil=require(_0xf4c8('0x19'));var db=require(_0xf4c8('0x1a'))['db'];var integrations=require(_0xf4c8('0x1b'));function respondWithStatusCode(_0x22e52c,_0x4fd107){_0x4fd107=_0x4fd107||0xcc;return function(_0x22b3b7){if(_0x22b3b7){return _0x22e52c[_0xf4c8('0x1c')](_0x4fd107);}return _0x22e52c[_0xf4c8('0x1d')](_0x4fd107)[_0xf4c8('0x1e')]();};}function respondWithResult(_0x5e3bfc,_0x43606f){_0x43606f=_0x43606f||0xc8;return function(_0x49556d){if(_0x49556d){return _0x5e3bfc['status'](_0x43606f)[_0xf4c8('0x1f')](_0x49556d);}};}function respondWithFilteredResult(_0x3c131a,_0x4d1b8d){return function(_0x23f632){if(_0x23f632){var _0x2967b0=typeof _0x4d1b8d[_0xf4c8('0x20')]===_0xf4c8('0x21')&&typeof _0x4d1b8d[_0xf4c8('0x22')]===_0xf4c8('0x21');var _0x199768=_0x23f632['count'];var _0x347fd4=_0x2967b0?0x0:_0x4d1b8d['offset'];var _0x479b61=_0x2967b0?_0x23f632[_0xf4c8('0x23')]:_0x4d1b8d[_0xf4c8('0x20')]+_0x4d1b8d['limit'];var _0x265ff3;if(_0x479b61>=_0x199768){_0x479b61=_0x199768;_0x265ff3=0xc8;}else{_0x265ff3=0xce;}_0x3c131a[_0xf4c8('0x1d')](_0x265ff3);return _0x3c131a[_0xf4c8('0x24')](_0xf4c8('0x25'),_0x347fd4+'-'+_0x479b61+'/'+_0x199768)[_0xf4c8('0x1f')](_0x23f632);}return null;};}function patchUpdates(_0x539cb1){return function(_0x9860c0){try{jsonpatch[_0xf4c8('0x26')](_0x9860c0,_0x539cb1,!![]);}catch(_0x1a0e9b){return BPromise[_0xf4c8('0x27')](_0x1a0e9b);}return _0x9860c0[_0xf4c8('0x28')]();};}function saveUpdates(_0x4f1475,_0x582434){return function(_0x127387){if(_0x127387){return _0x127387['update'](_0x4f1475)[_0xf4c8('0x29')](function(_0x2e8b50){return _0x2e8b50;});}return null;};}function removeEntity(_0x4011f5,_0x595a93){return function(_0xfc907d){if(_0xfc907d){return _0xfc907d[_0xf4c8('0x2a')]()[_0xf4c8('0x29')](function(){_0x4011f5[_0xf4c8('0x1d')](0xcc)['end']();});}};}function handleEntityNotFound(_0x2dd8c5,_0x2797ba){return function(_0x4a2916){if(!_0x4a2916){_0x2dd8c5['sendStatus'](0x194);}return _0x4a2916;};}function handleError(_0xcdd125,_0x3cb4b8){_0x3cb4b8=_0x3cb4b8||0x1f4;return function(_0x2218ac){logger[_0xf4c8('0x2b')](_0x2218ac[_0xf4c8('0x2c')]);if(_0x2218ac[_0xf4c8('0x2d')]){delete _0x2218ac[_0xf4c8('0x2d')];}_0xcdd125[_0xf4c8('0x1d')](_0x3cb4b8)[_0xf4c8('0x2e')](_0x2218ac);};}exports['index']=function(_0x29e975,_0x1919ba){var _0x4a67da={},_0x5151bc={},_0x5aa7fc={'count':0x0,'rows':[]};var _0x368394=_['map'](db[_0xf4c8('0x2f')][_0xf4c8('0x30')],function(_0x16a49f){return{'name':_0x16a49f[_0xf4c8('0x31')],'type':_0x16a49f['type'][_0xf4c8('0x32')]};});_0x5151bc[_0xf4c8('0x33')]=_['map'](_0x368394,_0xf4c8('0x2d'));_0x5151bc['query']=_[_0xf4c8('0x34')](_0x29e975[_0xf4c8('0x35')]);_0x5151bc[_0xf4c8('0x36')]=_[_0xf4c8('0x37')](_0x5151bc[_0xf4c8('0x33')],_0x5151bc[_0xf4c8('0x35')]);_0x4a67da[_0xf4c8('0x38')]=_[_0xf4c8('0x37')](_0x5151bc['model'],qs[_0xf4c8('0x39')](_0x29e975['query']['fields']));_0x4a67da['attributes']=_0x4a67da[_0xf4c8('0x38')]['length']?_0x4a67da[_0xf4c8('0x38')]:_0x5151bc['model'];if(!_0x29e975['query'][_0xf4c8('0x3a')](_0xf4c8('0x3b'))){_0x4a67da[_0xf4c8('0x22')]=qs[_0xf4c8('0x22')](_0x29e975['query']['limit']);_0x4a67da['offset']=qs[_0xf4c8('0x20')](_0x29e975[_0xf4c8('0x35')][_0xf4c8('0x20')]);}_0x4a67da[_0xf4c8('0x3c')]=qs[_0xf4c8('0x3d')](_0x29e975[_0xf4c8('0x35')][_0xf4c8('0x3d')]);_0x4a67da[_0xf4c8('0x3e')]=qs['filters'](_[_0xf4c8('0x3f')](_0x29e975[_0xf4c8('0x35')],_0x5151bc[_0xf4c8('0x36')]),_0x368394);if(_0x29e975[_0xf4c8('0x35')][_0xf4c8('0x40')]){_0x4a67da[_0xf4c8('0x3e')]=_[_0xf4c8('0x41')](_0x4a67da['where'],{'$or':_['map'](_0x368394,function(_0x3d62fb){if(_0x3d62fb[_0xf4c8('0x42')]!=='VIRTUAL'){var _0xed040b={};_0xed040b[_0x3d62fb[_0xf4c8('0x2d')]]={'$like':'%'+_0x29e975[_0xf4c8('0x35')][_0xf4c8('0x40')]+'%'};return _0xed040b;}})});}_0x4a67da=_[_0xf4c8('0x41')]({},_0x4a67da,_0x29e975[_0xf4c8('0x43')]);var _0x984862={'where':_0x4a67da[_0xf4c8('0x3e')]};return db['DeskAccount'][_0xf4c8('0x23')](_0x984862)['then'](function(_0x54dd0c){_0x5aa7fc[_0xf4c8('0x23')]=_0x54dd0c;if(_0x29e975[_0xf4c8('0x35')]['includeAll']){_0x4a67da[_0xf4c8('0x44')]=[{'all':!![]}];}return db[_0xf4c8('0x2f')][_0xf4c8('0x45')](_0x4a67da);})[_0xf4c8('0x29')](function(_0xc2926b){_0x5aa7fc['rows']=_0xc2926b;return _0x5aa7fc;})[_0xf4c8('0x29')](respondWithFilteredResult(_0x1919ba,_0x4a67da))[_0xf4c8('0x46')](handleError(_0x1919ba,null));};exports[_0xf4c8('0x47')]=function(_0x3830b7,_0x3e7493){var _0x1d526c={'raw':![],'where':{'id':_0x3830b7[_0xf4c8('0x48')]['id']}},_0x1913e4={};_0x1913e4['model']=_['keys'](db[_0xf4c8('0x2f')][_0xf4c8('0x30')]);_0x1913e4['query']=_[_0xf4c8('0x34')](_0x3830b7[_0xf4c8('0x35')]);_0x1913e4[_0xf4c8('0x36')]=_[_0xf4c8('0x37')](_0x1913e4['model'],_0x1913e4['query']);_0x1d526c[_0xf4c8('0x38')]=_[_0xf4c8('0x37')](_0x1913e4[_0xf4c8('0x33')],qs[_0xf4c8('0x39')](_0x3830b7[_0xf4c8('0x35')][_0xf4c8('0x39')]));_0x1d526c[_0xf4c8('0x38')]=_0x1d526c[_0xf4c8('0x38')][_0xf4c8('0x49')]?_0x1d526c[_0xf4c8('0x38')]:_0x1913e4[_0xf4c8('0x33')];if(_0x3830b7['query'][_0xf4c8('0x4a')]){_0x1d526c[_0xf4c8('0x44')]=[{'all':!![]}];}_0x1d526c=_[_0xf4c8('0x41')]({},_0x1d526c,_0x3830b7[_0xf4c8('0x43')]);return db['DeskAccount']['find'](_0x1d526c)[_0xf4c8('0x29')](handleEntityNotFound(_0x3e7493,null))[_0xf4c8('0x29')](respondWithResult(_0x3e7493,null))[_0xf4c8('0x46')](handleError(_0x3e7493,null));};exports[_0xf4c8('0x4b')]=function(_0x365e5f,_0x58a963){return db[_0xf4c8('0x2f')]['create'](_0x365e5f[_0xf4c8('0x4c')],{})[_0xf4c8('0x29')](respondWithResult(_0x58a963,0xc9))[_0xf4c8('0x46')](handleError(_0x58a963,null));};exports[_0xf4c8('0x4d')]=function(_0x401ec6,_0x500edd){if(_0x401ec6[_0xf4c8('0x4c')]['id']){delete _0x401ec6[_0xf4c8('0x4c')]['id'];}return db[_0xf4c8('0x2f')][_0xf4c8('0x4e')]({'where':{'id':_0x401ec6[_0xf4c8('0x48')]['id']}})[_0xf4c8('0x29')](handleEntityNotFound(_0x500edd,null))[_0xf4c8('0x29')](saveUpdates(_0x401ec6[_0xf4c8('0x4c')],null))[_0xf4c8('0x29')](respondWithResult(_0x500edd,null))[_0xf4c8('0x46')](handleError(_0x500edd,null));};exports[_0xf4c8('0x2a')]=function(_0x3a8846,_0x41fb89){return db[_0xf4c8('0x2f')][_0xf4c8('0x4e')]({'where':{'id':_0x3a8846['params']['id']}})['then'](handleEntityNotFound(_0x41fb89,null))[_0xf4c8('0x29')](removeEntity(_0x41fb89,null))[_0xf4c8('0x46')](handleError(_0x41fb89,null));};exports['getConfigurations']=function(_0x10e214,_0x8f190,_0x221435){var _0x23636f={};var _0x2dd6ef={};var _0x53e1ea;var _0x5c58fb;return db[_0xf4c8('0x2f')]['findOne']({'where':{'id':_0x10e214['params']['id']}})['then'](handleEntityNotFound(_0x8f190,null))[_0xf4c8('0x29')](function(_0x109eee){if(_0x109eee){_0x53e1ea=_0x109eee;_0x2dd6ef[_0xf4c8('0x33')]=_[_0xf4c8('0x34')](db[_0xf4c8('0x4f')][_0xf4c8('0x30')]);_0x2dd6ef['query']=_[_0xf4c8('0x34')](_0x10e214[_0xf4c8('0x35')]);_0x2dd6ef['filters']=_[_0xf4c8('0x37')](_0x2dd6ef['model'],_0x2dd6ef[_0xf4c8('0x35')]);_0x23636f['attributes']=_[_0xf4c8('0x37')](_0x2dd6ef[_0xf4c8('0x33')],qs[_0xf4c8('0x39')](_0x10e214[_0xf4c8('0x35')][_0xf4c8('0x39')]));_0x23636f['attributes']=_0x23636f['attributes'][_0xf4c8('0x49')]?_0x23636f['attributes']:_0x2dd6ef[_0xf4c8('0x33')];_0x23636f['order']=qs[_0xf4c8('0x3d')](_0x10e214['query'][_0xf4c8('0x3d')]);_0x23636f[_0xf4c8('0x3e')]=qs['filters'](_[_0xf4c8('0x3f')](_0x10e214[_0xf4c8('0x35')],_0x2dd6ef[_0xf4c8('0x36')]));if(_0x10e214[_0xf4c8('0x35')]['filter']){_0x23636f[_0xf4c8('0x3e')]=_['merge'](_0x23636f[_0xf4c8('0x3e')],{'$or':_[_0xf4c8('0x50')](_0x23636f[_0xf4c8('0x38')],function(_0x42e9a9){var _0x47bdf4={};_0x47bdf4[_0x42e9a9]={'$like':'%'+_0x10e214['query']['filter']+'%'};return _0x47bdf4;})});}_0x23636f=_[_0xf4c8('0x41')]({},_0x23636f,_0x10e214[_0xf4c8('0x43')]);return _0x53e1ea[_0xf4c8('0x51')](_0x23636f);}})[_0xf4c8('0x29')](function(_0x1592b9){if(_0x1592b9){_0x5c58fb=_0x1592b9[_0xf4c8('0x49')];if(!_0x10e214[_0xf4c8('0x35')]['hasOwnProperty'](_0xf4c8('0x3b'))){_0x23636f[_0xf4c8('0x22')]=qs[_0xf4c8('0x22')](_0x10e214[_0xf4c8('0x35')][_0xf4c8('0x22')]);_0x23636f[_0xf4c8('0x20')]=qs[_0xf4c8('0x20')](_0x10e214['query']['offset']);}return _0x53e1ea[_0xf4c8('0x51')](_0x23636f);}})[_0xf4c8('0x29')](function(_0x27ac70){if(_0x27ac70){return _0x27ac70?{'count':_0x5c58fb,'rows':_0x27ac70}:null;}})['then'](respondWithResult(_0x8f190,null))[_0xf4c8('0x46')](handleError(_0x8f190,null));};exports[_0xf4c8('0x52')]=function(_0x2b5354,_0xe1a85b,_0x479bdc){if(_0x2b5354[_0xf4c8('0x4c')]['id']){delete _0x2b5354[_0xf4c8('0x4c')]['id'];}return db[_0xf4c8('0x2f')][_0xf4c8('0x53')]({'where':{'id':_0x2b5354[_0xf4c8('0x48')]['id']}})[_0xf4c8('0x29')](handleEntityNotFound(_0xe1a85b,null))[_0xf4c8('0x29')](function(_0x2fa39a){if(_0x2fa39a){_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x54')]=_0x2fa39a['id'];_0x2b5354[_0xf4c8('0x4c')]['Subjects']=integrations[_0xf4c8('0x55')](_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x56')],_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x42')]);_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x57')]=integrations[_0xf4c8('0x58')](_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x56')],_0x2b5354[_0xf4c8('0x4c')][_0xf4c8('0x42')]);return db['DeskConfiguration'][_0xf4c8('0x4b')](_0x2b5354[_0xf4c8('0x4c')],{'include':[{'model':db['DeskField'],'as':'Subjects'},{'model':db[_0xf4c8('0x59')],'as':_0xf4c8('0x57')}]});}return null;})[_0xf4c8('0x29')](respondWithResult(_0xe1a85b,null))[_0xf4c8('0x46')](handleError(_0xe1a85b,null));};exports['getFields']=function(_0x46ca1a,_0x15ac70,_0x24d06c){var _0x535f8a=[{'name':_0xf4c8('0x5a'),'label':_0xf4c8('0x5a'),'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 _0x13e482='';return db[_0xf4c8('0x2f')][_0xf4c8('0x53')]({'where':{'id':_0x46ca1a[_0xf4c8('0x48')]['id']},'attributes':['id',_0xf4c8('0x5b'),_0xf4c8('0x5c'),_0xf4c8('0x5d'),_0xf4c8('0x5e'),_0xf4c8('0x5f'),_0xf4c8('0x60'),_0xf4c8('0x61'),_0xf4c8('0x62')]})[_0xf4c8('0x29')](handleEntityNotFound(_0x15ac70,null))[_0xf4c8('0x29')](function(_0x3f6564){if(_0x3f6564){_0x13e482=_0x3f6564[_0xf4c8('0x5b')];var _0x19f7d3=_0x13e482[_0xf4c8('0x63')](-0x1);if(_0x19f7d3==='/'){_0x13e482=_0x13e482['substring'](0x0,_0x13e482['lastIndexOf'](_0x19f7d3));}var _0x3dd299={'method':_0xf4c8('0x64'),'uri':util[_0xf4c8('0x65')](_0xf4c8('0x66'),_0x13e482),'headers':{'Content-Type':'application/json','Accept':_0xf4c8('0x67')},'json':!![]};if(_0x3f6564[_0xf4c8('0x5c')]===_0xf4c8('0x68')){_0x3dd299[_0xf4c8('0x69')][_0xf4c8('0x6a')]=util[_0xf4c8('0x65')](_0xf4c8('0x6b'),new Buffer(util['format'](_0xf4c8('0x6c'),_0x3f6564[_0xf4c8('0x5d')],_0x3f6564[_0xf4c8('0x5e')]))[_0xf4c8('0x6d')](_0xf4c8('0x6e')));}return rp(_0x3dd299)[_0xf4c8('0x29')](function(_0x559a41){return _0x535f8a[_0xf4c8('0x6f')](_[_0xf4c8('0x50')](_0x559a41['_embedded'][_0xf4c8('0x70')],function(_0x4bd582){return{'id':_0x4bd582[_0xf4c8('0x2d')],'name':_0x4bd582[_0xf4c8('0x2d')],'type':_0x4bd582[_0xf4c8('0x42')],'active':_0x4bd582[_0xf4c8('0x71')],'label':_0x4bd582[_0xf4c8('0x72')],'custom':!![]};}));});}})[_0xf4c8('0x29')](function(_0x93387f){return{'count':_0x93387f[_0xf4c8('0x49')],'rows':_0x93387f};})['then'](respondWithResult(_0x15ac70,null))[_0xf4c8('0x46')](function(_0x3db23e){var _0x59a131=_0x46ca1a['query'][_0xf4c8('0x73')]?0x1f4:_0x3db23e['statusCode']||0x1f4;logger['error'](_0xf4c8('0x74'),_0xf4c8('0x75'),_0x59a131,JSON[_0xf4c8('0x76')](_0x3db23e));delete _0x3db23e['name'];if(_0x59a131===0x191){_0x59a131=0x190;}_0x15ac70[_0xf4c8('0x1d')](_0x59a131)[_0xf4c8('0x2e')](_0x46ca1a['query'][_0xf4c8('0x73')]?{'message':_0xf4c8('0x77'),'statusCode':_0x3db23e['statusCode']}:_0x3db23e);});};