4aa6272b3e3489e8ce7f92096aba05402db8ce31
[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 _0x6fce=['includeAll','findAll','catch','show','params','fields','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','IntervalId','findOne','pick','getIntervals','addIntervals','sequelize','transaction','bulkCreate','eml-format','rimraf','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/extensions/rewrite','end','status','json','offset','undefined','limit','count','Content-Range','apply','reject','save','update','destroy','get','Intervals','UserProfileResource','sendStatus','error','stack','name','send','index','map','Interval','rawAttributes','fieldName','key','query','keys','filters','model','attributes','intersection','length','hasOwnProperty','order','sort','filter','merge','where','type','then'];(function(_0x4526f4,_0x2ba9b3){var _0x59b2db=function(_0x26200f){while(--_0x26200f){_0x4526f4['push'](_0x4526f4['shift']());}};_0x59b2db(++_0x2ba9b3);}(_0x6fce,0x75));var _0xe6fc=function(_0x25e7cb,_0x3579b7){_0x25e7cb=_0x25e7cb-0x0;var _0x375d92=_0x6fce[_0x25e7cb];return _0x375d92;};'use strict';var emlformat=require(_0xe6fc('0x0'));var rimraf=require(_0xe6fc('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xe6fc('0x2'));var rp=require(_0xe6fc('0x3'));var moment=require('moment');var BPromise=require(_0xe6fc('0x4'));var Mustache=require(_0xe6fc('0x5'));var util=require(_0xe6fc('0x6'));var path=require(_0xe6fc('0x7'));var sox=require(_0xe6fc('0x8'));var csv=require(_0xe6fc('0x9'));var ejs=require(_0xe6fc('0xa'));var fs=require('fs');var fs_extra=require(_0xe6fc('0xb'));var _=require(_0xe6fc('0xc'));var squel=require(_0xe6fc('0xd'));var crypto=require('crypto');var jsforce=require(_0xe6fc('0xe'));var deskjs=require(_0xe6fc('0xf'));var toCsv=require(_0xe6fc('0x9'));var querystring=require('querystring');var Papa=require(_0xe6fc('0x10'));var Redis=require(_0xe6fc('0x11'));var authService=require(_0xe6fc('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0xe6fc('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xe6fc('0x14'))(_0xe6fc('0x15'));var utils=require(_0xe6fc('0x16'));var config=require(_0xe6fc('0x17'));var licenseUtil=require(_0xe6fc('0x18'));var db=require(_0xe6fc('0x19'))['db'];var extensions=require(_0xe6fc('0x1a'));function respondWithStatusCode(_0x36b290,_0x12c81f){_0x12c81f=_0x12c81f||0xcc;return function(_0x569ce4){if(_0x569ce4){return _0x36b290['sendStatus'](_0x12c81f);}return _0x36b290['status'](_0x12c81f)[_0xe6fc('0x1b')]();};}function respondWithResult(_0x42dfcb,_0x311e58){_0x311e58=_0x311e58||0xc8;return function(_0x26871f){if(_0x26871f){return _0x42dfcb[_0xe6fc('0x1c')](_0x311e58)[_0xe6fc('0x1d')](_0x26871f);}};}function respondWithFilteredResult(_0x57ffa2,_0x1cb8df){return function(_0x53751c){if(_0x53751c){var _0x10b3bb=typeof _0x1cb8df[_0xe6fc('0x1e')]===_0xe6fc('0x1f')&&typeof _0x1cb8df[_0xe6fc('0x20')]===_0xe6fc('0x1f');var _0x237b7a=_0x53751c['count'];var _0x368e80=_0x10b3bb?0x0:_0x1cb8df[_0xe6fc('0x1e')];var _0x1481bd=_0x10b3bb?_0x53751c[_0xe6fc('0x21')]:_0x1cb8df[_0xe6fc('0x1e')]+_0x1cb8df[_0xe6fc('0x20')];var _0x24a16f;if(_0x1481bd>=_0x237b7a){_0x1481bd=_0x237b7a;_0x24a16f=0xc8;}else{_0x24a16f=0xce;}_0x57ffa2[_0xe6fc('0x1c')](_0x24a16f);return _0x57ffa2['set'](_0xe6fc('0x22'),_0x368e80+'-'+_0x1481bd+'/'+_0x237b7a)['json'](_0x53751c);}return null;};}function patchUpdates(_0x2815e9){return function(_0x5c1e98){try{jsonpatch[_0xe6fc('0x23')](_0x5c1e98,_0x2815e9,!![]);}catch(_0x2be6f5){return BPromise[_0xe6fc('0x24')](_0x2be6f5);}return _0x5c1e98[_0xe6fc('0x25')]();};}function saveUpdates(_0x39b347,_0x46b60e){return function(_0xacea98){if(_0xacea98){return _0xacea98[_0xe6fc('0x26')](_0x39b347)['then'](function(_0x5d44cd){return _0x5d44cd;});}return null;};}function removeEntity(_0xd6f401,_0x34fb1d){return function(_0xc272e){if(_0xc272e){return _0xc272e[_0xe6fc('0x27')]()['then'](function(){var _0x4582a8=_0xc272e[_0xe6fc('0x28')]({'plain':!![]});var _0x426e1f=_0xe6fc('0x29');return db[_0xe6fc('0x2a')]['destroy']({'where':{'type':_0x426e1f,'resourceId':_0x4582a8['id']}})['then'](function(){return _0xc272e;});})['then'](function(){_0xd6f401[_0xe6fc('0x1c')](0xcc)[_0xe6fc('0x1b')]();});}};}function handleEntityNotFound(_0x1e76a1,_0x17118c){return function(_0x4998f9){if(!_0x4998f9){_0x1e76a1[_0xe6fc('0x2b')](0x194);}return _0x4998f9;};}function handleError(_0x194d53,_0x2ca862){_0x2ca862=_0x2ca862||0x1f4;return function(_0x385326){logger[_0xe6fc('0x2c')](_0x385326[_0xe6fc('0x2d')]);if(_0x385326[_0xe6fc('0x2e')]){delete _0x385326['name'];}_0x194d53[_0xe6fc('0x1c')](_0x2ca862)[_0xe6fc('0x2f')](_0x385326);};}exports[_0xe6fc('0x30')]=function(_0x342cd6,_0x9c2374){var _0x49a39a={},_0x2918a1={},_0x8b8d56={'count':0x0,'rows':[]};var _0xec5b00=_[_0xe6fc('0x31')](db[_0xe6fc('0x32')][_0xe6fc('0x33')],function(_0x3833ac){return{'name':_0x3833ac[_0xe6fc('0x34')],'type':_0x3833ac['type'][_0xe6fc('0x35')]};});_0x2918a1['model']=_[_0xe6fc('0x31')](_0xec5b00,'name');_0x2918a1[_0xe6fc('0x36')]=_[_0xe6fc('0x37')](_0x342cd6[_0xe6fc('0x36')]);_0x2918a1[_0xe6fc('0x38')]=_['intersection'](_0x2918a1[_0xe6fc('0x39')],_0x2918a1[_0xe6fc('0x36')]);_0x49a39a[_0xe6fc('0x3a')]=_[_0xe6fc('0x3b')](_0x2918a1['model'],qs['fields'](_0x342cd6['query']['fields']));_0x49a39a[_0xe6fc('0x3a')]=_0x49a39a['attributes'][_0xe6fc('0x3c')]?_0x49a39a[_0xe6fc('0x3a')]:_0x2918a1[_0xe6fc('0x39')];if(!_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x3d')]('nolimit')){_0x49a39a['limit']=qs[_0xe6fc('0x20')](_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x20')]);_0x49a39a[_0xe6fc('0x1e')]=qs['offset'](_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x1e')]);}_0x49a39a[_0xe6fc('0x3e')]=qs[_0xe6fc('0x3f')](_0x342cd6[_0xe6fc('0x36')]['sort']);_0x49a39a['where']=qs[_0xe6fc('0x38')](_['pick'](_0x342cd6[_0xe6fc('0x36')],_0x2918a1['filters']),_0xec5b00);if(_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x40')]){_0x49a39a['where']=_[_0xe6fc('0x41')](_0x49a39a[_0xe6fc('0x42')],{'$or':_[_0xe6fc('0x31')](_0xec5b00,function(_0x9964ab){if(_0x9964ab[_0xe6fc('0x43')]!=='VIRTUAL'){var _0x36032f={};_0x36032f[_0x9964ab[_0xe6fc('0x2e')]]={'$like':'%'+_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x40')]+'%'};return _0x36032f;}})});}_0x49a39a=_[_0xe6fc('0x41')]({},_0x49a39a,_0x342cd6['options']);var _0x23ed23={'where':_0x49a39a['where']};return db[_0xe6fc('0x32')][_0xe6fc('0x21')](_0x23ed23)[_0xe6fc('0x44')](function(_0x462bdf){_0x8b8d56['count']=_0x462bdf;if(_0x342cd6[_0xe6fc('0x36')][_0xe6fc('0x45')]){_0x49a39a['include']=[{'all':!![]}];}return db[_0xe6fc('0x32')][_0xe6fc('0x46')](_0x49a39a);})[_0xe6fc('0x44')](function(_0x1f57d4){_0x8b8d56['rows']=_0x1f57d4;return _0x8b8d56;})[_0xe6fc('0x44')](respondWithFilteredResult(_0x9c2374,_0x49a39a))[_0xe6fc('0x47')](handleError(_0x9c2374,null));};exports[_0xe6fc('0x48')]=function(_0x4f0813,_0x329747){var _0x2d6b10={'raw':!![],'where':{'id':_0x4f0813[_0xe6fc('0x49')]['id']}},_0x555f35={};_0x555f35[_0xe6fc('0x39')]=_['keys'](db[_0xe6fc('0x32')]['rawAttributes']);_0x555f35[_0xe6fc('0x36')]=_[_0xe6fc('0x37')](_0x4f0813['query']);_0x555f35[_0xe6fc('0x38')]=_['intersection'](_0x555f35[_0xe6fc('0x39')],_0x555f35[_0xe6fc('0x36')]);_0x2d6b10[_0xe6fc('0x3a')]=_['intersection'](_0x555f35['model'],qs[_0xe6fc('0x4a')](_0x4f0813[_0xe6fc('0x36')]['fields']));_0x2d6b10[_0xe6fc('0x3a')]=_0x2d6b10[_0xe6fc('0x3a')]['length']?_0x2d6b10[_0xe6fc('0x3a')]:_0x555f35[_0xe6fc('0x39')];if(_0x4f0813[_0xe6fc('0x36')][_0xe6fc('0x45')]){_0x2d6b10['include']=[{'all':!![]}];}_0x2d6b10=_[_0xe6fc('0x41')]({},_0x2d6b10,_0x4f0813[_0xe6fc('0x4b')]);return db[_0xe6fc('0x32')][_0xe6fc('0x4c')](_0x2d6b10)[_0xe6fc('0x44')](handleEntityNotFound(_0x329747,null))[_0xe6fc('0x44')](respondWithResult(_0x329747,null))[_0xe6fc('0x47')](handleError(_0x329747,null));};exports[_0xe6fc('0x4d')]=function(_0x40da0b,_0x9512ad){return db[_0xe6fc('0x32')][_0xe6fc('0x4d')](_0x40da0b[_0xe6fc('0x4e')],{})['then'](function(_0x175723){var _0x7b7736=_0x40da0b[_0xe6fc('0x4f')][_0xe6fc('0x28')]({'plain':!![]});if(!_0x7b7736)throw new Error(_0xe6fc('0x50'));if(_0x7b7736[_0xe6fc('0x51')]===_0xe6fc('0x4f')){var _0xed5308=_0x175723[_0xe6fc('0x28')]({'plain':!![]});var _0x2b8baf=_0xe6fc('0x29');return db['UserProfileSection'][_0xe6fc('0x4c')]({'where':{'name':_0x2b8baf,'userProfileId':_0x7b7736[_0xe6fc('0x52')]},'raw':!![]})[_0xe6fc('0x44')](function(_0x511925){if(_0x511925&&_0x511925[_0xe6fc('0x53')]===0x0){return db[_0xe6fc('0x2a')][_0xe6fc('0x4d')]({'name':_0xed5308[_0xe6fc('0x2e')],'resourceId':_0xed5308['id'],'type':_0x511925[_0xe6fc('0x2e')],'sectionId':_0x511925['id']},{})[_0xe6fc('0x44')](function(){return _0x175723;});}else{return _0x175723;}})[_0xe6fc('0x47')](function(_0x1efd0b){logger[_0xe6fc('0x2c')](_0xe6fc('0x54'),_0x1efd0b);throw _0x1efd0b;});}return _0x175723;})[_0xe6fc('0x44')](respondWithResult(_0x9512ad,0xc9))[_0xe6fc('0x47')](handleError(_0x9512ad,null));};exports['update']=function(_0x47b3f4,_0x55db2b){if(_0x47b3f4['body']['id']){delete _0x47b3f4['body']['id'];}return db[_0xe6fc('0x32')][_0xe6fc('0x4c')]({'where':{'id':_0x47b3f4[_0xe6fc('0x49')]['id']}})[_0xe6fc('0x44')](handleEntityNotFound(_0x55db2b,null))[_0xe6fc('0x44')](saveUpdates(_0x47b3f4[_0xe6fc('0x4e')],null))[_0xe6fc('0x44')](respondWithResult(_0x55db2b,null))[_0xe6fc('0x47')](handleError(_0x55db2b,null));};exports[_0xe6fc('0x27')]=function(_0x550273,_0x416bcf){return db[_0xe6fc('0x32')]['find']({'where':{'id':_0x550273[_0xe6fc('0x49')]['id']}})[_0xe6fc('0x44')](handleEntityNotFound(_0x416bcf,null))[_0xe6fc('0x44')](removeEntity(_0x416bcf,null))['catch'](handleError(_0x416bcf,null));};exports['addInterval']=function(_0x520f51,_0x4829bf,_0x3770c1){if(_0x520f51[_0xe6fc('0x4e')]['id']){delete _0x520f51['body']['id'];}return db[_0xe6fc('0x32')]['find']({'where':{'id':_0x520f51['params']['id']}})[_0xe6fc('0x44')](handleEntityNotFound(_0x4829bf,null))[_0xe6fc('0x44')](function(_0x2af860){if(_0x2af860){_0x520f51[_0xe6fc('0x4e')][_0xe6fc('0x55')]=_0x2af860['id'];return db[_0xe6fc('0x32')][_0xe6fc('0x4d')](_0x520f51[_0xe6fc('0x4e')]);}})['then'](respondWithResult(_0x4829bf,null))[_0xe6fc('0x47')](handleError(_0x4829bf,null));};exports['getIntervals']=function(_0x3a0f34,_0x527c18,_0x5ab4a2){var _0x3f0c1a={};var _0x54f795={};var _0x2804bd;var _0x2a06d7;return db[_0xe6fc('0x32')][_0xe6fc('0x56')]({'where':{'id':_0x3a0f34['params']['id']}})[_0xe6fc('0x44')](handleEntityNotFound(_0x527c18,null))[_0xe6fc('0x44')](function(_0x328353){if(_0x328353){_0x2804bd=_0x328353;_0x54f795[_0xe6fc('0x39')]=_['keys'](db[_0xe6fc('0x32')]['rawAttributes']);_0x54f795[_0xe6fc('0x36')]=_[_0xe6fc('0x37')](_0x3a0f34[_0xe6fc('0x36')]);_0x54f795[_0xe6fc('0x38')]=_[_0xe6fc('0x3b')](_0x54f795[_0xe6fc('0x39')],_0x54f795[_0xe6fc('0x36')]);_0x3f0c1a[_0xe6fc('0x3a')]=_['intersection'](_0x54f795['model'],qs['fields'](_0x3a0f34[_0xe6fc('0x36')][_0xe6fc('0x4a')]));_0x3f0c1a[_0xe6fc('0x3a')]=_0x3f0c1a[_0xe6fc('0x3a')][_0xe6fc('0x3c')]?_0x3f0c1a[_0xe6fc('0x3a')]:_0x54f795[_0xe6fc('0x39')];_0x3f0c1a[_0xe6fc('0x3e')]=qs[_0xe6fc('0x3f')](_0x3a0f34[_0xe6fc('0x36')]['sort']);_0x3f0c1a[_0xe6fc('0x42')]=qs['filters'](_[_0xe6fc('0x57')](_0x3a0f34['query'],_0x54f795[_0xe6fc('0x38')]));if(_0x3a0f34[_0xe6fc('0x36')][_0xe6fc('0x40')]){_0x3f0c1a[_0xe6fc('0x42')]=_[_0xe6fc('0x41')](_0x3f0c1a['where'],{'$or':_[_0xe6fc('0x31')](_0x3f0c1a[_0xe6fc('0x3a')],function(_0x2e0f07){var _0x239d69={};_0x239d69[_0x2e0f07]={'$like':'%'+_0x3a0f34[_0xe6fc('0x36')][_0xe6fc('0x40')]+'%'};return _0x239d69;})});}_0x3f0c1a=_['merge']({},_0x3f0c1a,_0x3a0f34[_0xe6fc('0x4b')]);return _0x2804bd['getIntervals'](_0x3f0c1a);}})[_0xe6fc('0x44')](function(_0x570f99){if(_0x570f99){_0x2a06d7=_0x570f99[_0xe6fc('0x3c')];if(!_0x3a0f34[_0xe6fc('0x36')][_0xe6fc('0x3d')]('nolimit')){_0x3f0c1a[_0xe6fc('0x20')]=qs[_0xe6fc('0x20')](_0x3a0f34['query']['limit']);_0x3f0c1a[_0xe6fc('0x1e')]=qs[_0xe6fc('0x1e')](_0x3a0f34['query']['offset']);}return _0x2804bd[_0xe6fc('0x58')](_0x3f0c1a);}})[_0xe6fc('0x44')](function(_0x2900e3){if(_0x2900e3){return _0x2900e3?{'count':_0x2a06d7,'rows':_0x2900e3}:null;}})[_0xe6fc('0x44')](respondWithResult(_0x527c18,null))['catch'](handleError(_0x527c18,null));};exports[_0xe6fc('0x59')]=function(_0x334521,_0x4f7385,_0x3fd696){return db[_0xe6fc('0x32')][_0xe6fc('0x56')]({'where':{'id':_0x334521[_0xe6fc('0x49')]['id']}})['then'](handleEntityNotFound(_0x4f7385,null))['then'](function(_0x4ab395){if(_0x4ab395){return db[_0xe6fc('0x5a')][_0xe6fc('0x5b')](function(_0x5877ba){return db[_0xe6fc('0x32')][_0xe6fc('0x27')]({'where':{'IntervalId':_0x334521[_0xe6fc('0x49')]['id']},'transaction':_0x5877ba})[_0xe6fc('0x44')](function(_0x49a76b){var _0xb0668e=_[_0xe6fc('0x31')](_0x334521[_0xe6fc('0x4e')],function(_0x284d68){delete _0x284d68['id'];_0x284d68[_0xe6fc('0x55')]=_0x334521[_0xe6fc('0x49')]['id'];return _0x284d68;});return db[_0xe6fc('0x32')][_0xe6fc('0x5c')](_0xb0668e,{'transaction':_0x5877ba});});})[_0xe6fc('0x44')](function(){return db[_0xe6fc('0x32')][_0xe6fc('0x46')]({'where':{'IntervalId':_0x334521[_0xe6fc('0x49')]['id']}});});}})[_0xe6fc('0x44')](respondWithResult(_0x4f7385,null))['catch'](handleError(_0x4f7385,null));};