0f374088a6f6c4b36e483f7b4064614d74698a34
[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 _0x189f=['recipients','Invalid\x20\x27recipients\x27\x20object','chatqueue','faxqueue','mailqueue','openchannelqueue','smsqueue','voicequeue','team','some','includes','model','senderName','user','fullname','recipientsDescription','isEmpty','beep','SenderId','recipientsId','sortBy','join','sequelize','transaction','create','forEach','recipient','emit','notification:send','catch','Template','findOne','html','Invalid\x20\x27TemplateId\x27\x20parameter','render','startCase','toLowerCase','User','queue','Queue','ids','all','union','flatten','getAgents','map','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','mustache','util','path','ejs','lodash','squel','crypto','jsforce','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','redis','localhost','socket.io-emitter','sendStatus','status','end','json','count','offset','limit','set','reject','update','then','error','stack','send','sendNotification','hasOwnProperty','text','body','TemplateId','Sequelize','ValidationError','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','view'];(function(_0x211db0,_0x4356c8){var _0x805e20=function(_0x1503b9){while(--_0x1503b9){_0x211db0['push'](_0x211db0['shift']());}};_0x805e20(++_0x4356c8);}(_0x189f,0x145));var _0xf189=function(_0x4d79bc,_0x32c905){_0x4d79bc=_0x4d79bc-0x0;var _0x4b4760=_0x189f[_0x4d79bc];return _0x4b4760;};'use strict';var pdf=require(_0xf189('0x0'));var emlformat=require(_0xf189('0x1'));var rimraf=require(_0xf189('0x2'));var zipdir=require(_0xf189('0x3'));var jsonpatch=require(_0xf189('0x4'));var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0xf189('0x5'));var util=require(_0xf189('0x6'));var path=require(_0xf189('0x7'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xf189('0x8'));var fs=require('fs');var _=require(_0xf189('0x9'));var squel=require(_0xf189('0xa'));var crypto=require(_0xf189('0xb'));var jsforce=require(_0xf189('0xc'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0xf189('0xd'));var Papa=require(_0xf189('0xe'));var Redis=require('ioredis');var authService=require(_0xf189('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0xf189('0x10'));var hardwareService=require(_0xf189('0x11'));var logger=require(_0xf189('0x12'))(_0xf189('0x13'));var utils=require('../../config/utils');var config=require(_0xf189('0x14'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];config[_0xf189('0x15')]=_['defaults'](config[_0xf189('0x15')],{'host':_0xf189('0x16'),'port':0x18eb});var socket=require(_0xf189('0x17'))(new Redis(config['redis']));require('./notification.socket')['register'](socket);function respondWithStatusCode(_0x1634f2,_0x30bda1){_0x30bda1=_0x30bda1||0xcc;return function(_0x1e05d3){if(_0x1e05d3){return _0x1634f2[_0xf189('0x18')](_0x30bda1);}return _0x1634f2[_0xf189('0x19')](_0x30bda1)[_0xf189('0x1a')]();};}function respondWithResult(_0x5b7889,_0x40f6b0){_0x40f6b0=_0x40f6b0||0xc8;return function(_0x5e458c){if(_0x5e458c){return _0x5b7889[_0xf189('0x19')](_0x40f6b0)[_0xf189('0x1b')](_0x5e458c);}};}function respondWithFilteredResult(_0x568e4a,_0x5c5c55){return function(_0x543678){if(_0x543678){var _0x1b1e31=_0x543678[_0xf189('0x1c')],_0x2ec131=_0x5c5c55[_0xf189('0x1d')],_0x4912a6=_0x5c5c55[_0xf189('0x1d')]+_0x5c5c55[_0xf189('0x1e')],_0x52f4b8;if(_0x4912a6>=_0x1b1e31){_0x4912a6=_0x1b1e31;_0x52f4b8=0xc8;}else{_0x52f4b8=0xce;}_0x568e4a['status'](_0x52f4b8);return _0x568e4a[_0xf189('0x1f')]('Content-Range',_0x2ec131+'-'+_0x4912a6+'/'+_0x1b1e31)[_0xf189('0x1b')](_0x543678);}return null;};}function patchUpdates(_0x24c868){return function(_0x15ccd8){try{jsonpatch['apply'](_0x15ccd8,_0x24c868,!![]);}catch(_0x479085){return BPromise[_0xf189('0x20')](_0x479085);}return _0x15ccd8['save']();};}function saveUpdates(_0x507d4c,_0x1980da){return function(_0x21c240){if(_0x21c240){return _0x21c240[_0xf189('0x21')](_0x507d4c)[_0xf189('0x22')](function(_0xc28f41){return _0xc28f41;});}return null;};}function removeEntity(_0x17ee9c,_0x844a6){return function(_0xe70b39){if(_0xe70b39){return _0xe70b39['destroy']()['then'](function(){_0x17ee9c[_0xf189('0x19')](0xcc)[_0xf189('0x1a')]();});}};}function handleEntityNotFound(_0x3b6d81,_0x59730f){return function(_0x340697){if(!_0x340697){_0x3b6d81['sendStatus'](0x194);}return _0x340697;};}function handleError(_0x48eb70,_0x171e79){_0x171e79=_0x171e79||0x1f4;return function(_0x277546){logger[_0xf189('0x23')](_0x277546[_0xf189('0x24')]);if(_0x277546['name']){delete _0x277546['name'];}_0x48eb70[_0xf189('0x19')](_0x171e79)[_0xf189('0x25')](_0x277546);};}exports[_0xf189('0x26')]=function(_0x183af1,_0x31678e,_0x51a9b1){if(!_0x183af1['body'][_0xf189('0x27')](_0xf189('0x28'))&&!_0x183af1[_0xf189('0x29')]['hasOwnProperty'](_0xf189('0x2a'))){throw new db[(_0xf189('0x2b'))][(_0xf189('0x2c'))](_0xf189('0x2d'));}else{if(_0x183af1[_0xf189('0x29')][_0xf189('0x27')](_0xf189('0x2a'))&&!_0x183af1[_0xf189('0x29')]['hasOwnProperty'](_0xf189('0x2e'))){throw new db[(_0xf189('0x2b'))][(_0xf189('0x2c'))]('Missing\x20\x27view\x27\x20parameter');}if(!_0x183af1['body'][_0xf189('0x27')](_0xf189('0x2f'))){throw new db[(_0xf189('0x2b'))]['ValidationError']('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x5d8c08=JSON['parse'](_0x183af1[_0xf189('0x29')]['recipients']);}catch(_0x8de851){logger[_0xf189('0x23')](_0x8de851);throw new db[(_0xf189('0x2b'))][(_0xf189('0x2c'))](_0xf189('0x30'));}var _0x1f0bf4=[_0xf189('0x31'),_0xf189('0x32'),_0xf189('0x33'),_0xf189('0x34'),_0xf189('0x35'),_0xf189('0x36'),'whatsappqueue',_0xf189('0x37'),'user'];if(_[_0xf189('0x38')](_0x5d8c08,function(_0x4f4440){return!_[_0xf189('0x39')](_0x1f0bf4,_0x4f4440[_0xf189('0x3a')]['toLowerCase']());})){throw new db[(_0xf189('0x2b'))][(_0xf189('0x2c'))]('Invalid\x20model\x20in\x20\x27recipients\x27\x20object');}}}var _0xb2f2e={};_0xb2f2e[_0xf189('0x3b')]=_0x183af1[_0xf189('0x29')][_0xf189('0x27')]('senderName')?_0x183af1['body']['senderName']:_0x183af1[_0xf189('0x3c')][_0xf189('0x3d')];_0xb2f2e[_0xf189('0x3e')]=_[_0xf189('0x3f')](_0x183af1[_0xf189('0x29')][_0xf189('0x3e')])?null:_0x183af1[_0xf189('0x29')][_0xf189('0x3e')];_0xb2f2e[_0xf189('0x40')]=_0x183af1['body'][_0xf189('0x40')]||![];_0xb2f2e[_0xf189('0x41')]=_0x183af1[_0xf189('0x3c')]['id'];return getContent(_0x183af1['body'])['then'](function(_0x27dd45){_0xb2f2e['body']=_0x27dd45;return getRecipients(_0x5d8c08);})[_0xf189('0x22')](function(_0x337753){_0xb2f2e[_0xf189('0x42')]=_[_0xf189('0x43')](_0x337753)[_0xf189('0x44')](',');return db[_0xf189('0x45')][_0xf189('0x46')](function(_0x33ec76){return db['Notification'][_0xf189('0x47')](_0xb2f2e,{'raw':!![],'transaction':_0x33ec76})[_0xf189('0x22')](function(_0x165bdf){var _0x122bbd={'sender':_0xb2f2e[_0xf189('0x3b')],'description':_0xb2f2e['recipientsDescription'],'content':_0xb2f2e[_0xf189('0x29')],'beep':_0xb2f2e[_0xf189('0x40')]};_0x337753[_0xf189('0x48')](function(_0x36797f){_0x122bbd[_0xf189('0x49')]=_0x36797f;socket[_0xf189('0x4a')](_0xf189('0x4b'),_0x122bbd);});return _0x165bdf;});});})[_0xf189('0x22')](respondWithResult(_0x31678e,0xc9))[_0xf189('0x4c')](handleError(_0x31678e,null));};function getContent(_0x54d5e1){return new BPromise(function(_0x1af6fd,_0x1a4ed3){if(_0x54d5e1[_0xf189('0x28')]){_0x1af6fd(_0x54d5e1['text']);return;}return db[_0xf189('0x4d')][_0xf189('0x4e')]({'attributes':[_0xf189('0x4f')],'where':{'id':_0x54d5e1[_0xf189('0x2a')]},'raw':!![]})[_0xf189('0x22')](function(_0x5ecf24){if(!_0x5ecf24)throw new db[(_0xf189('0x2b'))][(_0xf189('0x2c'))](_0xf189('0x50'));var _0x278d27=Mustache[_0xf189('0x51')](_0x5ecf24['html'],_0x54d5e1[_0xf189('0x2e')]);_0x1af6fd(_0x278d27);})[_0xf189('0x4c')](function(_0x346beb){_0x1a4ed3(_0x346beb);});});}function getRecipients(_0x253442){return new BPromise(function(_0x5e650f,_0xf66512){var _0x3e7288=[];var _0x44f87e=[];_0x253442[_0xf189('0x48')](function(_0x14d488){var _0x3274f2=_[_0xf189('0x52')](_0x14d488[_0xf189('0x3a')][_0xf189('0x53')]());if(_0x3274f2===_0xf189('0x54')){_0x3e7288=_['union'](_0x3e7288,_0x14d488['ids']);}else{if(_[_0xf189('0x39')](_0x3274f2,_0xf189('0x55')))_0x3274f2=_0x3274f2['replace'](_0xf189('0x55'),_0xf189('0x56'));_0x14d488[_0xf189('0x57')][_0xf189('0x48')](function(_0x409056){_0x44f87e['push'](getAgents(_0x3274f2,_0x409056));});}});return BPromise[_0xf189('0x58')](_0x44f87e)['then'](function(_0x37bfa7){_0x3e7288=_[_0xf189('0x59')](_0x3e7288,_[_0xf189('0x5a')](_0x37bfa7));_0x5e650f(_0x3e7288);})[_0xf189('0x4c')](function(_0x425a5f){_0xf66512(_0x425a5f);});});}function getAgents(_0x135c2f,_0x107c19){return new BPromise(function(_0x3f5a91,_0x58088c){return db[_0x135c2f][_0xf189('0x4e')]({'where':{'id':_0x107c19}})['then'](function(_0x39ec65){return _0x39ec65[_0xf189('0x5b')]({'attributes':['id'],'raw':!![]})['then'](function(_0xb586e){var _0x10fefb=_[_0xf189('0x5c')](_0xb586e,'id');_0x3f5a91(_0x10fefb);});})[_0xf189('0x4c')](function(_0x584061){_0x58088c(_0x584061);});});}