61398bb3bd4298a2d5a77bd6f8e879857eb7dedb
[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 _0xa3cf=['keys','filters','intersection','attributes','model','fields','hasOwnProperty','nolimit','order','sort','pick','where','VIRTUAL','filter','merge','options','findAll','rows','catch','show','params','length','include','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','Scheduler','find','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','run','../../config/schedule','eml-format','rimraf','zip-dir','moment','bluebird','mustache','path','sox','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','update','then','get','UserProfileResource','destroy','sendStatus','error','stack','name','index','map','Schedule','rawAttributes','fieldName','type','key','query'];(function(_0x532e5a,_0x1a223c){var _0x8967ea=function(_0x49a7dc){while(--_0x49a7dc){_0x532e5a['push'](_0x532e5a['shift']());}};_0x8967ea(++_0x1a223c);}(_0xa3cf,0x117));var _0xfa3c=function(_0x3b619b,_0x11c639){_0x3b619b=_0x3b619b-0x0;var _0x539d8a=_0xa3cf[_0x3b619b];return _0x539d8a;};'use strict';var emlformat=require(_0xfa3c('0x0'));var rimraf=require(_0xfa3c('0x1'));var zipdir=require(_0xfa3c('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xfa3c('0x3'));var BPromise=require(_0xfa3c('0x4'));var Mustache=require(_0xfa3c('0x5'));var util=require('util');var path=require(_0xfa3c('0x6'));var sox=require(_0xfa3c('0x7'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xfa3c('0x8'));var squel=require(_0xfa3c('0x9'));var crypto=require(_0xfa3c('0xa'));var jsforce=require('jsforce');var deskjs=require(_0xfa3c('0xb'));var toCsv=require('to-csv');var querystring=require(_0xfa3c('0xc'));var Papa=require(_0xfa3c('0xd'));var Redis=require(_0xfa3c('0xe'));var authService=require(_0xfa3c('0xf'));var qs=require(_0xfa3c('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xfa3c('0x11'));var logger=require(_0xfa3c('0x12'))('api');var utils=require(_0xfa3c('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0xfa3c('0x14'));var db=require(_0xfa3c('0x15'))['db'];function respondWithStatusCode(_0x4193f7,_0x1537b2){_0x1537b2=_0x1537b2||0xcc;return function(_0x232662){if(_0x232662){return _0x4193f7['sendStatus'](_0x1537b2);}return _0x4193f7[_0xfa3c('0x16')](_0x1537b2)[_0xfa3c('0x17')]();};}function respondWithResult(_0x267d30,_0x3dcfd0){_0x3dcfd0=_0x3dcfd0||0xc8;return function(_0x46371d){if(_0x46371d){return _0x267d30[_0xfa3c('0x16')](_0x3dcfd0)[_0xfa3c('0x18')](_0x46371d);}};}function respondWithFilteredResult(_0x2c12e6,_0x1d6a98){return function(_0x2e9a7b){if(_0x2e9a7b){var _0x29f32b=typeof _0x1d6a98[_0xfa3c('0x19')]===_0xfa3c('0x1a')&&typeof _0x1d6a98[_0xfa3c('0x1b')]===_0xfa3c('0x1a');var _0x2f46a2=_0x2e9a7b[_0xfa3c('0x1c')];var _0x3ef13f=_0x29f32b?0x0:_0x1d6a98[_0xfa3c('0x19')];var _0x386b2f=_0x29f32b?_0x2e9a7b[_0xfa3c('0x1c')]:_0x1d6a98[_0xfa3c('0x19')]+_0x1d6a98['limit'];var _0x1a6b2d;if(_0x386b2f>=_0x2f46a2){_0x386b2f=_0x2f46a2;_0x1a6b2d=0xc8;}else{_0x1a6b2d=0xce;}_0x2c12e6[_0xfa3c('0x16')](_0x1a6b2d);return _0x2c12e6[_0xfa3c('0x1d')](_0xfa3c('0x1e'),_0x3ef13f+'-'+_0x386b2f+'/'+_0x2f46a2)[_0xfa3c('0x18')](_0x2e9a7b);}return null;};}function patchUpdates(_0x3eba24){return function(_0xdb3c8f){try{jsonpatch['apply'](_0xdb3c8f,_0x3eba24,!![]);}catch(_0xa779b1){return BPromise[_0xfa3c('0x1f')](_0xa779b1);}return _0xdb3c8f['save']();};}function saveUpdates(_0x253097,_0x429656){return function(_0x5e621c){if(_0x5e621c){return _0x5e621c[_0xfa3c('0x20')](_0x253097)[_0xfa3c('0x21')](function(_0x7eed63){return _0x7eed63;});}return null;};}function removeEntity(_0x3602a4,_0x12a3ec){return function(_0x57d244){if(_0x57d244){return _0x57d244['destroy']()[_0xfa3c('0x21')](function(){var _0x4d4798=_0x57d244[_0xfa3c('0x22')]({'plain':!![]});var _0x48fe39='Scheduler';return db[_0xfa3c('0x23')][_0xfa3c('0x24')]({'where':{'type':_0x48fe39,'resourceId':_0x4d4798['id']}})['then'](function(){return _0x57d244;});})[_0xfa3c('0x21')](function(){_0x3602a4[_0xfa3c('0x16')](0xcc)[_0xfa3c('0x17')]();});}};}function handleEntityNotFound(_0x38453c,_0x97cdac){return function(_0x2aec5e){if(!_0x2aec5e){_0x38453c[_0xfa3c('0x25')](0x194);}return _0x2aec5e;};}function handleError(_0xaa3d25,_0x57475b){_0x57475b=_0x57475b||0x1f4;return function(_0x4c1256){logger[_0xfa3c('0x26')](_0x4c1256[_0xfa3c('0x27')]);if(_0x4c1256[_0xfa3c('0x28')]){delete _0x4c1256[_0xfa3c('0x28')];}_0xaa3d25['status'](_0x57475b)['send'](_0x4c1256);};}exports[_0xfa3c('0x29')]=function(_0x47a26d,_0x2d135d){var _0x4b690e={},_0x3f6953={},_0x9ca8c4={'count':0x0,'rows':[]};var _0x30de68=_[_0xfa3c('0x2a')](db[_0xfa3c('0x2b')][_0xfa3c('0x2c')],function(_0x12d379){return{'name':_0x12d379[_0xfa3c('0x2d')],'type':_0x12d379[_0xfa3c('0x2e')][_0xfa3c('0x2f')]};});_0x3f6953['model']=_['map'](_0x30de68,_0xfa3c('0x28'));_0x3f6953[_0xfa3c('0x30')]=_[_0xfa3c('0x31')](_0x47a26d[_0xfa3c('0x30')]);_0x3f6953[_0xfa3c('0x32')]=_[_0xfa3c('0x33')](_0x3f6953['model'],_0x3f6953[_0xfa3c('0x30')]);_0x4b690e[_0xfa3c('0x34')]=_[_0xfa3c('0x33')](_0x3f6953[_0xfa3c('0x35')],qs[_0xfa3c('0x36')](_0x47a26d[_0xfa3c('0x30')][_0xfa3c('0x36')]));_0x4b690e['attributes']=_0x4b690e['attributes']['length']?_0x4b690e[_0xfa3c('0x34')]:_0x3f6953[_0xfa3c('0x35')];if(!_0x47a26d[_0xfa3c('0x30')][_0xfa3c('0x37')](_0xfa3c('0x38'))){_0x4b690e['limit']=qs['limit'](_0x47a26d[_0xfa3c('0x30')][_0xfa3c('0x1b')]);_0x4b690e['offset']=qs['offset'](_0x47a26d[_0xfa3c('0x30')][_0xfa3c('0x19')]);}_0x4b690e[_0xfa3c('0x39')]=qs[_0xfa3c('0x3a')](_0x47a26d['query'][_0xfa3c('0x3a')]);_0x4b690e['where']=qs[_0xfa3c('0x32')](_[_0xfa3c('0x3b')](_0x47a26d[_0xfa3c('0x30')],_0x3f6953[_0xfa3c('0x32')]),_0x30de68);if(_0x47a26d[_0xfa3c('0x30')]['filter']){_0x4b690e[_0xfa3c('0x3c')]=_['merge'](_0x4b690e[_0xfa3c('0x3c')],{'$or':_[_0xfa3c('0x2a')](_0x30de68,function(_0x13a0a5){if(_0x13a0a5[_0xfa3c('0x2e')]!==_0xfa3c('0x3d')){var _0xc3de95={};_0xc3de95[_0x13a0a5[_0xfa3c('0x28')]]={'$like':'%'+_0x47a26d[_0xfa3c('0x30')][_0xfa3c('0x3e')]+'%'};return _0xc3de95;}})});}_0x4b690e=_[_0xfa3c('0x3f')]({},_0x4b690e,_0x47a26d[_0xfa3c('0x40')]);var _0xc9dc00={'where':_0x4b690e[_0xfa3c('0x3c')]};return db[_0xfa3c('0x2b')]['count'](_0xc9dc00)['then'](function(_0x59efd0){_0x9ca8c4[_0xfa3c('0x1c')]=_0x59efd0;if(_0x47a26d[_0xfa3c('0x30')]['includeAll']){_0x4b690e['include']=[{'all':!![]}];}return db[_0xfa3c('0x2b')][_0xfa3c('0x41')](_0x4b690e);})[_0xfa3c('0x21')](function(_0x2497f2){_0x9ca8c4[_0xfa3c('0x42')]=_0x2497f2;return _0x9ca8c4;})[_0xfa3c('0x21')](respondWithFilteredResult(_0x2d135d,_0x4b690e))[_0xfa3c('0x43')](handleError(_0x2d135d,null));};exports[_0xfa3c('0x44')]=function(_0x2df138,_0x1b0864){var _0x117778={'raw':![],'where':{'id':_0x2df138[_0xfa3c('0x45')]['id']}},_0x534b25={};_0x534b25[_0xfa3c('0x35')]=_[_0xfa3c('0x31')](db[_0xfa3c('0x2b')]['rawAttributes']);_0x534b25[_0xfa3c('0x30')]=_[_0xfa3c('0x31')](_0x2df138[_0xfa3c('0x30')]);_0x534b25[_0xfa3c('0x32')]=_[_0xfa3c('0x33')](_0x534b25[_0xfa3c('0x35')],_0x534b25[_0xfa3c('0x30')]);_0x117778[_0xfa3c('0x34')]=_[_0xfa3c('0x33')](_0x534b25['model'],qs[_0xfa3c('0x36')](_0x2df138['query'][_0xfa3c('0x36')]));_0x117778['attributes']=_0x117778['attributes'][_0xfa3c('0x46')]?_0x117778[_0xfa3c('0x34')]:_0x534b25['model'];if(_0x2df138[_0xfa3c('0x30')]['includeAll']){_0x117778[_0xfa3c('0x47')]=[{'all':!![]}];}_0x117778=_[_0xfa3c('0x3f')]({},_0x117778,_0x2df138[_0xfa3c('0x40')]);return db[_0xfa3c('0x2b')]['find'](_0x117778)[_0xfa3c('0x21')](handleEntityNotFound(_0x1b0864,null))[_0xfa3c('0x21')](respondWithResult(_0x1b0864,null))[_0xfa3c('0x43')](handleError(_0x1b0864,null));};exports[_0xfa3c('0x48')]=function(_0x3cbb37,_0xa1b411){return db['Schedule']['create'](_0x3cbb37['body'],{})[_0xfa3c('0x21')](function(_0x210bae){var _0x10529b=_0x3cbb37[_0xfa3c('0x49')][_0xfa3c('0x22')]({'plain':!![]});if(!_0x10529b)throw new Error(_0xfa3c('0x4a'));if(_0x10529b['role']===_0xfa3c('0x49')){var _0x1ecf91=_0x210bae[_0xfa3c('0x22')]({'plain':!![]});var _0x3b329f=_0xfa3c('0x4b');return db['UserProfileSection'][_0xfa3c('0x4c')]({'where':{'name':_0x3b329f,'userProfileId':_0x10529b['userProfileId']},'raw':!![]})[_0xfa3c('0x21')](function(_0x50b4d0){if(_0x50b4d0&&_0x50b4d0[_0xfa3c('0x4d')]===0x0){return db[_0xfa3c('0x23')]['create']({'name':_0x1ecf91[_0xfa3c('0x28')],'resourceId':_0x1ecf91['id'],'type':_0x50b4d0[_0xfa3c('0x28')],'sectionId':_0x50b4d0['id']},{})['then'](function(){return _0x210bae;});}else{return _0x210bae;}})[_0xfa3c('0x43')](function(_0x1a646c){logger[_0xfa3c('0x26')](_0xfa3c('0x4e'),_0x1a646c);throw _0x1a646c;});}return _0x210bae;})[_0xfa3c('0x21')](respondWithResult(_0xa1b411,0xc9))[_0xfa3c('0x43')](handleError(_0xa1b411,null));};exports[_0xfa3c('0x20')]=function(_0x2f53b0,_0x1ddfbe){if(_0x2f53b0['body']['id']){delete _0x2f53b0['body']['id'];}return db['Schedule'][_0xfa3c('0x4c')]({'where':{'id':_0x2f53b0[_0xfa3c('0x45')]['id']}})[_0xfa3c('0x21')](handleEntityNotFound(_0x1ddfbe,null))[_0xfa3c('0x21')](saveUpdates(_0x2f53b0[_0xfa3c('0x4f')],null))[_0xfa3c('0x21')](respondWithResult(_0x1ddfbe,null))[_0xfa3c('0x43')](handleError(_0x1ddfbe,null));};exports[_0xfa3c('0x24')]=function(_0x518f9e,_0x4276f3){return db[_0xfa3c('0x2b')][_0xfa3c('0x4c')]({'where':{'id':_0x518f9e[_0xfa3c('0x45')]['id']}})['then'](handleEntityNotFound(_0x4276f3,null))[_0xfa3c('0x21')](removeEntity(_0x4276f3,null))[_0xfa3c('0x43')](handleError(_0x4276f3,null));};exports[_0xfa3c('0x50')]=function(_0x208614,_0x3fb273,_0x522a3f){var _0x4c4cef=require(_0xfa3c('0x51'));return db[_0xfa3c('0x2b')][_0xfa3c('0x4c')]({'where':{'id':_0x208614[_0xfa3c('0x45')]['id']},'raw':!![]})[_0xfa3c('0x21')](handleEntityNotFound(_0x3fb273,null))[_0xfa3c('0x21')](function(){_0x4c4cef[_0xfa3c('0x50')](_0x208614[_0xfa3c('0x45')]['id']);})['then'](respondWithStatusCode(_0x3fb273,null))['catch'](handleError(_0x3fb273,null));};