Built motion from commit (unavailable).|2.3.9
[motion2.git] / server / api / chatInternalMessage / chatInternalMessage.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 _0xf201=['error','stack','name','send','index','ChatInternalMessage','model','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','where','filters','pick','filter','merge','map','type','key','VIRTUAL','field','include','findAll','rows','catch','show','params','keys','rawAttributes','includeAll','options','find','create','body','describe','get','messages','lastMessageAt','createdAt','format','YYYY-MM-DD\x20HH:mm:ss','lastMessage','getUsers','User','admin','fullname','role','userpic','lastLoginAt','allowmessenger','showWebBar','-online','VoiceQueue','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','Agents','autoAssociation','agent','push','FromId','ToId','read','-createdAt','all','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','ejs','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../mysqldb','defaults','redis','./chatInternalMessage.socket','register','sendStatus','status','end','json','count','offset','set','Content-Range','apply','reject','save','update','then','destroy'];(function(_0x526bfb,_0x10b110){var _0x223485=function(_0x14bb52){while(--_0x14bb52){_0x526bfb['push'](_0x526bfb['shift']());}};_0x223485(++_0x10b110);}(_0xf201,0x199));var _0x1f20=function(_0x5bda3f,_0x36c489){_0x5bda3f=_0x5bda3f-0x0;var _0x316952=_0xf201[_0x5bda3f];return _0x316952;};'use strict';var pdf=require(_0x1f20('0x0'));var emlformat=require(_0x1f20('0x1'));var rimraf=require(_0x1f20('0x2'));var zipdir=require(_0x1f20('0x3'));var jsonpatch=require(_0x1f20('0x4'));var rp=require(_0x1f20('0x5'));var moment=require(_0x1f20('0x6'));var BPromise=require(_0x1f20('0x7'));var Mustache=require(_0x1f20('0x8'));var util=require(_0x1f20('0x9'));var path=require('path');var sox=require(_0x1f20('0xa'));var csv=require('to-csv');var ejs=require(_0x1f20('0xb'));var fs=require('fs');var _=require(_0x1f20('0xc'));var squel=require(_0x1f20('0xd'));var crypto=require(_0x1f20('0xe'));var jsforce=require(_0x1f20('0xf'));var deskjs=require(_0x1f20('0x10'));var toCsv=require(_0x1f20('0x11'));var querystring=require(_0x1f20('0x12'));var Papa=require(_0x1f20('0x13'));var authService=require('../../components/auth/service');var hardwareService=require('../../config/license/hardware');var Redis=require('ioredis');var qs=require(_0x1f20('0x14'));var logger=require(_0x1f20('0x15'))(_0x1f20('0x16'));var utils=require(_0x1f20('0x17'));var config=require('../../config/environment');var db=require(_0x1f20('0x18'))['db'];config['redis']=_[_0x1f20('0x19')](config[_0x1f20('0x1a')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x1f20('0x1a')]));require(_0x1f20('0x1b'))[_0x1f20('0x1c')](socket);function respondWithStatusCode(_0x4d3365,_0x31e35b){_0x31e35b=_0x31e35b||0xcc;return function(_0x31ef38){if(_0x31ef38){return _0x4d3365[_0x1f20('0x1d')](_0x31e35b);}return _0x4d3365[_0x1f20('0x1e')](_0x31e35b)[_0x1f20('0x1f')]();};}function respondWithResult(_0x2e76b8,_0x292012){_0x292012=_0x292012||0xc8;return function(_0xca42fa){if(_0xca42fa){return _0x2e76b8[_0x1f20('0x1e')](_0x292012)[_0x1f20('0x20')](_0xca42fa);}};}function respondWithFilteredResult(_0x480194,_0x461853){return function(_0x1003fc){if(_0x1003fc){var _0x541ea3=_0x1003fc[_0x1f20('0x21')],_0x2464b2=_0x461853[_0x1f20('0x22')],_0x14319e=_0x461853['offset']+_0x461853['limit'],_0x3c3d47;if(_0x14319e>=_0x541ea3){_0x14319e=_0x541ea3;_0x3c3d47=0xc8;}else{_0x3c3d47=0xce;}_0x480194[_0x1f20('0x1e')](_0x3c3d47);return _0x480194[_0x1f20('0x23')](_0x1f20('0x24'),_0x2464b2+'-'+_0x14319e+'/'+_0x541ea3)[_0x1f20('0x20')](_0x1003fc);}return null;};}function patchUpdates(_0x154a1a){return function(_0x531399){try{jsonpatch[_0x1f20('0x25')](_0x531399,_0x154a1a,!![]);}catch(_0x197fd7){return BPromise[_0x1f20('0x26')](_0x197fd7);}return _0x531399[_0x1f20('0x27')]();};}function saveUpdates(_0x304932,_0x2f740f){return function(_0x1e3937){if(_0x1e3937){return _0x1e3937[_0x1f20('0x28')](_0x304932)[_0x1f20('0x29')](function(_0x2f829d){return _0x2f829d;});}return null;};}function removeEntity(_0x126592,_0x294b64){return function(_0x1aaaa3){if(_0x1aaaa3){return _0x1aaaa3[_0x1f20('0x2a')]()[_0x1f20('0x29')](function(){_0x126592['status'](0xcc)[_0x1f20('0x1f')]();});}};}function handleEntityNotFound(_0x326231,_0x1e3002){return function(_0x46dc63){if(!_0x46dc63){_0x326231[_0x1f20('0x1d')](0x194);}return _0x46dc63;};}function handleError(_0x29ce45,_0x1d7110){_0x1d7110=_0x1d7110||0x1f4;return function(_0xef8dba){logger[_0x1f20('0x2b')](_0xef8dba[_0x1f20('0x2c')]);if(_0xef8dba[_0x1f20('0x2d')]){delete _0xef8dba['name'];}_0x29ce45[_0x1f20('0x1e')](_0x1d7110)[_0x1f20('0x2e')](_0xef8dba);};}exports[_0x1f20('0x2f')]=function(_0xca2600,_0x60c7a4){var _0x112c5f={},_0x15e805={},_0x1bbcd1={'count':0x0,'rows':[]};var _0x2bcde9=db[_0x1f20('0x30')]['rawAttributes'];_0x15e805[_0x1f20('0x31')]=_['keys'](_0x2bcde9);_0x15e805['query']=_['keys'](_0xca2600[_0x1f20('0x32')]);_0x15e805['filters']=_[_0x1f20('0x33')](_0x15e805[_0x1f20('0x31')],_0x15e805[_0x1f20('0x32')]);_0x112c5f[_0x1f20('0x34')]=_[_0x1f20('0x33')](_0x15e805['model'],qs['fields'](_0xca2600[_0x1f20('0x32')][_0x1f20('0x35')]));_0x112c5f[_0x1f20('0x34')]=_0x112c5f[_0x1f20('0x34')][_0x1f20('0x36')]?_0x112c5f[_0x1f20('0x34')]:_0x15e805[_0x1f20('0x31')];if(!_0xca2600[_0x1f20('0x32')][_0x1f20('0x37')](_0x1f20('0x38'))){_0x112c5f[_0x1f20('0x39')]=qs[_0x1f20('0x39')](_0xca2600['query'][_0x1f20('0x39')]);_0x112c5f[_0x1f20('0x22')]=qs[_0x1f20('0x22')](_0xca2600['query'][_0x1f20('0x22')]);}_0x112c5f[_0x1f20('0x3a')]=qs[_0x1f20('0x3b')](_0xca2600[_0x1f20('0x32')][_0x1f20('0x3b')]);_0x112c5f[_0x1f20('0x3c')]=qs[_0x1f20('0x3d')](_[_0x1f20('0x3e')](_0xca2600[_0x1f20('0x32')],_0x15e805[_0x1f20('0x3d')]));if(_0xca2600[_0x1f20('0x32')][_0x1f20('0x3f')]){_0x112c5f[_0x1f20('0x3c')]=_[_0x1f20('0x40')](_0x112c5f[_0x1f20('0x3c')],{'$or':_[_0x1f20('0x41')](_0x2bcde9,function(_0x569a6b){if(_0x569a6b[_0x1f20('0x42')][_0x1f20('0x43')]!==_0x1f20('0x44')){var _0x32329e={};_0x32329e[_0x569a6b[_0x1f20('0x45')]]={'$like':'%'+_0xca2600[_0x1f20('0x32')]['filter']+'%'};return _0x32329e;}})});}_0x112c5f=_['merge']({},_0x112c5f,_0xca2600['options']);var _0x5157e6={'where':_0x112c5f[_0x1f20('0x3c')]};return db[_0x1f20('0x30')]['count'](_0x5157e6)[_0x1f20('0x29')](function(_0x47c9d9){_0x1bbcd1['count']=_0x47c9d9;if(_0xca2600[_0x1f20('0x32')]['includeAll']){_0x112c5f[_0x1f20('0x46')]=[{'all':!![]}];}return db[_0x1f20('0x30')][_0x1f20('0x47')](_0x112c5f);})['then'](function(_0x5c5e3a){_0x1bbcd1[_0x1f20('0x48')]=_0x5c5e3a;return _0x1bbcd1;})['then'](respondWithFilteredResult(_0x60c7a4,_0x112c5f))[_0x1f20('0x49')](handleError(_0x60c7a4,null));};exports[_0x1f20('0x4a')]=function(_0x14d25c,_0x4f61bb){var _0x4b4200={'raw':!![],'where':{'id':_0x14d25c[_0x1f20('0x4b')]['id']}},_0x1232dd={};_0x1232dd[_0x1f20('0x31')]=_[_0x1f20('0x4c')](db[_0x1f20('0x30')][_0x1f20('0x4d')]);_0x1232dd[_0x1f20('0x32')]=_[_0x1f20('0x4c')](_0x14d25c['query']);_0x1232dd[_0x1f20('0x3d')]=_['intersection'](_0x1232dd['model'],_0x1232dd[_0x1f20('0x32')]);_0x4b4200[_0x1f20('0x34')]=_[_0x1f20('0x33')](_0x1232dd[_0x1f20('0x31')],qs[_0x1f20('0x35')](_0x14d25c[_0x1f20('0x32')][_0x1f20('0x35')]));_0x4b4200[_0x1f20('0x34')]=_0x4b4200[_0x1f20('0x34')]['length']?_0x4b4200['attributes']:_0x1232dd[_0x1f20('0x31')];if(_0x14d25c[_0x1f20('0x32')][_0x1f20('0x4e')]){_0x4b4200[_0x1f20('0x46')]=[{'all':!![]}];}_0x4b4200=_[_0x1f20('0x40')]({},_0x4b4200,_0x14d25c[_0x1f20('0x4f')]);return db['ChatInternalMessage'][_0x1f20('0x50')](_0x4b4200)[_0x1f20('0x29')](handleEntityNotFound(_0x4f61bb,null))['then'](respondWithResult(_0x4f61bb,null))[_0x1f20('0x49')](handleError(_0x4f61bb,null));};exports['create']=function(_0x26f1af,_0x4e19eb){return db[_0x1f20('0x30')][_0x1f20('0x51')](_0x26f1af[_0x1f20('0x52')],{})[_0x1f20('0x29')](respondWithResult(_0x4e19eb,0xc9))[_0x1f20('0x49')](handleError(_0x4e19eb,null));};exports[_0x1f20('0x28')]=function(_0x765ae1,_0x53d050){if(_0x765ae1[_0x1f20('0x52')]['id']){delete _0x765ae1[_0x1f20('0x52')]['id'];}return db['ChatInternalMessage'][_0x1f20('0x50')]({'where':{'id':_0x765ae1['params']['id']}})[_0x1f20('0x29')](handleEntityNotFound(_0x53d050,null))[_0x1f20('0x29')](saveUpdates(_0x765ae1[_0x1f20('0x52')],null))[_0x1f20('0x29')](respondWithResult(_0x53d050,null))[_0x1f20('0x49')](handleError(_0x53d050,null));};exports[_0x1f20('0x2a')]=function(_0x3455e5,_0x4309b7){return db[_0x1f20('0x30')][_0x1f20('0x50')]({'where':{'id':_0x3455e5[_0x1f20('0x4b')]['id']}})['then'](handleEntityNotFound(_0x4309b7,null))[_0x1f20('0x29')](removeEntity(_0x4309b7,null))[_0x1f20('0x49')](handleError(_0x4309b7,null));};exports[_0x1f20('0x53')]=function(_0x4c5158,_0x5cd201){return db['ChatInternalMessage']['describe']()['then'](respondWithResult(_0x5cd201,null))[_0x1f20('0x49')](handleError(_0x5cd201,null));};function wrapUser(_0xa41fab){return function(_0x12a2b8){var _0x2db5e9=_0xa41fab[_0x1f20('0x54')]({'plain':!![]});_0x2db5e9[_0x1f20('0x55')]=[];if(_0x12a2b8){_0x2db5e9[_0x1f20('0x56')]=_0x12a2b8[_0x1f20('0x57')];_0x2db5e9['lastMessage']=_0x12a2b8;}else{_0x2db5e9['lastMessageAt']=moment(0x0)[_0x1f20('0x58')](_0x1f20('0x59'));_0x2db5e9[_0x1f20('0x5a')]={};}return _0x2db5e9;};}exports[_0x1f20('0x5b')]=function(_0x446c0d,_0x306c45,_0x5b4dcd){var _0x3c6e5c=[];return db[_0x1f20('0x5c')][_0x1f20('0x47')]({'where':{'role':['agent',_0x1f20('0x5d'),'user']},'attributes':['id',_0x1f20('0x5e'),'name',_0x1f20('0x5f'),_0x1f20('0x60'),'internal','online',_0x1f20('0x61'),_0x1f20('0x62'),_0x1f20('0x63')],'order':qs['sort'](_0x1f20('0x64')),'include':[{'model':db[_0x1f20('0x65')],'as':'VoiceQueues','attributes':['id',_0x1f20('0x2d'),_0x1f20('0x42')],'through':{'attributes':[]}}]})[_0x1f20('0x29')](function(_0x3e3be0){if(_0x3e3be0){var _0x4744dd=_0x446c0d['user'][_0x1f20('0x54')]({'plain':!![]});if(!_0x4744dd)throw new Error(_0x1f20('0x66'));if(_0x4744dd[_0x1f20('0x5f')]===_0x1f20('0x67')){return db['UserProfileSection'][_0x1f20('0x50')]({'where':{'name':_0x1f20('0x68'),'userProfileId':_0x4744dd['userProfileId']},'raw':!![]})[_0x1f20('0x29')](function(_0x261e33){if(_0x261e33){if(_0x261e33[_0x1f20('0x69')]===0x0){return db['UserProfileResource'][_0x1f20('0x47')]({'where':{'sectionId':_0x261e33['id']},'raw':!![]})[_0x1f20('0x29')](function(_0x1eeb53){var _0xfdf7ee=[];if(_['isEmpty'](_0x1eeb53)){_0xfdf7ee=_['filter'](_0x3e3be0,function(_0x1acf09){if(_0x1acf09['role']!==_0x1f20('0x6a')){return _0x1acf09;}});return _0xfdf7ee;}else{for(var _0x5b417d=0x0;_0x5b417d<_0x3e3be0[_0x1f20('0x36')];_0x5b417d++){var _0x2548f9=_0x3e3be0[_0x5b417d];if(_0x2548f9[_0x1f20('0x5f')]===_0x1f20('0x6a')){var _0x4e17af=_['find'](_0x1eeb53,function(_0x5d45ed){return _0x5d45ed['resourceId']===_0x2548f9['id'];});if(_0x4e17af)_0xfdf7ee['push'](_0x2548f9);}else{_0xfdf7ee['push'](_0x2548f9);}}return _0xfdf7ee;}});}else{return _0x3e3be0;}}else{var _0x164943=_[_0x1f20('0x3f')](_0x3e3be0,function(_0x26cb48){if(_0x26cb48[_0x1f20('0x5f')]!=='agent'){return _0x26cb48;}});return _0x164943;}});}else{return _0x3e3be0;}}})[_0x1f20('0x29')](function(_0x2ffb1b){var _0x471202=[];if(_0x2ffb1b){_0x3c6e5c=_0x2ffb1b;for(var _0x1d77c4=0x0;_0x1d77c4<_0x2ffb1b[_0x1f20('0x36')];_0x1d77c4+=0x1){_0x3c6e5c[_0x1d77c4][_0x1f20('0x55')]=[];_0x471202[_0x1f20('0x6b')](db[_0x1f20('0x30')]['find']({'where':{'FromId':[_0x446c0d[_0x1f20('0x67')]['id'],_0x2ffb1b[_0x1d77c4]['id']],'ToId':[_0x446c0d[_0x1f20('0x67')]['id'],_0x2ffb1b[_0x1d77c4]['id']],'ChatGroupId':null},'attributes':['id',_0x1f20('0x57'),_0x1f20('0x6c'),_0x1f20('0x6d'),_0x1f20('0x6e'),_0x1f20('0x52')],'order':qs[_0x1f20('0x3b')](_0x1f20('0x6f')),'limit':0x1,'offset':0x0,'raw':!![]})[_0x1f20('0x29')](wrapUser(_0x2ffb1b[_0x1d77c4])));}return BPromise[_0x1f20('0x70')](_0x471202);}return[];})['then'](function(_0x5b33d8){return{'rows':_0x5b33d8,'count':_0x5b33d8['length']};})[_0x1f20('0x29')](respondWithResult(_0x306c45,null))[_0x1f20('0x49')](handleError(_0x306c45,null));};