ccc8872a58ba2b60a95318f9ae7ee57aa2f14f9b
[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 _0xf038=['get','messages','lastMessageAt','createdAt','format','lastMessage','getUsers','User','agent','admin','fullname','userpic','internal','online','lastLoginAt','allowmessenger','showWebBar','-online','VoiceQueues','type','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','isEmpty','length','push','user','FromId','ToId','-createdAt','all','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','localhost','socket.io-emitter','redis','./chatInternalMessage.socket','sendStatus','status','end','offset','undefined','count','limit','Content-Range','json','apply','reject','save','update','destroy','then','stack','name','send','index','map','ChatInternalMessage','rawAttributes','model','query','filters','attributes','intersection','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','includeAll','include','findAll','rows','catch','show','params','keys','options','create','body','find','describe'];(function(_0x23b204,_0x4b773c){var _0x29ac3b=function(_0x26dedf){while(--_0x26dedf){_0x23b204['push'](_0x23b204['shift']());}};_0x29ac3b(++_0x4b773c);}(_0xf038,0x1ea));var _0x8f03=function(_0x12e3e3,_0x501eaf){_0x12e3e3=_0x12e3e3-0x0;var _0x26602e=_0xf038[_0x12e3e3];return _0x26602e;};'use strict';var emlformat=require(_0x8f03('0x0'));var rimraf=require(_0x8f03('0x1'));var zipdir=require(_0x8f03('0x2'));var jsonpatch=require(_0x8f03('0x3'));var rp=require(_0x8f03('0x4'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x8f03('0x5'));var util=require(_0x8f03('0x6'));var path=require(_0x8f03('0x7'));var sox=require(_0x8f03('0x8'));var csv=require(_0x8f03('0x9'));var ejs=require(_0x8f03('0xa'));var fs=require('fs');var fs_extra=require(_0x8f03('0xb'));var _=require(_0x8f03('0xc'));var squel=require(_0x8f03('0xd'));var crypto=require(_0x8f03('0xe'));var jsforce=require(_0x8f03('0xf'));var deskjs=require(_0x8f03('0x10'));var toCsv=require(_0x8f03('0x9'));var querystring=require(_0x8f03('0x11'));var Papa=require('papaparse');var Redis=require(_0x8f03('0x12'));var authService=require(_0x8f03('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x8f03('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x8f03('0x15'))(_0x8f03('0x16'));var utils=require(_0x8f03('0x17'));var config=require(_0x8f03('0x18'));var licenseUtil=require(_0x8f03('0x19'));var db=require(_0x8f03('0x1a'))['db'];config['redis']=_['defaults'](config['redis'],{'host':_0x8f03('0x1b'),'port':0x18eb});var socket=require(_0x8f03('0x1c'))(new Redis(config[_0x8f03('0x1d')]));require(_0x8f03('0x1e'))['register'](socket);function respondWithStatusCode(_0x5bdbf2,_0x44f3cb){_0x44f3cb=_0x44f3cb||0xcc;return function(_0x58b779){if(_0x58b779){return _0x5bdbf2[_0x8f03('0x1f')](_0x44f3cb);}return _0x5bdbf2[_0x8f03('0x20')](_0x44f3cb)[_0x8f03('0x21')]();};}function respondWithResult(_0x512ca9,_0x295d6){_0x295d6=_0x295d6||0xc8;return function(_0x3d2c7f){if(_0x3d2c7f){return _0x512ca9[_0x8f03('0x20')](_0x295d6)['json'](_0x3d2c7f);}};}function respondWithFilteredResult(_0xca857d,_0x295a48){return function(_0x330af9){if(_0x330af9){var _0xd11178=typeof _0x295a48[_0x8f03('0x22')]==='undefined'&&typeof _0x295a48['limit']===_0x8f03('0x23');var _0x474fdb=_0x330af9[_0x8f03('0x24')];var _0x5e368c=_0xd11178?0x0:_0x295a48[_0x8f03('0x22')];var _0x2738d5=_0xd11178?_0x330af9[_0x8f03('0x24')]:_0x295a48[_0x8f03('0x22')]+_0x295a48[_0x8f03('0x25')];var _0x14c714;if(_0x2738d5>=_0x474fdb){_0x2738d5=_0x474fdb;_0x14c714=0xc8;}else{_0x14c714=0xce;}_0xca857d[_0x8f03('0x20')](_0x14c714);return _0xca857d['set'](_0x8f03('0x26'),_0x5e368c+'-'+_0x2738d5+'/'+_0x474fdb)[_0x8f03('0x27')](_0x330af9);}return null;};}function patchUpdates(_0x5402bb){return function(_0xfc4058){try{jsonpatch[_0x8f03('0x28')](_0xfc4058,_0x5402bb,!![]);}catch(_0x49632f){return BPromise[_0x8f03('0x29')](_0x49632f);}return _0xfc4058[_0x8f03('0x2a')]();};}function saveUpdates(_0x580f02,_0x5b8919){return function(_0x37f537){if(_0x37f537){return _0x37f537[_0x8f03('0x2b')](_0x580f02)['then'](function(_0x385ce1){return _0x385ce1;});}return null;};}function removeEntity(_0xe11c87,_0x44b20d){return function(_0x49d176){if(_0x49d176){return _0x49d176[_0x8f03('0x2c')]()[_0x8f03('0x2d')](function(){_0xe11c87[_0x8f03('0x20')](0xcc)[_0x8f03('0x21')]();});}};}function handleEntityNotFound(_0x1f8071,_0x15d017){return function(_0x4f8b08){if(!_0x4f8b08){_0x1f8071['sendStatus'](0x194);}return _0x4f8b08;};}function handleError(_0x3907f8,_0x55c641){_0x55c641=_0x55c641||0x1f4;return function(_0xdcaa50){logger['error'](_0xdcaa50[_0x8f03('0x2e')]);if(_0xdcaa50[_0x8f03('0x2f')]){delete _0xdcaa50[_0x8f03('0x2f')];}_0x3907f8[_0x8f03('0x20')](_0x55c641)[_0x8f03('0x30')](_0xdcaa50);};}exports[_0x8f03('0x31')]=function(_0x525ee5,_0x3357f2){var _0x56bf55={},_0x562cb9={},_0x546482={'count':0x0,'rows':[]};var _0x49ec7f=_[_0x8f03('0x32')](db[_0x8f03('0x33')][_0x8f03('0x34')],function(_0x3ce409){return{'name':_0x3ce409['fieldName'],'type':_0x3ce409['type']['key']};});_0x562cb9[_0x8f03('0x35')]=_[_0x8f03('0x32')](_0x49ec7f,_0x8f03('0x2f'));_0x562cb9['query']=_['keys'](_0x525ee5[_0x8f03('0x36')]);_0x562cb9[_0x8f03('0x37')]=_['intersection'](_0x562cb9[_0x8f03('0x35')],_0x562cb9[_0x8f03('0x36')]);_0x56bf55[_0x8f03('0x38')]=_[_0x8f03('0x39')](_0x562cb9[_0x8f03('0x35')],qs[_0x8f03('0x3a')](_0x525ee5[_0x8f03('0x36')][_0x8f03('0x3a')]));_0x56bf55['attributes']=_0x56bf55[_0x8f03('0x38')]['length']?_0x56bf55[_0x8f03('0x38')]:_0x562cb9[_0x8f03('0x35')];if(!_0x525ee5[_0x8f03('0x36')][_0x8f03('0x3b')](_0x8f03('0x3c'))){_0x56bf55['limit']=qs[_0x8f03('0x25')](_0x525ee5[_0x8f03('0x36')]['limit']);_0x56bf55[_0x8f03('0x22')]=qs[_0x8f03('0x22')](_0x525ee5[_0x8f03('0x36')]['offset']);}_0x56bf55[_0x8f03('0x3d')]=qs['sort'](_0x525ee5[_0x8f03('0x36')][_0x8f03('0x3e')]);_0x56bf55[_0x8f03('0x3f')]=qs['filters'](_[_0x8f03('0x40')](_0x525ee5[_0x8f03('0x36')],_0x562cb9[_0x8f03('0x37')]),_0x49ec7f);if(_0x525ee5[_0x8f03('0x36')][_0x8f03('0x41')]){_0x56bf55['where']=_[_0x8f03('0x42')](_0x56bf55['where'],{'$or':_[_0x8f03('0x32')](_0x49ec7f,function(_0x395873){if(_0x395873['type']!=='VIRTUAL'){var _0x14ad0f={};_0x14ad0f[_0x395873[_0x8f03('0x2f')]]={'$like':'%'+_0x525ee5[_0x8f03('0x36')][_0x8f03('0x41')]+'%'};return _0x14ad0f;}})});}_0x56bf55=_[_0x8f03('0x42')]({},_0x56bf55,_0x525ee5['options']);var _0x4825cf={'where':_0x56bf55[_0x8f03('0x3f')]};return db[_0x8f03('0x33')][_0x8f03('0x24')](_0x4825cf)['then'](function(_0x268498){_0x546482[_0x8f03('0x24')]=_0x268498;if(_0x525ee5[_0x8f03('0x36')][_0x8f03('0x43')]){_0x56bf55[_0x8f03('0x44')]=[{'all':!![]}];}return db[_0x8f03('0x33')][_0x8f03('0x45')](_0x56bf55);})['then'](function(_0x2a44ea){_0x546482[_0x8f03('0x46')]=_0x2a44ea;return _0x546482;})[_0x8f03('0x2d')](respondWithFilteredResult(_0x3357f2,_0x56bf55))[_0x8f03('0x47')](handleError(_0x3357f2,null));};exports[_0x8f03('0x48')]=function(_0xcdef67,_0x5eb5cb){var _0x3fb31e={'raw':!![],'where':{'id':_0xcdef67[_0x8f03('0x49')]['id']}},_0x75adeb={};_0x75adeb[_0x8f03('0x35')]=_[_0x8f03('0x4a')](db[_0x8f03('0x33')][_0x8f03('0x34')]);_0x75adeb[_0x8f03('0x36')]=_[_0x8f03('0x4a')](_0xcdef67['query']);_0x75adeb['filters']=_['intersection'](_0x75adeb[_0x8f03('0x35')],_0x75adeb['query']);_0x3fb31e['attributes']=_['intersection'](_0x75adeb[_0x8f03('0x35')],qs[_0x8f03('0x3a')](_0xcdef67[_0x8f03('0x36')][_0x8f03('0x3a')]));_0x3fb31e[_0x8f03('0x38')]=_0x3fb31e[_0x8f03('0x38')]['length']?_0x3fb31e[_0x8f03('0x38')]:_0x75adeb[_0x8f03('0x35')];if(_0xcdef67[_0x8f03('0x36')][_0x8f03('0x43')]){_0x3fb31e[_0x8f03('0x44')]=[{'all':!![]}];}_0x3fb31e=_['merge']({},_0x3fb31e,_0xcdef67[_0x8f03('0x4b')]);return db[_0x8f03('0x33')]['find'](_0x3fb31e)[_0x8f03('0x2d')](handleEntityNotFound(_0x5eb5cb,null))['then'](respondWithResult(_0x5eb5cb,null))[_0x8f03('0x47')](handleError(_0x5eb5cb,null));};exports[_0x8f03('0x4c')]=function(_0x1f58e0,_0x4cc274){return db['ChatInternalMessage']['create'](_0x1f58e0[_0x8f03('0x4d')],{})[_0x8f03('0x2d')](respondWithResult(_0x4cc274,0xc9))[_0x8f03('0x47')](handleError(_0x4cc274,null));};exports[_0x8f03('0x2b')]=function(_0x368dde,_0x5aa2e0){if(_0x368dde[_0x8f03('0x4d')]['id']){delete _0x368dde[_0x8f03('0x4d')]['id'];}return db[_0x8f03('0x33')][_0x8f03('0x4e')]({'where':{'id':_0x368dde['params']['id']}})['then'](handleEntityNotFound(_0x5aa2e0,null))[_0x8f03('0x2d')](saveUpdates(_0x368dde[_0x8f03('0x4d')],null))['then'](respondWithResult(_0x5aa2e0,null))[_0x8f03('0x47')](handleError(_0x5aa2e0,null));};exports[_0x8f03('0x2c')]=function(_0x2bad2c,_0x3a9d72){return db[_0x8f03('0x33')][_0x8f03('0x4e')]({'where':{'id':_0x2bad2c['params']['id']}})[_0x8f03('0x2d')](handleEntityNotFound(_0x3a9d72,null))[_0x8f03('0x2d')](removeEntity(_0x3a9d72,null))[_0x8f03('0x47')](handleError(_0x3a9d72,null));};exports[_0x8f03('0x4f')]=function(_0x254ef0,_0x1473a5){return db['ChatInternalMessage'][_0x8f03('0x4f')]()[_0x8f03('0x2d')](respondWithResult(_0x1473a5,null))[_0x8f03('0x47')](handleError(_0x1473a5,null));};function wrapUser(_0x2c3d18){return function(_0xe298a5){var _0xe75147=_0x2c3d18[_0x8f03('0x50')]({'plain':!![]});_0xe75147[_0x8f03('0x51')]=[];if(_0xe298a5){_0xe75147[_0x8f03('0x52')]=_0xe298a5[_0x8f03('0x53')];_0xe75147['lastMessage']=_0xe298a5;}else{_0xe75147[_0x8f03('0x52')]=moment(0x0)[_0x8f03('0x54')]('YYYY-MM-DD\x20HH:mm:ss');_0xe75147[_0x8f03('0x55')]={};}return _0xe75147;};}exports[_0x8f03('0x56')]=function(_0x2fb0ea,_0x2b61ea,_0x40a581){var _0x17a829=[];return db[_0x8f03('0x57')]['findAll']({'where':{'role':[_0x8f03('0x58'),_0x8f03('0x59'),'user']},'attributes':['id',_0x8f03('0x5a'),'name','role',_0x8f03('0x5b'),_0x8f03('0x5c'),_0x8f03('0x5d'),_0x8f03('0x5e'),_0x8f03('0x5f'),_0x8f03('0x60')],'order':qs[_0x8f03('0x3e')](_0x8f03('0x61')),'include':[{'model':db['VoiceQueue'],'as':_0x8f03('0x62'),'attributes':['id',_0x8f03('0x2f'),_0x8f03('0x63')],'through':{'attributes':[]}}]})[_0x8f03('0x2d')](function(_0xaac356){if(_0xaac356){var _0x11d991=_0x2fb0ea['user'][_0x8f03('0x50')]({'plain':!![]});if(!_0x11d991)throw new Error(_0x8f03('0x64'));if(_0x11d991[_0x8f03('0x65')]==='user'){return db[_0x8f03('0x66')][_0x8f03('0x4e')]({'where':{'name':'Agents','userProfileId':_0x11d991[_0x8f03('0x67')]},'raw':!![]})[_0x8f03('0x2d')](function(_0x5b68f7){if(_0x5b68f7){if(_0x5b68f7[_0x8f03('0x68')]===0x0){return db[_0x8f03('0x69')][_0x8f03('0x45')]({'where':{'sectionId':_0x5b68f7['id']},'raw':!![]})['then'](function(_0x2afc0f){var _0x43b0fa=[];if(_[_0x8f03('0x6a')](_0x2afc0f)){_0x43b0fa=_[_0x8f03('0x41')](_0xaac356,function(_0x54bbbe){if(_0x54bbbe['role']!==_0x8f03('0x58')){return _0x54bbbe;}});return _0x43b0fa;}else{for(var _0x42fb2=0x0;_0x42fb2<_0xaac356[_0x8f03('0x6b')];_0x42fb2++){var _0xdfb7c6=_0xaac356[_0x42fb2];if(_0xdfb7c6[_0x8f03('0x65')]===_0x8f03('0x58')){var _0x2e4dc7=_[_0x8f03('0x4e')](_0x2afc0f,function(_0x14fc38){return _0x14fc38['resourceId']===_0xdfb7c6['id'];});if(_0x2e4dc7)_0x43b0fa[_0x8f03('0x6c')](_0xdfb7c6);}else{_0x43b0fa[_0x8f03('0x6c')](_0xdfb7c6);}}return _0x43b0fa;}});}else{return _0xaac356;}}else{var _0x202322=_[_0x8f03('0x41')](_0xaac356,function(_0x4a9833){if(_0x4a9833['role']!==_0x8f03('0x58')){return _0x4a9833;}});return _0x202322;}});}else{return _0xaac356;}}})[_0x8f03('0x2d')](function(_0x4e4947){var _0x20845e=[];if(_0x4e4947){_0x17a829=_0x4e4947;for(var _0x38d747=0x0;_0x38d747<_0x4e4947['length'];_0x38d747+=0x1){_0x17a829[_0x38d747][_0x8f03('0x51')]=[];_0x20845e[_0x8f03('0x6c')](db[_0x8f03('0x33')]['find']({'where':{'FromId':[_0x2fb0ea[_0x8f03('0x6d')]['id'],_0x4e4947[_0x38d747]['id']],'ToId':[_0x2fb0ea[_0x8f03('0x6d')]['id'],_0x4e4947[_0x38d747]['id']],'ChatGroupId':null},'attributes':['id',_0x8f03('0x53'),_0x8f03('0x6e'),_0x8f03('0x6f'),'read','body'],'order':qs[_0x8f03('0x3e')](_0x8f03('0x70')),'limit':0x1,'offset':0x0,'raw':!![]})[_0x8f03('0x2d')](wrapUser(_0x4e4947[_0x38d747])));}return BPromise[_0x8f03('0x71')](_0x20845e);}return[];})[_0x8f03('0x2d')](function(_0xc87025){return{'rows':_0xc87025,'count':_0xc87025[_0x8f03('0x6b')]};})[_0x8f03('0x2d')](respondWithResult(_0x2b61ea,null))[_0x8f03('0x47')](handleError(_0x2b61ea,null));};