9ee41d3dd8b77ecf9ac098d45655cc80dbb1cb56
[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 _0x7b4a=['text','TemplateId','Sequelize','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','ValidationError','recipients','parse','Invalid\x20\x27recipients\x27\x20object','chatqueue','faxqueue','mailqueue','smsqueue','whatsappqueue','team','user','includes','model','toLowerCase','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','fullname','isEmpty','recipientsDescription','beep','SenderId','sortBy','join','sequelize','transaction','UserNotification','emit','userNotification:send','catch','Template','findOne','Invalid\x20\x27TemplateId\x27\x20parameter','render','html','view','startCase','union','ids','queue','replace','forEach','push','all','getAgents','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','path','sox','lodash','crypto','to-csv','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','redis','defaults','socket.io-emitter','./userNotification.socket','register','sendStatus','status','undefined','count','offset','limit','Content-Range','json','apply','reject','save','update','then','destroy','end','error','stack','name','send','sendUserNotification','body','hasOwnProperty'];(function(_0x18ee80,_0x2adf1c){var _0x2f60c2=function(_0x2bd265){while(--_0x2bd265){_0x18ee80['push'](_0x18ee80['shift']());}};_0x2f60c2(++_0x2adf1c);}(_0x7b4a,0x8e));var _0xa7b4=function(_0x2223be,_0x4e4034){_0x2223be=_0x2223be-0x0;var _0x453496=_0x7b4a[_0x2223be];return _0x453496;};'use strict';var emlformat=require(_0xa7b4('0x0'));var rimraf=require(_0xa7b4('0x1'));var zipdir=require(_0xa7b4('0x2'));var jsonpatch=require(_0xa7b4('0x3'));var rp=require(_0xa7b4('0x4'));var moment=require('moment');var BPromise=require(_0xa7b4('0x5'));var Mustache=require('mustache');var util=require('util');var path=require(_0xa7b4('0x6'));var sox=require(_0xa7b4('0x7'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xa7b4('0x8'));var squel=require('squel');var crypto=require(_0xa7b4('0x9'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xa7b4('0xa'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xa7b4('0xb'));var authService=require('../../components/auth/service');var qs=require(_0xa7b4('0xc'));var as=require(_0xa7b4('0xd'));var hardwareService=require(_0xa7b4('0xe'));var logger=require(_0xa7b4('0xf'))(_0xa7b4('0x10'));var utils=require(_0xa7b4('0x11'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xa7b4('0x12'))['db'];config[_0xa7b4('0x13')]=_[_0xa7b4('0x14')](config[_0xa7b4('0x13')],{'host':'localhost','port':0x18eb});var socket=require(_0xa7b4('0x15'))(new Redis(config['redis']));require(_0xa7b4('0x16'))[_0xa7b4('0x17')](socket);function respondWithStatusCode(_0x2ab334,_0x3e94d4){_0x3e94d4=_0x3e94d4||0xcc;return function(_0x220ff5){if(_0x220ff5){return _0x2ab334[_0xa7b4('0x18')](_0x3e94d4);}return _0x2ab334[_0xa7b4('0x19')](_0x3e94d4)['end']();};}function respondWithResult(_0x1f1ad6,_0x3588d9){_0x3588d9=_0x3588d9||0xc8;return function(_0x5ebd7a){if(_0x5ebd7a){return _0x1f1ad6[_0xa7b4('0x19')](_0x3588d9)['json'](_0x5ebd7a);}};}function respondWithFilteredResult(_0x235d4a,_0x49d8a1){return function(_0x20e6d0){if(_0x20e6d0){var _0x140126=typeof _0x49d8a1['offset']===_0xa7b4('0x1a')&&typeof _0x49d8a1['limit']===_0xa7b4('0x1a');var _0x1859d8=_0x20e6d0[_0xa7b4('0x1b')];var _0x5d38a6=_0x140126?0x0:_0x49d8a1[_0xa7b4('0x1c')];var _0x420b49=_0x140126?_0x20e6d0[_0xa7b4('0x1b')]:_0x49d8a1[_0xa7b4('0x1c')]+_0x49d8a1[_0xa7b4('0x1d')];var _0x2c4133;if(_0x420b49>=_0x1859d8){_0x420b49=_0x1859d8;_0x2c4133=0xc8;}else{_0x2c4133=0xce;}_0x235d4a[_0xa7b4('0x19')](_0x2c4133);return _0x235d4a['set'](_0xa7b4('0x1e'),_0x5d38a6+'-'+_0x420b49+'/'+_0x1859d8)[_0xa7b4('0x1f')](_0x20e6d0);}return null;};}function patchUpdates(_0x2c425f){return function(_0x5026d0){try{jsonpatch[_0xa7b4('0x20')](_0x5026d0,_0x2c425f,!![]);}catch(_0x3b68ed){return BPromise[_0xa7b4('0x21')](_0x3b68ed);}return _0x5026d0[_0xa7b4('0x22')]();};}function saveUpdates(_0x58c769,_0x253dc5){return function(_0x3e9123){if(_0x3e9123){return _0x3e9123[_0xa7b4('0x23')](_0x58c769)[_0xa7b4('0x24')](function(_0x2aaaca){return _0x2aaaca;});}return null;};}function removeEntity(_0x49efc3,_0x478bad){return function(_0x25d19d){if(_0x25d19d){return _0x25d19d[_0xa7b4('0x25')]()[_0xa7b4('0x24')](function(){_0x49efc3['status'](0xcc)[_0xa7b4('0x26')]();});}};}function handleEntityNotFound(_0x5bf757,_0x38a64b){return function(_0x3fa9ca){if(!_0x3fa9ca){_0x5bf757[_0xa7b4('0x18')](0x194);}return _0x3fa9ca;};}function handleError(_0x4603b5,_0x2be053){_0x2be053=_0x2be053||0x1f4;return function(_0x201598){logger[_0xa7b4('0x27')](_0x201598[_0xa7b4('0x28')]);if(_0x201598[_0xa7b4('0x29')]){delete _0x201598[_0xa7b4('0x29')];}_0x4603b5[_0xa7b4('0x19')](_0x2be053)[_0xa7b4('0x2a')](_0x201598);};}exports[_0xa7b4('0x2b')]=function(_0x1e9a2c,_0x1aa3cc,_0x5c330a){if(!_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x2d')](_0xa7b4('0x2e'))&&!_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x2d')](_0xa7b4('0x2f'))){throw new db[(_0xa7b4('0x30'))]['ValidationError'](_0xa7b4('0x31'));}else{if(_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x2d')](_0xa7b4('0x2f'))&&!_0x1e9a2c['body'][_0xa7b4('0x2d')]('view')){throw new db[(_0xa7b4('0x30'))][(_0xa7b4('0x32'))]('Missing\x20\x27view\x27\x20parameter');}if(!_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x2d')](_0xa7b4('0x33'))){throw new db['Sequelize'][(_0xa7b4('0x32'))]('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x2199e0=JSON[_0xa7b4('0x34')](_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x33')]);}catch(_0x4788cf){logger['error'](_0x4788cf);throw new db['Sequelize'][(_0xa7b4('0x32'))](_0xa7b4('0x35'));}var _0x195ef1=[_0xa7b4('0x36'),_0xa7b4('0x37'),_0xa7b4('0x38'),'openchannelqueue',_0xa7b4('0x39'),'voicequeue',_0xa7b4('0x3a'),_0xa7b4('0x3b'),_0xa7b4('0x3c')];if(_['some'](_0x2199e0,function(_0x57c8b4){return!_[_0xa7b4('0x3d')](_0x195ef1,_0x57c8b4[_0xa7b4('0x3e')][_0xa7b4('0x3f')]());})){throw new db[(_0xa7b4('0x30'))][(_0xa7b4('0x32'))](_0xa7b4('0x40'));}}}var _0x296b4d={};_0x296b4d[_0xa7b4('0x41')]=_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x2d')](_0xa7b4('0x41'))?_0x1e9a2c[_0xa7b4('0x2c')]['senderName']:_0x1e9a2c['user'][_0xa7b4('0x42')];_0x296b4d['recipientsDescription']=_[_0xa7b4('0x43')](_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x44')])?null:_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x44')];_0x296b4d[_0xa7b4('0x45')]=_0x1e9a2c[_0xa7b4('0x2c')][_0xa7b4('0x45')]||![];_0x296b4d[_0xa7b4('0x46')]=_0x1e9a2c[_0xa7b4('0x3c')]['id'];return getContent(_0x1e9a2c['body'])[_0xa7b4('0x24')](function(_0x350eff){_0x296b4d['body']=_0x350eff;return getRecipients(_0x2199e0);})[_0xa7b4('0x24')](function(_0x2f4077){_0x296b4d['recipientsId']=_[_0xa7b4('0x47')](_0x2f4077)[_0xa7b4('0x48')](',');return db[_0xa7b4('0x49')][_0xa7b4('0x4a')](function(_0x59ab8f){return db[_0xa7b4('0x4b')]['create'](_0x296b4d,{'raw':!![],'transaction':_0x59ab8f})[_0xa7b4('0x24')](function(_0x5c64c4){var _0x55b3dd={'sender':_0x296b4d['senderName'],'description':_0x296b4d[_0xa7b4('0x44')],'content':_0x296b4d['body'],'beep':_0x296b4d['beep']};_0x2f4077['forEach'](function(_0x341646){_0x55b3dd['recipient']=_0x341646;socket[_0xa7b4('0x4c')](_0xa7b4('0x4d'),_0x55b3dd);});return _0x5c64c4;});});})[_0xa7b4('0x24')](respondWithResult(_0x1aa3cc,0xc9))[_0xa7b4('0x4e')](handleError(_0x1aa3cc,null));};function getContent(_0x1f34cb){return new BPromise(function(_0x1062ac,_0x5405c9){if(_0x1f34cb[_0xa7b4('0x2e')]){_0x1062ac(_0x1f34cb[_0xa7b4('0x2e')]);return;}return db[_0xa7b4('0x4f')][_0xa7b4('0x50')]({'attributes':['html'],'where':{'id':_0x1f34cb[_0xa7b4('0x2f')]},'raw':!![]})[_0xa7b4('0x24')](function(_0x298601){if(!_0x298601)throw new db['Sequelize']['ValidationError'](_0xa7b4('0x51'));var _0x23d3a8=Mustache[_0xa7b4('0x52')](_0x298601[_0xa7b4('0x53')],_0x1f34cb[_0xa7b4('0x54')]);_0x1062ac(_0x23d3a8);})['catch'](function(_0x1f1b2a){_0x5405c9(_0x1f1b2a);});});}function getRecipients(_0x167563){return new BPromise(function(_0x3ea077,_0x27f951){var _0x9dad8c=[];var _0x5e4235=[];_0x167563['forEach'](function(_0x1aea9e){var _0x140d1b=_[_0xa7b4('0x55')](_0x1aea9e[_0xa7b4('0x3e')][_0xa7b4('0x3f')]());if(_0x140d1b==='User'){_0x9dad8c=_[_0xa7b4('0x56')](_0x9dad8c,_0x1aea9e[_0xa7b4('0x57')]);}else{if(_[_0xa7b4('0x3d')](_0x140d1b,_0xa7b4('0x58')))_0x140d1b=_0x140d1b[_0xa7b4('0x59')](_0xa7b4('0x58'),'Queue');_0x1aea9e[_0xa7b4('0x57')][_0xa7b4('0x5a')](function(_0x5082ee){_0x5e4235[_0xa7b4('0x5b')](getAgents(_0x140d1b,_0x5082ee));});}});return BPromise[_0xa7b4('0x5c')](_0x5e4235)[_0xa7b4('0x24')](function(_0x413d93){_0x9dad8c=_[_0xa7b4('0x56')](_0x9dad8c,_['flatten'](_0x413d93));_0x3ea077(_0x9dad8c);})[_0xa7b4('0x4e')](function(_0x4c438e){_0x27f951(_0x4c438e);});});}function getAgents(_0x289815,_0x58c24b){return new BPromise(function(_0x1b07ed,_0x2037fc){return db[_0x289815][_0xa7b4('0x50')]({'where':{'id':_0x58c24b}})['then'](function(_0x3cfd4f){return _0x3cfd4f[_0xa7b4('0x5d')]({'attributes':['id'],'raw':!![]})[_0xa7b4('0x24')](function(_0x527102){var _0x10d104=_['map'](_0x527102,'id');_0x1b07ed(_0x10d104);});})[_0xa7b4('0x4e')](function(_0x8550ae){_0x2037fc(_0x8550ae);});});}