Built motion from commit (unavailable).|2.5.2
[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 _0x36e6=['sendStatus','status','end','count','offset','limit','Content-Range','apply','reject','save','update','then','destroy','name','body','hasOwnProperty','text','Sequelize','ValidationError','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','Missing\x20\x27view\x27\x20parameter','recipients','parse','error','chatqueue','faxqueue','mailqueue','openchannelqueue','voicequeue','whatsappqueue','team','user','some','includes','model','toLowerCase','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','fullname','recipientsDescription','isEmpty','beep','SenderId','recipientsId','join','sequelize','transaction','Notification','emit','catch','Template','findOne','html','Invalid\x20\x27TemplateId\x27\x20parameter','render','view','forEach','User','union','queue','replace','Queue','ids','all','getAgents','map','html-pdf','eml-format','rimraf','zip-dir','request-promise','moment','path','sox','lodash','squel','crypto','to-csv','querystring','../../components/auth/service','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./notification.socket','register'];(function(_0x3a5565,_0x334bdb){var _0x2c2574=function(_0x26ab73){while(--_0x26ab73){_0x3a5565['push'](_0x3a5565['shift']());}};_0x2c2574(++_0x334bdb);}(_0x36e6,0x1aa));var _0x636e=function(_0x3ab51d,_0x2fa01d){_0x3ab51d=_0x3ab51d-0x0;var _0x29a04e=_0x36e6[_0x3ab51d];return _0x29a04e;};'use strict';var pdf=require(_0x636e('0x0'));var emlformat=require(_0x636e('0x1'));var rimraf=require(_0x636e('0x2'));var zipdir=require(_0x636e('0x3'));var jsonpatch=require('fast-json-patch');var rp=require(_0x636e('0x4'));var moment=require(_0x636e('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require('util');var path=require(_0x636e('0x6'));var sox=require(_0x636e('0x7'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0x636e('0x8'));var squel=require(_0x636e('0x9'));var crypto=require(_0x636e('0xa'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x636e('0xb'));var querystring=require(_0x636e('0xc'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x636e('0xd'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x636e('0xe'))(_0x636e('0xf'));var utils=require('../../config/utils');var config=require(_0x636e('0x10'));var db=require(_0x636e('0x11'))['db'];config[_0x636e('0x12')]=_[_0x636e('0x13')](config['redis'],{'host':_0x636e('0x14'),'port':0x18eb});var socket=require(_0x636e('0x15'))(new Redis(config[_0x636e('0x12')]));require(_0x636e('0x16'))[_0x636e('0x17')](socket);function respondWithStatusCode(_0x343343,_0x216313){_0x216313=_0x216313||0xcc;return function(_0xa18901){if(_0xa18901){return _0x343343[_0x636e('0x18')](_0x216313);}return _0x343343[_0x636e('0x19')](_0x216313)[_0x636e('0x1a')]();};}function respondWithResult(_0x491d11,_0x4db7c9){_0x4db7c9=_0x4db7c9||0xc8;return function(_0x466be4){if(_0x466be4){return _0x491d11[_0x636e('0x19')](_0x4db7c9)['json'](_0x466be4);}};}function respondWithFilteredResult(_0x55748c,_0x32b331){return function(_0x3258db){if(_0x3258db){var _0x164953=_0x3258db[_0x636e('0x1b')],_0x1f1997=_0x32b331[_0x636e('0x1c')],_0x4c4193=_0x32b331[_0x636e('0x1c')]+_0x32b331[_0x636e('0x1d')],_0x3d2129;if(_0x4c4193>=_0x164953){_0x4c4193=_0x164953;_0x3d2129=0xc8;}else{_0x3d2129=0xce;}_0x55748c['status'](_0x3d2129);return _0x55748c['set'](_0x636e('0x1e'),_0x1f1997+'-'+_0x4c4193+'/'+_0x164953)['json'](_0x3258db);}return null;};}function patchUpdates(_0x2369e4){return function(_0x407c56){try{jsonpatch[_0x636e('0x1f')](_0x407c56,_0x2369e4,!![]);}catch(_0x1d1dea){return BPromise[_0x636e('0x20')](_0x1d1dea);}return _0x407c56[_0x636e('0x21')]();};}function saveUpdates(_0x30a070,_0xc2bef2){return function(_0x55f6fc){if(_0x55f6fc){return _0x55f6fc[_0x636e('0x22')](_0x30a070)[_0x636e('0x23')](function(_0x28d2f8){return _0x28d2f8;});}return null;};}function removeEntity(_0x1a5d01,_0x124ef9){return function(_0x150158){if(_0x150158){return _0x150158[_0x636e('0x24')]()[_0x636e('0x23')](function(){_0x1a5d01[_0x636e('0x19')](0xcc)[_0x636e('0x1a')]();});}};}function handleEntityNotFound(_0x4d9ee2,_0x134c18){return function(_0x50a450){if(!_0x50a450){_0x4d9ee2[_0x636e('0x18')](0x194);}return _0x50a450;};}function handleError(_0x31f72f,_0x16fbaf){_0x16fbaf=_0x16fbaf||0x1f4;return function(_0x63a389){logger['error'](_0x63a389['stack']);if(_0x63a389[_0x636e('0x25')]){delete _0x63a389[_0x636e('0x25')];}_0x31f72f[_0x636e('0x19')](_0x16fbaf)['send'](_0x63a389);};}exports['sendNotification']=function(_0x41986a,_0x23c43d,_0x14ceae){if(!_0x41986a[_0x636e('0x26')][_0x636e('0x27')](_0x636e('0x28'))&&!_0x41986a[_0x636e('0x26')][_0x636e('0x27')]('TemplateId')){throw new db[(_0x636e('0x29'))][(_0x636e('0x2a'))](_0x636e('0x2b'));}else{if(_0x41986a[_0x636e('0x26')][_0x636e('0x27')]('TemplateId')&&!_0x41986a[_0x636e('0x26')][_0x636e('0x27')]('view')){throw new db[(_0x636e('0x29'))][(_0x636e('0x2a'))](_0x636e('0x2c'));}if(!_0x41986a[_0x636e('0x26')][_0x636e('0x27')](_0x636e('0x2d'))){throw new db[(_0x636e('0x29'))][(_0x636e('0x2a'))]('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x173f01=JSON[_0x636e('0x2e')](_0x41986a[_0x636e('0x26')]['recipients']);}catch(_0xd81da5){logger[_0x636e('0x2f')](_0xd81da5);throw new db[(_0x636e('0x29'))]['ValidationError']('Invalid\x20\x27recipients\x27\x20object');}var _0x3a700b=[_0x636e('0x30'),_0x636e('0x31'),_0x636e('0x32'),_0x636e('0x33'),'smsqueue',_0x636e('0x34'),_0x636e('0x35'),_0x636e('0x36'),_0x636e('0x37')];if(_[_0x636e('0x38')](_0x173f01,function(_0x353b18){return!_[_0x636e('0x39')](_0x3a700b,_0x353b18[_0x636e('0x3a')][_0x636e('0x3b')]());})){throw new db['Sequelize'][(_0x636e('0x2a'))](_0x636e('0x3c'));}}}var _0x5a1eef={};_0x5a1eef[_0x636e('0x3d')]=_0x41986a['body'][_0x636e('0x27')](_0x636e('0x3d'))?_0x41986a['body'][_0x636e('0x3d')]:_0x41986a[_0x636e('0x37')][_0x636e('0x3e')];_0x5a1eef[_0x636e('0x3f')]=_[_0x636e('0x40')](_0x41986a[_0x636e('0x26')][_0x636e('0x3f')])?null:_0x41986a['body']['recipientsDescription'];_0x5a1eef[_0x636e('0x41')]=_0x41986a[_0x636e('0x26')]['beep']||![];_0x5a1eef[_0x636e('0x42')]=_0x41986a[_0x636e('0x37')]['id'];return getContent(_0x41986a[_0x636e('0x26')])[_0x636e('0x23')](function(_0x47f180){_0x5a1eef[_0x636e('0x26')]=_0x47f180;return getRecipients(_0x173f01);})[_0x636e('0x23')](function(_0x2e58b2){_0x5a1eef[_0x636e('0x43')]=_['sortBy'](_0x2e58b2)[_0x636e('0x44')](',');return db[_0x636e('0x45')][_0x636e('0x46')](function(_0xf6fda0){return db[_0x636e('0x47')]['create'](_0x5a1eef,{'raw':!![],'transaction':_0xf6fda0})[_0x636e('0x23')](function(_0x2be537){var _0x5a8386={'sender':_0x5a1eef[_0x636e('0x3d')],'description':_0x5a1eef[_0x636e('0x3f')],'content':_0x5a1eef[_0x636e('0x26')],'beep':_0x5a1eef[_0x636e('0x41')]};_0x2e58b2['forEach'](function(_0x181a00){_0x5a8386['recipient']=_0x181a00;socket[_0x636e('0x48')]('notification:send',_0x5a8386);});return _0x2be537;});});})[_0x636e('0x23')](respondWithResult(_0x23c43d,0xc9))[_0x636e('0x49')](handleError(_0x23c43d,null));};function getContent(_0x464080){return new BPromise(function(_0x207862,_0x30609b){if(_0x464080[_0x636e('0x28')]){_0x207862(_0x464080[_0x636e('0x28')]);return;}return db[_0x636e('0x4a')][_0x636e('0x4b')]({'attributes':[_0x636e('0x4c')],'where':{'id':_0x464080['TemplateId']},'raw':!![]})['then'](function(_0x4b2264){if(!_0x4b2264)throw new db[(_0x636e('0x29'))][(_0x636e('0x2a'))](_0x636e('0x4d'));var _0x57c97f=Mustache[_0x636e('0x4e')](_0x4b2264[_0x636e('0x4c')],_0x464080[_0x636e('0x4f')]);_0x207862(_0x57c97f);})[_0x636e('0x49')](function(_0x241a30){_0x30609b(_0x241a30);});});}function getRecipients(_0x264238){return new BPromise(function(_0x278479,_0x54bdc8){var _0x569530=[];var _0x14db0f=[];_0x264238[_0x636e('0x50')](function(_0x4a29bc){var _0x344181=_['startCase'](_0x4a29bc[_0x636e('0x3a')][_0x636e('0x3b')]());if(_0x344181===_0x636e('0x51')){_0x569530=_[_0x636e('0x52')](_0x569530,_0x4a29bc['ids']);}else{if(_[_0x636e('0x39')](_0x344181,_0x636e('0x53')))_0x344181=_0x344181[_0x636e('0x54')](_0x636e('0x53'),_0x636e('0x55'));_0x4a29bc[_0x636e('0x56')][_0x636e('0x50')](function(_0x4c1b7c){_0x14db0f['push'](getAgents(_0x344181,_0x4c1b7c));});}});return BPromise[_0x636e('0x57')](_0x14db0f)[_0x636e('0x23')](function(_0x1407d4){_0x569530=_[_0x636e('0x52')](_0x569530,_['flatten'](_0x1407d4));_0x278479(_0x569530);})['catch'](function(_0x266aff){_0x54bdc8(_0x266aff);});});}function getAgents(_0x176300,_0x393724){return new BPromise(function(_0x3c9c98,_0x39fcd2){return db[_0x176300]['findOne']({'where':{'id':_0x393724}})[_0x636e('0x23')](function(_0x1a9a44){return _0x1a9a44[_0x636e('0x58')]({'attributes':['id'],'raw':!![]})[_0x636e('0x23')](function(_0x11d01b){var _0x3bd68c=_[_0x636e('0x59')](_0x11d01b,'id');_0x3c9c98(_0x3bd68c);});})['catch'](function(_0x436a9b){_0x39fcd2(_0x436a9b);});});}