Built motion from commit 97f2f211.|2.6.31
[motion2.git] / server / api / userNotification / userNotification.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 _0x9f70=['user','some','includes','model','toLowerCase','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','fullname','isEmpty','recipientsDescription','beep','SenderId','recipientsId','join','sequelize','UserNotification','create','forEach','recipient','userNotification:send','catch','text','Template','findOne','html','Invalid\x20\x27TemplateId\x27\x20parameter','startCase','User','union','queue','replace','ids','push','flatten','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./userNotification.socket','register','sendStatus','status','end','json','undefined','count','offset','limit','set','Content-Range','apply','save','update','then','destroy','error','stack','name','send','sendUserNotification','body','hasOwnProperty','TemplateId','Sequelize','view','ValidationError','recipients','parse','Invalid\x20\x27recipients\x27\x20object','faxqueue','smsqueue','whatsappqueue','team'];(function(_0xbc5d73,_0x4d7000){var _0xcf16e1=function(_0x5569b7){while(--_0x5569b7){_0xbc5d73['push'](_0xbc5d73['shift']());}};_0xcf16e1(++_0x4d7000);}(_0x9f70,0x14b));var _0x09f7=function(_0x277a69,_0x127dd5){_0x277a69=_0x277a69-0x0;var _0x45f9db=_0x9f70[_0x277a69];return _0x45f9db;};'use strict';var emlformat=require(_0x09f7('0x0'));var rimraf=require(_0x09f7('0x1'));var zipdir=require(_0x09f7('0x2'));var jsonpatch=require(_0x09f7('0x3'));var rp=require(_0x09f7('0x4'));var moment=require(_0x09f7('0x5'));var BPromise=require(_0x09f7('0x6'));var Mustache=require(_0x09f7('0x7'));var util=require(_0x09f7('0x8'));var path=require('path');var sox=require(_0x09f7('0x9'));var csv=require(_0x09f7('0xa'));var ejs=require(_0x09f7('0xb'));var fs=require('fs');var fs_extra=require(_0x09f7('0xc'));var _=require(_0x09f7('0xd'));var squel=require(_0x09f7('0xe'));var crypto=require(_0x09f7('0xf'));var jsforce=require(_0x09f7('0x10'));var deskjs=require(_0x09f7('0x11'));var toCsv=require(_0x09f7('0xa'));var querystring=require(_0x09f7('0x12'));var Papa=require(_0x09f7('0x13'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x09f7('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x09f7('0x15'))(_0x09f7('0x16'));var utils=require(_0x09f7('0x17'));var config=require('../../config/environment');var licenseUtil=require(_0x09f7('0x18'));var db=require(_0x09f7('0x19'))['db'];config[_0x09f7('0x1a')]=_[_0x09f7('0x1b')](config['redis'],{'host':_0x09f7('0x1c'),'port':0x18eb});var socket=require(_0x09f7('0x1d'))(new Redis(config['redis']));require(_0x09f7('0x1e'))[_0x09f7('0x1f')](socket);function respondWithStatusCode(_0x4d2a39,_0x549212){_0x549212=_0x549212||0xcc;return function(_0x4c8ed2){if(_0x4c8ed2){return _0x4d2a39[_0x09f7('0x20')](_0x549212);}return _0x4d2a39[_0x09f7('0x21')](_0x549212)[_0x09f7('0x22')]();};}function respondWithResult(_0x280d23,_0x529984){_0x529984=_0x529984||0xc8;return function(_0x5e403c){if(_0x5e403c){return _0x280d23['status'](_0x529984)[_0x09f7('0x23')](_0x5e403c);}};}function respondWithFilteredResult(_0x573386,_0x1e83be){return function(_0x942369){if(_0x942369){var _0x112b7d=typeof _0x1e83be['offset']==='undefined'&&typeof _0x1e83be['limit']===_0x09f7('0x24');var _0x215787=_0x942369[_0x09f7('0x25')];var _0x4a0af2=_0x112b7d?0x0:_0x1e83be['offset'];var _0x591691=_0x112b7d?_0x942369[_0x09f7('0x25')]:_0x1e83be[_0x09f7('0x26')]+_0x1e83be[_0x09f7('0x27')];var _0x390c03;if(_0x591691>=_0x215787){_0x591691=_0x215787;_0x390c03=0xc8;}else{_0x390c03=0xce;}_0x573386[_0x09f7('0x21')](_0x390c03);return _0x573386[_0x09f7('0x28')](_0x09f7('0x29'),_0x4a0af2+'-'+_0x591691+'/'+_0x215787)[_0x09f7('0x23')](_0x942369);}return null;};}function patchUpdates(_0x5b3d89){return function(_0x4a97e6){try{jsonpatch[_0x09f7('0x2a')](_0x4a97e6,_0x5b3d89,!![]);}catch(_0x5c83a8){return BPromise['reject'](_0x5c83a8);}return _0x4a97e6[_0x09f7('0x2b')]();};}function saveUpdates(_0x30b309,_0x5addc1){return function(_0x4e5100){if(_0x4e5100){return _0x4e5100[_0x09f7('0x2c')](_0x30b309)[_0x09f7('0x2d')](function(_0xe82b25){return _0xe82b25;});}return null;};}function removeEntity(_0x265da8,_0x4007eb){return function(_0xe4762){if(_0xe4762){return _0xe4762[_0x09f7('0x2e')]()[_0x09f7('0x2d')](function(){_0x265da8[_0x09f7('0x21')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3aff3e,_0x39db2c){return function(_0x249cc8){if(!_0x249cc8){_0x3aff3e['sendStatus'](0x194);}return _0x249cc8;};}function handleError(_0x35bd49,_0x3ab4ee){_0x3ab4ee=_0x3ab4ee||0x1f4;return function(_0x4034fe){logger[_0x09f7('0x2f')](_0x4034fe[_0x09f7('0x30')]);if(_0x4034fe['name']){delete _0x4034fe[_0x09f7('0x31')];}_0x35bd49['status'](_0x3ab4ee)[_0x09f7('0x32')](_0x4034fe);};}exports[_0x09f7('0x33')]=function(_0x394235,_0x5c3103,_0x44d40a){if(!_0x394235[_0x09f7('0x34')][_0x09f7('0x35')]('text')&&!_0x394235[_0x09f7('0x34')]['hasOwnProperty'](_0x09f7('0x36'))){throw new db[(_0x09f7('0x37'))]['ValidationError']('Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27');}else{if(_0x394235['body'][_0x09f7('0x35')](_0x09f7('0x36'))&&!_0x394235['body']['hasOwnProperty'](_0x09f7('0x38'))){throw new db['Sequelize'][(_0x09f7('0x39'))]('Missing\x20\x27view\x27\x20parameter');}if(!_0x394235[_0x09f7('0x34')][_0x09f7('0x35')](_0x09f7('0x3a'))){throw new db[(_0x09f7('0x37'))]['ValidationError']('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x354718=JSON[_0x09f7('0x3b')](_0x394235[_0x09f7('0x34')][_0x09f7('0x3a')]);}catch(_0x2caa42){logger[_0x09f7('0x2f')](_0x2caa42);throw new db['Sequelize']['ValidationError'](_0x09f7('0x3c'));}var _0x36c1b4=['chatqueue',_0x09f7('0x3d'),'mailqueue','openchannelqueue',_0x09f7('0x3e'),'voicequeue',_0x09f7('0x3f'),_0x09f7('0x40'),_0x09f7('0x41')];if(_[_0x09f7('0x42')](_0x354718,function(_0x2d786b){return!_[_0x09f7('0x43')](_0x36c1b4,_0x2d786b[_0x09f7('0x44')][_0x09f7('0x45')]());})){throw new db['Sequelize']['ValidationError'](_0x09f7('0x46'));}}}var _0x167cf7={};_0x167cf7[_0x09f7('0x47')]=_0x394235[_0x09f7('0x34')]['hasOwnProperty'](_0x09f7('0x47'))?_0x394235['body']['senderName']:_0x394235[_0x09f7('0x41')][_0x09f7('0x48')];_0x167cf7['recipientsDescription']=_[_0x09f7('0x49')](_0x394235['body'][_0x09f7('0x4a')])?null:_0x394235['body'][_0x09f7('0x4a')];_0x167cf7[_0x09f7('0x4b')]=_0x394235[_0x09f7('0x34')][_0x09f7('0x4b')]||![];_0x167cf7[_0x09f7('0x4c')]=_0x394235[_0x09f7('0x41')]['id'];return getContent(_0x394235[_0x09f7('0x34')])[_0x09f7('0x2d')](function(_0x4781ef){_0x167cf7['body']=_0x4781ef;return getRecipients(_0x354718);})[_0x09f7('0x2d')](function(_0x48e1dc){_0x167cf7[_0x09f7('0x4d')]=_['sortBy'](_0x48e1dc)[_0x09f7('0x4e')](',');return db[_0x09f7('0x4f')]['transaction'](function(_0x2e17c4){return db[_0x09f7('0x50')][_0x09f7('0x51')](_0x167cf7,{'raw':!![],'transaction':_0x2e17c4})[_0x09f7('0x2d')](function(_0x63f48d){var _0x2588e3={'sender':_0x167cf7[_0x09f7('0x47')],'description':_0x167cf7['recipientsDescription'],'content':_0x167cf7[_0x09f7('0x34')],'beep':_0x167cf7[_0x09f7('0x4b')]};_0x48e1dc[_0x09f7('0x52')](function(_0x5d5db8){_0x2588e3[_0x09f7('0x53')]=_0x5d5db8;socket['emit'](_0x09f7('0x54'),_0x2588e3);});return _0x63f48d;});});})[_0x09f7('0x2d')](respondWithResult(_0x5c3103,0xc9))[_0x09f7('0x55')](handleError(_0x5c3103,null));};function getContent(_0x311d17){return new BPromise(function(_0x1e7c03,_0x535b55){if(_0x311d17[_0x09f7('0x56')]){_0x1e7c03(_0x311d17[_0x09f7('0x56')]);return;}return db[_0x09f7('0x57')][_0x09f7('0x58')]({'attributes':[_0x09f7('0x59')],'where':{'id':_0x311d17['TemplateId']},'raw':!![]})[_0x09f7('0x2d')](function(_0x1ae3db){if(!_0x1ae3db)throw new db[(_0x09f7('0x37'))][(_0x09f7('0x39'))](_0x09f7('0x5a'));var _0x3ab2cd=Mustache['render'](_0x1ae3db[_0x09f7('0x59')],_0x311d17['view']);_0x1e7c03(_0x3ab2cd);})['catch'](function(_0x4d6247){_0x535b55(_0x4d6247);});});}function getRecipients(_0x1ec440){return new BPromise(function(_0x49ac26,_0x59bc6c){var _0x3b4e0e=[];var _0x4b6165=[];_0x1ec440['forEach'](function(_0x4e34c4){var _0x543820=_[_0x09f7('0x5b')](_0x4e34c4[_0x09f7('0x44')][_0x09f7('0x45')]());if(_0x543820===_0x09f7('0x5c')){_0x3b4e0e=_[_0x09f7('0x5d')](_0x3b4e0e,_0x4e34c4['ids']);}else{if(_[_0x09f7('0x43')](_0x543820,_0x09f7('0x5e')))_0x543820=_0x543820[_0x09f7('0x5f')](_0x09f7('0x5e'),'Queue');_0x4e34c4[_0x09f7('0x60')][_0x09f7('0x52')](function(_0x413264){_0x4b6165[_0x09f7('0x61')](getAgents(_0x543820,_0x413264));});}});return BPromise['all'](_0x4b6165)[_0x09f7('0x2d')](function(_0x13f304){_0x3b4e0e=_['union'](_0x3b4e0e,_[_0x09f7('0x62')](_0x13f304));_0x49ac26(_0x3b4e0e);})[_0x09f7('0x55')](function(_0x368b4e){_0x59bc6c(_0x368b4e);});});}function getAgents(_0x4a1c68,_0x44fdae){return new BPromise(function(_0x1b91a4,_0x3b1db7){return db[_0x4a1c68][_0x09f7('0x58')]({'where':{'id':_0x44fdae}})['then'](function(_0x26c5eb){return _0x26c5eb['getAgents']({'attributes':['id'],'raw':!![]})[_0x09f7('0x2d')](function(_0x1456fa){var _0x2d0feb=_['map'](_0x1456fa,'id');_0x1b91a4(_0x2d0feb);});})[_0x09f7('0x55')](function(_0x50fedb){_0x3b1db7(_0x50fedb);});});}