963a4dc2d09eebb98d60cbb287defeb7c612b14e
[motion2.git] / server / api / voiceMail / voiceMail.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 _0xab40=['intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','map','type','VIRTUAL','VoiceMail','count','include','findAll','rows','catch','show','params','length','includeAll','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Voicemails','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','getMessages','findOne','mailbox','VoiceMailMessage','findAndCountAll','eml-format','rimraf','moment','bluebird','mustache','util','path','ejs','lodash','squel','crypto','jsforce','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','apply','reject','save','then','get','UserProfileResource','destroy','error','stack','name','rawAttributes','fieldName','model','query','keys','filters'];(function(_0x811031,_0x438b90){var _0x186997=function(_0x6e29fd){while(--_0x6e29fd){_0x811031['push'](_0x811031['shift']());}};_0x186997(++_0x438b90);}(_0xab40,0xcd));var _0x0ab4=function(_0x3b233d,_0x2daa31){_0x3b233d=_0x3b233d-0x0;var _0x48c1c0=_0xab40[_0x3b233d];return _0x48c1c0;};'use strict';var emlformat=require(_0x0ab4('0x0'));var rimraf=require(_0x0ab4('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x0ab4('0x2'));var BPromise=require(_0x0ab4('0x3'));var Mustache=require(_0x0ab4('0x4'));var util=require(_0x0ab4('0x5'));var path=require(_0x0ab4('0x6'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x0ab4('0x7'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x0ab4('0x8'));var squel=require(_0x0ab4('0x9'));var crypto=require(_0x0ab4('0xa'));var jsforce=require(_0x0ab4('0xb'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x0ab4('0xc'));var Redis=require(_0x0ab4('0xd'));var authService=require(_0x0ab4('0xe'));var qs=require(_0x0ab4('0xf'));var as=require(_0x0ab4('0x10'));var hardwareService=require(_0x0ab4('0x11'));var logger=require(_0x0ab4('0x12'))(_0x0ab4('0x13'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x0ab4('0x14'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x249dc3,_0x45e6fd){_0x45e6fd=_0x45e6fd||0xcc;return function(_0x1e7555){if(_0x1e7555){return _0x249dc3[_0x0ab4('0x15')](_0x45e6fd);}return _0x249dc3[_0x0ab4('0x16')](_0x45e6fd)[_0x0ab4('0x17')]();};}function respondWithResult(_0x112162,_0x2cc644){_0x2cc644=_0x2cc644||0xc8;return function(_0x3cb1df){if(_0x3cb1df){return _0x112162[_0x0ab4('0x16')](_0x2cc644)[_0x0ab4('0x18')](_0x3cb1df);}};}function respondWithFilteredResult(_0x36d246,_0x293e20){return function(_0x45fad1){if(_0x45fad1){var _0x2a83a8=typeof _0x293e20[_0x0ab4('0x19')]===_0x0ab4('0x1a')&&typeof _0x293e20[_0x0ab4('0x1b')]==='undefined';var _0x5000d4=_0x45fad1['count'];var _0x17422f=_0x2a83a8?0x0:_0x293e20[_0x0ab4('0x19')];var _0x2f50d2=_0x2a83a8?_0x45fad1['count']:_0x293e20[_0x0ab4('0x19')]+_0x293e20[_0x0ab4('0x1b')];var _0x212ebd;if(_0x2f50d2>=_0x5000d4){_0x2f50d2=_0x5000d4;_0x212ebd=0xc8;}else{_0x212ebd=0xce;}_0x36d246[_0x0ab4('0x16')](_0x212ebd);return _0x36d246['set']('Content-Range',_0x17422f+'-'+_0x2f50d2+'/'+_0x5000d4)[_0x0ab4('0x18')](_0x45fad1);}return null;};}function patchUpdates(_0x5b1ed5){return function(_0x2b0e29){try{jsonpatch[_0x0ab4('0x1c')](_0x2b0e29,_0x5b1ed5,!![]);}catch(_0x5bb1d0){return BPromise[_0x0ab4('0x1d')](_0x5bb1d0);}return _0x2b0e29[_0x0ab4('0x1e')]();};}function saveUpdates(_0x2c55b3,_0x1d5c14){return function(_0x12d06d){if(_0x12d06d){return _0x12d06d['update'](_0x2c55b3)['then'](function(_0x24b929){return _0x24b929;});}return null;};}function removeEntity(_0x14087f,_0x392afe){return function(_0x13c882){if(_0x13c882){return _0x13c882['destroy']()[_0x0ab4('0x1f')](function(){var _0x3caa9d=_0x13c882[_0x0ab4('0x20')]({'plain':!![]});var _0x4ea9aa='Voicemails';return db[_0x0ab4('0x21')][_0x0ab4('0x22')]({'where':{'type':_0x4ea9aa,'resourceId':_0x3caa9d['id']}})[_0x0ab4('0x1f')](function(){return _0x13c882;});})['then'](function(){_0x14087f['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x19ecc6,_0x5ba3a2){return function(_0x3bf844){if(!_0x3bf844){_0x19ecc6[_0x0ab4('0x15')](0x194);}return _0x3bf844;};}function handleError(_0x4f1aeb,_0x4bd747){_0x4bd747=_0x4bd747||0x1f4;return function(_0x4d0cc1){logger[_0x0ab4('0x23')](_0x4d0cc1[_0x0ab4('0x24')]);if(_0x4d0cc1[_0x0ab4('0x25')]){delete _0x4d0cc1[_0x0ab4('0x25')];}_0x4f1aeb[_0x0ab4('0x16')](_0x4bd747)['send'](_0x4d0cc1);};}exports['index']=function(_0x580d56,_0x2e7a97){var _0x2b66d5={},_0x155d96={},_0x182bbe={'count':0x0,'rows':[]};var _0x46eda7=_['map'](db['VoiceMail'][_0x0ab4('0x26')],function(_0xe6682){return{'name':_0xe6682[_0x0ab4('0x27')],'type':_0xe6682['type']['key']};});_0x155d96[_0x0ab4('0x28')]=_['map'](_0x46eda7,_0x0ab4('0x25'));_0x155d96[_0x0ab4('0x29')]=_[_0x0ab4('0x2a')](_0x580d56[_0x0ab4('0x29')]);_0x155d96[_0x0ab4('0x2b')]=_[_0x0ab4('0x2c')](_0x155d96['model'],_0x155d96[_0x0ab4('0x29')]);_0x2b66d5[_0x0ab4('0x2d')]=_[_0x0ab4('0x2c')](_0x155d96[_0x0ab4('0x28')],qs['fields'](_0x580d56['query'][_0x0ab4('0x2e')]));_0x2b66d5[_0x0ab4('0x2d')]=_0x2b66d5[_0x0ab4('0x2d')]['length']?_0x2b66d5['attributes']:_0x155d96['model'];if(!_0x580d56[_0x0ab4('0x29')][_0x0ab4('0x2f')](_0x0ab4('0x30'))){_0x2b66d5['limit']=qs['limit'](_0x580d56['query'][_0x0ab4('0x1b')]);_0x2b66d5[_0x0ab4('0x19')]=qs[_0x0ab4('0x19')](_0x580d56[_0x0ab4('0x29')][_0x0ab4('0x19')]);}_0x2b66d5[_0x0ab4('0x31')]=qs[_0x0ab4('0x32')](_0x580d56[_0x0ab4('0x29')][_0x0ab4('0x32')]);_0x2b66d5[_0x0ab4('0x33')]=qs['filters'](_[_0x0ab4('0x34')](_0x580d56[_0x0ab4('0x29')],_0x155d96[_0x0ab4('0x2b')]),_0x46eda7);if(_0x580d56[_0x0ab4('0x29')][_0x0ab4('0x35')]){_0x2b66d5[_0x0ab4('0x33')]=_[_0x0ab4('0x36')](_0x2b66d5[_0x0ab4('0x33')],{'$or':_[_0x0ab4('0x37')](_0x46eda7,function(_0x3774ca){if(_0x3774ca[_0x0ab4('0x38')]!==_0x0ab4('0x39')){var _0x4a05c4={};_0x4a05c4[_0x3774ca[_0x0ab4('0x25')]]={'$like':'%'+_0x580d56[_0x0ab4('0x29')][_0x0ab4('0x35')]+'%'};return _0x4a05c4;}})});}_0x2b66d5=_[_0x0ab4('0x36')]({},_0x2b66d5,_0x580d56['options']);var _0x42bece={'where':_0x2b66d5['where']};return db[_0x0ab4('0x3a')][_0x0ab4('0x3b')](_0x42bece)[_0x0ab4('0x1f')](function(_0x245021){_0x182bbe[_0x0ab4('0x3b')]=_0x245021;if(_0x580d56[_0x0ab4('0x29')]['includeAll']){_0x2b66d5[_0x0ab4('0x3c')]=[{'all':!![]}];}return db[_0x0ab4('0x3a')][_0x0ab4('0x3d')](_0x2b66d5);})['then'](function(_0x395e0b){_0x182bbe[_0x0ab4('0x3e')]=_0x395e0b;return _0x182bbe;})[_0x0ab4('0x1f')](respondWithFilteredResult(_0x2e7a97,_0x2b66d5))[_0x0ab4('0x3f')](handleError(_0x2e7a97,null));};exports[_0x0ab4('0x40')]=function(_0x5aa047,_0x5d70eb){var _0x498845={'raw':!![],'where':{'id':_0x5aa047[_0x0ab4('0x41')]['id']}},_0x2f54dd={};_0x2f54dd['model']=_[_0x0ab4('0x2a')](db['VoiceMail'][_0x0ab4('0x26')]);_0x2f54dd['query']=_[_0x0ab4('0x2a')](_0x5aa047[_0x0ab4('0x29')]);_0x2f54dd[_0x0ab4('0x2b')]=_[_0x0ab4('0x2c')](_0x2f54dd[_0x0ab4('0x28')],_0x2f54dd[_0x0ab4('0x29')]);_0x498845['attributes']=_[_0x0ab4('0x2c')](_0x2f54dd[_0x0ab4('0x28')],qs[_0x0ab4('0x2e')](_0x5aa047[_0x0ab4('0x29')][_0x0ab4('0x2e')]));_0x498845[_0x0ab4('0x2d')]=_0x498845['attributes'][_0x0ab4('0x42')]?_0x498845['attributes']:_0x2f54dd[_0x0ab4('0x28')];if(_0x5aa047[_0x0ab4('0x29')][_0x0ab4('0x43')]){_0x498845['include']=[{'all':!![]}];}_0x498845=_[_0x0ab4('0x36')]({},_0x498845,_0x5aa047[_0x0ab4('0x44')]);return db[_0x0ab4('0x3a')][_0x0ab4('0x45')](_0x498845)[_0x0ab4('0x1f')](handleEntityNotFound(_0x5d70eb,null))[_0x0ab4('0x1f')](respondWithResult(_0x5d70eb,null))['catch'](handleError(_0x5d70eb,null));};exports[_0x0ab4('0x46')]=function(_0xdcc118,_0x302894){return db[_0x0ab4('0x3a')][_0x0ab4('0x46')](_0xdcc118[_0x0ab4('0x47')],{})[_0x0ab4('0x1f')](function(_0x356bc9){var _0x22402e=_0xdcc118[_0x0ab4('0x48')][_0x0ab4('0x20')]({'plain':!![]});if(!_0x22402e)throw new Error(_0x0ab4('0x49'));if(_0x22402e[_0x0ab4('0x4a')]==='user'){var _0x2c3a0a=_0x356bc9[_0x0ab4('0x20')]({'plain':!![]});var _0x5800cc=_0x0ab4('0x4b');return db['UserProfileSection']['find']({'where':{'name':_0x5800cc,'userProfileId':_0x22402e[_0x0ab4('0x4c')]},'raw':!![]})[_0x0ab4('0x1f')](function(_0x156e22){if(_0x156e22&&_0x156e22['autoAssociation']===0x0){return db[_0x0ab4('0x21')][_0x0ab4('0x46')]({'name':_0x2c3a0a[_0x0ab4('0x25')],'resourceId':_0x2c3a0a['id'],'type':_0x156e22[_0x0ab4('0x25')],'sectionId':_0x156e22['id']},{})[_0x0ab4('0x1f')](function(){return _0x356bc9;});}else{return _0x356bc9;}})[_0x0ab4('0x3f')](function(_0x1eea3b){logger['error'](_0x0ab4('0x4d'),_0x1eea3b);throw _0x1eea3b;});}return _0x356bc9;})['then'](respondWithResult(_0x302894,0xc9))[_0x0ab4('0x3f')](handleError(_0x302894,null));};exports['update']=function(_0x2597f2,_0x5550ff){if(_0x2597f2['body']['id']){delete _0x2597f2[_0x0ab4('0x47')]['id'];}return db[_0x0ab4('0x3a')][_0x0ab4('0x45')]({'where':{'id':_0x2597f2[_0x0ab4('0x41')]['id']}})['then'](handleEntityNotFound(_0x5550ff,null))[_0x0ab4('0x1f')](saveUpdates(_0x2597f2[_0x0ab4('0x47')],null))[_0x0ab4('0x1f')](respondWithResult(_0x5550ff,null))[_0x0ab4('0x3f')](handleError(_0x5550ff,null));};exports[_0x0ab4('0x22')]=function(_0x2354fa,_0x134437){return db[_0x0ab4('0x3a')][_0x0ab4('0x45')]({'where':{'id':_0x2354fa[_0x0ab4('0x41')]['id']}})[_0x0ab4('0x1f')](handleEntityNotFound(_0x134437,null))[_0x0ab4('0x1f')](removeEntity(_0x134437,null))[_0x0ab4('0x3f')](handleError(_0x134437,null));};exports[_0x0ab4('0x4e')]=function(_0x438f38,_0x1e5635,_0x340558){return db[_0x0ab4('0x3a')][_0x0ab4('0x4f')]({'where':{'id':_0x438f38['params']['id']}})[_0x0ab4('0x1f')](handleEntityNotFound(_0x1e5635,null))[_0x0ab4('0x1f')](function(_0x3eba1e){var _0x308f7a;var _0x32e38c={},_0x3bcb90={};_0x3bcb90[_0x0ab4('0x28')]=_[_0x0ab4('0x2a')](db['VoiceMailMessage'][_0x0ab4('0x26')]);_0x3bcb90[_0x0ab4('0x29')]=_['keys'](_0x438f38['query']);_0x3bcb90[_0x0ab4('0x2b')]=_[_0x0ab4('0x2c')](_0x3bcb90['model'],_0x3bcb90['query']);_0x32e38c[_0x0ab4('0x2d')]=_['intersection'](_0x3bcb90['model'],qs[_0x0ab4('0x2e')](_0x438f38[_0x0ab4('0x29')][_0x0ab4('0x2e')]));_0x32e38c[_0x0ab4('0x2d')]=_0x32e38c[_0x0ab4('0x2d')][_0x0ab4('0x42')]?_0x32e38c[_0x0ab4('0x2d')]:_0x3bcb90['model'];if(!_0x438f38[_0x0ab4('0x29')][_0x0ab4('0x2f')](_0x0ab4('0x30'))){_0x32e38c[_0x0ab4('0x1b')]=qs[_0x0ab4('0x1b')](_0x438f38[_0x0ab4('0x29')]['limit']);_0x32e38c[_0x0ab4('0x19')]=qs[_0x0ab4('0x19')](_0x438f38['query'][_0x0ab4('0x19')]);}_0x32e38c['order']=qs[_0x0ab4('0x32')](_0x438f38['query'][_0x0ab4('0x32')]);_0x32e38c[_0x0ab4('0x33')]=qs[_0x0ab4('0x2b')](_[_0x0ab4('0x34')](_0x438f38['query'],_0x3bcb90['filters']));if(_0x438f38[_0x0ab4('0x29')][_0x0ab4('0x35')]){_0x32e38c['where']=_[_0x0ab4('0x36')](_0x32e38c[_0x0ab4('0x33')],{'$or':_[_0x0ab4('0x37')](_0x32e38c['attributes'],function(_0x295490){var _0xd3fb04={};_0xd3fb04[_0x295490]={'$like':'%'+_0x438f38[_0x0ab4('0x29')][_0x0ab4('0x35')]+'%'};return _0xd3fb04;})});}_0x32e38c[_0x0ab4('0x33')]=_[_0x0ab4('0x36')](_0x32e38c[_0x0ab4('0x33')],{'mailboxuser':_0x3eba1e[_0x0ab4('0x50')]});_0x32e38c=_['merge']({},_0x32e38c,_0x438f38['options']);return db[_0x0ab4('0x51')][_0x0ab4('0x52')](_0x32e38c)[_0x0ab4('0x1f')](respondWithFilteredResult(_0x1e5635,_0x32e38c));})[_0x0ab4('0x3f')](handleError(_0x1e5635,null));};