Built motion from commit 092f8824.|2.5.49
[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 _0x6f25=['where','filter','merge','options','includeAll','include','findAll','rows','show','params','fields','find','catch','create','body','update','describe','createdAt','lastMessage','lastMessageAt','format','YYYY-MM-DD\x20HH:mm:ss','agent','admin','user','fullname','role','userpic','internal','online','lastLoginAt','-online','VoiceQueue','VoiceQueues','get','UserProfileSection','Agents','userProfileId','autoAssociation','UserProfileResource','resourceId','push','messages','FromId','ToId','read','all','eml-format','zip-dir','fast-json-patch','moment','mustache','path','to-csv','ejs','lodash','crypto','jsforce','querystring','papaparse','ioredis','../../components/parsers/advancedSearch','api','../../config/utils','../../config/license/util','defaults','redis','localhost','socket.io-emitter','./chatInternalMessage.socket','register','status','end','json','offset','limit','count','set','Content-Range','apply','reject','then','destroy','sendStatus','error','stack','name','index','map','ChatInternalMessage','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','length','hasOwnProperty','nolimit','order','sort'];(function(_0x54d506,_0xabf5c2){var _0x5d3040=function(_0x102670){while(--_0x102670){_0x54d506['push'](_0x54d506['shift']());}};_0x5d3040(++_0xabf5c2);}(_0x6f25,0x98));var _0x56f2=function(_0x4ab277,_0x405b3f){_0x4ab277=_0x4ab277-0x0;var _0x3d654d=_0x6f25[_0x4ab277];return _0x3d654d;};'use strict';var emlformat=require(_0x56f2('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x56f2('0x1'));var jsonpatch=require(_0x56f2('0x2'));var rp=require('request-promise');var moment=require(_0x56f2('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x56f2('0x4'));var util=require('util');var path=require(_0x56f2('0x5'));var sox=require('sox');var csv=require(_0x56f2('0x6'));var ejs=require(_0x56f2('0x7'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x56f2('0x8'));var squel=require('squel');var crypto=require(_0x56f2('0x9'));var jsforce=require(_0x56f2('0xa'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x56f2('0xb'));var Papa=require(_0x56f2('0xc'));var Redis=require(_0x56f2('0xd'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x56f2('0xe'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x56f2('0xf'));var utils=require(_0x56f2('0x10'));var config=require('../../config/environment');var licenseUtil=require(_0x56f2('0x11'));var db=require('../../mysqldb')['db'];config['redis']=_[_0x56f2('0x12')](config[_0x56f2('0x13')],{'host':_0x56f2('0x14'),'port':0x18eb});var socket=require(_0x56f2('0x15'))(new Redis(config['redis']));require(_0x56f2('0x16'))[_0x56f2('0x17')](socket);function respondWithStatusCode(_0xc29a9d,_0x3c0024){_0x3c0024=_0x3c0024||0xcc;return function(_0x39bd29){if(_0x39bd29){return _0xc29a9d['sendStatus'](_0x3c0024);}return _0xc29a9d[_0x56f2('0x18')](_0x3c0024)[_0x56f2('0x19')]();};}function respondWithResult(_0x57d395,_0x21d9e2){_0x21d9e2=_0x21d9e2||0xc8;return function(_0x507485){if(_0x507485){return _0x57d395[_0x56f2('0x18')](_0x21d9e2)[_0x56f2('0x1a')](_0x507485);}};}function respondWithFilteredResult(_0x2828d9,_0x222cef){return function(_0x1710e9){if(_0x1710e9){var _0x3c0827=typeof _0x222cef[_0x56f2('0x1b')]==='undefined'&&typeof _0x222cef[_0x56f2('0x1c')]==='undefined';var _0x1a9454=_0x1710e9[_0x56f2('0x1d')];var _0x50b0d3=_0x3c0827?0x0:_0x222cef[_0x56f2('0x1b')];var _0x257d47=_0x3c0827?_0x1710e9[_0x56f2('0x1d')]:_0x222cef[_0x56f2('0x1b')]+_0x222cef[_0x56f2('0x1c')];var _0xf33184;if(_0x257d47>=_0x1a9454){_0x257d47=_0x1a9454;_0xf33184=0xc8;}else{_0xf33184=0xce;}_0x2828d9[_0x56f2('0x18')](_0xf33184);return _0x2828d9[_0x56f2('0x1e')](_0x56f2('0x1f'),_0x50b0d3+'-'+_0x257d47+'/'+_0x1a9454)[_0x56f2('0x1a')](_0x1710e9);}return null;};}function patchUpdates(_0x1250d4){return function(_0x3dd78b){try{jsonpatch[_0x56f2('0x20')](_0x3dd78b,_0x1250d4,!![]);}catch(_0x5d35dd){return BPromise[_0x56f2('0x21')](_0x5d35dd);}return _0x3dd78b['save']();};}function saveUpdates(_0x1c9b20,_0x5c9ea8){return function(_0x2e9ced){if(_0x2e9ced){return _0x2e9ced['update'](_0x1c9b20)[_0x56f2('0x22')](function(_0x241b7b){return _0x241b7b;});}return null;};}function removeEntity(_0x41c22d,_0x2de57e){return function(_0x4fcc98){if(_0x4fcc98){return _0x4fcc98[_0x56f2('0x23')]()['then'](function(){_0x41c22d[_0x56f2('0x18')](0xcc)[_0x56f2('0x19')]();});}};}function handleEntityNotFound(_0x23312b,_0x5888de){return function(_0x2708ad){if(!_0x2708ad){_0x23312b[_0x56f2('0x24')](0x194);}return _0x2708ad;};}function handleError(_0x397fac,_0x6d5808){_0x6d5808=_0x6d5808||0x1f4;return function(_0x592c8f){logger[_0x56f2('0x25')](_0x592c8f[_0x56f2('0x26')]);if(_0x592c8f[_0x56f2('0x27')]){delete _0x592c8f['name'];}_0x397fac[_0x56f2('0x18')](_0x6d5808)['send'](_0x592c8f);};}exports[_0x56f2('0x28')]=function(_0x1fe802,_0xa72eca){var _0x8be49b={},_0x3dccda={},_0x585061={'count':0x0,'rows':[]};var _0x39d852=_[_0x56f2('0x29')](db[_0x56f2('0x2a')][_0x56f2('0x2b')],function(_0x5195ef){return{'name':_0x5195ef[_0x56f2('0x2c')],'type':_0x5195ef[_0x56f2('0x2d')][_0x56f2('0x2e')]};});_0x3dccda[_0x56f2('0x2f')]=_['map'](_0x39d852,_0x56f2('0x27'));_0x3dccda[_0x56f2('0x30')]=_[_0x56f2('0x31')](_0x1fe802[_0x56f2('0x30')]);_0x3dccda[_0x56f2('0x32')]=_[_0x56f2('0x33')](_0x3dccda[_0x56f2('0x2f')],_0x3dccda[_0x56f2('0x30')]);_0x8be49b[_0x56f2('0x34')]=_[_0x56f2('0x33')](_0x3dccda['model'],qs['fields'](_0x1fe802[_0x56f2('0x30')]['fields']));_0x8be49b[_0x56f2('0x34')]=_0x8be49b[_0x56f2('0x34')][_0x56f2('0x35')]?_0x8be49b['attributes']:_0x3dccda[_0x56f2('0x2f')];if(!_0x1fe802[_0x56f2('0x30')][_0x56f2('0x36')](_0x56f2('0x37'))){_0x8be49b[_0x56f2('0x1c')]=qs['limit'](_0x1fe802['query'][_0x56f2('0x1c')]);_0x8be49b[_0x56f2('0x1b')]=qs[_0x56f2('0x1b')](_0x1fe802[_0x56f2('0x30')][_0x56f2('0x1b')]);}_0x8be49b[_0x56f2('0x38')]=qs['sort'](_0x1fe802[_0x56f2('0x30')][_0x56f2('0x39')]);_0x8be49b[_0x56f2('0x3a')]=qs[_0x56f2('0x32')](_['pick'](_0x1fe802[_0x56f2('0x30')],_0x3dccda[_0x56f2('0x32')]),_0x39d852);if(_0x1fe802[_0x56f2('0x30')][_0x56f2('0x3b')]){_0x8be49b['where']=_[_0x56f2('0x3c')](_0x8be49b[_0x56f2('0x3a')],{'$or':_[_0x56f2('0x29')](_0x39d852,function(_0x4456bd){if(_0x4456bd[_0x56f2('0x2d')]!=='VIRTUAL'){var _0x168c7b={};_0x168c7b[_0x4456bd[_0x56f2('0x27')]]={'$like':'%'+_0x1fe802[_0x56f2('0x30')][_0x56f2('0x3b')]+'%'};return _0x168c7b;}})});}_0x8be49b=_[_0x56f2('0x3c')]({},_0x8be49b,_0x1fe802[_0x56f2('0x3d')]);var _0x3ba8d9={'where':_0x8be49b[_0x56f2('0x3a')]};return db['ChatInternalMessage']['count'](_0x3ba8d9)[_0x56f2('0x22')](function(_0x28ff00){_0x585061['count']=_0x28ff00;if(_0x1fe802[_0x56f2('0x30')][_0x56f2('0x3e')]){_0x8be49b[_0x56f2('0x3f')]=[{'all':!![]}];}return db[_0x56f2('0x2a')][_0x56f2('0x40')](_0x8be49b);})['then'](function(_0x150356){_0x585061[_0x56f2('0x41')]=_0x150356;return _0x585061;})[_0x56f2('0x22')](respondWithFilteredResult(_0xa72eca,_0x8be49b))['catch'](handleError(_0xa72eca,null));};exports[_0x56f2('0x42')]=function(_0x5c5852,_0x3bf5bc){var _0x4430be={'raw':!![],'where':{'id':_0x5c5852[_0x56f2('0x43')]['id']}},_0x3598ac={};_0x3598ac[_0x56f2('0x2f')]=_[_0x56f2('0x31')](db[_0x56f2('0x2a')][_0x56f2('0x2b')]);_0x3598ac[_0x56f2('0x30')]=_[_0x56f2('0x31')](_0x5c5852['query']);_0x3598ac[_0x56f2('0x32')]=_['intersection'](_0x3598ac[_0x56f2('0x2f')],_0x3598ac[_0x56f2('0x30')]);_0x4430be['attributes']=_['intersection'](_0x3598ac[_0x56f2('0x2f')],qs['fields'](_0x5c5852[_0x56f2('0x30')][_0x56f2('0x44')]));_0x4430be[_0x56f2('0x34')]=_0x4430be[_0x56f2('0x34')]['length']?_0x4430be[_0x56f2('0x34')]:_0x3598ac[_0x56f2('0x2f')];if(_0x5c5852['query'][_0x56f2('0x3e')]){_0x4430be['include']=[{'all':!![]}];}_0x4430be=_[_0x56f2('0x3c')]({},_0x4430be,_0x5c5852[_0x56f2('0x3d')]);return db[_0x56f2('0x2a')][_0x56f2('0x45')](_0x4430be)[_0x56f2('0x22')](handleEntityNotFound(_0x3bf5bc,null))['then'](respondWithResult(_0x3bf5bc,null))[_0x56f2('0x46')](handleError(_0x3bf5bc,null));};exports[_0x56f2('0x47')]=function(_0x4e3ab0,_0x377dd1){return db['ChatInternalMessage']['create'](_0x4e3ab0[_0x56f2('0x48')],{})[_0x56f2('0x22')](respondWithResult(_0x377dd1,0xc9))['catch'](handleError(_0x377dd1,null));};exports[_0x56f2('0x49')]=function(_0x75ec0f,_0x2fb426){if(_0x75ec0f[_0x56f2('0x48')]['id']){delete _0x75ec0f[_0x56f2('0x48')]['id'];}return db[_0x56f2('0x2a')][_0x56f2('0x45')]({'where':{'id':_0x75ec0f[_0x56f2('0x43')]['id']}})[_0x56f2('0x22')](handleEntityNotFound(_0x2fb426,null))[_0x56f2('0x22')](saveUpdates(_0x75ec0f['body'],null))[_0x56f2('0x22')](respondWithResult(_0x2fb426,null))[_0x56f2('0x46')](handleError(_0x2fb426,null));};exports[_0x56f2('0x23')]=function(_0x4fcd79,_0x517558){return db[_0x56f2('0x2a')][_0x56f2('0x45')]({'where':{'id':_0x4fcd79[_0x56f2('0x43')]['id']}})['then'](handleEntityNotFound(_0x517558,null))[_0x56f2('0x22')](removeEntity(_0x517558,null))[_0x56f2('0x46')](handleError(_0x517558,null));};exports[_0x56f2('0x4a')]=function(_0x1cb234,_0x20fb86){return db[_0x56f2('0x2a')][_0x56f2('0x4a')]()[_0x56f2('0x22')](respondWithResult(_0x20fb86,null))[_0x56f2('0x46')](handleError(_0x20fb86,null));};function wrapUser(_0x51dc68){return function(_0x2a3c70){var _0xcc1666=_0x51dc68['get']({'plain':!![]});_0xcc1666['messages']=[];if(_0x2a3c70){_0xcc1666['lastMessageAt']=_0x2a3c70[_0x56f2('0x4b')];_0xcc1666[_0x56f2('0x4c')]=_0x2a3c70;}else{_0xcc1666[_0x56f2('0x4d')]=moment(0x0)[_0x56f2('0x4e')](_0x56f2('0x4f'));_0xcc1666['lastMessage']={};}return _0xcc1666;};}exports['getUsers']=function(_0xe35590,_0x3d8952,_0x48b0cb){var _0x5172ca=[];return db['User'][_0x56f2('0x40')]({'where':{'role':[_0x56f2('0x50'),_0x56f2('0x51'),_0x56f2('0x52')]},'attributes':['id',_0x56f2('0x53'),'name',_0x56f2('0x54'),_0x56f2('0x55'),_0x56f2('0x56'),_0x56f2('0x57'),_0x56f2('0x58'),'allowmessenger','showWebBar'],'order':qs[_0x56f2('0x39')](_0x56f2('0x59')),'include':[{'model':db[_0x56f2('0x5a')],'as':_0x56f2('0x5b'),'attributes':['id',_0x56f2('0x27'),_0x56f2('0x2d')],'through':{'attributes':[]}}]})[_0x56f2('0x22')](function(_0x598b52){if(_0x598b52){var _0x1c9d4e=_0xe35590[_0x56f2('0x52')][_0x56f2('0x5c')]({'plain':!![]});if(!_0x1c9d4e)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x1c9d4e['role']===_0x56f2('0x52')){return db[_0x56f2('0x5d')][_0x56f2('0x45')]({'where':{'name':_0x56f2('0x5e'),'userProfileId':_0x1c9d4e[_0x56f2('0x5f')]},'raw':!![]})[_0x56f2('0x22')](function(_0x18488a){if(_0x18488a){if(_0x18488a[_0x56f2('0x60')]===0x0){return db[_0x56f2('0x61')]['findAll']({'where':{'sectionId':_0x18488a['id']},'raw':!![]})[_0x56f2('0x22')](function(_0x793e59){var _0x3fe9ea=[];if(_['isEmpty'](_0x793e59)){_0x3fe9ea=_[_0x56f2('0x3b')](_0x598b52,function(_0x2c0605){if(_0x2c0605[_0x56f2('0x54')]!==_0x56f2('0x50')){return _0x2c0605;}});return _0x3fe9ea;}else{for(var _0x1ab70e=0x0;_0x1ab70e<_0x598b52['length'];_0x1ab70e++){var _0x487be8=_0x598b52[_0x1ab70e];if(_0x487be8['role']===_0x56f2('0x50')){var _0x5a8e7b=_['find'](_0x793e59,function(_0x2816ff){return _0x2816ff[_0x56f2('0x62')]===_0x487be8['id'];});if(_0x5a8e7b)_0x3fe9ea[_0x56f2('0x63')](_0x487be8);}else{_0x3fe9ea[_0x56f2('0x63')](_0x487be8);}}return _0x3fe9ea;}});}else{return _0x598b52;}}else{var _0x52e688=_[_0x56f2('0x3b')](_0x598b52,function(_0xf118a9){if(_0xf118a9[_0x56f2('0x54')]!=='agent'){return _0xf118a9;}});return _0x52e688;}});}else{return _0x598b52;}}})['then'](function(_0x14f24f){var _0x37c0b6=[];if(_0x14f24f){_0x5172ca=_0x14f24f;for(var _0x304607=0x0;_0x304607<_0x14f24f[_0x56f2('0x35')];_0x304607+=0x1){_0x5172ca[_0x304607][_0x56f2('0x64')]=[];_0x37c0b6[_0x56f2('0x63')](db[_0x56f2('0x2a')][_0x56f2('0x45')]({'where':{'FromId':[_0xe35590[_0x56f2('0x52')]['id'],_0x14f24f[_0x304607]['id']],'ToId':[_0xe35590[_0x56f2('0x52')]['id'],_0x14f24f[_0x304607]['id']],'ChatGroupId':null},'attributes':['id','createdAt',_0x56f2('0x65'),_0x56f2('0x66'),_0x56f2('0x67'),'body'],'order':qs[_0x56f2('0x39')]('-createdAt'),'limit':0x1,'offset':0x0,'raw':!![]})[_0x56f2('0x22')](wrapUser(_0x14f24f[_0x304607])));}return BPromise[_0x56f2('0x68')](_0x37c0b6);}return[];})['then'](function(_0x1e9c91){return{'rows':_0x1e9c91,'count':_0x1e9c91['length']};})[_0x56f2('0x22')](respondWithResult(_0x3d8952,null))[_0x56f2('0x46')](handleError(_0x3d8952,null));};