020cbdc284279a9640343a7e85eaaf3a7c7b2624
[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 _0x6ce2=['sox','ejs','fs-extra','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','reject','save','update','then','get','Intervals','UserProfileResource','destroy','error','stack','name','send','index','map','Interval','rawAttributes','key','query','keys','filters','intersection','attributes','model','fields','length','hasOwnProperty','nolimit','order','sort','pick','where','type','merge','options','includeAll','include','findAll','rows','catch','params','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addInterval','findOne','filter','addIntervals','transaction','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache'];(function(_0x4ae1c8,_0x3f18c1){var _0x2eb247=function(_0x3574bc){while(--_0x3574bc){_0x4ae1c8['push'](_0x4ae1c8['shift']());}};_0x2eb247(++_0x3f18c1);}(_0x6ce2,0xfe));var _0x26ce=function(_0xbca1b1,_0x33f0ce){_0xbca1b1=_0xbca1b1-0x0;var _0x4e1686=_0x6ce2[_0xbca1b1];return _0x4e1686;};'use strict';var emlformat=require(_0x26ce('0x0'));var rimraf=require(_0x26ce('0x1'));var zipdir=require(_0x26ce('0x2'));var jsonpatch=require(_0x26ce('0x3'));var rp=require(_0x26ce('0x4'));var moment=require(_0x26ce('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x26ce('0x6'));var util=require('util');var path=require('path');var sox=require(_0x26ce('0x7'));var csv=require('to-csv');var ejs=require(_0x26ce('0x8'));var fs=require('fs');var fs_extra=require(_0x26ce('0x9'));var _=require(_0x26ce('0xa'));var squel=require(_0x26ce('0xb'));var crypto=require('crypto');var jsforce=require(_0x26ce('0xc'));var deskjs=require(_0x26ce('0xd'));var toCsv=require(_0x26ce('0xe'));var querystring=require(_0x26ce('0xf'));var Papa=require(_0x26ce('0x10'));var Redis=require(_0x26ce('0x11'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x26ce('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x26ce('0x13'));var utils=require(_0x26ce('0x14'));var config=require(_0x26ce('0x15'));var licenseUtil=require('../../config/license/util');var db=require(_0x26ce('0x16'))['db'];var extensions=require('../../components/extensions/rewrite');function respondWithStatusCode(_0x58585b,_0x575db9){_0x575db9=_0x575db9||0xcc;return function(_0x4246b1){if(_0x4246b1){return _0x58585b[_0x26ce('0x17')](_0x575db9);}return _0x58585b[_0x26ce('0x18')](_0x575db9)[_0x26ce('0x19')]();};}function respondWithResult(_0x1bce46,_0x1d0eaf){_0x1d0eaf=_0x1d0eaf||0xc8;return function(_0x13f768){if(_0x13f768){return _0x1bce46[_0x26ce('0x18')](_0x1d0eaf)[_0x26ce('0x1a')](_0x13f768);}};}function respondWithFilteredResult(_0x159849,_0x43c83e){return function(_0x4d37d4){if(_0x4d37d4){var _0x5871ae=typeof _0x43c83e[_0x26ce('0x1b')]===_0x26ce('0x1c')&&typeof _0x43c83e[_0x26ce('0x1d')]==='undefined';var _0x5f228f=_0x4d37d4[_0x26ce('0x1e')];var _0x1d744b=_0x5871ae?0x0:_0x43c83e[_0x26ce('0x1b')];var _0x5ecfbf=_0x5871ae?_0x4d37d4[_0x26ce('0x1e')]:_0x43c83e[_0x26ce('0x1b')]+_0x43c83e[_0x26ce('0x1d')];var _0x4488a7;if(_0x5ecfbf>=_0x5f228f){_0x5ecfbf=_0x5f228f;_0x4488a7=0xc8;}else{_0x4488a7=0xce;}_0x159849[_0x26ce('0x18')](_0x4488a7);return _0x159849['set'](_0x26ce('0x1f'),_0x1d744b+'-'+_0x5ecfbf+'/'+_0x5f228f)[_0x26ce('0x1a')](_0x4d37d4);}return null;};}function patchUpdates(_0x316a9e){return function(_0x34ac0f){try{jsonpatch[_0x26ce('0x20')](_0x34ac0f,_0x316a9e,!![]);}catch(_0x3022a5){return BPromise[_0x26ce('0x21')](_0x3022a5);}return _0x34ac0f[_0x26ce('0x22')]();};}function saveUpdates(_0x166992,_0x2c5b80){return function(_0x36fcec){if(_0x36fcec){return _0x36fcec[_0x26ce('0x23')](_0x166992)[_0x26ce('0x24')](function(_0x38defa){return _0x38defa;});}return null;};}function removeEntity(_0x1acabc,_0x52a354){return function(_0x276f44){if(_0x276f44){return _0x276f44['destroy']()[_0x26ce('0x24')](function(){var _0x7f9c80=_0x276f44[_0x26ce('0x25')]({'plain':!![]});var _0x4be760=_0x26ce('0x26');return db[_0x26ce('0x27')][_0x26ce('0x28')]({'where':{'type':_0x4be760,'resourceId':_0x7f9c80['id']}})[_0x26ce('0x24')](function(){return _0x276f44;});})['then'](function(){_0x1acabc[_0x26ce('0x18')](0xcc)[_0x26ce('0x19')]();});}};}function handleEntityNotFound(_0x4a7d5d,_0x5eec53){return function(_0x282579){if(!_0x282579){_0x4a7d5d['sendStatus'](0x194);}return _0x282579;};}function handleError(_0x4089b8,_0x9a28d9){_0x9a28d9=_0x9a28d9||0x1f4;return function(_0x3d335f){logger[_0x26ce('0x29')](_0x3d335f[_0x26ce('0x2a')]);if(_0x3d335f[_0x26ce('0x2b')]){delete _0x3d335f[_0x26ce('0x2b')];}_0x4089b8[_0x26ce('0x18')](_0x9a28d9)[_0x26ce('0x2c')](_0x3d335f);};}exports[_0x26ce('0x2d')]=function(_0x43067c,_0x160236){var _0x3c0c72={},_0x3c075b={},_0x4d9a59={'count':0x0,'rows':[]};var _0x903cdc=_[_0x26ce('0x2e')](db[_0x26ce('0x2f')][_0x26ce('0x30')],function(_0x56deea){return{'name':_0x56deea['fieldName'],'type':_0x56deea['type'][_0x26ce('0x31')]};});_0x3c075b['model']=_[_0x26ce('0x2e')](_0x903cdc,_0x26ce('0x2b'));_0x3c075b[_0x26ce('0x32')]=_[_0x26ce('0x33')](_0x43067c[_0x26ce('0x32')]);_0x3c075b[_0x26ce('0x34')]=_[_0x26ce('0x35')](_0x3c075b['model'],_0x3c075b[_0x26ce('0x32')]);_0x3c0c72[_0x26ce('0x36')]=_[_0x26ce('0x35')](_0x3c075b[_0x26ce('0x37')],qs[_0x26ce('0x38')](_0x43067c['query']['fields']));_0x3c0c72['attributes']=_0x3c0c72[_0x26ce('0x36')][_0x26ce('0x39')]?_0x3c0c72[_0x26ce('0x36')]:_0x3c075b[_0x26ce('0x37')];if(!_0x43067c[_0x26ce('0x32')][_0x26ce('0x3a')](_0x26ce('0x3b'))){_0x3c0c72[_0x26ce('0x1d')]=qs[_0x26ce('0x1d')](_0x43067c[_0x26ce('0x32')][_0x26ce('0x1d')]);_0x3c0c72[_0x26ce('0x1b')]=qs[_0x26ce('0x1b')](_0x43067c['query']['offset']);}_0x3c0c72[_0x26ce('0x3c')]=qs[_0x26ce('0x3d')](_0x43067c[_0x26ce('0x32')]['sort']);_0x3c0c72['where']=qs[_0x26ce('0x34')](_[_0x26ce('0x3e')](_0x43067c[_0x26ce('0x32')],_0x3c075b[_0x26ce('0x34')]),_0x903cdc);if(_0x43067c['query']['filter']){_0x3c0c72[_0x26ce('0x3f')]=_['merge'](_0x3c0c72[_0x26ce('0x3f')],{'$or':_[_0x26ce('0x2e')](_0x903cdc,function(_0x14d800){if(_0x14d800[_0x26ce('0x40')]!=='VIRTUAL'){var _0x31f91b={};_0x31f91b[_0x14d800[_0x26ce('0x2b')]]={'$like':'%'+_0x43067c['query']['filter']+'%'};return _0x31f91b;}})});}_0x3c0c72=_[_0x26ce('0x41')]({},_0x3c0c72,_0x43067c[_0x26ce('0x42')]);var _0x57f08e={'where':_0x3c0c72[_0x26ce('0x3f')]};return db['Interval'][_0x26ce('0x1e')](_0x57f08e)['then'](function(_0x10cf5a){_0x4d9a59[_0x26ce('0x1e')]=_0x10cf5a;if(_0x43067c['query'][_0x26ce('0x43')]){_0x3c0c72[_0x26ce('0x44')]=[{'all':!![]}];}return db[_0x26ce('0x2f')][_0x26ce('0x45')](_0x3c0c72);})[_0x26ce('0x24')](function(_0x580df1){_0x4d9a59[_0x26ce('0x46')]=_0x580df1;return _0x4d9a59;})[_0x26ce('0x24')](respondWithFilteredResult(_0x160236,_0x3c0c72))[_0x26ce('0x47')](handleError(_0x160236,null));};exports['show']=function(_0x13372a,_0x28012c){var _0x3e0f38={'raw':!![],'where':{'id':_0x13372a[_0x26ce('0x48')]['id']}},_0x50fe8a={};_0x50fe8a[_0x26ce('0x37')]=_[_0x26ce('0x33')](db[_0x26ce('0x2f')][_0x26ce('0x30')]);_0x50fe8a[_0x26ce('0x32')]=_[_0x26ce('0x33')](_0x13372a['query']);_0x50fe8a[_0x26ce('0x34')]=_[_0x26ce('0x35')](_0x50fe8a[_0x26ce('0x37')],_0x50fe8a[_0x26ce('0x32')]);_0x3e0f38[_0x26ce('0x36')]=_[_0x26ce('0x35')](_0x50fe8a[_0x26ce('0x37')],qs[_0x26ce('0x38')](_0x13372a['query']['fields']));_0x3e0f38[_0x26ce('0x36')]=_0x3e0f38[_0x26ce('0x36')][_0x26ce('0x39')]?_0x3e0f38['attributes']:_0x50fe8a[_0x26ce('0x37')];if(_0x13372a[_0x26ce('0x32')][_0x26ce('0x43')]){_0x3e0f38['include']=[{'all':!![]}];}_0x3e0f38=_[_0x26ce('0x41')]({},_0x3e0f38,_0x13372a[_0x26ce('0x42')]);return db['Interval'][_0x26ce('0x49')](_0x3e0f38)['then'](handleEntityNotFound(_0x28012c,null))[_0x26ce('0x24')](respondWithResult(_0x28012c,null))[_0x26ce('0x47')](handleError(_0x28012c,null));};exports[_0x26ce('0x4a')]=function(_0x4e44db,_0x41122e){return db[_0x26ce('0x2f')][_0x26ce('0x4a')](_0x4e44db[_0x26ce('0x4b')],{})[_0x26ce('0x24')](function(_0x2fb217){var _0x2cf366=_0x4e44db[_0x26ce('0x4c')][_0x26ce('0x25')]({'plain':!![]});if(!_0x2cf366)throw new Error(_0x26ce('0x4d'));if(_0x2cf366[_0x26ce('0x4e')]==='user'){var _0x4f7072=_0x2fb217['get']({'plain':!![]});var _0x1bceaf=_0x26ce('0x26');return db[_0x26ce('0x4f')]['find']({'where':{'name':_0x1bceaf,'userProfileId':_0x2cf366['userProfileId']},'raw':!![]})['then'](function(_0x35185c){if(_0x35185c&&_0x35185c[_0x26ce('0x50')]===0x0){return db[_0x26ce('0x27')]['create']({'name':_0x4f7072['name'],'resourceId':_0x4f7072['id'],'type':_0x35185c['name'],'sectionId':_0x35185c['id']},{})[_0x26ce('0x24')](function(){return _0x2fb217;});}else{return _0x2fb217;}})[_0x26ce('0x47')](function(_0x1fcb78){logger['error'](_0x26ce('0x51'),_0x1fcb78);throw _0x1fcb78;});}return _0x2fb217;})[_0x26ce('0x24')](respondWithResult(_0x41122e,0xc9))[_0x26ce('0x47')](handleError(_0x41122e,null));};exports['update']=function(_0xe2f276,_0xf7143b){if(_0xe2f276[_0x26ce('0x4b')]['id']){delete _0xe2f276[_0x26ce('0x4b')]['id'];}return db[_0x26ce('0x2f')]['find']({'where':{'id':_0xe2f276[_0x26ce('0x48')]['id']}})['then'](handleEntityNotFound(_0xf7143b,null))[_0x26ce('0x24')](saveUpdates(_0xe2f276['body'],null))[_0x26ce('0x24')](respondWithResult(_0xf7143b,null))['catch'](handleError(_0xf7143b,null));};exports[_0x26ce('0x28')]=function(_0x1ac354,_0x30cae){return db[_0x26ce('0x2f')][_0x26ce('0x49')]({'where':{'id':_0x1ac354[_0x26ce('0x48')]['id']}})['then'](handleEntityNotFound(_0x30cae,null))[_0x26ce('0x24')](removeEntity(_0x30cae,null))[_0x26ce('0x47')](handleError(_0x30cae,null));};exports[_0x26ce('0x52')]=function(_0x5635ea,_0x53cc1f,_0x5e9091){if(_0x5635ea[_0x26ce('0x4b')]['id']){delete _0x5635ea[_0x26ce('0x4b')]['id'];}return db[_0x26ce('0x2f')][_0x26ce('0x49')]({'where':{'id':_0x5635ea[_0x26ce('0x48')]['id']}})[_0x26ce('0x24')](handleEntityNotFound(_0x53cc1f,null))['then'](function(_0x413e76){if(_0x413e76){_0x5635ea[_0x26ce('0x4b')]['IntervalId']=_0x413e76['id'];return db['Interval']['create'](_0x5635ea[_0x26ce('0x4b')]);}})['then'](respondWithResult(_0x53cc1f,null))[_0x26ce('0x47')](handleError(_0x53cc1f,null));};exports['getIntervals']=function(_0xac64e9,_0x4b31eb,_0x47e443){var _0x4504b7={};var _0x1e113c={};var _0x43bee6;var _0x282f5e;return db[_0x26ce('0x2f')][_0x26ce('0x53')]({'where':{'id':_0xac64e9['params']['id']}})['then'](handleEntityNotFound(_0x4b31eb,null))['then'](function(_0x4fcfc7){if(_0x4fcfc7){_0x43bee6=_0x4fcfc7;_0x1e113c[_0x26ce('0x37')]=_[_0x26ce('0x33')](db[_0x26ce('0x2f')][_0x26ce('0x30')]);_0x1e113c[_0x26ce('0x32')]=_['keys'](_0xac64e9[_0x26ce('0x32')]);_0x1e113c[_0x26ce('0x34')]=_[_0x26ce('0x35')](_0x1e113c['model'],_0x1e113c[_0x26ce('0x32')]);_0x4504b7['attributes']=_[_0x26ce('0x35')](_0x1e113c[_0x26ce('0x37')],qs[_0x26ce('0x38')](_0xac64e9[_0x26ce('0x32')]['fields']));_0x4504b7[_0x26ce('0x36')]=_0x4504b7['attributes'][_0x26ce('0x39')]?_0x4504b7[_0x26ce('0x36')]:_0x1e113c[_0x26ce('0x37')];_0x4504b7['order']=qs[_0x26ce('0x3d')](_0xac64e9['query']['sort']);_0x4504b7[_0x26ce('0x3f')]=qs[_0x26ce('0x34')](_['pick'](_0xac64e9['query'],_0x1e113c[_0x26ce('0x34')]));if(_0xac64e9[_0x26ce('0x32')]['filter']){_0x4504b7[_0x26ce('0x3f')]=_['merge'](_0x4504b7['where'],{'$or':_[_0x26ce('0x2e')](_0x4504b7[_0x26ce('0x36')],function(_0x1eab97){var _0x4adade={};_0x4adade[_0x1eab97]={'$like':'%'+_0xac64e9[_0x26ce('0x32')][_0x26ce('0x54')]+'%'};return _0x4adade;})});}_0x4504b7=_[_0x26ce('0x41')]({},_0x4504b7,_0xac64e9['options']);return _0x43bee6['getIntervals'](_0x4504b7);}})[_0x26ce('0x24')](function(_0x2d5177){if(_0x2d5177){_0x282f5e=_0x2d5177['length'];if(!_0xac64e9['query'][_0x26ce('0x3a')](_0x26ce('0x3b'))){_0x4504b7[_0x26ce('0x1d')]=qs['limit'](_0xac64e9[_0x26ce('0x32')][_0x26ce('0x1d')]);_0x4504b7[_0x26ce('0x1b')]=qs[_0x26ce('0x1b')](_0xac64e9[_0x26ce('0x32')][_0x26ce('0x1b')]);}return _0x43bee6['getIntervals'](_0x4504b7);}})[_0x26ce('0x24')](function(_0x57a261){if(_0x57a261){return _0x57a261?{'count':_0x282f5e,'rows':_0x57a261}:null;}})[_0x26ce('0x24')](respondWithResult(_0x4b31eb,null))[_0x26ce('0x47')](handleError(_0x4b31eb,null));};exports[_0x26ce('0x55')]=function(_0x4408c9,_0x22b479,_0x10572f){return db[_0x26ce('0x2f')]['findOne']({'where':{'id':_0x4408c9['params']['id']}})[_0x26ce('0x24')](handleEntityNotFound(_0x22b479,null))[_0x26ce('0x24')](function(_0x36ed4c){if(_0x36ed4c){return db['sequelize'][_0x26ce('0x56')](function(_0x49053d){return db[_0x26ce('0x2f')][_0x26ce('0x28')]({'where':{'IntervalId':_0x4408c9['params']['id']},'transaction':_0x49053d})[_0x26ce('0x24')](function(_0x35a463){var _0x5ec1de=_[_0x26ce('0x2e')](_0x4408c9[_0x26ce('0x4b')],function(_0x6615de){delete _0x6615de['id'];_0x6615de['IntervalId']=_0x4408c9[_0x26ce('0x48')]['id'];return _0x6615de;});return db[_0x26ce('0x2f')]['bulkCreate'](_0x5ec1de,{'transaction':_0x49053d});});})[_0x26ce('0x24')](function(){return db['Interval'][_0x26ce('0x45')]({'where':{'IntervalId':_0x4408c9[_0x26ce('0x48')]['id']}});});}})['then'](respondWithResult(_0x22b479,null))[_0x26ce('0x47')](handleError(_0x22b479,null));};