12bfdbe498e7e81923f47191e31b96425fcd8639
[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 _0x654e=['path','sox','to-csv','ejs','fs-extra','squel','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','defaults','redis','localhost','./userNotification.socket','register','status','end','json','offset','undefined','count','set','Content-Range','apply','reject','save','update','destroy','then','sendStatus','name','send','sendUserNotification','body','hasOwnProperty','text','TemplateId','Sequelize','ValidationError','recipients','Missing\x20\x27recipients\x27\x20parameter','parse','Invalid\x20\x27recipients\x27\x20object','chatqueue','faxqueue','openchannelqueue','smsqueue','voicequeue','team','some','includes','model','Invalid\x20model\x20in\x20\x27recipients\x27\x20object','senderName','user','fullname','recipientsDescription','beep','recipientsId','sequelize','transaction','create','forEach','emit','catch','Template','html','Invalid\x20\x27TemplateId\x27\x20parameter','render','startCase','toLowerCase','User','union','ids','replace','queue','Queue','push','flatten','getAgents','map','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','util'];(function(_0x12a53b,_0x3ea3da){var _0x28b49c=function(_0x28b727){while(--_0x28b727){_0x12a53b['push'](_0x12a53b['shift']());}};_0x28b49c(++_0x3ea3da);}(_0x654e,0x1d4));var _0xe654=function(_0x34b9c5,_0x42700f){_0x34b9c5=_0x34b9c5-0x0;var _0x10b193=_0x654e[_0x34b9c5];return _0x10b193;};'use strict';var emlformat=require(_0xe654('0x0'));var rimraf=require(_0xe654('0x1'));var zipdir=require(_0xe654('0x2'));var jsonpatch=require(_0xe654('0x3'));var rp=require(_0xe654('0x4'));var moment=require(_0xe654('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xe654('0x6'));var path=require(_0xe654('0x7'));var sox=require(_0xe654('0x8'));var csv=require(_0xe654('0x9'));var ejs=require(_0xe654('0xa'));var fs=require('fs');var fs_extra=require(_0xe654('0xb'));var _=require('lodash');var squel=require(_0xe654('0xc'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xe654('0xd'));var toCsv=require(_0xe654('0x9'));var querystring=require(_0xe654('0xe'));var Papa=require('papaparse');var Redis=require(_0xe654('0xf'));var authService=require(_0xe654('0x10'));var qs=require(_0xe654('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0xe654('0x12'))(_0xe654('0x13'));var utils=require(_0xe654('0x14'));var config=require(_0xe654('0x15'));var licenseUtil=require(_0xe654('0x16'));var db=require(_0xe654('0x17'))['db'];config['redis']=_[_0xe654('0x18')](config[_0xe654('0x19')],{'host':_0xe654('0x1a'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xe654('0x19')]));require(_0xe654('0x1b'))[_0xe654('0x1c')](socket);function respondWithStatusCode(_0x5e00f9,_0x32e30a){_0x32e30a=_0x32e30a||0xcc;return function(_0x56a653){if(_0x56a653){return _0x5e00f9['sendStatus'](_0x32e30a);}return _0x5e00f9[_0xe654('0x1d')](_0x32e30a)[_0xe654('0x1e')]();};}function respondWithResult(_0xa96cac,_0x4682b9){_0x4682b9=_0x4682b9||0xc8;return function(_0x3f2c4e){if(_0x3f2c4e){return _0xa96cac[_0xe654('0x1d')](_0x4682b9)[_0xe654('0x1f')](_0x3f2c4e);}};}function respondWithFilteredResult(_0x2eb480,_0x1f64f4){return function(_0x17b54c){if(_0x17b54c){var _0x470e03=typeof _0x1f64f4[_0xe654('0x20')]===_0xe654('0x21')&&typeof _0x1f64f4['limit']===_0xe654('0x21');var _0x53f250=_0x17b54c['count'];var _0x158c7a=_0x470e03?0x0:_0x1f64f4[_0xe654('0x20')];var _0x24e898=_0x470e03?_0x17b54c[_0xe654('0x22')]:_0x1f64f4[_0xe654('0x20')]+_0x1f64f4['limit'];var _0x40a756;if(_0x24e898>=_0x53f250){_0x24e898=_0x53f250;_0x40a756=0xc8;}else{_0x40a756=0xce;}_0x2eb480[_0xe654('0x1d')](_0x40a756);return _0x2eb480[_0xe654('0x23')](_0xe654('0x24'),_0x158c7a+'-'+_0x24e898+'/'+_0x53f250)[_0xe654('0x1f')](_0x17b54c);}return null;};}function patchUpdates(_0x3631ed){return function(_0x1e309d){try{jsonpatch[_0xe654('0x25')](_0x1e309d,_0x3631ed,!![]);}catch(_0x20efc5){return BPromise[_0xe654('0x26')](_0x20efc5);}return _0x1e309d[_0xe654('0x27')]();};}function saveUpdates(_0x3d023d,_0xf8c2e8){return function(_0x44d8ac){if(_0x44d8ac){return _0x44d8ac[_0xe654('0x28')](_0x3d023d)['then'](function(_0x304667){return _0x304667;});}return null;};}function removeEntity(_0x30724e,_0x30987d){return function(_0x17676d){if(_0x17676d){return _0x17676d[_0xe654('0x29')]()[_0xe654('0x2a')](function(){_0x30724e[_0xe654('0x1d')](0xcc)[_0xe654('0x1e')]();});}};}function handleEntityNotFound(_0x1b80c6,_0x421036){return function(_0x4d86fa){if(!_0x4d86fa){_0x1b80c6[_0xe654('0x2b')](0x194);}return _0x4d86fa;};}function handleError(_0x293209,_0x3c0249){_0x3c0249=_0x3c0249||0x1f4;return function(_0x561f42){logger['error'](_0x561f42['stack']);if(_0x561f42[_0xe654('0x2c')]){delete _0x561f42['name'];}_0x293209[_0xe654('0x1d')](_0x3c0249)[_0xe654('0x2d')](_0x561f42);};}exports[_0xe654('0x2e')]=function(_0x29d5fc,_0x4c90f2,_0x40303c){if(!_0x29d5fc[_0xe654('0x2f')][_0xe654('0x30')](_0xe654('0x31'))&&!_0x29d5fc[_0xe654('0x2f')][_0xe654('0x30')](_0xe654('0x32'))){throw new db[(_0xe654('0x33'))]['ValidationError']('Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27');}else{if(_0x29d5fc[_0xe654('0x2f')][_0xe654('0x30')](_0xe654('0x32'))&&!_0x29d5fc['body']['hasOwnProperty']('view')){throw new db[(_0xe654('0x33'))][(_0xe654('0x34'))]('Missing\x20\x27view\x27\x20parameter');}if(!_0x29d5fc[_0xe654('0x2f')][_0xe654('0x30')](_0xe654('0x35'))){throw new db[(_0xe654('0x33'))][(_0xe654('0x34'))](_0xe654('0x36'));}else{try{var _0x5ea2b1=JSON[_0xe654('0x37')](_0x29d5fc[_0xe654('0x2f')][_0xe654('0x35')]);}catch(_0x49523f){logger['error'](_0x49523f);throw new db[(_0xe654('0x33'))][(_0xe654('0x34'))](_0xe654('0x38'));}var _0x22f6c5=[_0xe654('0x39'),_0xe654('0x3a'),'mailqueue',_0xe654('0x3b'),_0xe654('0x3c'),_0xe654('0x3d'),'whatsappqueue',_0xe654('0x3e'),'user'];if(_[_0xe654('0x3f')](_0x5ea2b1,function(_0x330371){return!_[_0xe654('0x40')](_0x22f6c5,_0x330371[_0xe654('0x41')]['toLowerCase']());})){throw new db[(_0xe654('0x33'))]['ValidationError'](_0xe654('0x42'));}}}var _0x13f508={};_0x13f508[_0xe654('0x43')]=_0x29d5fc[_0xe654('0x2f')][_0xe654('0x30')](_0xe654('0x43'))?_0x29d5fc['body']['senderName']:_0x29d5fc[_0xe654('0x44')][_0xe654('0x45')];_0x13f508[_0xe654('0x46')]=_['isEmpty'](_0x29d5fc[_0xe654('0x2f')]['recipientsDescription'])?null:_0x29d5fc[_0xe654('0x2f')][_0xe654('0x46')];_0x13f508[_0xe654('0x47')]=_0x29d5fc[_0xe654('0x2f')][_0xe654('0x47')]||![];_0x13f508['SenderId']=_0x29d5fc['user']['id'];return getContent(_0x29d5fc[_0xe654('0x2f')])[_0xe654('0x2a')](function(_0xc11a45){_0x13f508[_0xe654('0x2f')]=_0xc11a45;return getRecipients(_0x5ea2b1);})['then'](function(_0x335fc7){_0x13f508[_0xe654('0x48')]=_['sortBy'](_0x335fc7)['join'](',');return db[_0xe654('0x49')][_0xe654('0x4a')](function(_0x2bcf30){return db['UserNotification'][_0xe654('0x4b')](_0x13f508,{'raw':!![],'transaction':_0x2bcf30})[_0xe654('0x2a')](function(_0x2fe282){var _0xa1d372={'sender':_0x13f508['senderName'],'description':_0x13f508[_0xe654('0x46')],'content':_0x13f508['body'],'beep':_0x13f508[_0xe654('0x47')]};_0x335fc7[_0xe654('0x4c')](function(_0x3e814f){_0xa1d372['recipient']=_0x3e814f;socket[_0xe654('0x4d')]('userNotification:send',_0xa1d372);});return _0x2fe282;});});})[_0xe654('0x2a')](respondWithResult(_0x4c90f2,0xc9))[_0xe654('0x4e')](handleError(_0x4c90f2,null));};function getContent(_0x46b74f){return new BPromise(function(_0x34185f,_0x15837e){if(_0x46b74f[_0xe654('0x31')]){_0x34185f(_0x46b74f[_0xe654('0x31')]);return;}return db[_0xe654('0x4f')]['findOne']({'attributes':[_0xe654('0x50')],'where':{'id':_0x46b74f[_0xe654('0x32')]},'raw':!![]})['then'](function(_0x194816){if(!_0x194816)throw new db[(_0xe654('0x33'))][(_0xe654('0x34'))](_0xe654('0x51'));var _0x52ea04=Mustache[_0xe654('0x52')](_0x194816[_0xe654('0x50')],_0x46b74f['view']);_0x34185f(_0x52ea04);})[_0xe654('0x4e')](function(_0x33796){_0x15837e(_0x33796);});});}function getRecipients(_0x22ac35){return new BPromise(function(_0x49685e,_0x589eba){var _0x460b16=[];var _0x1b5594=[];_0x22ac35['forEach'](function(_0x18fde9){var _0x26f3c9=_[_0xe654('0x53')](_0x18fde9['model'][_0xe654('0x54')]());if(_0x26f3c9===_0xe654('0x55')){_0x460b16=_[_0xe654('0x56')](_0x460b16,_0x18fde9[_0xe654('0x57')]);}else{if(_[_0xe654('0x40')](_0x26f3c9,'queue'))_0x26f3c9=_0x26f3c9[_0xe654('0x58')](_0xe654('0x59'),_0xe654('0x5a'));_0x18fde9[_0xe654('0x57')]['forEach'](function(_0x3bc819){_0x1b5594[_0xe654('0x5b')](getAgents(_0x26f3c9,_0x3bc819));});}});return BPromise['all'](_0x1b5594)[_0xe654('0x2a')](function(_0xee62eb){_0x460b16=_[_0xe654('0x56')](_0x460b16,_[_0xe654('0x5c')](_0xee62eb));_0x49685e(_0x460b16);})[_0xe654('0x4e')](function(_0x5869bd){_0x589eba(_0x5869bd);});});}function getAgents(_0x543b0d,_0x2eac48){return new BPromise(function(_0x4ff74d,_0xeba036){return db[_0x543b0d]['findOne']({'where':{'id':_0x2eac48}})['then'](function(_0x2e6282){return _0x2e6282[_0xe654('0x5d')]({'attributes':['id'],'raw':!![]})[_0xe654('0x2a')](function(_0x5ceed7){var _0x1519a1=_[_0xe654('0x5e')](_0x5ceed7,'id');_0x4ff74d(_0x1519a1);});})[_0xe654('0x4e')](function(_0x8d4550){_0xeba036(_0x8d4550);});});}