c61eebcaca01c10b62fde13c7bbd8eb99e434f51
[motion2.git] / server / api / disposition / disposition.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 _0x69f8=['ejs','fs-extra','squel','crypto','jsforce','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','then','destroy','get','Dispositions','error','stack','name','send','index','Disposition','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','merge','map','VIRTUAL','options','include','findAll','rows','show','params','rawAttributes','includeAll','find','catch','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv'];(function(_0x2c8caa,_0x74fa11){var _0xddbe98=function(_0x267774){while(--_0x267774){_0x2c8caa['push'](_0x2c8caa['shift']());}};_0xddbe98(++_0x74fa11);}(_0x69f8,0x14c));var _0x869f=function(_0x88dc75,_0x526f60){_0x88dc75=_0x88dc75-0x0;var _0x5b8282=_0x69f8[_0x88dc75];return _0x5b8282;};'use strict';var emlformat=require(_0x869f('0x0'));var rimraf=require(_0x869f('0x1'));var zipdir=require(_0x869f('0x2'));var jsonpatch=require(_0x869f('0x3'));var rp=require(_0x869f('0x4'));var moment=require(_0x869f('0x5'));var BPromise=require(_0x869f('0x6'));var Mustache=require(_0x869f('0x7'));var util=require(_0x869f('0x8'));var path=require(_0x869f('0x9'));var sox=require(_0x869f('0xa'));var csv=require(_0x869f('0xb'));var ejs=require(_0x869f('0xc'));var fs=require('fs');var fs_extra=require(_0x869f('0xd'));var _=require('lodash');var squel=require(_0x869f('0xe'));var crypto=require(_0x869f('0xf'));var jsforce=require(_0x869f('0x10'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x869f('0x11'));var Papa=require('papaparse');var Redis=require(_0x869f('0x12'));var authService=require(_0x869f('0x13'));var qs=require(_0x869f('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x869f('0x15'));var logger=require('../../config/logger')(_0x869f('0x16'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x869f('0x17'));var db=require(_0x869f('0x18'))['db'];function respondWithStatusCode(_0x1afee7,_0x1d511b){_0x1d511b=_0x1d511b||0xcc;return function(_0x3bdca3){if(_0x3bdca3){return _0x1afee7[_0x869f('0x19')](_0x1d511b);}return _0x1afee7[_0x869f('0x1a')](_0x1d511b)[_0x869f('0x1b')]();};}function respondWithResult(_0x523855,_0x181817){_0x181817=_0x181817||0xc8;return function(_0x23a62b){if(_0x23a62b){return _0x523855[_0x869f('0x1a')](_0x181817)[_0x869f('0x1c')](_0x23a62b);}};}function respondWithFilteredResult(_0x213dca,_0x10ad23){return function(_0x7acd9e){if(_0x7acd9e){var _0x38290e=typeof _0x10ad23[_0x869f('0x1d')]===_0x869f('0x1e')&&typeof _0x10ad23[_0x869f('0x1f')]===_0x869f('0x1e');var _0x21b043=_0x7acd9e[_0x869f('0x20')];var _0x28c0a8=_0x38290e?0x0:_0x10ad23[_0x869f('0x1d')];var _0x1e0d75=_0x38290e?_0x7acd9e[_0x869f('0x20')]:_0x10ad23[_0x869f('0x1d')]+_0x10ad23[_0x869f('0x1f')];var _0x1078d6;if(_0x1e0d75>=_0x21b043){_0x1e0d75=_0x21b043;_0x1078d6=0xc8;}else{_0x1078d6=0xce;}_0x213dca[_0x869f('0x1a')](_0x1078d6);return _0x213dca[_0x869f('0x21')](_0x869f('0x22'),_0x28c0a8+'-'+_0x1e0d75+'/'+_0x21b043)[_0x869f('0x1c')](_0x7acd9e);}return null;};}function patchUpdates(_0x34cff7){return function(_0x5d1e4f){try{jsonpatch['apply'](_0x5d1e4f,_0x34cff7,!![]);}catch(_0x2689ca){return BPromise[_0x869f('0x23')](_0x2689ca);}return _0x5d1e4f[_0x869f('0x24')]();};}function saveUpdates(_0x41e75d,_0x58c941){return function(_0x256e3e){if(_0x256e3e){return _0x256e3e[_0x869f('0x25')](_0x41e75d)[_0x869f('0x26')](function(_0x25e8c4){return _0x25e8c4;});}return null;};}function removeEntity(_0x3a9ea7,_0x3dbdf5){return function(_0x3bf910){if(_0x3bf910){return _0x3bf910[_0x869f('0x27')]()[_0x869f('0x26')](function(){var _0x2ccf57=_0x3bf910[_0x869f('0x28')]({'plain':!![]});var _0x52f06d=_0x869f('0x29');return db['UserProfileResource'][_0x869f('0x27')]({'where':{'type':_0x52f06d,'resourceId':_0x2ccf57['id']}})[_0x869f('0x26')](function(){return _0x3bf910;});})['then'](function(){_0x3a9ea7['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x3380d2,_0x2361ec){return function(_0x121709){if(!_0x121709){_0x3380d2[_0x869f('0x19')](0x194);}return _0x121709;};}function handleError(_0x44810c,_0x25a039){_0x25a039=_0x25a039||0x1f4;return function(_0x6b475){logger[_0x869f('0x2a')](_0x6b475[_0x869f('0x2b')]);if(_0x6b475[_0x869f('0x2c')]){delete _0x6b475[_0x869f('0x2c')];}_0x44810c['status'](_0x25a039)[_0x869f('0x2d')](_0x6b475);};}exports[_0x869f('0x2e')]=function(_0x2993a4,_0x417720){var _0x61f09b={},_0x4628c0={},_0x2bf8bb={'count':0x0,'rows':[]};var _0x1f9792=_['map'](db[_0x869f('0x2f')]['rawAttributes'],function(_0x41de15){return{'name':_0x41de15[_0x869f('0x30')],'type':_0x41de15[_0x869f('0x31')][_0x869f('0x32')]};});_0x4628c0[_0x869f('0x33')]=_['map'](_0x1f9792,_0x869f('0x2c'));_0x4628c0[_0x869f('0x34')]=_[_0x869f('0x35')](_0x2993a4[_0x869f('0x34')]);_0x4628c0[_0x869f('0x36')]=_[_0x869f('0x37')](_0x4628c0[_0x869f('0x33')],_0x4628c0[_0x869f('0x34')]);_0x61f09b[_0x869f('0x38')]=_[_0x869f('0x37')](_0x4628c0[_0x869f('0x33')],qs[_0x869f('0x39')](_0x2993a4[_0x869f('0x34')]['fields']));_0x61f09b[_0x869f('0x38')]=_0x61f09b[_0x869f('0x38')][_0x869f('0x3a')]?_0x61f09b[_0x869f('0x38')]:_0x4628c0[_0x869f('0x33')];if(!_0x2993a4['query'][_0x869f('0x3b')]('nolimit')){_0x61f09b[_0x869f('0x1f')]=qs[_0x869f('0x1f')](_0x2993a4[_0x869f('0x34')][_0x869f('0x1f')]);_0x61f09b[_0x869f('0x1d')]=qs[_0x869f('0x1d')](_0x2993a4[_0x869f('0x34')][_0x869f('0x1d')]);}_0x61f09b[_0x869f('0x3c')]=qs[_0x869f('0x3d')](_0x2993a4[_0x869f('0x34')][_0x869f('0x3d')]);_0x61f09b[_0x869f('0x3e')]=qs[_0x869f('0x36')](_[_0x869f('0x3f')](_0x2993a4['query'],_0x4628c0[_0x869f('0x36')]),_0x1f9792);if(_0x2993a4['query']['filter']){_0x61f09b[_0x869f('0x3e')]=_[_0x869f('0x40')](_0x61f09b[_0x869f('0x3e')],{'$or':_[_0x869f('0x41')](_0x1f9792,function(_0x577c7a){if(_0x577c7a[_0x869f('0x31')]!==_0x869f('0x42')){var _0x299964={};_0x299964[_0x577c7a[_0x869f('0x2c')]]={'$like':'%'+_0x2993a4[_0x869f('0x34')]['filter']+'%'};return _0x299964;}})});}_0x61f09b=_[_0x869f('0x40')]({},_0x61f09b,_0x2993a4[_0x869f('0x43')]);var _0x4fd91c={'where':_0x61f09b[_0x869f('0x3e')]};return db[_0x869f('0x2f')][_0x869f('0x20')](_0x4fd91c)[_0x869f('0x26')](function(_0x460785){_0x2bf8bb['count']=_0x460785;if(_0x2993a4['query']['includeAll']){_0x61f09b[_0x869f('0x44')]=[{'all':!![]}];}return db[_0x869f('0x2f')][_0x869f('0x45')](_0x61f09b);})[_0x869f('0x26')](function(_0x1245dd){_0x2bf8bb[_0x869f('0x46')]=_0x1245dd;return _0x2bf8bb;})['then'](respondWithFilteredResult(_0x417720,_0x61f09b))['catch'](handleError(_0x417720,null));};exports[_0x869f('0x47')]=function(_0xf6ba4,_0x1b03f9){var _0x2ca1dc={'raw':!![],'where':{'id':_0xf6ba4[_0x869f('0x48')]['id']}},_0x14077c={};_0x14077c[_0x869f('0x33')]=_[_0x869f('0x35')](db['Disposition'][_0x869f('0x49')]);_0x14077c[_0x869f('0x34')]=_['keys'](_0xf6ba4['query']);_0x14077c['filters']=_['intersection'](_0x14077c['model'],_0x14077c[_0x869f('0x34')]);_0x2ca1dc[_0x869f('0x38')]=_[_0x869f('0x37')](_0x14077c[_0x869f('0x33')],qs[_0x869f('0x39')](_0xf6ba4['query'][_0x869f('0x39')]));_0x2ca1dc[_0x869f('0x38')]=_0x2ca1dc[_0x869f('0x38')][_0x869f('0x3a')]?_0x2ca1dc['attributes']:_0x14077c['model'];if(_0xf6ba4[_0x869f('0x34')][_0x869f('0x4a')]){_0x2ca1dc[_0x869f('0x44')]=[{'all':!![]}];}_0x2ca1dc=_['merge']({},_0x2ca1dc,_0xf6ba4[_0x869f('0x43')]);return db['Disposition'][_0x869f('0x4b')](_0x2ca1dc)[_0x869f('0x26')](handleEntityNotFound(_0x1b03f9,null))[_0x869f('0x26')](respondWithResult(_0x1b03f9,null))[_0x869f('0x4c')](handleError(_0x1b03f9,null));};exports[_0x869f('0x4d')]=function(_0x5228c7,_0x19c1d8){return db['Disposition'][_0x869f('0x4d')](_0x5228c7[_0x869f('0x4e')],{})[_0x869f('0x26')](function(_0x3b38b0){var _0x5e5c59=_0x5228c7[_0x869f('0x4f')][_0x869f('0x28')]({'plain':!![]});if(!_0x5e5c59)throw new Error(_0x869f('0x50'));if(_0x5e5c59['role']===_0x869f('0x4f')){var _0x555d9a=_0x3b38b0[_0x869f('0x28')]({'plain':!![]});var _0x3712c4=_0x869f('0x29');return db[_0x869f('0x51')][_0x869f('0x4b')]({'where':{'name':_0x3712c4,'userProfileId':_0x5e5c59[_0x869f('0x52')]},'raw':!![]})[_0x869f('0x26')](function(_0x45bb7e){if(_0x45bb7e&&_0x45bb7e[_0x869f('0x53')]===0x0){return db[_0x869f('0x54')]['create']({'name':_0x555d9a[_0x869f('0x2c')],'resourceId':_0x555d9a['id'],'type':_0x45bb7e[_0x869f('0x2c')],'sectionId':_0x45bb7e['id']},{})['then'](function(){return _0x3b38b0;});}else{return _0x3b38b0;}})[_0x869f('0x4c')](function(_0x59ba43){logger[_0x869f('0x2a')](_0x869f('0x55'),_0x59ba43);throw _0x59ba43;});}return _0x3b38b0;})[_0x869f('0x26')](respondWithResult(_0x19c1d8,0xc9))[_0x869f('0x4c')](handleError(_0x19c1d8,null));};exports[_0x869f('0x25')]=function(_0x2dc888,_0x3c5d03){if(_0x2dc888[_0x869f('0x4e')]['id']){delete _0x2dc888[_0x869f('0x4e')]['id'];}return db[_0x869f('0x2f')][_0x869f('0x4b')]({'where':{'id':_0x2dc888['params']['id']}})[_0x869f('0x26')](handleEntityNotFound(_0x3c5d03,null))[_0x869f('0x26')](saveUpdates(_0x2dc888[_0x869f('0x4e')],null))['then'](respondWithResult(_0x3c5d03,null))['catch'](handleError(_0x3c5d03,null));};exports[_0x869f('0x27')]=function(_0x540825,_0x477773){return db[_0x869f('0x2f')]['find']({'where':{'id':_0x540825['params']['id']}})[_0x869f('0x26')](handleEntityNotFound(_0x477773,null))['then'](removeEntity(_0x477773,null))[_0x869f('0x4c')](handleError(_0x477773,null));};