Built motion from commit 9efc8423.|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 _0xf810=['Template','html','Invalid\x20\x27TemplateId\x27\x20parameter','render','startCase','User','union','ids','replace','queue','push','all','flatten','catch','findOne','getAgents','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','sox','ejs','fs-extra','lodash','crypto','desk.js','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','apply','reject','save','update','then','error','stack','name','send','sendUserNotification','hasOwnProperty','text','body','TemplateId','Sequelize','ValidationError','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','view','Missing\x20\x27view\x27\x20parameter','recipients','Invalid\x20\x27recipients\x27\x20object','chatqueue','faxqueue','mailqueue','openchannelqueue','smsqueue','voicequeue','user','includes','model','toLowerCase','senderName','recipientsDescription','beep','SenderId','recipientsId','sortBy','join','sequelize','transaction','UserNotification','create','forEach','recipient','emit','userNotification:send'];(function(_0x56fe98,_0x43dc16){var _0x212f4f=function(_0xc3427d){while(--_0xc3427d){_0x56fe98['push'](_0x56fe98['shift']());}};_0x212f4f(++_0x43dc16);}(_0xf810,0x142));var _0x0f81=function(_0x290e51,_0x108e58){_0x290e51=_0x290e51-0x0;var _0x1e901b=_0xf810[_0x290e51];return _0x1e901b;};'use strict';var emlformat=require(_0x0f81('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x0f81('0x1'));var jsonpatch=require(_0x0f81('0x2'));var rp=require(_0x0f81('0x3'));var moment=require(_0x0f81('0x4'));var BPromise=require(_0x0f81('0x5'));var Mustache=require(_0x0f81('0x6'));var util=require('util');var path=require(_0x0f81('0x7'));var sox=require(_0x0f81('0x8'));var csv=require('to-csv');var ejs=require(_0x0f81('0x9'));var fs=require('fs');var fs_extra=require(_0x0f81('0xa'));var _=require(_0x0f81('0xb'));var squel=require('squel');var crypto=require(_0x0f81('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x0f81('0xd'));var toCsv=require(_0x0f81('0xe'));var querystring=require('querystring');var Papa=require(_0x0f81('0xf'));var Redis=require(_0x0f81('0x10'));var authService=require(_0x0f81('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x0f81('0x12'));var hardwareService=require(_0x0f81('0x13'));var logger=require(_0x0f81('0x14'))(_0x0f81('0x15'));var utils=require(_0x0f81('0x16'));var config=require(_0x0f81('0x17'));var licenseUtil=require(_0x0f81('0x18'));var db=require(_0x0f81('0x19'))['db'];config[_0x0f81('0x1a')]=_[_0x0f81('0x1b')](config[_0x0f81('0x1a')],{'host':_0x0f81('0x1c'),'port':0x18eb});var socket=require(_0x0f81('0x1d'))(new Redis(config['redis']));require('./userNotification.socket')['register'](socket);function respondWithStatusCode(_0x2f7c16,_0x3e26fd){_0x3e26fd=_0x3e26fd||0xcc;return function(_0xed9406){if(_0xed9406){return _0x2f7c16[_0x0f81('0x1e')](_0x3e26fd);}return _0x2f7c16[_0x0f81('0x1f')](_0x3e26fd)[_0x0f81('0x20')]();};}function respondWithResult(_0x1bebd3,_0x3a495e){_0x3a495e=_0x3a495e||0xc8;return function(_0x133226){if(_0x133226){return _0x1bebd3['status'](_0x3a495e)[_0x0f81('0x21')](_0x133226);}};}function respondWithFilteredResult(_0x531a22,_0x4966af){return function(_0x3dddf2){if(_0x3dddf2){var _0x2141a1=typeof _0x4966af[_0x0f81('0x22')]===_0x0f81('0x23')&&typeof _0x4966af['limit']==='undefined';var _0x31d4a8=_0x3dddf2[_0x0f81('0x24')];var _0x5d0545=_0x2141a1?0x0:_0x4966af['offset'];var _0x4a036b=_0x2141a1?_0x3dddf2['count']:_0x4966af[_0x0f81('0x22')]+_0x4966af[_0x0f81('0x25')];var _0x1da162;if(_0x4a036b>=_0x31d4a8){_0x4a036b=_0x31d4a8;_0x1da162=0xc8;}else{_0x1da162=0xce;}_0x531a22[_0x0f81('0x1f')](_0x1da162);return _0x531a22[_0x0f81('0x26')](_0x0f81('0x27'),_0x5d0545+'-'+_0x4a036b+'/'+_0x31d4a8)[_0x0f81('0x21')](_0x3dddf2);}return null;};}function patchUpdates(_0x361a4e){return function(_0x283616){try{jsonpatch[_0x0f81('0x28')](_0x283616,_0x361a4e,!![]);}catch(_0x421e9a){return BPromise[_0x0f81('0x29')](_0x421e9a);}return _0x283616[_0x0f81('0x2a')]();};}function saveUpdates(_0x3c4a0d,_0x1f67ae){return function(_0x44689a){if(_0x44689a){return _0x44689a[_0x0f81('0x2b')](_0x3c4a0d)['then'](function(_0x49c223){return _0x49c223;});}return null;};}function removeEntity(_0x4f60bf,_0x56b705){return function(_0x219560){if(_0x219560){return _0x219560['destroy']()[_0x0f81('0x2c')](function(){_0x4f60bf['status'](0xcc)[_0x0f81('0x20')]();});}};}function handleEntityNotFound(_0x446232,_0x3c9af0){return function(_0x2106b5){if(!_0x2106b5){_0x446232[_0x0f81('0x1e')](0x194);}return _0x2106b5;};}function handleError(_0x279d3e,_0x156fe9){_0x156fe9=_0x156fe9||0x1f4;return function(_0x1e2203){logger[_0x0f81('0x2d')](_0x1e2203[_0x0f81('0x2e')]);if(_0x1e2203[_0x0f81('0x2f')]){delete _0x1e2203[_0x0f81('0x2f')];}_0x279d3e[_0x0f81('0x1f')](_0x156fe9)[_0x0f81('0x30')](_0x1e2203);};}exports[_0x0f81('0x31')]=function(_0x51b641,_0xfd1ab,_0x187d83){if(!_0x51b641['body'][_0x0f81('0x32')](_0x0f81('0x33'))&&!_0x51b641[_0x0f81('0x34')]['hasOwnProperty'](_0x0f81('0x35'))){throw new db[(_0x0f81('0x36'))][(_0x0f81('0x37'))](_0x0f81('0x38'));}else{if(_0x51b641[_0x0f81('0x34')][_0x0f81('0x32')](_0x0f81('0x35'))&&!_0x51b641[_0x0f81('0x34')][_0x0f81('0x32')](_0x0f81('0x39'))){throw new db[(_0x0f81('0x36'))][(_0x0f81('0x37'))](_0x0f81('0x3a'));}if(!_0x51b641[_0x0f81('0x34')][_0x0f81('0x32')]('recipients')){throw new db['Sequelize'][(_0x0f81('0x37'))]('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x58b8ed=JSON['parse'](_0x51b641[_0x0f81('0x34')][_0x0f81('0x3b')]);}catch(_0x4d15a9){logger[_0x0f81('0x2d')](_0x4d15a9);throw new db[(_0x0f81('0x36'))][(_0x0f81('0x37'))](_0x0f81('0x3c'));}var _0x30107e=[_0x0f81('0x3d'),_0x0f81('0x3e'),_0x0f81('0x3f'),_0x0f81('0x40'),_0x0f81('0x41'),_0x0f81('0x42'),'whatsappqueue','team',_0x0f81('0x43')];if(_['some'](_0x58b8ed,function(_0x45f179){return!_[_0x0f81('0x44')](_0x30107e,_0x45f179[_0x0f81('0x45')][_0x0f81('0x46')]());})){throw new db['Sequelize'][(_0x0f81('0x37'))]('Invalid\x20model\x20in\x20\x27recipients\x27\x20object');}}}var _0x1e8f32={};_0x1e8f32[_0x0f81('0x47')]=_0x51b641[_0x0f81('0x34')][_0x0f81('0x32')](_0x0f81('0x47'))?_0x51b641[_0x0f81('0x34')][_0x0f81('0x47')]:_0x51b641['user']['fullname'];_0x1e8f32[_0x0f81('0x48')]=_['isEmpty'](_0x51b641[_0x0f81('0x34')][_0x0f81('0x48')])?null:_0x51b641[_0x0f81('0x34')]['recipientsDescription'];_0x1e8f32['beep']=_0x51b641['body'][_0x0f81('0x49')]||![];_0x1e8f32[_0x0f81('0x4a')]=_0x51b641['user']['id'];return getContent(_0x51b641['body'])[_0x0f81('0x2c')](function(_0x4e5e56){_0x1e8f32[_0x0f81('0x34')]=_0x4e5e56;return getRecipients(_0x58b8ed);})[_0x0f81('0x2c')](function(_0x15af8c){_0x1e8f32[_0x0f81('0x4b')]=_[_0x0f81('0x4c')](_0x15af8c)[_0x0f81('0x4d')](',');return db[_0x0f81('0x4e')][_0x0f81('0x4f')](function(_0x5558c0){return db[_0x0f81('0x50')][_0x0f81('0x51')](_0x1e8f32,{'raw':!![],'transaction':_0x5558c0})[_0x0f81('0x2c')](function(_0x1099e4){var _0x676e38={'sender':_0x1e8f32['senderName'],'description':_0x1e8f32[_0x0f81('0x48')],'content':_0x1e8f32[_0x0f81('0x34')],'beep':_0x1e8f32[_0x0f81('0x49')]};_0x15af8c[_0x0f81('0x52')](function(_0x2a83b6){_0x676e38[_0x0f81('0x53')]=_0x2a83b6;socket[_0x0f81('0x54')](_0x0f81('0x55'),_0x676e38);});return _0x1099e4;});});})['then'](respondWithResult(_0xfd1ab,0xc9))['catch'](handleError(_0xfd1ab,null));};function getContent(_0x243b5b){return new BPromise(function(_0x4743dd,_0x31d478){if(_0x243b5b[_0x0f81('0x33')]){_0x4743dd(_0x243b5b['text']);return;}return db[_0x0f81('0x56')]['findOne']({'attributes':[_0x0f81('0x57')],'where':{'id':_0x243b5b[_0x0f81('0x35')]},'raw':!![]})[_0x0f81('0x2c')](function(_0x2cea11){if(!_0x2cea11)throw new db['Sequelize'][(_0x0f81('0x37'))](_0x0f81('0x58'));var _0x193995=Mustache[_0x0f81('0x59')](_0x2cea11['html'],_0x243b5b['view']);_0x4743dd(_0x193995);})['catch'](function(_0x445d12){_0x31d478(_0x445d12);});});}function getRecipients(_0x257ed6){return new BPromise(function(_0x236cd2,_0x24b2cb){var _0xedb32f=[];var _0x3602f2=[];_0x257ed6[_0x0f81('0x52')](function(_0x316a6a){var _0x3bf857=_[_0x0f81('0x5a')](_0x316a6a['model']['toLowerCase']());if(_0x3bf857===_0x0f81('0x5b')){_0xedb32f=_[_0x0f81('0x5c')](_0xedb32f,_0x316a6a[_0x0f81('0x5d')]);}else{if(_['includes'](_0x3bf857,'queue'))_0x3bf857=_0x3bf857[_0x0f81('0x5e')](_0x0f81('0x5f'),'Queue');_0x316a6a[_0x0f81('0x5d')][_0x0f81('0x52')](function(_0x4e6427){_0x3602f2[_0x0f81('0x60')](getAgents(_0x3bf857,_0x4e6427));});}});return BPromise[_0x0f81('0x61')](_0x3602f2)[_0x0f81('0x2c')](function(_0x4f3171){_0xedb32f=_[_0x0f81('0x5c')](_0xedb32f,_[_0x0f81('0x62')](_0x4f3171));_0x236cd2(_0xedb32f);})[_0x0f81('0x63')](function(_0x3e6ea8){_0x24b2cb(_0x3e6ea8);});});}function getAgents(_0x6f3c89,_0x11a99c){return new BPromise(function(_0x280d3a,_0x4d498a){return db[_0x6f3c89][_0x0f81('0x64')]({'where':{'id':_0x11a99c}})['then'](function(_0x481176){return _0x481176[_0x0f81('0x65')]({'attributes':['id'],'raw':!![]})[_0x0f81('0x2c')](function(_0x24bb99){var _0x490a19=_['map'](_0x24bb99,'id');_0x280d3a(_0x490a19);});})[_0x0f81('0x63')](function(_0x392f22){_0x4d498a(_0x392f22);});});}