839d361149132b74a7ff0196a49ace95f3d6bacd
[motion2.git] / server / api / schedule / schedule.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 _0x91cc=['ejs','lodash','squel','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','status','json','count','offset','limit','set','Content-Range','apply','reject','update','then','get','Scheduler','UserProfileResource','destroy','error','stack','name','send','index','map','Schedule','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','length','hasOwnProperty','order','sort','where','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','params','fields','find','create','body','user','role','UserProfileSection','userProfileId','autoAssociation','../../config/schedule','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox'];(function(_0x321bb2,_0x202794){var _0x9c12ed=function(_0x263dd3){while(--_0x263dd3){_0x321bb2['push'](_0x321bb2['shift']());}};_0x9c12ed(++_0x202794);}(_0x91cc,0xa0));var _0xc91c=function(_0x68ee8b,_0x3cc6c5){_0x68ee8b=_0x68ee8b-0x0;var _0x5148f7=_0x91cc[_0x68ee8b];return _0x5148f7;};'use strict';var pdf=require(_0xc91c('0x0'));var emlformat=require(_0xc91c('0x1'));var rimraf=require(_0xc91c('0x2'));var zipdir=require(_0xc91c('0x3'));var jsonpatch=require(_0xc91c('0x4'));var rp=require(_0xc91c('0x5'));var moment=require(_0xc91c('0x6'));var BPromise=require(_0xc91c('0x7'));var Mustache=require(_0xc91c('0x8'));var util=require(_0xc91c('0x9'));var path=require(_0xc91c('0xa'));var sox=require(_0xc91c('0xb'));var csv=require('to-csv');var ejs=require(_0xc91c('0xc'));var fs=require('fs');var _=require(_0xc91c('0xd'));var squel=require(_0xc91c('0xe'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xc91c('0xf'));var querystring=require('querystring');var Papa=require(_0xc91c('0x10'));var Redis=require(_0xc91c('0x11'));var authService=require(_0xc91c('0x12'));var qs=require(_0xc91c('0x13'));var as=require(_0xc91c('0x14'));var hardwareService=require(_0xc91c('0x15'));var logger=require('../../config/logger')(_0xc91c('0x16'));var utils=require(_0xc91c('0x17'));var config=require(_0xc91c('0x18'));var db=require(_0xc91c('0x19'))['db'];function respondWithStatusCode(_0x2b2676,_0x77540f){_0x77540f=_0x77540f||0xcc;return function(_0x4f1be0){if(_0x4f1be0){return _0x2b2676[_0xc91c('0x1a')](_0x77540f);}return _0x2b2676['status'](_0x77540f)[_0xc91c('0x1b')]();};}function respondWithResult(_0x1d9f11,_0x2b5c90){_0x2b5c90=_0x2b5c90||0xc8;return function(_0x1962ec){if(_0x1962ec){return _0x1d9f11[_0xc91c('0x1c')](_0x2b5c90)[_0xc91c('0x1d')](_0x1962ec);}};}function respondWithFilteredResult(_0x352317,_0x475eb2){return function(_0x1cdb6b){if(_0x1cdb6b){var _0x244967=_0x1cdb6b[_0xc91c('0x1e')],_0x3ce64e=_0x475eb2[_0xc91c('0x1f')],_0x3aebb0=_0x475eb2[_0xc91c('0x1f')]+_0x475eb2[_0xc91c('0x20')],_0x13baf2;if(_0x3aebb0>=_0x244967){_0x3aebb0=_0x244967;_0x13baf2=0xc8;}else{_0x13baf2=0xce;}_0x352317['status'](_0x13baf2);return _0x352317[_0xc91c('0x21')](_0xc91c('0x22'),_0x3ce64e+'-'+_0x3aebb0+'/'+_0x244967)['json'](_0x1cdb6b);}return null;};}function patchUpdates(_0x130021){return function(_0x514bc0){try{jsonpatch[_0xc91c('0x23')](_0x514bc0,_0x130021,!![]);}catch(_0x9c800e){return BPromise[_0xc91c('0x24')](_0x9c800e);}return _0x514bc0['save']();};}function saveUpdates(_0x1bb460,_0x47fc25){return function(_0xdda6ee){if(_0xdda6ee){return _0xdda6ee[_0xc91c('0x25')](_0x1bb460)[_0xc91c('0x26')](function(_0x522c61){return _0x522c61;});}return null;};}function removeEntity(_0x9ae333,_0x5b62dd){return function(_0x2e645c){if(_0x2e645c){return _0x2e645c['destroy']()[_0xc91c('0x26')](function(){var _0x286f88=_0x2e645c[_0xc91c('0x27')]({'plain':!![]});var _0x4a1d11=_0xc91c('0x28');return db[_0xc91c('0x29')][_0xc91c('0x2a')]({'where':{'type':_0x4a1d11,'resourceId':_0x286f88['id']}})[_0xc91c('0x26')](function(){return _0x2e645c;});})[_0xc91c('0x26')](function(){_0x9ae333['status'](0xcc)[_0xc91c('0x1b')]();});}};}function handleEntityNotFound(_0x48f64f,_0x20ca3a){return function(_0xdabfb3){if(!_0xdabfb3){_0x48f64f['sendStatus'](0x194);}return _0xdabfb3;};}function handleError(_0x34a795,_0x249fe9){_0x249fe9=_0x249fe9||0x1f4;return function(_0x403b46){logger[_0xc91c('0x2b')](_0x403b46[_0xc91c('0x2c')]);if(_0x403b46['name']){delete _0x403b46[_0xc91c('0x2d')];}_0x34a795['status'](_0x249fe9)[_0xc91c('0x2e')](_0x403b46);};}exports[_0xc91c('0x2f')]=function(_0x56ebea,_0x41a931){var _0x2d7fca={},_0x1a5f65={},_0x34d898={'count':0x0,'rows':[]};var _0x3b1837=_[_0xc91c('0x30')](db[_0xc91c('0x31')][_0xc91c('0x32')],function(_0x139594){return{'name':_0x139594[_0xc91c('0x33')],'type':_0x139594[_0xc91c('0x34')][_0xc91c('0x35')]};});_0x1a5f65[_0xc91c('0x36')]=_[_0xc91c('0x30')](_0x3b1837,'name');_0x1a5f65[_0xc91c('0x37')]=_[_0xc91c('0x38')](_0x56ebea[_0xc91c('0x37')]);_0x1a5f65[_0xc91c('0x39')]=_[_0xc91c('0x3a')](_0x1a5f65[_0xc91c('0x36')],_0x1a5f65[_0xc91c('0x37')]);_0x2d7fca[_0xc91c('0x3b')]=_[_0xc91c('0x3a')](_0x1a5f65['model'],qs['fields'](_0x56ebea[_0xc91c('0x37')]['fields']));_0x2d7fca['attributes']=_0x2d7fca[_0xc91c('0x3b')][_0xc91c('0x3c')]?_0x2d7fca[_0xc91c('0x3b')]:_0x1a5f65[_0xc91c('0x36')];if(!_0x56ebea[_0xc91c('0x37')][_0xc91c('0x3d')]('nolimit')){_0x2d7fca[_0xc91c('0x20')]=qs[_0xc91c('0x20')](_0x56ebea[_0xc91c('0x37')][_0xc91c('0x20')]);_0x2d7fca[_0xc91c('0x1f')]=qs[_0xc91c('0x1f')](_0x56ebea[_0xc91c('0x37')][_0xc91c('0x1f')]);}_0x2d7fca[_0xc91c('0x3e')]=qs[_0xc91c('0x3f')](_0x56ebea[_0xc91c('0x37')][_0xc91c('0x3f')]);_0x2d7fca[_0xc91c('0x40')]=qs['filters'](_['pick'](_0x56ebea[_0xc91c('0x37')],_0x1a5f65[_0xc91c('0x39')]),_0x3b1837);if(_0x56ebea[_0xc91c('0x37')][_0xc91c('0x41')]){_0x2d7fca[_0xc91c('0x40')]=_[_0xc91c('0x42')](_0x2d7fca['where'],{'$or':_[_0xc91c('0x30')](_0x3b1837,function(_0x3127a9){if(_0x3127a9[_0xc91c('0x34')]!==_0xc91c('0x43')){var _0x4ebca0={};_0x4ebca0[_0x3127a9['name']]={'$like':'%'+_0x56ebea[_0xc91c('0x37')][_0xc91c('0x41')]+'%'};return _0x4ebca0;}})});}_0x2d7fca=_[_0xc91c('0x42')]({},_0x2d7fca,_0x56ebea[_0xc91c('0x44')]);var _0x1c8119={'where':_0x2d7fca['where']};return db[_0xc91c('0x31')][_0xc91c('0x1e')](_0x1c8119)[_0xc91c('0x26')](function(_0x4c43ce){_0x34d898['count']=_0x4c43ce;if(_0x56ebea['query'][_0xc91c('0x45')]){_0x2d7fca[_0xc91c('0x46')]=[{'all':!![]}];}return db[_0xc91c('0x31')][_0xc91c('0x47')](_0x2d7fca);})[_0xc91c('0x26')](function(_0x2031e7){_0x34d898[_0xc91c('0x48')]=_0x2031e7;return _0x34d898;})[_0xc91c('0x26')](respondWithFilteredResult(_0x41a931,_0x2d7fca))[_0xc91c('0x49')](handleError(_0x41a931,null));};exports[_0xc91c('0x4a')]=function(_0x40c47f,_0x668f42){var _0x4e936c={'raw':![],'where':{'id':_0x40c47f[_0xc91c('0x4b')]['id']}},_0x57000d={};_0x57000d[_0xc91c('0x36')]=_[_0xc91c('0x38')](db[_0xc91c('0x31')][_0xc91c('0x32')]);_0x57000d[_0xc91c('0x37')]=_[_0xc91c('0x38')](_0x40c47f[_0xc91c('0x37')]);_0x57000d[_0xc91c('0x39')]=_[_0xc91c('0x3a')](_0x57000d[_0xc91c('0x36')],_0x57000d[_0xc91c('0x37')]);_0x4e936c[_0xc91c('0x3b')]=_[_0xc91c('0x3a')](_0x57000d[_0xc91c('0x36')],qs[_0xc91c('0x4c')](_0x40c47f['query']['fields']));_0x4e936c['attributes']=_0x4e936c[_0xc91c('0x3b')][_0xc91c('0x3c')]?_0x4e936c['attributes']:_0x57000d['model'];if(_0x40c47f[_0xc91c('0x37')][_0xc91c('0x45')]){_0x4e936c['include']=[{'all':!![]}];}_0x4e936c=_[_0xc91c('0x42')]({},_0x4e936c,_0x40c47f[_0xc91c('0x44')]);return db[_0xc91c('0x31')][_0xc91c('0x4d')](_0x4e936c)[_0xc91c('0x26')](handleEntityNotFound(_0x668f42,null))[_0xc91c('0x26')](respondWithResult(_0x668f42,null))['catch'](handleError(_0x668f42,null));};exports[_0xc91c('0x4e')]=function(_0x5ff30b,_0x5b94b1){return db['Schedule'][_0xc91c('0x4e')](_0x5ff30b[_0xc91c('0x4f')],{})[_0xc91c('0x26')](function(_0x3bb9a9){var _0x4ec812=_0x5ff30b[_0xc91c('0x50')][_0xc91c('0x27')]({'plain':!![]});if(!_0x4ec812)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x4ec812[_0xc91c('0x51')]===_0xc91c('0x50')){var _0x288655=_0x3bb9a9[_0xc91c('0x27')]({'plain':!![]});var _0x4025ec=_0xc91c('0x28');return db[_0xc91c('0x52')][_0xc91c('0x4d')]({'where':{'name':_0x4025ec,'userProfileId':_0x4ec812[_0xc91c('0x53')]},'raw':!![]})[_0xc91c('0x26')](function(_0x2469ee){if(_0x2469ee&&_0x2469ee[_0xc91c('0x54')]===0x0){return db['UserProfileResource'][_0xc91c('0x4e')]({'name':_0x288655['name'],'resourceId':_0x288655['id'],'type':_0x2469ee[_0xc91c('0x2d')],'sectionId':_0x2469ee['id']},{})[_0xc91c('0x26')](function(){return _0x3bb9a9;});}else{return _0x3bb9a9;}})[_0xc91c('0x49')](function(_0x3b2760){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3b2760);throw _0x3b2760;});}return _0x3bb9a9;})[_0xc91c('0x26')](respondWithResult(_0x5b94b1,0xc9))[_0xc91c('0x49')](handleError(_0x5b94b1,null));};exports['update']=function(_0x49f716,_0x4f2574){if(_0x49f716[_0xc91c('0x4f')]['id']){delete _0x49f716[_0xc91c('0x4f')]['id'];}return db['Schedule']['find']({'where':{'id':_0x49f716[_0xc91c('0x4b')]['id']}})[_0xc91c('0x26')](handleEntityNotFound(_0x4f2574,null))[_0xc91c('0x26')](saveUpdates(_0x49f716[_0xc91c('0x4f')],null))[_0xc91c('0x26')](respondWithResult(_0x4f2574,null))[_0xc91c('0x49')](handleError(_0x4f2574,null));};exports[_0xc91c('0x2a')]=function(_0x2c8d03,_0xea670a){return db[_0xc91c('0x31')][_0xc91c('0x4d')]({'where':{'id':_0x2c8d03[_0xc91c('0x4b')]['id']}})[_0xc91c('0x26')](handleEntityNotFound(_0xea670a,null))[_0xc91c('0x26')](removeEntity(_0xea670a,null))['catch'](handleError(_0xea670a,null));};exports['run']=function(_0x405458,_0x12f70f,_0x448116){var _0x861ac1=require(_0xc91c('0x55'));return db[_0xc91c('0x31')][_0xc91c('0x4d')]({'where':{'id':_0x405458[_0xc91c('0x4b')]['id']},'raw':!![]})[_0xc91c('0x26')](handleEntityNotFound(_0x12f70f,null))[_0xc91c('0x26')](function(){_0x861ac1['run'](_0x405458[_0xc91c('0x4b')]['id']);})[_0xc91c('0x26')](respondWithStatusCode(_0x12f70f,null))[_0xc91c('0x49')](handleError(_0x12f70f,null));};