2fde2dea4bfa6d35db4cba92d5410b60aaba806b
[motion2.git] / server / api / notification / notification.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 _0x5f61=['mustache','util','to-csv','ejs','lodash','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','redis','defaults','socket.io-emitter','./notification.socket','register','sendStatus','status','end','count','offset','limit','set','Content-Range','json','apply','update','then','destroy','error','stack','name','send','sendNotification','hasOwnProperty','text','body','TemplateId','Sequelize','view','Missing\x20\x27view\x27\x20parameter','parse','recipients','ValidationError','faxqueue','mailqueue','smsqueue','whatsappqueue','some','includes','model','toLowerCase','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','user','recipientsDescription','beep','SenderId','join','transaction','Notification','forEach','recipient','emit','catch','html','Invalid\x20\x27TemplateId\x27\x20parameter','render','union','queue','replace','Queue','ids','push','findOne','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird'];(function(_0x40f6e3,_0x17073a){var _0x146eb4=function(_0x443377){while(--_0x443377){_0x40f6e3['push'](_0x40f6e3['shift']());}};_0x146eb4(++_0x17073a);}(_0x5f61,0xa7));var _0x15f6=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0x5f61[_0x20a584];return _0x4b41a8;};'use strict';var pdf=require(_0x15f6('0x0'));var emlformat=require(_0x15f6('0x1'));var rimraf=require(_0x15f6('0x2'));var zipdir=require(_0x15f6('0x3'));var jsonpatch=require(_0x15f6('0x4'));var rp=require(_0x15f6('0x5'));var moment=require('moment');var BPromise=require(_0x15f6('0x6'));var Mustache=require(_0x15f6('0x7'));var util=require(_0x15f6('0x8'));var path=require('path');var sox=require('sox');var csv=require(_0x15f6('0x9'));var ejs=require(_0x15f6('0xa'));var fs=require('fs');var _=require(_0x15f6('0xb'));var squel=require('squel');var crypto=require(_0x15f6('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x15f6('0xd'));var toCsv=require('to-csv');var querystring=require(_0x15f6('0xe'));var Papa=require('papaparse');var Redis=require(_0x15f6('0xf'));var authService=require(_0x15f6('0x10'));var qs=require(_0x15f6('0x11'));var as=require(_0x15f6('0x12'));var hardwareService=require(_0x15f6('0x13'));var logger=require(_0x15f6('0x14'))(_0x15f6('0x15'));var utils=require(_0x15f6('0x16'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0x15f6('0x17')]=_[_0x15f6('0x18')](config[_0x15f6('0x17')],{'host':'localhost','port':0x18eb});var socket=require(_0x15f6('0x19'))(new Redis(config['redis']));require(_0x15f6('0x1a'))[_0x15f6('0x1b')](socket);function respondWithStatusCode(_0x1bf34c,_0x3eba64){_0x3eba64=_0x3eba64||0xcc;return function(_0x1723d0){if(_0x1723d0){return _0x1bf34c[_0x15f6('0x1c')](_0x3eba64);}return _0x1bf34c[_0x15f6('0x1d')](_0x3eba64)[_0x15f6('0x1e')]();};}function respondWithResult(_0x4b5d66,_0x406ae6){_0x406ae6=_0x406ae6||0xc8;return function(_0x14c750){if(_0x14c750){return _0x4b5d66['status'](_0x406ae6)['json'](_0x14c750);}};}function respondWithFilteredResult(_0x4dbba9,_0x37e65f){return function(_0x399cc8){if(_0x399cc8){var _0x2d5dd7=_0x399cc8[_0x15f6('0x1f')],_0x4770dd=_0x37e65f[_0x15f6('0x20')],_0x1f8ed3=_0x37e65f[_0x15f6('0x20')]+_0x37e65f[_0x15f6('0x21')],_0x2f61cb;if(_0x1f8ed3>=_0x2d5dd7){_0x1f8ed3=_0x2d5dd7;_0x2f61cb=0xc8;}else{_0x2f61cb=0xce;}_0x4dbba9[_0x15f6('0x1d')](_0x2f61cb);return _0x4dbba9[_0x15f6('0x22')](_0x15f6('0x23'),_0x4770dd+'-'+_0x1f8ed3+'/'+_0x2d5dd7)[_0x15f6('0x24')](_0x399cc8);}return null;};}function patchUpdates(_0x413556){return function(_0x274ef5){try{jsonpatch[_0x15f6('0x25')](_0x274ef5,_0x413556,!![]);}catch(_0x3d9069){return BPromise['reject'](_0x3d9069);}return _0x274ef5['save']();};}function saveUpdates(_0x3a0a4b,_0x31755f){return function(_0x5a7394){if(_0x5a7394){return _0x5a7394[_0x15f6('0x26')](_0x3a0a4b)[_0x15f6('0x27')](function(_0x51f6c0){return _0x51f6c0;});}return null;};}function removeEntity(_0x3e9ee8,_0x484206){return function(_0x11d28e){if(_0x11d28e){return _0x11d28e[_0x15f6('0x28')]()[_0x15f6('0x27')](function(){_0x3e9ee8[_0x15f6('0x1d')](0xcc)['end']();});}};}function handleEntityNotFound(_0x229586,_0xc6fba7){return function(_0x1430ac){if(!_0x1430ac){_0x229586[_0x15f6('0x1c')](0x194);}return _0x1430ac;};}function handleError(_0x297233,_0x1134c0){_0x1134c0=_0x1134c0||0x1f4;return function(_0x432146){logger[_0x15f6('0x29')](_0x432146[_0x15f6('0x2a')]);if(_0x432146['name']){delete _0x432146[_0x15f6('0x2b')];}_0x297233[_0x15f6('0x1d')](_0x1134c0)[_0x15f6('0x2c')](_0x432146);};}exports[_0x15f6('0x2d')]=function(_0x9de652,_0x20c965,_0x52f1fd){if(!_0x9de652['body'][_0x15f6('0x2e')](_0x15f6('0x2f'))&&!_0x9de652[_0x15f6('0x30')][_0x15f6('0x2e')](_0x15f6('0x31'))){throw new db[(_0x15f6('0x32'))]['ValidationError']('Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27');}else{if(_0x9de652[_0x15f6('0x30')][_0x15f6('0x2e')](_0x15f6('0x31'))&&!_0x9de652[_0x15f6('0x30')][_0x15f6('0x2e')](_0x15f6('0x33'))){throw new db[(_0x15f6('0x32'))]['ValidationError'](_0x15f6('0x34'));}if(!_0x9de652[_0x15f6('0x30')]['hasOwnProperty']('recipients')){throw new db[(_0x15f6('0x32'))]['ValidationError']('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x23f7b1=JSON[_0x15f6('0x35')](_0x9de652['body'][_0x15f6('0x36')]);}catch(_0xac67dc){logger[_0x15f6('0x29')](_0xac67dc);throw new db[(_0x15f6('0x32'))][(_0x15f6('0x37'))]('Invalid\x20\x27recipients\x27\x20object');}var _0x451600=['chatqueue',_0x15f6('0x38'),_0x15f6('0x39'),'openchannelqueue',_0x15f6('0x3a'),'voicequeue',_0x15f6('0x3b'),'team','user'];if(_[_0x15f6('0x3c')](_0x23f7b1,function(_0x1714ed){return!_[_0x15f6('0x3d')](_0x451600,_0x1714ed[_0x15f6('0x3e')][_0x15f6('0x3f')]());})){throw new db[(_0x15f6('0x32'))][(_0x15f6('0x37'))](_0x15f6('0x40'));}}}var _0x3a6ad1={};_0x3a6ad1['senderName']=_0x9de652['body'][_0x15f6('0x2e')](_0x15f6('0x41'))?_0x9de652[_0x15f6('0x30')][_0x15f6('0x41')]:_0x9de652[_0x15f6('0x42')]['fullname'];_0x3a6ad1[_0x15f6('0x43')]=_['isEmpty'](_0x9de652['body'][_0x15f6('0x43')])?null:_0x9de652[_0x15f6('0x30')][_0x15f6('0x43')];_0x3a6ad1[_0x15f6('0x44')]=_0x9de652[_0x15f6('0x30')][_0x15f6('0x44')]||![];_0x3a6ad1[_0x15f6('0x45')]=_0x9de652['user']['id'];return getContent(_0x9de652['body'])['then'](function(_0x86861){_0x3a6ad1[_0x15f6('0x30')]=_0x86861;return getRecipients(_0x23f7b1);})[_0x15f6('0x27')](function(_0x36ea87){_0x3a6ad1['recipientsId']=_['sortBy'](_0x36ea87)[_0x15f6('0x46')](',');return db['sequelize'][_0x15f6('0x47')](function(_0x2169f8){return db[_0x15f6('0x48')]['create'](_0x3a6ad1,{'raw':!![],'transaction':_0x2169f8})[_0x15f6('0x27')](function(_0x523aa0){var _0x530f95={'sender':_0x3a6ad1[_0x15f6('0x41')],'description':_0x3a6ad1[_0x15f6('0x43')],'content':_0x3a6ad1[_0x15f6('0x30')],'beep':_0x3a6ad1['beep']};_0x36ea87[_0x15f6('0x49')](function(_0x417861){_0x530f95[_0x15f6('0x4a')]=_0x417861;socket[_0x15f6('0x4b')]('notification:send',_0x530f95);});return _0x523aa0;});});})[_0x15f6('0x27')](respondWithResult(_0x20c965,0xc9))[_0x15f6('0x4c')](handleError(_0x20c965,null));};function getContent(_0x3a2bea){return new BPromise(function(_0x1e0daf,_0x7f63ac){if(_0x3a2bea['text']){_0x1e0daf(_0x3a2bea[_0x15f6('0x2f')]);return;}return db['Template']['findOne']({'attributes':[_0x15f6('0x4d')],'where':{'id':_0x3a2bea['TemplateId']},'raw':!![]})[_0x15f6('0x27')](function(_0x22bb5b){if(!_0x22bb5b)throw new db[(_0x15f6('0x32'))][(_0x15f6('0x37'))](_0x15f6('0x4e'));var _0x3bf131=Mustache[_0x15f6('0x4f')](_0x22bb5b[_0x15f6('0x4d')],_0x3a2bea[_0x15f6('0x33')]);_0x1e0daf(_0x3bf131);})[_0x15f6('0x4c')](function(_0x114d58){_0x7f63ac(_0x114d58);});});}function getRecipients(_0x36d84d){return new BPromise(function(_0x1c87fb,_0x2265e9){var _0x193714=[];var _0xf1c346=[];_0x36d84d['forEach'](function(_0x4e6d8c){var _0x49bb26=_['startCase'](_0x4e6d8c[_0x15f6('0x3e')][_0x15f6('0x3f')]());if(_0x49bb26==='User'){_0x193714=_[_0x15f6('0x50')](_0x193714,_0x4e6d8c['ids']);}else{if(_[_0x15f6('0x3d')](_0x49bb26,_0x15f6('0x51')))_0x49bb26=_0x49bb26[_0x15f6('0x52')](_0x15f6('0x51'),_0x15f6('0x53'));_0x4e6d8c[_0x15f6('0x54')]['forEach'](function(_0x4ef91b){_0xf1c346[_0x15f6('0x55')](getAgents(_0x49bb26,_0x4ef91b));});}});return BPromise['all'](_0xf1c346)[_0x15f6('0x27')](function(_0x46f687){_0x193714=_[_0x15f6('0x50')](_0x193714,_['flatten'](_0x46f687));_0x1c87fb(_0x193714);})[_0x15f6('0x4c')](function(_0x2eea5e){_0x2265e9(_0x2eea5e);});});}function getAgents(_0x491480,_0x4655cb){return new BPromise(function(_0x4108e3,_0x56c3a9){return db[_0x491480][_0x15f6('0x56')]({'where':{'id':_0x4655cb}})['then'](function(_0x3cc6f7){return _0x3cc6f7['getAgents']({'attributes':['id'],'raw':!![]})[_0x15f6('0x27')](function(_0x9da89a){var _0x2b37c5=_['map'](_0x9da89a,'id');_0x4108e3(_0x2b37c5);});})[_0x15f6('0x4c')](function(_0x344515){_0x56c3a9(_0x344515);});});}