ab366fcaf0481d4853429555f26fe9f0af864c34
[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 _0x1f58=['length','hasOwnProperty','nolimit','order','sort','where','options','rows','params','keys','includeAll','include','find','catch','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','addInterval','IntervalId','getIntervals','findOne','pick','filter','sequelize','transaction','bulkCreate','findAll','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','to-csv','ejs','squel','crypto','jsforce','querystring','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/extensions/rewrite','sendStatus','status','json','offset','undefined','count','limit','set','apply','save','update','get','Intervals','UserProfileResource','destroy','then','error','stack','name','send','merge','body','interaction','account','index','Interval','rawAttributes','fieldName','type','key','map','query','filters','intersection','attributes','model','fields'];(function(_0x1d1779,_0x284147){var _0x2377e9=function(_0x299f6f){while(--_0x299f6f){_0x1d1779['push'](_0x1d1779['shift']());}};_0x2377e9(++_0x284147);}(_0x1f58,0x78));var _0x81f5=function(_0x1c9748,_0x2cb2ae){_0x1c9748=_0x1c9748-0x0;var _0x1e8444=_0x1f58[_0x1c9748];return _0x1e8444;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x81f5('0x0'));var zipdir=require(_0x81f5('0x1'));var jsonpatch=require(_0x81f5('0x2'));var rp=require(_0x81f5('0x3'));var moment=require(_0x81f5('0x4'));var BPromise=require(_0x81f5('0x5'));var Mustache=require(_0x81f5('0x6'));var util=require(_0x81f5('0x7'));var path=require('path');var sox=require('sox');var csv=require(_0x81f5('0x8'));var ejs=require(_0x81f5('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x81f5('0xa'));var crypto=require(_0x81f5('0xb'));var jsforce=require(_0x81f5('0xc'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x81f5('0xd'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x81f5('0xe'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x81f5('0xf'));var logger=require(_0x81f5('0x10'))(_0x81f5('0x11'));var utils=require(_0x81f5('0x12'));var config=require(_0x81f5('0x13'));var licenseUtil=require(_0x81f5('0x14'));var db=require(_0x81f5('0x15'))['db'];var extensions=require(_0x81f5('0x16'));function respondWithStatusCode(_0x5da2a3,_0x2c5acb){_0x2c5acb=_0x2c5acb||0xcc;return function(_0x1ca8ba){if(_0x1ca8ba){return _0x5da2a3[_0x81f5('0x17')](_0x2c5acb);}return _0x5da2a3[_0x81f5('0x18')](_0x2c5acb)['end']();};}function respondWithResult(_0x5b5858,_0x2f38d1){_0x2f38d1=_0x2f38d1||0xc8;return function(_0x8e0f2e){if(_0x8e0f2e){return _0x5b5858[_0x81f5('0x18')](_0x2f38d1)[_0x81f5('0x19')](_0x8e0f2e);}};}function respondWithFilteredResult(_0x59b4d2,_0x471e32){return function(_0x316757){if(_0x316757){var _0x664d95=typeof _0x471e32[_0x81f5('0x1a')]===_0x81f5('0x1b')&&typeof _0x471e32['limit']===_0x81f5('0x1b');var _0x24f5b2=_0x316757[_0x81f5('0x1c')];var _0x2ea6f8=_0x664d95?0x0:_0x471e32['offset'];var _0x21b497=_0x664d95?_0x316757['count']:_0x471e32[_0x81f5('0x1a')]+_0x471e32[_0x81f5('0x1d')];var _0x5c4d7b;if(_0x21b497>=_0x24f5b2){_0x21b497=_0x24f5b2;_0x5c4d7b=0xc8;}else{_0x5c4d7b=0xce;}_0x59b4d2[_0x81f5('0x18')](_0x5c4d7b);return _0x59b4d2[_0x81f5('0x1e')]('Content-Range',_0x2ea6f8+'-'+_0x21b497+'/'+_0x24f5b2)[_0x81f5('0x19')](_0x316757);}return null;};}function patchUpdates(_0x318d90){return function(_0x52846f){try{jsonpatch[_0x81f5('0x1f')](_0x52846f,_0x318d90,!![]);}catch(_0x1b32bb){return BPromise['reject'](_0x1b32bb);}return _0x52846f[_0x81f5('0x20')]();};}function saveUpdates(_0x6c76b1,_0x1291db){return function(_0x1c0c81){if(_0x1c0c81){return _0x1c0c81[_0x81f5('0x21')](_0x6c76b1)['then'](function(_0x187772){return _0x187772;});}return null;};}function removeEntity(_0x1c4149,_0x236548){return function(_0x2f2fe4){if(_0x2f2fe4){return _0x2f2fe4['destroy']()['then'](function(){var _0x42c2f6=_0x2f2fe4[_0x81f5('0x22')]({'plain':!![]});var _0x2c8aef=_0x81f5('0x23');return db[_0x81f5('0x24')][_0x81f5('0x25')]({'where':{'type':_0x2c8aef,'resourceId':_0x42c2f6['id']}})[_0x81f5('0x26')](function(){return _0x2f2fe4;});})[_0x81f5('0x26')](function(){_0x1c4149['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x27cdd7,_0x521513){return function(_0x1be6df){if(!_0x1be6df){_0x27cdd7[_0x81f5('0x17')](0x194);}return _0x1be6df;};}function handleError(_0x3f1e10,_0x3ca90f){_0x3ca90f=_0x3ca90f||0x1f4;return function(_0x554611){logger[_0x81f5('0x27')](_0x554611[_0x81f5('0x28')]);if(_0x554611[_0x81f5('0x29')]){delete _0x554611[_0x81f5('0x29')];}_0x3f1e10[_0x81f5('0x18')](_0x3ca90f)[_0x81f5('0x2a')](_0x554611);};}function getMustacheRender(_0x203a2d,_0x2dc89a){return Mustache['render'](_0x203a2d,_[_0x81f5('0x2b')](_0x2dc89a[_0x81f5('0x2c')],{'contact':_0x2dc89a['contact'],'message':_0x2dc89a['message'],'agent':_0x2dc89a['agent'],'interaction':_0x2dc89a[_0x81f5('0x2d')],'account':_0x2dc89a[_0x81f5('0x2e')]}))||'';}exports[_0x81f5('0x2f')]=function(_0x4a1ea8,_0x3752a1){var _0x334e93={},_0x4f8a6c={},_0x4a5cbd={'count':0x0,'rows':[]};var _0x3e0937=_['map'](db[_0x81f5('0x30')][_0x81f5('0x31')],function(_0x293698){return{'name':_0x293698[_0x81f5('0x32')],'type':_0x293698[_0x81f5('0x33')][_0x81f5('0x34')]};});_0x4f8a6c['model']=_[_0x81f5('0x35')](_0x3e0937,_0x81f5('0x29'));_0x4f8a6c[_0x81f5('0x36')]=_['keys'](_0x4a1ea8[_0x81f5('0x36')]);_0x4f8a6c[_0x81f5('0x37')]=_[_0x81f5('0x38')](_0x4f8a6c['model'],_0x4f8a6c[_0x81f5('0x36')]);_0x334e93[_0x81f5('0x39')]=_[_0x81f5('0x38')](_0x4f8a6c[_0x81f5('0x3a')],qs[_0x81f5('0x3b')](_0x4a1ea8[_0x81f5('0x36')]['fields']));_0x334e93[_0x81f5('0x39')]=_0x334e93[_0x81f5('0x39')][_0x81f5('0x3c')]?_0x334e93[_0x81f5('0x39')]:_0x4f8a6c[_0x81f5('0x3a')];if(!_0x4a1ea8[_0x81f5('0x36')][_0x81f5('0x3d')](_0x81f5('0x3e'))){_0x334e93[_0x81f5('0x1d')]=qs[_0x81f5('0x1d')](_0x4a1ea8[_0x81f5('0x36')][_0x81f5('0x1d')]);_0x334e93[_0x81f5('0x1a')]=qs[_0x81f5('0x1a')](_0x4a1ea8[_0x81f5('0x36')]['offset']);}_0x334e93[_0x81f5('0x3f')]=qs[_0x81f5('0x40')](_0x4a1ea8[_0x81f5('0x36')][_0x81f5('0x40')]);_0x334e93[_0x81f5('0x41')]=qs[_0x81f5('0x37')](_['pick'](_0x4a1ea8[_0x81f5('0x36')],_0x4f8a6c[_0x81f5('0x37')]),_0x3e0937);if(_0x4a1ea8['query']['filter']){_0x334e93['where']=_['merge'](_0x334e93[_0x81f5('0x41')],{'$or':_[_0x81f5('0x35')](_0x3e0937,function(_0x1b1077){if(_0x1b1077[_0x81f5('0x33')]!=='VIRTUAL'){var _0x357b80={};_0x357b80[_0x1b1077[_0x81f5('0x29')]]={'$like':'%'+_0x4a1ea8[_0x81f5('0x36')]['filter']+'%'};return _0x357b80;}})});}_0x334e93=_['merge']({},_0x334e93,_0x4a1ea8[_0x81f5('0x42')]);var _0x5bf069={'where':_0x334e93[_0x81f5('0x41')]};return db[_0x81f5('0x30')][_0x81f5('0x1c')](_0x5bf069)[_0x81f5('0x26')](function(_0x5f1302){_0x4a5cbd[_0x81f5('0x1c')]=_0x5f1302;if(_0x4a1ea8['query']['includeAll']){_0x334e93['include']=[{'all':!![]}];}return db[_0x81f5('0x30')]['findAll'](_0x334e93);})[_0x81f5('0x26')](function(_0x8aafa8){_0x4a5cbd[_0x81f5('0x43')]=_0x8aafa8;return _0x4a5cbd;})[_0x81f5('0x26')](respondWithFilteredResult(_0x3752a1,_0x334e93))['catch'](handleError(_0x3752a1,null));};exports['show']=function(_0x2c791a,_0x48b51b){var _0x4ea842={'raw':!![],'where':{'id':_0x2c791a[_0x81f5('0x44')]['id']}},_0xfa49a5={};_0xfa49a5[_0x81f5('0x3a')]=_[_0x81f5('0x45')](db['Interval'][_0x81f5('0x31')]);_0xfa49a5[_0x81f5('0x36')]=_['keys'](_0x2c791a['query']);_0xfa49a5['filters']=_['intersection'](_0xfa49a5[_0x81f5('0x3a')],_0xfa49a5[_0x81f5('0x36')]);_0x4ea842[_0x81f5('0x39')]=_[_0x81f5('0x38')](_0xfa49a5['model'],qs['fields'](_0x2c791a['query'][_0x81f5('0x3b')]));_0x4ea842[_0x81f5('0x39')]=_0x4ea842[_0x81f5('0x39')][_0x81f5('0x3c')]?_0x4ea842['attributes']:_0xfa49a5[_0x81f5('0x3a')];if(_0x2c791a[_0x81f5('0x36')][_0x81f5('0x46')]){_0x4ea842[_0x81f5('0x47')]=[{'all':!![]}];}_0x4ea842=_[_0x81f5('0x2b')]({},_0x4ea842,_0x2c791a[_0x81f5('0x42')]);return db[_0x81f5('0x30')][_0x81f5('0x48')](_0x4ea842)[_0x81f5('0x26')](handleEntityNotFound(_0x48b51b,null))['then'](respondWithResult(_0x48b51b,null))[_0x81f5('0x49')](handleError(_0x48b51b,null));};exports['create']=function(_0x70830a,_0x33fb62){return db['Interval'][_0x81f5('0x4a')](_0x70830a[_0x81f5('0x2c')],{})[_0x81f5('0x26')](function(_0x224b30){var _0x46a591=_0x70830a[_0x81f5('0x4b')]['get']({'plain':!![]});if(!_0x46a591)throw new Error(_0x81f5('0x4c'));if(_0x46a591[_0x81f5('0x4d')]===_0x81f5('0x4b')){var _0x48f3bb=_0x224b30[_0x81f5('0x22')]({'plain':!![]});var _0x5213a0='Intervals';return db[_0x81f5('0x4e')][_0x81f5('0x48')]({'where':{'name':_0x5213a0,'userProfileId':_0x46a591[_0x81f5('0x4f')]},'raw':!![]})['then'](function(_0x5c43c2){if(_0x5c43c2&&_0x5c43c2['autoAssociation']===0x0){return db[_0x81f5('0x24')][_0x81f5('0x4a')]({'name':_0x48f3bb[_0x81f5('0x29')],'resourceId':_0x48f3bb['id'],'type':_0x5c43c2[_0x81f5('0x29')],'sectionId':_0x5c43c2['id']},{})[_0x81f5('0x26')](function(){return _0x224b30;});}else{return _0x224b30;}})[_0x81f5('0x49')](function(_0x477928){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x477928);throw _0x477928;});}return _0x224b30;})[_0x81f5('0x26')](respondWithResult(_0x33fb62,0xc9))[_0x81f5('0x49')](handleError(_0x33fb62,null));};exports[_0x81f5('0x21')]=function(_0x3eee59,_0x2aeded){if(_0x3eee59[_0x81f5('0x2c')]['id']){delete _0x3eee59[_0x81f5('0x2c')]['id'];}return db[_0x81f5('0x30')][_0x81f5('0x48')]({'where':{'id':_0x3eee59[_0x81f5('0x44')]['id']}})['then'](handleEntityNotFound(_0x2aeded,null))['then'](saveUpdates(_0x3eee59[_0x81f5('0x2c')],null))[_0x81f5('0x26')](respondWithResult(_0x2aeded,null))[_0x81f5('0x49')](handleError(_0x2aeded,null));};exports[_0x81f5('0x25')]=function(_0x51e581,_0x511f6d){return db[_0x81f5('0x30')][_0x81f5('0x48')]({'where':{'id':_0x51e581['params']['id']}})[_0x81f5('0x26')](handleEntityNotFound(_0x511f6d,null))[_0x81f5('0x26')](removeEntity(_0x511f6d,null))[_0x81f5('0x49')](handleError(_0x511f6d,null));};exports[_0x81f5('0x50')]=function(_0x3912ea,_0xac27bc,_0x189a21){if(_0x3912ea[_0x81f5('0x2c')]['id']){delete _0x3912ea['body']['id'];}return db['Interval'][_0x81f5('0x48')]({'where':{'id':_0x3912ea[_0x81f5('0x44')]['id']}})[_0x81f5('0x26')](handleEntityNotFound(_0xac27bc,null))[_0x81f5('0x26')](function(_0xf760e0){if(_0xf760e0){_0x3912ea['body'][_0x81f5('0x51')]=_0xf760e0['id'];return db[_0x81f5('0x30')]['create'](_0x3912ea[_0x81f5('0x2c')]);}})[_0x81f5('0x26')](respondWithResult(_0xac27bc,null))[_0x81f5('0x49')](handleError(_0xac27bc,null));};exports[_0x81f5('0x52')]=function(_0x577113,_0x12e882,_0x5a8b0d){var _0x21906d={};var _0x1696ab={};var _0x174816;var _0x34d26e;return db[_0x81f5('0x30')][_0x81f5('0x53')]({'where':{'id':_0x577113[_0x81f5('0x44')]['id']}})[_0x81f5('0x26')](handleEntityNotFound(_0x12e882,null))[_0x81f5('0x26')](function(_0x315622){if(_0x315622){_0x174816=_0x315622;_0x1696ab[_0x81f5('0x3a')]=_[_0x81f5('0x45')](db[_0x81f5('0x30')][_0x81f5('0x31')]);_0x1696ab[_0x81f5('0x36')]=_['keys'](_0x577113['query']);_0x1696ab[_0x81f5('0x37')]=_['intersection'](_0x1696ab[_0x81f5('0x3a')],_0x1696ab[_0x81f5('0x36')]);_0x21906d[_0x81f5('0x39')]=_[_0x81f5('0x38')](_0x1696ab['model'],qs[_0x81f5('0x3b')](_0x577113[_0x81f5('0x36')][_0x81f5('0x3b')]));_0x21906d[_0x81f5('0x39')]=_0x21906d['attributes'][_0x81f5('0x3c')]?_0x21906d['attributes']:_0x1696ab['model'];_0x21906d[_0x81f5('0x3f')]=qs[_0x81f5('0x40')](_0x577113[_0x81f5('0x36')][_0x81f5('0x40')]);_0x21906d[_0x81f5('0x41')]=qs[_0x81f5('0x37')](_[_0x81f5('0x54')](_0x577113[_0x81f5('0x36')],_0x1696ab[_0x81f5('0x37')]));if(_0x577113['query'][_0x81f5('0x55')]){_0x21906d[_0x81f5('0x41')]=_[_0x81f5('0x2b')](_0x21906d[_0x81f5('0x41')],{'$or':_[_0x81f5('0x35')](_0x21906d[_0x81f5('0x39')],function(_0x2f32e1){var _0x2c4b20={};_0x2c4b20[_0x2f32e1]={'$like':'%'+_0x577113['query'][_0x81f5('0x55')]+'%'};return _0x2c4b20;})});}_0x21906d=_[_0x81f5('0x2b')]({},_0x21906d,_0x577113[_0x81f5('0x42')]);return _0x174816[_0x81f5('0x52')](_0x21906d);}})[_0x81f5('0x26')](function(_0x2cc452){if(_0x2cc452){_0x34d26e=_0x2cc452[_0x81f5('0x3c')];if(!_0x577113[_0x81f5('0x36')][_0x81f5('0x3d')](_0x81f5('0x3e'))){_0x21906d['limit']=qs[_0x81f5('0x1d')](_0x577113[_0x81f5('0x36')][_0x81f5('0x1d')]);_0x21906d['offset']=qs[_0x81f5('0x1a')](_0x577113[_0x81f5('0x36')][_0x81f5('0x1a')]);}return _0x174816[_0x81f5('0x52')](_0x21906d);}})[_0x81f5('0x26')](function(_0xc4a392){if(_0xc4a392){return _0xc4a392?{'count':_0x34d26e,'rows':_0xc4a392}:null;}})[_0x81f5('0x26')](respondWithResult(_0x12e882,null))[_0x81f5('0x49')](handleError(_0x12e882,null));};exports['addIntervals']=function(_0x17f4d7,_0x2433e1,_0x47aed9){return db[_0x81f5('0x30')][_0x81f5('0x53')]({'where':{'id':_0x17f4d7[_0x81f5('0x44')]['id']}})[_0x81f5('0x26')](handleEntityNotFound(_0x2433e1,null))[_0x81f5('0x26')](function(_0x48e327){if(_0x48e327){return db[_0x81f5('0x56')][_0x81f5('0x57')](function(_0x1a171a){return db['Interval'][_0x81f5('0x25')]({'where':{'IntervalId':_0x17f4d7['params']['id']},'transaction':_0x1a171a})[_0x81f5('0x26')](function(_0x55f673){var _0x42319d=_[_0x81f5('0x35')](_0x17f4d7[_0x81f5('0x2c')],function(_0x46cd80){delete _0x46cd80['id'];_0x46cd80[_0x81f5('0x51')]=_0x17f4d7[_0x81f5('0x44')]['id'];return _0x46cd80;});return db[_0x81f5('0x30')][_0x81f5('0x58')](_0x42319d,{'transaction':_0x1a171a});});})[_0x81f5('0x26')](function(){return db['Interval'][_0x81f5('0x59')]({'where':{'IntervalId':_0x17f4d7['params']['id']}});});}})[_0x81f5('0x26')](respondWithResult(_0x2433e1,null))['catch'](handleError(_0x2433e1,null));};