Built motion from commit (unavailable).|2.5.4
[motion2.git] / server / api / interval / interval.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 _0x6ccc=['params','length','includeAll','options','find','create','body','user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','addInterval','IntervalId','getIntervals','findOne','nolimit','addIntervals','sequelize','transaction','bulkCreate','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','path','sox','to-csv','ejs','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','count','offset','Content-Range','apply','reject','save','then','destroy','get','Intervals','UserProfileResource','error','stack','name','send','index','map','Interval','rawAttributes','model','query','keys','filters','intersection','attributes','fields','limit','order','sort','where','pick','merge','type','filter','findAll','rows','catch','show'];(function(_0x29e722,_0x33e8a7){var _0x5231ca=function(_0xdeada8){while(--_0xdeada8){_0x29e722['push'](_0x29e722['shift']());}};_0x5231ca(++_0x33e8a7);}(_0x6ccc,0x6f));var _0xc6cc=function(_0x259aaf,_0x5dbec1){_0x259aaf=_0x259aaf-0x0;var _0x366663=_0x6ccc[_0x259aaf];return _0x366663;};'use strict';var pdf=require(_0xc6cc('0x0'));var emlformat=require(_0xc6cc('0x1'));var rimraf=require(_0xc6cc('0x2'));var zipdir=require(_0xc6cc('0x3'));var jsonpatch=require(_0xc6cc('0x4'));var rp=require('request-promise');var moment=require(_0xc6cc('0x5'));var BPromise=require(_0xc6cc('0x6'));var Mustache=require('mustache');var util=require('util');var path=require(_0xc6cc('0x7'));var sox=require(_0xc6cc('0x8'));var csv=require(_0xc6cc('0x9'));var ejs=require(_0xc6cc('0xa'));var fs=require('fs');var _=require('lodash');var squel=require('squel');var crypto=require(_0xc6cc('0xb'));var jsforce=require(_0xc6cc('0xc'));var deskjs=require('desk.js');var toCsv=require(_0xc6cc('0x9'));var querystring=require(_0xc6cc('0xd'));var Papa=require(_0xc6cc('0xe'));var Redis=require(_0xc6cc('0xf'));var authService=require(_0xc6cc('0x10'));var qs=require(_0xc6cc('0x11'));var as=require(_0xc6cc('0x12'));var hardwareService=require(_0xc6cc('0x13'));var logger=require(_0xc6cc('0x14'))(_0xc6cc('0x15'));var utils=require(_0xc6cc('0x16'));var config=require(_0xc6cc('0x17'));var db=require(_0xc6cc('0x18'))['db'];var extensions=require('../../components/extensions/rewrite');function respondWithStatusCode(_0x1ffb47,_0x11c4ac){_0x11c4ac=_0x11c4ac||0xcc;return function(_0x1186ae){if(_0x1186ae){return _0x1ffb47[_0xc6cc('0x19')](_0x11c4ac);}return _0x1ffb47[_0xc6cc('0x1a')](_0x11c4ac)[_0xc6cc('0x1b')]();};}function respondWithResult(_0x927a1b,_0x4b3034){_0x4b3034=_0x4b3034||0xc8;return function(_0x29e286){if(_0x29e286){return _0x927a1b['status'](_0x4b3034)[_0xc6cc('0x1c')](_0x29e286);}};}function respondWithFilteredResult(_0x34e5f0,_0x5adb8c){return function(_0x292465){if(_0x292465){var _0x363b5f=_0x292465[_0xc6cc('0x1d')],_0x204f21=_0x5adb8c[_0xc6cc('0x1e')],_0x292b87=_0x5adb8c[_0xc6cc('0x1e')]+_0x5adb8c['limit'],_0x2dfc58;if(_0x292b87>=_0x363b5f){_0x292b87=_0x363b5f;_0x2dfc58=0xc8;}else{_0x2dfc58=0xce;}_0x34e5f0[_0xc6cc('0x1a')](_0x2dfc58);return _0x34e5f0['set'](_0xc6cc('0x1f'),_0x204f21+'-'+_0x292b87+'/'+_0x363b5f)[_0xc6cc('0x1c')](_0x292465);}return null;};}function patchUpdates(_0xd802ef){return function(_0x83408b){try{jsonpatch[_0xc6cc('0x20')](_0x83408b,_0xd802ef,!![]);}catch(_0x828690){return BPromise[_0xc6cc('0x21')](_0x828690);}return _0x83408b[_0xc6cc('0x22')]();};}function saveUpdates(_0x2efd8f,_0x59af47){return function(_0x10692e){if(_0x10692e){return _0x10692e['update'](_0x2efd8f)[_0xc6cc('0x23')](function(_0x2fe50e){return _0x2fe50e;});}return null;};}function removeEntity(_0xe3d269,_0x4074f2){return function(_0x2eff8f){if(_0x2eff8f){return _0x2eff8f[_0xc6cc('0x24')]()[_0xc6cc('0x23')](function(){var _0x4f389c=_0x2eff8f[_0xc6cc('0x25')]({'plain':!![]});var _0x145da5=_0xc6cc('0x26');return db[_0xc6cc('0x27')][_0xc6cc('0x24')]({'where':{'type':_0x145da5,'resourceId':_0x4f389c['id']}})['then'](function(){return _0x2eff8f;});})[_0xc6cc('0x23')](function(){_0xe3d269[_0xc6cc('0x1a')](0xcc)[_0xc6cc('0x1b')]();});}};}function handleEntityNotFound(_0x524fa6,_0x205ba1){return function(_0x351578){if(!_0x351578){_0x524fa6[_0xc6cc('0x19')](0x194);}return _0x351578;};}function handleError(_0x50b2fb,_0x471a4a){_0x471a4a=_0x471a4a||0x1f4;return function(_0x368efc){logger[_0xc6cc('0x28')](_0x368efc[_0xc6cc('0x29')]);if(_0x368efc[_0xc6cc('0x2a')]){delete _0x368efc[_0xc6cc('0x2a')];}_0x50b2fb[_0xc6cc('0x1a')](_0x471a4a)[_0xc6cc('0x2b')](_0x368efc);};}exports[_0xc6cc('0x2c')]=function(_0x51e47b,_0x292bee){var _0x2c9200={},_0x5a1608={},_0x4d9b9d={'count':0x0,'rows':[]};var _0x316619=_[_0xc6cc('0x2d')](db[_0xc6cc('0x2e')][_0xc6cc('0x2f')],function(_0x33385f){return{'name':_0x33385f['fieldName'],'type':_0x33385f['type']['key']};});_0x5a1608[_0xc6cc('0x30')]=_['map'](_0x316619,'name');_0x5a1608[_0xc6cc('0x31')]=_[_0xc6cc('0x32')](_0x51e47b[_0xc6cc('0x31')]);_0x5a1608[_0xc6cc('0x33')]=_[_0xc6cc('0x34')](_0x5a1608['model'],_0x5a1608['query']);_0x2c9200[_0xc6cc('0x35')]=_[_0xc6cc('0x34')](_0x5a1608[_0xc6cc('0x30')],qs[_0xc6cc('0x36')](_0x51e47b[_0xc6cc('0x31')][_0xc6cc('0x36')]));_0x2c9200[_0xc6cc('0x35')]=_0x2c9200[_0xc6cc('0x35')]['length']?_0x2c9200[_0xc6cc('0x35')]:_0x5a1608['model'];if(!_0x51e47b[_0xc6cc('0x31')]['hasOwnProperty']('nolimit')){_0x2c9200[_0xc6cc('0x37')]=qs[_0xc6cc('0x37')](_0x51e47b[_0xc6cc('0x31')][_0xc6cc('0x37')]);_0x2c9200[_0xc6cc('0x1e')]=qs[_0xc6cc('0x1e')](_0x51e47b[_0xc6cc('0x31')][_0xc6cc('0x1e')]);}_0x2c9200[_0xc6cc('0x38')]=qs[_0xc6cc('0x39')](_0x51e47b[_0xc6cc('0x31')][_0xc6cc('0x39')]);_0x2c9200[_0xc6cc('0x3a')]=qs[_0xc6cc('0x33')](_[_0xc6cc('0x3b')](_0x51e47b[_0xc6cc('0x31')],_0x5a1608[_0xc6cc('0x33')]),_0x316619);if(_0x51e47b[_0xc6cc('0x31')]['filter']){_0x2c9200[_0xc6cc('0x3a')]=_[_0xc6cc('0x3c')](_0x2c9200[_0xc6cc('0x3a')],{'$or':_[_0xc6cc('0x2d')](_0x316619,function(_0x3b75f6){if(_0x3b75f6[_0xc6cc('0x3d')]!=='VIRTUAL'){var _0x306a87={};_0x306a87[_0x3b75f6[_0xc6cc('0x2a')]]={'$like':'%'+_0x51e47b['query'][_0xc6cc('0x3e')]+'%'};return _0x306a87;}})});}_0x2c9200=_[_0xc6cc('0x3c')]({},_0x2c9200,_0x51e47b['options']);var _0x60e0d7={'where':_0x2c9200['where']};return db[_0xc6cc('0x2e')]['count'](_0x60e0d7)[_0xc6cc('0x23')](function(_0x284758){_0x4d9b9d['count']=_0x284758;if(_0x51e47b['query']['includeAll']){_0x2c9200['include']=[{'all':!![]}];}return db[_0xc6cc('0x2e')][_0xc6cc('0x3f')](_0x2c9200);})[_0xc6cc('0x23')](function(_0x421203){_0x4d9b9d[_0xc6cc('0x40')]=_0x421203;return _0x4d9b9d;})[_0xc6cc('0x23')](respondWithFilteredResult(_0x292bee,_0x2c9200))[_0xc6cc('0x41')](handleError(_0x292bee,null));};exports[_0xc6cc('0x42')]=function(_0x1a9136,_0x1f8b2c){var _0x4d3608={'raw':!![],'where':{'id':_0x1a9136[_0xc6cc('0x43')]['id']}},_0x4b15eb={};_0x4b15eb[_0xc6cc('0x30')]=_['keys'](db[_0xc6cc('0x2e')][_0xc6cc('0x2f')]);_0x4b15eb['query']=_[_0xc6cc('0x32')](_0x1a9136[_0xc6cc('0x31')]);_0x4b15eb[_0xc6cc('0x33')]=_[_0xc6cc('0x34')](_0x4b15eb[_0xc6cc('0x30')],_0x4b15eb[_0xc6cc('0x31')]);_0x4d3608[_0xc6cc('0x35')]=_[_0xc6cc('0x34')](_0x4b15eb[_0xc6cc('0x30')],qs['fields'](_0x1a9136[_0xc6cc('0x31')][_0xc6cc('0x36')]));_0x4d3608[_0xc6cc('0x35')]=_0x4d3608[_0xc6cc('0x35')][_0xc6cc('0x44')]?_0x4d3608['attributes']:_0x4b15eb[_0xc6cc('0x30')];if(_0x1a9136[_0xc6cc('0x31')][_0xc6cc('0x45')]){_0x4d3608['include']=[{'all':!![]}];}_0x4d3608=_[_0xc6cc('0x3c')]({},_0x4d3608,_0x1a9136[_0xc6cc('0x46')]);return db['Interval'][_0xc6cc('0x47')](_0x4d3608)[_0xc6cc('0x23')](handleEntityNotFound(_0x1f8b2c,null))[_0xc6cc('0x23')](respondWithResult(_0x1f8b2c,null))[_0xc6cc('0x41')](handleError(_0x1f8b2c,null));};exports[_0xc6cc('0x48')]=function(_0x51f235,_0x1e3574){return db[_0xc6cc('0x2e')][_0xc6cc('0x48')](_0x51f235[_0xc6cc('0x49')],{})[_0xc6cc('0x23')](function(_0x239c35){var _0x3cacf5=_0x51f235[_0xc6cc('0x4a')][_0xc6cc('0x25')]({'plain':!![]});if(!_0x3cacf5)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3cacf5['role']==='user'){var _0x3a7e90=_0x239c35[_0xc6cc('0x25')]({'plain':!![]});var _0x453a97=_0xc6cc('0x26');return db[_0xc6cc('0x4b')]['find']({'where':{'name':_0x453a97,'userProfileId':_0x3cacf5[_0xc6cc('0x4c')]},'raw':!![]})[_0xc6cc('0x23')](function(_0x1b12ff){if(_0x1b12ff&&_0x1b12ff[_0xc6cc('0x4d')]===0x0){return db[_0xc6cc('0x27')][_0xc6cc('0x48')]({'name':_0x3a7e90[_0xc6cc('0x2a')],'resourceId':_0x3a7e90['id'],'type':_0x1b12ff[_0xc6cc('0x2a')],'sectionId':_0x1b12ff['id']},{})[_0xc6cc('0x23')](function(){return _0x239c35;});}else{return _0x239c35;}})[_0xc6cc('0x41')](function(_0x3d1a61){logger['error'](_0xc6cc('0x4e'),_0x3d1a61);throw _0x3d1a61;});}return _0x239c35;})[_0xc6cc('0x23')](respondWithResult(_0x1e3574,0xc9))[_0xc6cc('0x41')](handleError(_0x1e3574,null));};exports[_0xc6cc('0x4f')]=function(_0x429adb,_0x31e265){if(_0x429adb[_0xc6cc('0x49')]['id']){delete _0x429adb['body']['id'];}return db[_0xc6cc('0x2e')][_0xc6cc('0x47')]({'where':{'id':_0x429adb['params']['id']}})[_0xc6cc('0x23')](handleEntityNotFound(_0x31e265,null))[_0xc6cc('0x23')](saveUpdates(_0x429adb[_0xc6cc('0x49')],null))[_0xc6cc('0x23')](respondWithResult(_0x31e265,null))[_0xc6cc('0x41')](handleError(_0x31e265,null));};exports[_0xc6cc('0x24')]=function(_0x133bf7,_0x12b7a5){return db[_0xc6cc('0x2e')]['find']({'where':{'id':_0x133bf7[_0xc6cc('0x43')]['id']}})['then'](handleEntityNotFound(_0x12b7a5,null))[_0xc6cc('0x23')](removeEntity(_0x12b7a5,null))['catch'](handleError(_0x12b7a5,null));};exports[_0xc6cc('0x50')]=function(_0x5de6b8,_0x59a698,_0x1e5e75){if(_0x5de6b8[_0xc6cc('0x49')]['id']){delete _0x5de6b8[_0xc6cc('0x49')]['id'];}return db['Interval'][_0xc6cc('0x47')]({'where':{'id':_0x5de6b8['params']['id']}})['then'](handleEntityNotFound(_0x59a698,null))['then'](function(_0x194f27){if(_0x194f27){_0x5de6b8[_0xc6cc('0x49')][_0xc6cc('0x51')]=_0x194f27['id'];return db[_0xc6cc('0x2e')][_0xc6cc('0x48')](_0x5de6b8[_0xc6cc('0x49')]);}})[_0xc6cc('0x23')](respondWithResult(_0x59a698,null))[_0xc6cc('0x41')](handleError(_0x59a698,null));};exports[_0xc6cc('0x52')]=function(_0x50bb86,_0x1f916a,_0x5ad65b){var _0x4ceb0f={};var _0x44e57d={};var _0x445f41;var _0x43e03a;return db['Interval'][_0xc6cc('0x53')]({'where':{'id':_0x50bb86['params']['id']}})[_0xc6cc('0x23')](handleEntityNotFound(_0x1f916a,null))['then'](function(_0x3e4504){if(_0x3e4504){_0x445f41=_0x3e4504;_0x44e57d['model']=_[_0xc6cc('0x32')](db[_0xc6cc('0x2e')]['rawAttributes']);_0x44e57d['query']=_[_0xc6cc('0x32')](_0x50bb86[_0xc6cc('0x31')]);_0x44e57d['filters']=_['intersection'](_0x44e57d[_0xc6cc('0x30')],_0x44e57d['query']);_0x4ceb0f['attributes']=_[_0xc6cc('0x34')](_0x44e57d[_0xc6cc('0x30')],qs[_0xc6cc('0x36')](_0x50bb86['query'][_0xc6cc('0x36')]));_0x4ceb0f[_0xc6cc('0x35')]=_0x4ceb0f['attributes'][_0xc6cc('0x44')]?_0x4ceb0f[_0xc6cc('0x35')]:_0x44e57d[_0xc6cc('0x30')];_0x4ceb0f['order']=qs['sort'](_0x50bb86['query'][_0xc6cc('0x39')]);_0x4ceb0f[_0xc6cc('0x3a')]=qs['filters'](_['pick'](_0x50bb86[_0xc6cc('0x31')],_0x44e57d['filters']));if(_0x50bb86[_0xc6cc('0x31')][_0xc6cc('0x3e')]){_0x4ceb0f[_0xc6cc('0x3a')]=_[_0xc6cc('0x3c')](_0x4ceb0f[_0xc6cc('0x3a')],{'$or':_[_0xc6cc('0x2d')](_0x4ceb0f[_0xc6cc('0x35')],function(_0x58db00){var _0x3c6f70={};_0x3c6f70[_0x58db00]={'$like':'%'+_0x50bb86[_0xc6cc('0x31')][_0xc6cc('0x3e')]+'%'};return _0x3c6f70;})});}_0x4ceb0f=_['merge']({},_0x4ceb0f,_0x50bb86[_0xc6cc('0x46')]);return _0x445f41[_0xc6cc('0x52')](_0x4ceb0f);}})[_0xc6cc('0x23')](function(_0xe0ad14){if(_0xe0ad14){_0x43e03a=_0xe0ad14[_0xc6cc('0x44')];if(!_0x50bb86['query']['hasOwnProperty'](_0xc6cc('0x54'))){_0x4ceb0f[_0xc6cc('0x37')]=qs['limit'](_0x50bb86[_0xc6cc('0x31')][_0xc6cc('0x37')]);_0x4ceb0f[_0xc6cc('0x1e')]=qs[_0xc6cc('0x1e')](_0x50bb86['query'][_0xc6cc('0x1e')]);}return _0x445f41[_0xc6cc('0x52')](_0x4ceb0f);}})[_0xc6cc('0x23')](function(_0x329a51){if(_0x329a51){return _0x329a51?{'count':_0x43e03a,'rows':_0x329a51}:null;}})['then'](respondWithResult(_0x1f916a,null))[_0xc6cc('0x41')](handleError(_0x1f916a,null));};exports[_0xc6cc('0x55')]=function(_0x5812dc,_0xbbec8,_0x70e883){return db['Interval'][_0xc6cc('0x53')]({'where':{'id':_0x5812dc[_0xc6cc('0x43')]['id']}})['then'](handleEntityNotFound(_0xbbec8,null))['then'](function(_0x14524c){if(_0x14524c){return db[_0xc6cc('0x56')][_0xc6cc('0x57')](function(_0x3c8527){return db[_0xc6cc('0x2e')][_0xc6cc('0x24')]({'where':{'IntervalId':_0x5812dc[_0xc6cc('0x43')]['id']},'transaction':_0x3c8527})['then'](function(_0x3973da){var _0xe336f6=_['map'](_0x5812dc['body'],function(_0x451157){delete _0x451157['id'];_0x451157['IntervalId']=_0x5812dc[_0xc6cc('0x43')]['id'];return _0x451157;});return db[_0xc6cc('0x2e')][_0xc6cc('0x58')](_0xe336f6,{'transaction':_0x3c8527});});})[_0xc6cc('0x23')](function(){return db[_0xc6cc('0x2e')][_0xc6cc('0x3f')]({'where':{'IntervalId':_0x5812dc[_0xc6cc('0x43')]['id']}});});}})[_0xc6cc('0x23')](respondWithResult(_0xbbec8,null))[_0xc6cc('0x41')](handleError(_0xbbec8,null));};