Built motion from commit (unavailable).|2.5.31
[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 _0xf510=['zip-dir','fast-json-patch','request-promise','moment','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','count','limit','Content-Range','apply','save','update','then','destroy','error','stack','name','send','merge','body','message','interaction','index','map','VoiceMail','fieldName','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','type','VIRTUAL','options','includeAll','include','findAll','rows','show','params','find','catch','create','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','getMessages','findOne','VoiceMailMessage','rawAttributes','findAndCountAll','eml-format','rimraf'];(function(_0x5863e0,_0x4a3d7c){var _0x213289=function(_0x1e8dd6){while(--_0x1e8dd6){_0x5863e0['push'](_0x5863e0['shift']());}};_0x213289(++_0x4a3d7c);}(_0xf510,0x162));var _0x0f51=function(_0x3954fb,_0xdbc1b9){_0x3954fb=_0x3954fb-0x0;var _0x58d45b=_0xf510[_0x3954fb];return _0x58d45b;};'use strict';var emlformat=require(_0x0f51('0x0'));var rimraf=require(_0x0f51('0x1'));var zipdir=require(_0x0f51('0x2'));var jsonpatch=require(_0x0f51('0x3'));var rp=require(_0x0f51('0x4'));var moment=require(_0x0f51('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require('util');var path=require('path');var sox=require(_0x0f51('0x6'));var csv=require(_0x0f51('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x0f51('0x8'));var _=require(_0x0f51('0x9'));var squel=require(_0x0f51('0xa'));var crypto=require(_0x0f51('0xb'));var jsforce=require(_0x0f51('0xc'));var deskjs=require(_0x0f51('0xd'));var toCsv=require(_0x0f51('0x7'));var querystring=require(_0x0f51('0xe'));var Papa=require(_0x0f51('0xf'));var Redis=require(_0x0f51('0x10'));var authService=require(_0x0f51('0x11'));var qs=require(_0x0f51('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x0f51('0x13'));var logger=require(_0x0f51('0x14'))(_0x0f51('0x15'));var utils=require(_0x0f51('0x16'));var config=require(_0x0f51('0x17'));var licenseUtil=require(_0x0f51('0x18'));var db=require(_0x0f51('0x19'))['db'];function respondWithStatusCode(_0x29e633,_0x3b932a){_0x3b932a=_0x3b932a||0xcc;return function(_0x3d2fda){if(_0x3d2fda){return _0x29e633['sendStatus'](_0x3b932a);}return _0x29e633[_0x0f51('0x1a')](_0x3b932a)[_0x0f51('0x1b')]();};}function respondWithResult(_0x811d6c,_0x31b22d){_0x31b22d=_0x31b22d||0xc8;return function(_0x4fcfcc){if(_0x4fcfcc){return _0x811d6c[_0x0f51('0x1a')](_0x31b22d)[_0x0f51('0x1c')](_0x4fcfcc);}};}function respondWithFilteredResult(_0x1c459a,_0x3ad8d7){return function(_0x2847ef){if(_0x2847ef){var _0x3cd1e0=typeof _0x3ad8d7[_0x0f51('0x1d')]===_0x0f51('0x1e')&&typeof _0x3ad8d7['limit']==='undefined';var _0x192e52=_0x2847ef[_0x0f51('0x1f')];var _0x40e9ad=_0x3cd1e0?0x0:_0x3ad8d7[_0x0f51('0x1d')];var _0x354e1c=_0x3cd1e0?_0x2847ef[_0x0f51('0x1f')]:_0x3ad8d7[_0x0f51('0x1d')]+_0x3ad8d7[_0x0f51('0x20')];var _0x95b05b;if(_0x354e1c>=_0x192e52){_0x354e1c=_0x192e52;_0x95b05b=0xc8;}else{_0x95b05b=0xce;}_0x1c459a[_0x0f51('0x1a')](_0x95b05b);return _0x1c459a['set'](_0x0f51('0x21'),_0x40e9ad+'-'+_0x354e1c+'/'+_0x192e52)[_0x0f51('0x1c')](_0x2847ef);}return null;};}function patchUpdates(_0x3e4e8a){return function(_0x4b2149){try{jsonpatch[_0x0f51('0x22')](_0x4b2149,_0x3e4e8a,!![]);}catch(_0x52a3b9){return BPromise['reject'](_0x52a3b9);}return _0x4b2149[_0x0f51('0x23')]();};}function saveUpdates(_0xc6e29e,_0x1910e7){return function(_0x36958d){if(_0x36958d){return _0x36958d[_0x0f51('0x24')](_0xc6e29e)[_0x0f51('0x25')](function(_0x469d6a){return _0x469d6a;});}return null;};}function removeEntity(_0x321c4a,_0x2a57d2){return function(_0xca3ce7){if(_0xca3ce7){return _0xca3ce7[_0x0f51('0x26')]()[_0x0f51('0x25')](function(){var _0x1069e6=_0xca3ce7['get']({'plain':!![]});var _0x4edcad='Voicemails';return db['UserProfileResource'][_0x0f51('0x26')]({'where':{'type':_0x4edcad,'resourceId':_0x1069e6['id']}})[_0x0f51('0x25')](function(){return _0xca3ce7;});})[_0x0f51('0x25')](function(){_0x321c4a[_0x0f51('0x1a')](0xcc)['end']();});}};}function handleEntityNotFound(_0x2400d4,_0x30dbf4){return function(_0x56d94a){if(!_0x56d94a){_0x2400d4['sendStatus'](0x194);}return _0x56d94a;};}function handleError(_0x3bc88a,_0x35e7f0){_0x35e7f0=_0x35e7f0||0x1f4;return function(_0x1c0425){logger[_0x0f51('0x27')](_0x1c0425[_0x0f51('0x28')]);if(_0x1c0425['name']){delete _0x1c0425[_0x0f51('0x29')];}_0x3bc88a['status'](_0x35e7f0)[_0x0f51('0x2a')](_0x1c0425);};}function getMustacheRender(_0x428e9c,_0x11382b){return Mustache['render'](_0x428e9c,_[_0x0f51('0x2b')](_0x11382b[_0x0f51('0x2c')],{'contact':_0x11382b['contact'],'message':_0x11382b[_0x0f51('0x2d')],'agent':_0x11382b['agent'],'interaction':_0x11382b[_0x0f51('0x2e')],'account':_0x11382b['account']}))||'';}exports[_0x0f51('0x2f')]=function(_0x124047,_0x4c6dc5){var _0x2e56a1={},_0x5b097c={},_0x5dd7ab={'count':0x0,'rows':[]};var _0x25f00c=_[_0x0f51('0x30')](db[_0x0f51('0x31')]['rawAttributes'],function(_0x2f5b90){return{'name':_0x2f5b90[_0x0f51('0x32')],'type':_0x2f5b90['type']['key']};});_0x5b097c[_0x0f51('0x33')]=_['map'](_0x25f00c,'name');_0x5b097c[_0x0f51('0x34')]=_[_0x0f51('0x35')](_0x124047[_0x0f51('0x34')]);_0x5b097c[_0x0f51('0x36')]=_['intersection'](_0x5b097c[_0x0f51('0x33')],_0x5b097c['query']);_0x2e56a1[_0x0f51('0x37')]=_[_0x0f51('0x38')](_0x5b097c[_0x0f51('0x33')],qs[_0x0f51('0x39')](_0x124047['query'][_0x0f51('0x39')]));_0x2e56a1[_0x0f51('0x37')]=_0x2e56a1[_0x0f51('0x37')][_0x0f51('0x3a')]?_0x2e56a1[_0x0f51('0x37')]:_0x5b097c[_0x0f51('0x33')];if(!_0x124047['query'][_0x0f51('0x3b')](_0x0f51('0x3c'))){_0x2e56a1[_0x0f51('0x20')]=qs[_0x0f51('0x20')](_0x124047[_0x0f51('0x34')][_0x0f51('0x20')]);_0x2e56a1[_0x0f51('0x1d')]=qs['offset'](_0x124047['query'][_0x0f51('0x1d')]);}_0x2e56a1[_0x0f51('0x3d')]=qs[_0x0f51('0x3e')](_0x124047[_0x0f51('0x34')][_0x0f51('0x3e')]);_0x2e56a1[_0x0f51('0x3f')]=qs['filters'](_[_0x0f51('0x40')](_0x124047[_0x0f51('0x34')],_0x5b097c[_0x0f51('0x36')]),_0x25f00c);if(_0x124047['query'][_0x0f51('0x41')]){_0x2e56a1[_0x0f51('0x3f')]=_[_0x0f51('0x2b')](_0x2e56a1[_0x0f51('0x3f')],{'$or':_['map'](_0x25f00c,function(_0x461236){if(_0x461236[_0x0f51('0x42')]!==_0x0f51('0x43')){var _0x444d74={};_0x444d74[_0x461236[_0x0f51('0x29')]]={'$like':'%'+_0x124047[_0x0f51('0x34')][_0x0f51('0x41')]+'%'};return _0x444d74;}})});}_0x2e56a1=_[_0x0f51('0x2b')]({},_0x2e56a1,_0x124047[_0x0f51('0x44')]);var _0x579aeb={'where':_0x2e56a1[_0x0f51('0x3f')]};return db['VoiceMail'][_0x0f51('0x1f')](_0x579aeb)[_0x0f51('0x25')](function(_0x365e7b){_0x5dd7ab[_0x0f51('0x1f')]=_0x365e7b;if(_0x124047['query'][_0x0f51('0x45')]){_0x2e56a1[_0x0f51('0x46')]=[{'all':!![]}];}return db[_0x0f51('0x31')][_0x0f51('0x47')](_0x2e56a1);})['then'](function(_0x13c89a){_0x5dd7ab[_0x0f51('0x48')]=_0x13c89a;return _0x5dd7ab;})[_0x0f51('0x25')](respondWithFilteredResult(_0x4c6dc5,_0x2e56a1))['catch'](handleError(_0x4c6dc5,null));};exports[_0x0f51('0x49')]=function(_0x47081a,_0x205782){var _0x300fd6={'raw':!![],'where':{'id':_0x47081a[_0x0f51('0x4a')]['id']}},_0x7ef0de={};_0x7ef0de[_0x0f51('0x33')]=_[_0x0f51('0x35')](db[_0x0f51('0x31')]['rawAttributes']);_0x7ef0de[_0x0f51('0x34')]=_[_0x0f51('0x35')](_0x47081a[_0x0f51('0x34')]);_0x7ef0de['filters']=_[_0x0f51('0x38')](_0x7ef0de[_0x0f51('0x33')],_0x7ef0de['query']);_0x300fd6[_0x0f51('0x37')]=_['intersection'](_0x7ef0de[_0x0f51('0x33')],qs['fields'](_0x47081a[_0x0f51('0x34')][_0x0f51('0x39')]));_0x300fd6[_0x0f51('0x37')]=_0x300fd6['attributes'][_0x0f51('0x3a')]?_0x300fd6[_0x0f51('0x37')]:_0x7ef0de[_0x0f51('0x33')];if(_0x47081a['query'][_0x0f51('0x45')]){_0x300fd6[_0x0f51('0x46')]=[{'all':!![]}];}_0x300fd6=_[_0x0f51('0x2b')]({},_0x300fd6,_0x47081a[_0x0f51('0x44')]);return db[_0x0f51('0x31')][_0x0f51('0x4b')](_0x300fd6)[_0x0f51('0x25')](handleEntityNotFound(_0x205782,null))[_0x0f51('0x25')](respondWithResult(_0x205782,null))[_0x0f51('0x4c')](handleError(_0x205782,null));};exports[_0x0f51('0x4d')]=function(_0x17de0d,_0x5575ae){return db[_0x0f51('0x31')][_0x0f51('0x4d')](_0x17de0d[_0x0f51('0x2c')],{})[_0x0f51('0x25')](function(_0x2ba2f4){var _0xbd95a1=_0x17de0d['user'][_0x0f51('0x4e')]({'plain':!![]});if(!_0xbd95a1)throw new Error(_0x0f51('0x4f'));if(_0xbd95a1[_0x0f51('0x50')]==='user'){var _0x4f425a=_0x2ba2f4['get']({'plain':!![]});var _0x2b7370='Voicemails';return db[_0x0f51('0x51')]['find']({'where':{'name':_0x2b7370,'userProfileId':_0xbd95a1[_0x0f51('0x52')]},'raw':!![]})[_0x0f51('0x25')](function(_0x1cefcd){if(_0x1cefcd&&_0x1cefcd['autoAssociation']===0x0){return db['UserProfileResource'][_0x0f51('0x4d')]({'name':_0x4f425a['name'],'resourceId':_0x4f425a['id'],'type':_0x1cefcd[_0x0f51('0x29')],'sectionId':_0x1cefcd['id']},{})[_0x0f51('0x25')](function(){return _0x2ba2f4;});}else{return _0x2ba2f4;}})[_0x0f51('0x4c')](function(_0x2c0ecc){logger['error'](_0x0f51('0x53'),_0x2c0ecc);throw _0x2c0ecc;});}return _0x2ba2f4;})[_0x0f51('0x25')](respondWithResult(_0x5575ae,0xc9))[_0x0f51('0x4c')](handleError(_0x5575ae,null));};exports[_0x0f51('0x24')]=function(_0x14fc13,_0x9c6d33){if(_0x14fc13[_0x0f51('0x2c')]['id']){delete _0x14fc13[_0x0f51('0x2c')]['id'];}return db[_0x0f51('0x31')]['find']({'where':{'id':_0x14fc13[_0x0f51('0x4a')]['id']}})[_0x0f51('0x25')](handleEntityNotFound(_0x9c6d33,null))[_0x0f51('0x25')](saveUpdates(_0x14fc13[_0x0f51('0x2c')],null))[_0x0f51('0x25')](respondWithResult(_0x9c6d33,null))[_0x0f51('0x4c')](handleError(_0x9c6d33,null));};exports[_0x0f51('0x26')]=function(_0x32a624,_0x14023f){return db['VoiceMail'][_0x0f51('0x4b')]({'where':{'id':_0x32a624[_0x0f51('0x4a')]['id']}})[_0x0f51('0x25')](handleEntityNotFound(_0x14023f,null))[_0x0f51('0x25')](removeEntity(_0x14023f,null))['catch'](handleError(_0x14023f,null));};exports[_0x0f51('0x54')]=function(_0x5df038,_0x1e41de,_0x3383e1){return db[_0x0f51('0x31')][_0x0f51('0x55')]({'where':{'id':_0x5df038[_0x0f51('0x4a')]['id']}})[_0x0f51('0x25')](handleEntityNotFound(_0x1e41de,null))[_0x0f51('0x25')](function(_0x11459b){var _0x28094e;var _0x3b306b={},_0x56f781={};_0x56f781[_0x0f51('0x33')]=_[_0x0f51('0x35')](db[_0x0f51('0x56')][_0x0f51('0x57')]);_0x56f781[_0x0f51('0x34')]=_['keys'](_0x5df038[_0x0f51('0x34')]);_0x56f781[_0x0f51('0x36')]=_[_0x0f51('0x38')](_0x56f781[_0x0f51('0x33')],_0x56f781[_0x0f51('0x34')]);_0x3b306b[_0x0f51('0x37')]=_[_0x0f51('0x38')](_0x56f781['model'],qs[_0x0f51('0x39')](_0x5df038[_0x0f51('0x34')][_0x0f51('0x39')]));_0x3b306b['attributes']=_0x3b306b[_0x0f51('0x37')][_0x0f51('0x3a')]?_0x3b306b[_0x0f51('0x37')]:_0x56f781[_0x0f51('0x33')];if(!_0x5df038[_0x0f51('0x34')][_0x0f51('0x3b')]('nolimit')){_0x3b306b['limit']=qs[_0x0f51('0x20')](_0x5df038[_0x0f51('0x34')]['limit']);_0x3b306b['offset']=qs[_0x0f51('0x1d')](_0x5df038[_0x0f51('0x34')][_0x0f51('0x1d')]);}_0x3b306b[_0x0f51('0x3d')]=qs['sort'](_0x5df038[_0x0f51('0x34')][_0x0f51('0x3e')]);_0x3b306b['where']=qs['filters'](_['pick'](_0x5df038['query'],_0x56f781[_0x0f51('0x36')]));if(_0x5df038[_0x0f51('0x34')][_0x0f51('0x41')]){_0x3b306b[_0x0f51('0x3f')]=_[_0x0f51('0x2b')](_0x3b306b[_0x0f51('0x3f')],{'$or':_[_0x0f51('0x30')](_0x3b306b[_0x0f51('0x37')],function(_0x372dc4){var _0x11965e={};_0x11965e[_0x372dc4]={'$like':'%'+_0x5df038['query'][_0x0f51('0x41')]+'%'};return _0x11965e;})});}_0x3b306b['where']=_[_0x0f51('0x2b')](_0x3b306b['where'],{'mailboxuser':_0x11459b['mailbox']});_0x3b306b=_[_0x0f51('0x2b')]({},_0x3b306b,_0x5df038[_0x0f51('0x44')]);return db[_0x0f51('0x56')][_0x0f51('0x58')](_0x3b306b)[_0x0f51('0x25')](respondWithFilteredResult(_0x1e41de,_0x3b306b));})[_0x0f51('0x4c')](handleError(_0x1e41de,null));};