Built motion from commit (unavailable).|2.5.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 _0x2ef9=['socket.io-emitter','./userNotification.socket','sendStatus','status','end','offset','undefined','limit','count','set','Content-Range','reject','then','destroy','stack','send','body','hasOwnProperty','text','TemplateId','Sequelize','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','view','ValidationError','Missing\x20\x27view\x27\x20parameter','Missing\x20\x27recipients\x27\x20parameter','parse','recipients','chatqueue','faxqueue','openchannelqueue','smsqueue','voicequeue','whatsappqueue','team','some','includes','model','toLowerCase','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','user','fullname','recipientsDescription','isEmpty','SenderId','sortBy','join','sequelize','transaction','UserNotification','forEach','recipient','emit','catch','html','render','startCase','union','ids','queue','Queue','push','all','flatten','findOne','map','eml-format','rimraf','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','squel','crypto','desk.js','querystring','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','defaults'];(function(_0x6e14b9,_0x8ebd1){var _0x3f6b2b=function(_0x70eec6){while(--_0x70eec6){_0x6e14b9['push'](_0x6e14b9['shift']());}};_0x3f6b2b(++_0x8ebd1);}(_0x2ef9,0x14e));var _0x92ef=function(_0x314cdf,_0x1122ed){_0x314cdf=_0x314cdf-0x0;var _0x5df90f=_0x2ef9[_0x314cdf];return _0x5df90f;};'use strict';var emlformat=require(_0x92ef('0x0'));var rimraf=require(_0x92ef('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x92ef('0x2'));var moment=require(_0x92ef('0x3'));var BPromise=require(_0x92ef('0x4'));var Mustache=require(_0x92ef('0x5'));var util=require(_0x92ef('0x6'));var path=require(_0x92ef('0x7'));var sox=require(_0x92ef('0x8'));var csv=require(_0x92ef('0x9'));var ejs=require(_0x92ef('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x92ef('0xb'));var crypto=require(_0x92ef('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x92ef('0xd'));var toCsv=require(_0x92ef('0x9'));var querystring=require(_0x92ef('0xe'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x92ef('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x92ef('0x10'));var logger=require(_0x92ef('0x11'))(_0x92ef('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x92ef('0x13'));var db=require(_0x92ef('0x14'))['db'];config['redis']=_[_0x92ef('0x15')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x92ef('0x16'))(new Redis(config['redis']));require(_0x92ef('0x17'))['register'](socket);function respondWithStatusCode(_0x2ff999,_0x3aac04){_0x3aac04=_0x3aac04||0xcc;return function(_0x3692f2){if(_0x3692f2){return _0x2ff999[_0x92ef('0x18')](_0x3aac04);}return _0x2ff999[_0x92ef('0x19')](_0x3aac04)[_0x92ef('0x1a')]();};}function respondWithResult(_0xa4043f,_0x337d2b){_0x337d2b=_0x337d2b||0xc8;return function(_0x5e0389){if(_0x5e0389){return _0xa4043f['status'](_0x337d2b)['json'](_0x5e0389);}};}function respondWithFilteredResult(_0x40bc33,_0x32a3ae){return function(_0x5d300e){if(_0x5d300e){var _0x1e4b7c=typeof _0x32a3ae[_0x92ef('0x1b')]===_0x92ef('0x1c')&&typeof _0x32a3ae[_0x92ef('0x1d')]==='undefined';var _0x38d524=_0x5d300e[_0x92ef('0x1e')];var _0x506ca7=_0x1e4b7c?0x0:_0x32a3ae['offset'];var _0x484634=_0x1e4b7c?_0x5d300e['count']:_0x32a3ae[_0x92ef('0x1b')]+_0x32a3ae[_0x92ef('0x1d')];var _0x4fe0fd;if(_0x484634>=_0x38d524){_0x484634=_0x38d524;_0x4fe0fd=0xc8;}else{_0x4fe0fd=0xce;}_0x40bc33[_0x92ef('0x19')](_0x4fe0fd);return _0x40bc33[_0x92ef('0x1f')](_0x92ef('0x20'),_0x506ca7+'-'+_0x484634+'/'+_0x38d524)['json'](_0x5d300e);}return null;};}function patchUpdates(_0x3798fb){return function(_0x279dc2){try{jsonpatch['apply'](_0x279dc2,_0x3798fb,!![]);}catch(_0x2b36e4){return BPromise[_0x92ef('0x21')](_0x2b36e4);}return _0x279dc2['save']();};}function saveUpdates(_0x2232e8,_0x21bc00){return function(_0x5f27d7){if(_0x5f27d7){return _0x5f27d7['update'](_0x2232e8)[_0x92ef('0x22')](function(_0x548cab){return _0x548cab;});}return null;};}function removeEntity(_0x200964,_0x348295){return function(_0x3515da){if(_0x3515da){return _0x3515da[_0x92ef('0x23')]()['then'](function(){_0x200964[_0x92ef('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x394da4,_0x193ad1){return function(_0x50d75f){if(!_0x50d75f){_0x394da4['sendStatus'](0x194);}return _0x50d75f;};}function handleError(_0x39dd28,_0x2db470){_0x2db470=_0x2db470||0x1f4;return function(_0x20f8c2){logger['error'](_0x20f8c2[_0x92ef('0x24')]);if(_0x20f8c2['name']){delete _0x20f8c2['name'];}_0x39dd28[_0x92ef('0x19')](_0x2db470)[_0x92ef('0x25')](_0x20f8c2);};}exports['sendUserNotification']=function(_0x4678d9,_0x5e0bb1,_0x1f016c){if(!_0x4678d9[_0x92ef('0x26')][_0x92ef('0x27')](_0x92ef('0x28'))&&!_0x4678d9['body'][_0x92ef('0x27')](_0x92ef('0x29'))){throw new db[(_0x92ef('0x2a'))]['ValidationError'](_0x92ef('0x2b'));}else{if(_0x4678d9[_0x92ef('0x26')][_0x92ef('0x27')](_0x92ef('0x29'))&&!_0x4678d9[_0x92ef('0x26')]['hasOwnProperty'](_0x92ef('0x2c'))){throw new db[(_0x92ef('0x2a'))][(_0x92ef('0x2d'))](_0x92ef('0x2e'));}if(!_0x4678d9[_0x92ef('0x26')][_0x92ef('0x27')]('recipients')){throw new db['Sequelize']['ValidationError'](_0x92ef('0x2f'));}else{try{var _0x232cb2=JSON[_0x92ef('0x30')](_0x4678d9[_0x92ef('0x26')][_0x92ef('0x31')]);}catch(_0x366990){logger['error'](_0x366990);throw new db[(_0x92ef('0x2a'))]['ValidationError']('Invalid\x20\x27recipients\x27\x20object');}var _0x3059dc=[_0x92ef('0x32'),_0x92ef('0x33'),'mailqueue',_0x92ef('0x34'),_0x92ef('0x35'),_0x92ef('0x36'),_0x92ef('0x37'),_0x92ef('0x38'),'user'];if(_[_0x92ef('0x39')](_0x232cb2,function(_0x5c8916){return!_[_0x92ef('0x3a')](_0x3059dc,_0x5c8916[_0x92ef('0x3b')][_0x92ef('0x3c')]());})){throw new db[(_0x92ef('0x2a'))][(_0x92ef('0x2d'))](_0x92ef('0x3d'));}}}var _0xfb1060={};_0xfb1060[_0x92ef('0x3e')]=_0x4678d9[_0x92ef('0x26')][_0x92ef('0x27')]('senderName')?_0x4678d9['body'][_0x92ef('0x3e')]:_0x4678d9[_0x92ef('0x3f')][_0x92ef('0x40')];_0xfb1060[_0x92ef('0x41')]=_[_0x92ef('0x42')](_0x4678d9[_0x92ef('0x26')][_0x92ef('0x41')])?null:_0x4678d9[_0x92ef('0x26')][_0x92ef('0x41')];_0xfb1060['beep']=_0x4678d9[_0x92ef('0x26')]['beep']||![];_0xfb1060[_0x92ef('0x43')]=_0x4678d9[_0x92ef('0x3f')]['id'];return getContent(_0x4678d9[_0x92ef('0x26')])[_0x92ef('0x22')](function(_0x296030){_0xfb1060[_0x92ef('0x26')]=_0x296030;return getRecipients(_0x232cb2);})[_0x92ef('0x22')](function(_0x168e88){_0xfb1060['recipientsId']=_[_0x92ef('0x44')](_0x168e88)[_0x92ef('0x45')](',');return db[_0x92ef('0x46')][_0x92ef('0x47')](function(_0x50443a){return db[_0x92ef('0x48')]['create'](_0xfb1060,{'raw':!![],'transaction':_0x50443a})[_0x92ef('0x22')](function(_0x553210){var _0x1ff1f9={'sender':_0xfb1060['senderName'],'description':_0xfb1060['recipientsDescription'],'content':_0xfb1060[_0x92ef('0x26')],'beep':_0xfb1060['beep']};_0x168e88[_0x92ef('0x49')](function(_0x3eea3d){_0x1ff1f9[_0x92ef('0x4a')]=_0x3eea3d;socket[_0x92ef('0x4b')]('userNotification:send',_0x1ff1f9);});return _0x553210;});});})[_0x92ef('0x22')](respondWithResult(_0x5e0bb1,0xc9))[_0x92ef('0x4c')](handleError(_0x5e0bb1,null));};function getContent(_0x4deedf){return new BPromise(function(_0x1c44f6,_0x371828){if(_0x4deedf[_0x92ef('0x28')]){_0x1c44f6(_0x4deedf[_0x92ef('0x28')]);return;}return db['Template']['findOne']({'attributes':[_0x92ef('0x4d')],'where':{'id':_0x4deedf['TemplateId']},'raw':!![]})[_0x92ef('0x22')](function(_0x3f8b50){if(!_0x3f8b50)throw new db['Sequelize']['ValidationError']('Invalid\x20\x27TemplateId\x27\x20parameter');var _0x2af94f=Mustache[_0x92ef('0x4e')](_0x3f8b50[_0x92ef('0x4d')],_0x4deedf[_0x92ef('0x2c')]);_0x1c44f6(_0x2af94f);})[_0x92ef('0x4c')](function(_0x198076){_0x371828(_0x198076);});});}function getRecipients(_0x575647){return new BPromise(function(_0x2b92dc,_0x2870f9){var _0xd324d9=[];var _0x118fda=[];_0x575647[_0x92ef('0x49')](function(_0xe41074){var _0x781f17=_[_0x92ef('0x4f')](_0xe41074[_0x92ef('0x3b')][_0x92ef('0x3c')]());if(_0x781f17==='User'){_0xd324d9=_[_0x92ef('0x50')](_0xd324d9,_0xe41074[_0x92ef('0x51')]);}else{if(_[_0x92ef('0x3a')](_0x781f17,'queue'))_0x781f17=_0x781f17['replace'](_0x92ef('0x52'),_0x92ef('0x53'));_0xe41074[_0x92ef('0x51')][_0x92ef('0x49')](function(_0x588abe){_0x118fda[_0x92ef('0x54')](getAgents(_0x781f17,_0x588abe));});}});return BPromise[_0x92ef('0x55')](_0x118fda)['then'](function(_0x2d08e3){_0xd324d9=_[_0x92ef('0x50')](_0xd324d9,_[_0x92ef('0x56')](_0x2d08e3));_0x2b92dc(_0xd324d9);})[_0x92ef('0x4c')](function(_0x2ffef6){_0x2870f9(_0x2ffef6);});});}function getAgents(_0x186301,_0x21802a){return new BPromise(function(_0x1b0264,_0x840570){return db[_0x186301][_0x92ef('0x57')]({'where':{'id':_0x21802a}})[_0x92ef('0x22')](function(_0x3481d7){return _0x3481d7['getAgents']({'attributes':['id'],'raw':!![]})['then'](function(_0x530f7b){var _0x1f8508=_[_0x92ef('0x58')](_0x530f7b,'id');_0x1b0264(_0x1f8508);});})[_0x92ef('0x4c')](function(_0x20f661){_0x840570(_0x20f661);});});}