f12589843ee4d81c4bc30347e25c209efb3ddd8c
[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 _0xf266=['senderName','fullname','recipientsDescription','beep','SenderId','recipientsId','sortBy','join','sequelize','transaction','UserNotification','create','forEach','recipient','emit','userNotification:send','catch','text','Template','findOne','html','Invalid\x20\x27TemplateId\x27\x20parameter','view','startCase','User','union','ids','queue','Queue','push','all','flatten','getAgents','map','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','crypto','jsforce','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../mysqldb','redis','localhost','socket.io-emitter','register','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','then','destroy','stack','name','send','sendUserNotification','hasOwnProperty','body','TemplateId','Sequelize','ValidationError','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','recipients','parse','error','Invalid\x20\x27recipients\x27\x20object','chatqueue','faxqueue','mailqueue','openchannelqueue','smsqueue','voicequeue','user','some','includes','model'];(function(_0x79c24f,_0x18517b){var _0x1ffb70=function(_0x5a09db){while(--_0x5a09db){_0x79c24f['push'](_0x79c24f['shift']());}};_0x1ffb70(++_0x18517b);}(_0xf266,0x148));var _0x6f26=function(_0x21aec3,_0x5c97e9){_0x21aec3=_0x21aec3-0x0;var _0x288bbc=_0xf266[_0x21aec3];return _0x288bbc;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x6f26('0x0'));var zipdir=require(_0x6f26('0x1'));var jsonpatch=require(_0x6f26('0x2'));var rp=require(_0x6f26('0x3'));var moment=require(_0x6f26('0x4'));var BPromise=require(_0x6f26('0x5'));var Mustache=require(_0x6f26('0x6'));var util=require('util');var path=require(_0x6f26('0x7'));var sox=require('sox');var csv=require(_0x6f26('0x8'));var ejs=require(_0x6f26('0x9'));var fs=require('fs');var fs_extra=require(_0x6f26('0xa'));var _=require('lodash');var squel=require('squel');var crypto=require(_0x6f26('0xb'));var jsforce=require(_0x6f26('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x6f26('0x8'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x6f26('0xd'));var authService=require(_0x6f26('0xe'));var qs=require(_0x6f26('0xf'));var as=require(_0x6f26('0x10'));var hardwareService=require(_0x6f26('0x11'));var logger=require(_0x6f26('0x12'))('api');var utils=require(_0x6f26('0x13'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x6f26('0x14'))['db'];config[_0x6f26('0x15')]=_['defaults'](config[_0x6f26('0x15')],{'host':_0x6f26('0x16'),'port':0x18eb});var socket=require(_0x6f26('0x17'))(new Redis(config[_0x6f26('0x15')]));require('./userNotification.socket')[_0x6f26('0x18')](socket);function respondWithStatusCode(_0x5889c0,_0x3efdfa){_0x3efdfa=_0x3efdfa||0xcc;return function(_0x5e40a2){if(_0x5e40a2){return _0x5889c0[_0x6f26('0x19')](_0x3efdfa);}return _0x5889c0['status'](_0x3efdfa)[_0x6f26('0x1a')]();};}function respondWithResult(_0x284ec8,_0x3f6a57){_0x3f6a57=_0x3f6a57||0xc8;return function(_0x45dacc){if(_0x45dacc){return _0x284ec8[_0x6f26('0x1b')](_0x3f6a57)[_0x6f26('0x1c')](_0x45dacc);}};}function respondWithFilteredResult(_0x16d44f,_0x2d3794){return function(_0x18cb08){if(_0x18cb08){var _0x2e1fda=typeof _0x2d3794[_0x6f26('0x1d')]===_0x6f26('0x1e')&&typeof _0x2d3794[_0x6f26('0x1f')]==='undefined';var _0x1f05ed=_0x18cb08['count'];var _0x5d1ba4=_0x2e1fda?0x0:_0x2d3794[_0x6f26('0x1d')];var _0x310c98=_0x2e1fda?_0x18cb08[_0x6f26('0x20')]:_0x2d3794[_0x6f26('0x1d')]+_0x2d3794['limit'];var _0x5a0a7d;if(_0x310c98>=_0x1f05ed){_0x310c98=_0x1f05ed;_0x5a0a7d=0xc8;}else{_0x5a0a7d=0xce;}_0x16d44f[_0x6f26('0x1b')](_0x5a0a7d);return _0x16d44f[_0x6f26('0x21')](_0x6f26('0x22'),_0x5d1ba4+'-'+_0x310c98+'/'+_0x1f05ed)[_0x6f26('0x1c')](_0x18cb08);}return null;};}function patchUpdates(_0x3417bd){return function(_0x310be9){try{jsonpatch[_0x6f26('0x23')](_0x310be9,_0x3417bd,!![]);}catch(_0x57544b){return BPromise[_0x6f26('0x24')](_0x57544b);}return _0x310be9[_0x6f26('0x25')]();};}function saveUpdates(_0x2ff154,_0x6c4ec3){return function(_0x2f831a){if(_0x2f831a){return _0x2f831a['update'](_0x2ff154)[_0x6f26('0x26')](function(_0x57eef3){return _0x57eef3;});}return null;};}function removeEntity(_0x30efd7,_0x248ed1){return function(_0xb3af78){if(_0xb3af78){return _0xb3af78[_0x6f26('0x27')]()[_0x6f26('0x26')](function(){_0x30efd7['status'](0xcc)[_0x6f26('0x1a')]();});}};}function handleEntityNotFound(_0x43e243,_0x4bbdf8){return function(_0x33f1c5){if(!_0x33f1c5){_0x43e243[_0x6f26('0x19')](0x194);}return _0x33f1c5;};}function handleError(_0x591bca,_0x197099){_0x197099=_0x197099||0x1f4;return function(_0x5b7325){logger['error'](_0x5b7325[_0x6f26('0x28')]);if(_0x5b7325['name']){delete _0x5b7325[_0x6f26('0x29')];}_0x591bca['status'](_0x197099)[_0x6f26('0x2a')](_0x5b7325);};}exports[_0x6f26('0x2b')]=function(_0x4a8d0d,_0x454bc2,_0x4c623a){if(!_0x4a8d0d['body'][_0x6f26('0x2c')]('text')&&!_0x4a8d0d[_0x6f26('0x2d')][_0x6f26('0x2c')](_0x6f26('0x2e'))){throw new db[(_0x6f26('0x2f'))][(_0x6f26('0x30'))](_0x6f26('0x31'));}else{if(_0x4a8d0d['body'][_0x6f26('0x2c')](_0x6f26('0x2e'))&&!_0x4a8d0d[_0x6f26('0x2d')]['hasOwnProperty']('view')){throw new db[(_0x6f26('0x2f'))][(_0x6f26('0x30'))]('Missing\x20\x27view\x27\x20parameter');}if(!_0x4a8d0d[_0x6f26('0x2d')]['hasOwnProperty'](_0x6f26('0x32'))){throw new db[(_0x6f26('0x2f'))][(_0x6f26('0x30'))]('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x50b3b9=JSON[_0x6f26('0x33')](_0x4a8d0d['body'][_0x6f26('0x32')]);}catch(_0x4c90fc){logger[_0x6f26('0x34')](_0x4c90fc);throw new db['Sequelize'][(_0x6f26('0x30'))](_0x6f26('0x35'));}var _0x3581a3=[_0x6f26('0x36'),_0x6f26('0x37'),_0x6f26('0x38'),_0x6f26('0x39'),_0x6f26('0x3a'),_0x6f26('0x3b'),'whatsappqueue','team',_0x6f26('0x3c')];if(_[_0x6f26('0x3d')](_0x50b3b9,function(_0x3cbe95){return!_[_0x6f26('0x3e')](_0x3581a3,_0x3cbe95[_0x6f26('0x3f')]['toLowerCase']());})){throw new db['Sequelize'][(_0x6f26('0x30'))]('Invalid\x20model\x20in\x20\x27recipients\x27\x20object');}}}var _0x33fad2={};_0x33fad2[_0x6f26('0x40')]=_0x4a8d0d[_0x6f26('0x2d')]['hasOwnProperty'](_0x6f26('0x40'))?_0x4a8d0d[_0x6f26('0x2d')][_0x6f26('0x40')]:_0x4a8d0d[_0x6f26('0x3c')][_0x6f26('0x41')];_0x33fad2['recipientsDescription']=_['isEmpty'](_0x4a8d0d[_0x6f26('0x2d')]['recipientsDescription'])?null:_0x4a8d0d[_0x6f26('0x2d')][_0x6f26('0x42')];_0x33fad2[_0x6f26('0x43')]=_0x4a8d0d[_0x6f26('0x2d')][_0x6f26('0x43')]||![];_0x33fad2[_0x6f26('0x44')]=_0x4a8d0d[_0x6f26('0x3c')]['id'];return getContent(_0x4a8d0d[_0x6f26('0x2d')])['then'](function(_0x4d32ec){_0x33fad2[_0x6f26('0x2d')]=_0x4d32ec;return getRecipients(_0x50b3b9);})[_0x6f26('0x26')](function(_0x4b8aff){_0x33fad2[_0x6f26('0x45')]=_[_0x6f26('0x46')](_0x4b8aff)[_0x6f26('0x47')](',');return db[_0x6f26('0x48')][_0x6f26('0x49')](function(_0xa5ca91){return db[_0x6f26('0x4a')][_0x6f26('0x4b')](_0x33fad2,{'raw':!![],'transaction':_0xa5ca91})[_0x6f26('0x26')](function(_0x5c77a9){var _0x598ef3={'sender':_0x33fad2[_0x6f26('0x40')],'description':_0x33fad2['recipientsDescription'],'content':_0x33fad2['body'],'beep':_0x33fad2[_0x6f26('0x43')]};_0x4b8aff[_0x6f26('0x4c')](function(_0x47c033){_0x598ef3[_0x6f26('0x4d')]=_0x47c033;socket[_0x6f26('0x4e')](_0x6f26('0x4f'),_0x598ef3);});return _0x5c77a9;});});})[_0x6f26('0x26')](respondWithResult(_0x454bc2,0xc9))[_0x6f26('0x50')](handleError(_0x454bc2,null));};function getContent(_0x1d5c71){return new BPromise(function(_0xa5ff83,_0x3e16ec){if(_0x1d5c71[_0x6f26('0x51')]){_0xa5ff83(_0x1d5c71['text']);return;}return db[_0x6f26('0x52')][_0x6f26('0x53')]({'attributes':[_0x6f26('0x54')],'where':{'id':_0x1d5c71[_0x6f26('0x2e')]},'raw':!![]})[_0x6f26('0x26')](function(_0x217e0){if(!_0x217e0)throw new db['Sequelize']['ValidationError'](_0x6f26('0x55'));var _0x14642a=Mustache['render'](_0x217e0['html'],_0x1d5c71[_0x6f26('0x56')]);_0xa5ff83(_0x14642a);})['catch'](function(_0x4b7d50){_0x3e16ec(_0x4b7d50);});});}function getRecipients(_0x129f1c){return new BPromise(function(_0x31f840,_0x21c4ed){var _0x1d4f97=[];var _0xae7406=[];_0x129f1c[_0x6f26('0x4c')](function(_0xa2b36e){var _0x3e3fda=_[_0x6f26('0x57')](_0xa2b36e[_0x6f26('0x3f')]['toLowerCase']());if(_0x3e3fda===_0x6f26('0x58')){_0x1d4f97=_[_0x6f26('0x59')](_0x1d4f97,_0xa2b36e[_0x6f26('0x5a')]);}else{if(_[_0x6f26('0x3e')](_0x3e3fda,'queue'))_0x3e3fda=_0x3e3fda['replace'](_0x6f26('0x5b'),_0x6f26('0x5c'));_0xa2b36e[_0x6f26('0x5a')][_0x6f26('0x4c')](function(_0x2cbd43){_0xae7406[_0x6f26('0x5d')](getAgents(_0x3e3fda,_0x2cbd43));});}});return BPromise[_0x6f26('0x5e')](_0xae7406)[_0x6f26('0x26')](function(_0x471519){_0x1d4f97=_[_0x6f26('0x59')](_0x1d4f97,_[_0x6f26('0x5f')](_0x471519));_0x31f840(_0x1d4f97);})[_0x6f26('0x50')](function(_0x3fd0e8){_0x21c4ed(_0x3fd0e8);});});}function getAgents(_0x152e07,_0x4b4eab){return new BPromise(function(_0x5a38cc,_0x2756a9){return db[_0x152e07][_0x6f26('0x53')]({'where':{'id':_0x4b4eab}})[_0x6f26('0x26')](function(_0x510817){return _0x510817[_0x6f26('0x60')]({'attributes':['id'],'raw':!![]})[_0x6f26('0x26')](function(_0x18e793){var _0x5a8c89=_[_0x6f26('0x61')](_0x18e793,'id');_0x5a38cc(_0x5a8c89);});})[_0x6f26('0x50')](function(_0x58c43d){_0x2756a9(_0x58c43d);});});}