Built motion from commit b5996064.|2.6.21
[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 _0x73f4=['end','json','offset','undefined','limit','count','set','Content-Range','apply','save','update','then','destroy','sendStatus','error','stack','name','send','sendUserNotification','body','hasOwnProperty','text','TemplateId','ValidationError','Must\x20specify\x20at\x20least\x20one\x20argument\x20for\x20\x27text\x27\x20or\x20\x27TemplateId\x27','view','Sequelize','Missing\x20\x27view\x27\x20parameter','recipients','parse','Invalid\x20\x27recipients\x27\x20object','chatqueue','mailqueue','team','user','includes','senderName','fullname','recipientsDescription','isEmpty','beep','SenderId','sortBy','join','sequelize','transaction','UserNotification','create','recipient','emit','userNotification:send','catch','findOne','html','startCase','model','toLowerCase','union','ids','queue','replace','Queue','forEach','push','all','flatten','map','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','localhost','register','status'];(function(_0xf53be4,_0x4f8e01){var _0x3fd007=function(_0x315764){while(--_0x315764){_0xf53be4['push'](_0xf53be4['shift']());}};_0x3fd007(++_0x4f8e01);}(_0x73f4,0x1cb));var _0x473f=function(_0x279d37,_0x14de3f){_0x279d37=_0x279d37-0x0;var _0x530173=_0x73f4[_0x279d37];return _0x530173;};'use strict';var emlformat=require(_0x473f('0x0'));var rimraf=require(_0x473f('0x1'));var zipdir=require(_0x473f('0x2'));var jsonpatch=require(_0x473f('0x3'));var rp=require(_0x473f('0x4'));var moment=require(_0x473f('0x5'));var BPromise=require(_0x473f('0x6'));var Mustache=require(_0x473f('0x7'));var util=require(_0x473f('0x8'));var path=require(_0x473f('0x9'));var sox=require(_0x473f('0xa'));var csv=require(_0x473f('0xb'));var ejs=require(_0x473f('0xc'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x473f('0xd'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x473f('0xe'));var deskjs=require(_0x473f('0xf'));var toCsv=require(_0x473f('0xb'));var querystring=require('querystring');var Papa=require(_0x473f('0x10'));var Redis=require(_0x473f('0x11'));var authService=require(_0x473f('0x12'));var qs=require(_0x473f('0x13'));var as=require(_0x473f('0x14'));var hardwareService=require(_0x473f('0x15'));var logger=require(_0x473f('0x16'))('api');var utils=require(_0x473f('0x17'));var config=require(_0x473f('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0x473f('0x19'))['db'];config[_0x473f('0x1a')]=_[_0x473f('0x1b')](config[_0x473f('0x1a')],{'host':_0x473f('0x1c'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x473f('0x1a')]));require('./userNotification.socket')[_0x473f('0x1d')](socket);function respondWithStatusCode(_0x1fa068,_0x522e0f){_0x522e0f=_0x522e0f||0xcc;return function(_0x547bba){if(_0x547bba){return _0x1fa068['sendStatus'](_0x522e0f);}return _0x1fa068[_0x473f('0x1e')](_0x522e0f)[_0x473f('0x1f')]();};}function respondWithResult(_0x4914d3,_0x440e01){_0x440e01=_0x440e01||0xc8;return function(_0x5ef2eb){if(_0x5ef2eb){return _0x4914d3[_0x473f('0x1e')](_0x440e01)[_0x473f('0x20')](_0x5ef2eb);}};}function respondWithFilteredResult(_0x335204,_0x3d3d01){return function(_0x500836){if(_0x500836){var _0x169fc4=typeof _0x3d3d01[_0x473f('0x21')]===_0x473f('0x22')&&typeof _0x3d3d01[_0x473f('0x23')]===_0x473f('0x22');var _0x30b139=_0x500836['count'];var _0x2d00de=_0x169fc4?0x0:_0x3d3d01[_0x473f('0x21')];var _0x3664e8=_0x169fc4?_0x500836[_0x473f('0x24')]:_0x3d3d01[_0x473f('0x21')]+_0x3d3d01[_0x473f('0x23')];var _0x2fe6f8;if(_0x3664e8>=_0x30b139){_0x3664e8=_0x30b139;_0x2fe6f8=0xc8;}else{_0x2fe6f8=0xce;}_0x335204['status'](_0x2fe6f8);return _0x335204[_0x473f('0x25')](_0x473f('0x26'),_0x2d00de+'-'+_0x3664e8+'/'+_0x30b139)[_0x473f('0x20')](_0x500836);}return null;};}function patchUpdates(_0x2ec3ea){return function(_0x1fb05d){try{jsonpatch[_0x473f('0x27')](_0x1fb05d,_0x2ec3ea,!![]);}catch(_0x1d8bc2){return BPromise['reject'](_0x1d8bc2);}return _0x1fb05d[_0x473f('0x28')]();};}function saveUpdates(_0xd8ee97,_0x556120){return function(_0x3005c7){if(_0x3005c7){return _0x3005c7[_0x473f('0x29')](_0xd8ee97)[_0x473f('0x2a')](function(_0x3c6071){return _0x3c6071;});}return null;};}function removeEntity(_0x17da92,_0x57a0c2){return function(_0x3be589){if(_0x3be589){return _0x3be589[_0x473f('0x2b')]()[_0x473f('0x2a')](function(){_0x17da92['status'](0xcc)[_0x473f('0x1f')]();});}};}function handleEntityNotFound(_0x4bc39a,_0x5a86d1){return function(_0x325476){if(!_0x325476){_0x4bc39a[_0x473f('0x2c')](0x194);}return _0x325476;};}function handleError(_0x346e45,_0x58bafc){_0x58bafc=_0x58bafc||0x1f4;return function(_0x2f8a1e){logger[_0x473f('0x2d')](_0x2f8a1e[_0x473f('0x2e')]);if(_0x2f8a1e[_0x473f('0x2f')]){delete _0x2f8a1e['name'];}_0x346e45[_0x473f('0x1e')](_0x58bafc)[_0x473f('0x30')](_0x2f8a1e);};}exports[_0x473f('0x31')]=function(_0x1c9ada,_0x18b2b7,_0xddddbe){if(!_0x1c9ada[_0x473f('0x32')][_0x473f('0x33')](_0x473f('0x34'))&&!_0x1c9ada['body']['hasOwnProperty'](_0x473f('0x35'))){throw new db['Sequelize'][(_0x473f('0x36'))](_0x473f('0x37'));}else{if(_0x1c9ada[_0x473f('0x32')][_0x473f('0x33')](_0x473f('0x35'))&&!_0x1c9ada['body'][_0x473f('0x33')](_0x473f('0x38'))){throw new db[(_0x473f('0x39'))][(_0x473f('0x36'))](_0x473f('0x3a'));}if(!_0x1c9ada[_0x473f('0x32')]['hasOwnProperty'](_0x473f('0x3b'))){throw new db[(_0x473f('0x39'))][(_0x473f('0x36'))]('Missing\x20\x27recipients\x27\x20parameter');}else{try{var _0x12de44=JSON[_0x473f('0x3c')](_0x1c9ada[_0x473f('0x32')][_0x473f('0x3b')]);}catch(_0xfbdd1e){logger[_0x473f('0x2d')](_0xfbdd1e);throw new db['Sequelize'][(_0x473f('0x36'))](_0x473f('0x3d'));}var _0x3a8899=[_0x473f('0x3e'),'faxqueue',_0x473f('0x3f'),'openchannelqueue','smsqueue','voicequeue','whatsappqueue',_0x473f('0x40'),_0x473f('0x41')];if(_['some'](_0x12de44,function(_0x5e3782){return!_[_0x473f('0x42')](_0x3a8899,_0x5e3782['model']['toLowerCase']());})){throw new db[(_0x473f('0x39'))]['ValidationError']('Invalid\x20model\x20in\x20\x27recipients\x27\x20object');}}}var _0x1a960b={};_0x1a960b[_0x473f('0x43')]=_0x1c9ada['body'][_0x473f('0x33')](_0x473f('0x43'))?_0x1c9ada['body'][_0x473f('0x43')]:_0x1c9ada['user'][_0x473f('0x44')];_0x1a960b[_0x473f('0x45')]=_[_0x473f('0x46')](_0x1c9ada[_0x473f('0x32')][_0x473f('0x45')])?null:_0x1c9ada[_0x473f('0x32')][_0x473f('0x45')];_0x1a960b[_0x473f('0x47')]=_0x1c9ada[_0x473f('0x32')]['beep']||![];_0x1a960b[_0x473f('0x48')]=_0x1c9ada['user']['id'];return getContent(_0x1c9ada['body'])[_0x473f('0x2a')](function(_0x3df069){_0x1a960b[_0x473f('0x32')]=_0x3df069;return getRecipients(_0x12de44);})[_0x473f('0x2a')](function(_0x17e242){_0x1a960b['recipientsId']=_[_0x473f('0x49')](_0x17e242)[_0x473f('0x4a')](',');return db[_0x473f('0x4b')][_0x473f('0x4c')](function(_0x1b86c6){return db[_0x473f('0x4d')][_0x473f('0x4e')](_0x1a960b,{'raw':!![],'transaction':_0x1b86c6})[_0x473f('0x2a')](function(_0x3f25ba){var _0x293eed={'sender':_0x1a960b[_0x473f('0x43')],'description':_0x1a960b[_0x473f('0x45')],'content':_0x1a960b[_0x473f('0x32')],'beep':_0x1a960b[_0x473f('0x47')]};_0x17e242['forEach'](function(_0x597a46){_0x293eed[_0x473f('0x4f')]=_0x597a46;socket[_0x473f('0x50')](_0x473f('0x51'),_0x293eed);});return _0x3f25ba;});});})[_0x473f('0x2a')](respondWithResult(_0x18b2b7,0xc9))[_0x473f('0x52')](handleError(_0x18b2b7,null));};function getContent(_0x3cf285){return new BPromise(function(_0x3aa563,_0x2f8bd8){if(_0x3cf285['text']){_0x3aa563(_0x3cf285['text']);return;}return db['Template'][_0x473f('0x53')]({'attributes':[_0x473f('0x54')],'where':{'id':_0x3cf285[_0x473f('0x35')]},'raw':!![]})['then'](function(_0x384f5f){if(!_0x384f5f)throw new db['Sequelize'][(_0x473f('0x36'))]('Invalid\x20\x27TemplateId\x27\x20parameter');var _0x4087f0=Mustache['render'](_0x384f5f['html'],_0x3cf285[_0x473f('0x38')]);_0x3aa563(_0x4087f0);})[_0x473f('0x52')](function(_0x4db4fb){_0x2f8bd8(_0x4db4fb);});});}function getRecipients(_0x2ca9e1){return new BPromise(function(_0x3fa972,_0x2fe884){var _0x3b52b2=[];var _0x5e1f91=[];_0x2ca9e1['forEach'](function(_0x59f13d){var _0x455440=_[_0x473f('0x55')](_0x59f13d[_0x473f('0x56')][_0x473f('0x57')]());if(_0x455440==='User'){_0x3b52b2=_[_0x473f('0x58')](_0x3b52b2,_0x59f13d[_0x473f('0x59')]);}else{if(_['includes'](_0x455440,_0x473f('0x5a')))_0x455440=_0x455440[_0x473f('0x5b')](_0x473f('0x5a'),_0x473f('0x5c'));_0x59f13d['ids'][_0x473f('0x5d')](function(_0xdd5c2a){_0x5e1f91[_0x473f('0x5e')](getAgents(_0x455440,_0xdd5c2a));});}});return BPromise[_0x473f('0x5f')](_0x5e1f91)['then'](function(_0x160011){_0x3b52b2=_[_0x473f('0x58')](_0x3b52b2,_[_0x473f('0x60')](_0x160011));_0x3fa972(_0x3b52b2);})[_0x473f('0x52')](function(_0x4046a3){_0x2fe884(_0x4046a3);});});}function getAgents(_0x4a31d4,_0x4fdd78){return new BPromise(function(_0xfe6730,_0x309f54){return db[_0x4a31d4][_0x473f('0x53')]({'where':{'id':_0x4fdd78}})[_0x473f('0x2a')](function(_0x5e7bf8){return _0x5e7bf8['getAgents']({'attributes':['id'],'raw':!![]})[_0x473f('0x2a')](function(_0x298851){var _0x641fab=_[_0x473f('0x61')](_0x298851,'id');_0xfe6730(_0x641fab);});})[_0x473f('0x52')](function(_0x466c47){_0x309f54(_0x466c47);});});}