Built motion from commit ff78e754.|2.6.29
[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 _0xf788=['Agents','userProfileId','autoAssociation','isEmpty','push','FromId','ToId','read','-createdAt','eml-format','rimraf','zip-dir','moment','mustache','util','path','sox','to-csv','ejs','fs-extra','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost','./chatInternalMessage.socket','register','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','save','then','destroy','sendStatus','error','stack','name','send','index','ChatInternalMessage','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','attributes','fields','length','nolimit','sort','where','pick','filter','merge','options','includeAll','findAll','rows','catch','params','include','find','create','body','update','get','messages','lastMessageAt','createdAt','lastMessage','format','YYYY-MM-DD\x20HH:mm:ss','getUsers','User','agent','admin','user','fullname','userpic','online','allowmessenger','showWebBar','-online','VoiceQueue','VoiceQueues','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection'];(function(_0x5dfccc,_0x141d48){var _0x5b08c0=function(_0x41556d){while(--_0x41556d){_0x5dfccc['push'](_0x5dfccc['shift']());}};_0x5b08c0(++_0x141d48);}(_0xf788,0x79));var _0x8f78=function(_0x5c351c,_0x2046d8){_0x5c351c=_0x5c351c-0x0;var _0x26f3b3=_0xf788[_0x5c351c];return _0x26f3b3;};'use strict';var emlformat=require(_0x8f78('0x0'));var rimraf=require(_0x8f78('0x1'));var zipdir=require(_0x8f78('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x8f78('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x8f78('0x4'));var util=require(_0x8f78('0x5'));var path=require(_0x8f78('0x6'));var sox=require(_0x8f78('0x7'));var csv=require(_0x8f78('0x8'));var ejs=require(_0x8f78('0x9'));var fs=require('fs');var fs_extra=require(_0x8f78('0xa'));var _=require('lodash');var squel=require('squel');var crypto=require(_0x8f78('0xb'));var jsforce=require(_0x8f78('0xc'));var deskjs=require(_0x8f78('0xd'));var toCsv=require(_0x8f78('0x8'));var querystring=require(_0x8f78('0xe'));var Papa=require(_0x8f78('0xf'));var Redis=require(_0x8f78('0x10'));var authService=require(_0x8f78('0x11'));var qs=require(_0x8f78('0x12'));var as=require(_0x8f78('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x8f78('0x14'))(_0x8f78('0x15'));var utils=require(_0x8f78('0x16'));var config=require('../../config/environment');var licenseUtil=require(_0x8f78('0x17'));var db=require(_0x8f78('0x18'))['db'];config[_0x8f78('0x19')]=_[_0x8f78('0x1a')](config['redis'],{'host':_0x8f78('0x1b'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require(_0x8f78('0x1c'))[_0x8f78('0x1d')](socket);function respondWithStatusCode(_0x5157b5,_0x1d51e3){_0x1d51e3=_0x1d51e3||0xcc;return function(_0x9709f4){if(_0x9709f4){return _0x5157b5['sendStatus'](_0x1d51e3);}return _0x5157b5[_0x8f78('0x1e')](_0x1d51e3)[_0x8f78('0x1f')]();};}function respondWithResult(_0x28dc81,_0x2a96c5){_0x2a96c5=_0x2a96c5||0xc8;return function(_0x52a394){if(_0x52a394){return _0x28dc81[_0x8f78('0x1e')](_0x2a96c5)[_0x8f78('0x20')](_0x52a394);}};}function respondWithFilteredResult(_0x3f107a,_0x18515c){return function(_0x235627){if(_0x235627){var _0x1e0f18=typeof _0x18515c[_0x8f78('0x21')]===_0x8f78('0x22')&&typeof _0x18515c[_0x8f78('0x23')]===_0x8f78('0x22');var _0x1efa79=_0x235627[_0x8f78('0x24')];var _0x50a42c=_0x1e0f18?0x0:_0x18515c[_0x8f78('0x21')];var _0x3b8796=_0x1e0f18?_0x235627[_0x8f78('0x24')]:_0x18515c['offset']+_0x18515c['limit'];var _0x36fc3f;if(_0x3b8796>=_0x1efa79){_0x3b8796=_0x1efa79;_0x36fc3f=0xc8;}else{_0x36fc3f=0xce;}_0x3f107a[_0x8f78('0x1e')](_0x36fc3f);return _0x3f107a[_0x8f78('0x25')](_0x8f78('0x26'),_0x50a42c+'-'+_0x3b8796+'/'+_0x1efa79)[_0x8f78('0x20')](_0x235627);}return null;};}function patchUpdates(_0x27820d){return function(_0x3fc9bc){try{jsonpatch[_0x8f78('0x27')](_0x3fc9bc,_0x27820d,!![]);}catch(_0x6258d8){return BPromise['reject'](_0x6258d8);}return _0x3fc9bc[_0x8f78('0x28')]();};}function saveUpdates(_0x47538c,_0x21a34a){return function(_0x2ab7cf){if(_0x2ab7cf){return _0x2ab7cf['update'](_0x47538c)[_0x8f78('0x29')](function(_0x250aa0){return _0x250aa0;});}return null;};}function removeEntity(_0x20d0e4,_0x2d7e68){return function(_0x52c33c){if(_0x52c33c){return _0x52c33c[_0x8f78('0x2a')]()[_0x8f78('0x29')](function(){_0x20d0e4[_0x8f78('0x1e')](0xcc)[_0x8f78('0x1f')]();});}};}function handleEntityNotFound(_0x10e4c1,_0x27c74a){return function(_0x5d77ff){if(!_0x5d77ff){_0x10e4c1[_0x8f78('0x2b')](0x194);}return _0x5d77ff;};}function handleError(_0x49e505,_0x8868d4){_0x8868d4=_0x8868d4||0x1f4;return function(_0x1b7960){logger[_0x8f78('0x2c')](_0x1b7960[_0x8f78('0x2d')]);if(_0x1b7960[_0x8f78('0x2e')]){delete _0x1b7960['name'];}_0x49e505['status'](_0x8868d4)[_0x8f78('0x2f')](_0x1b7960);};}exports[_0x8f78('0x30')]=function(_0x43514c,_0x3d999b){var _0x5a6460={},_0x3c3c85={},_0x3d363d={'count':0x0,'rows':[]};var _0x11dfc9=_['map'](db[_0x8f78('0x31')][_0x8f78('0x32')],function(_0xa5bd5d){return{'name':_0xa5bd5d[_0x8f78('0x33')],'type':_0xa5bd5d[_0x8f78('0x34')][_0x8f78('0x35')]};});_0x3c3c85[_0x8f78('0x36')]=_[_0x8f78('0x37')](_0x11dfc9,_0x8f78('0x2e'));_0x3c3c85[_0x8f78('0x38')]=_[_0x8f78('0x39')](_0x43514c[_0x8f78('0x38')]);_0x3c3c85[_0x8f78('0x3a')]=_[_0x8f78('0x3b')](_0x3c3c85[_0x8f78('0x36')],_0x3c3c85[_0x8f78('0x38')]);_0x5a6460[_0x8f78('0x3c')]=_[_0x8f78('0x3b')](_0x3c3c85['model'],qs['fields'](_0x43514c[_0x8f78('0x38')][_0x8f78('0x3d')]));_0x5a6460[_0x8f78('0x3c')]=_0x5a6460[_0x8f78('0x3c')][_0x8f78('0x3e')]?_0x5a6460[_0x8f78('0x3c')]:_0x3c3c85[_0x8f78('0x36')];if(!_0x43514c['query']['hasOwnProperty'](_0x8f78('0x3f'))){_0x5a6460[_0x8f78('0x23')]=qs['limit'](_0x43514c[_0x8f78('0x38')][_0x8f78('0x23')]);_0x5a6460['offset']=qs[_0x8f78('0x21')](_0x43514c[_0x8f78('0x38')][_0x8f78('0x21')]);}_0x5a6460['order']=qs[_0x8f78('0x40')](_0x43514c[_0x8f78('0x38')][_0x8f78('0x40')]);_0x5a6460[_0x8f78('0x41')]=qs['filters'](_[_0x8f78('0x42')](_0x43514c[_0x8f78('0x38')],_0x3c3c85[_0x8f78('0x3a')]),_0x11dfc9);if(_0x43514c[_0x8f78('0x38')][_0x8f78('0x43')]){_0x5a6460[_0x8f78('0x41')]=_[_0x8f78('0x44')](_0x5a6460['where'],{'$or':_[_0x8f78('0x37')](_0x11dfc9,function(_0x126016){if(_0x126016[_0x8f78('0x34')]!=='VIRTUAL'){var _0x486ceb={};_0x486ceb[_0x126016['name']]={'$like':'%'+_0x43514c[_0x8f78('0x38')]['filter']+'%'};return _0x486ceb;}})});}_0x5a6460=_[_0x8f78('0x44')]({},_0x5a6460,_0x43514c[_0x8f78('0x45')]);var _0x25faa1={'where':_0x5a6460['where']};return db['ChatInternalMessage'][_0x8f78('0x24')](_0x25faa1)[_0x8f78('0x29')](function(_0x1b07fc){_0x3d363d[_0x8f78('0x24')]=_0x1b07fc;if(_0x43514c[_0x8f78('0x38')][_0x8f78('0x46')]){_0x5a6460['include']=[{'all':!![]}];}return db[_0x8f78('0x31')][_0x8f78('0x47')](_0x5a6460);})['then'](function(_0x1b3617){_0x3d363d[_0x8f78('0x48')]=_0x1b3617;return _0x3d363d;})['then'](respondWithFilteredResult(_0x3d999b,_0x5a6460))[_0x8f78('0x49')](handleError(_0x3d999b,null));};exports['show']=function(_0x32d813,_0x514e66){var _0x39170d={'raw':!![],'where':{'id':_0x32d813[_0x8f78('0x4a')]['id']}},_0x216b9c={};_0x216b9c['model']=_[_0x8f78('0x39')](db[_0x8f78('0x31')][_0x8f78('0x32')]);_0x216b9c[_0x8f78('0x38')]=_[_0x8f78('0x39')](_0x32d813[_0x8f78('0x38')]);_0x216b9c[_0x8f78('0x3a')]=_[_0x8f78('0x3b')](_0x216b9c[_0x8f78('0x36')],_0x216b9c[_0x8f78('0x38')]);_0x39170d['attributes']=_[_0x8f78('0x3b')](_0x216b9c[_0x8f78('0x36')],qs['fields'](_0x32d813[_0x8f78('0x38')][_0x8f78('0x3d')]));_0x39170d[_0x8f78('0x3c')]=_0x39170d[_0x8f78('0x3c')][_0x8f78('0x3e')]?_0x39170d[_0x8f78('0x3c')]:_0x216b9c[_0x8f78('0x36')];if(_0x32d813['query']['includeAll']){_0x39170d[_0x8f78('0x4b')]=[{'all':!![]}];}_0x39170d=_[_0x8f78('0x44')]({},_0x39170d,_0x32d813[_0x8f78('0x45')]);return db[_0x8f78('0x31')][_0x8f78('0x4c')](_0x39170d)[_0x8f78('0x29')](handleEntityNotFound(_0x514e66,null))[_0x8f78('0x29')](respondWithResult(_0x514e66,null))[_0x8f78('0x49')](handleError(_0x514e66,null));};exports[_0x8f78('0x4d')]=function(_0x5bc2cf,_0x5c194b){return db[_0x8f78('0x31')][_0x8f78('0x4d')](_0x5bc2cf[_0x8f78('0x4e')],{})['then'](respondWithResult(_0x5c194b,0xc9))[_0x8f78('0x49')](handleError(_0x5c194b,null));};exports[_0x8f78('0x4f')]=function(_0x36d14e,_0x41ab6d){if(_0x36d14e['body']['id']){delete _0x36d14e[_0x8f78('0x4e')]['id'];}return db[_0x8f78('0x31')][_0x8f78('0x4c')]({'where':{'id':_0x36d14e[_0x8f78('0x4a')]['id']}})[_0x8f78('0x29')](handleEntityNotFound(_0x41ab6d,null))[_0x8f78('0x29')](saveUpdates(_0x36d14e[_0x8f78('0x4e')],null))[_0x8f78('0x29')](respondWithResult(_0x41ab6d,null))['catch'](handleError(_0x41ab6d,null));};exports[_0x8f78('0x2a')]=function(_0x433247,_0x5cf1f1){return db[_0x8f78('0x31')][_0x8f78('0x4c')]({'where':{'id':_0x433247['params']['id']}})['then'](handleEntityNotFound(_0x5cf1f1,null))[_0x8f78('0x29')](removeEntity(_0x5cf1f1,null))[_0x8f78('0x49')](handleError(_0x5cf1f1,null));};exports['describe']=function(_0x8d1d5f,_0x5db1c6){return db[_0x8f78('0x31')]['describe']()[_0x8f78('0x29')](respondWithResult(_0x5db1c6,null))[_0x8f78('0x49')](handleError(_0x5db1c6,null));};function wrapUser(_0x28bdb8){return function(_0x2baedb){var _0x2bab66=_0x28bdb8[_0x8f78('0x50')]({'plain':!![]});_0x2bab66[_0x8f78('0x51')]=[];if(_0x2baedb){_0x2bab66[_0x8f78('0x52')]=_0x2baedb[_0x8f78('0x53')];_0x2bab66[_0x8f78('0x54')]=_0x2baedb;}else{_0x2bab66['lastMessageAt']=moment(0x0)[_0x8f78('0x55')](_0x8f78('0x56'));_0x2bab66[_0x8f78('0x54')]={};}return _0x2bab66;};}exports[_0x8f78('0x57')]=function(_0x293bbd,_0x395384,_0xfd4818){var _0x4066e4=[];return db[_0x8f78('0x58')][_0x8f78('0x47')]({'where':{'role':[_0x8f78('0x59'),_0x8f78('0x5a'),_0x8f78('0x5b')]},'attributes':['id',_0x8f78('0x5c'),_0x8f78('0x2e'),'role',_0x8f78('0x5d'),'internal',_0x8f78('0x5e'),'lastLoginAt',_0x8f78('0x5f'),_0x8f78('0x60')],'order':qs[_0x8f78('0x40')](_0x8f78('0x61')),'include':[{'model':db[_0x8f78('0x62')],'as':_0x8f78('0x63'),'attributes':['id','name',_0x8f78('0x34')],'through':{'attributes':[]}}]})[_0x8f78('0x29')](function(_0x4001e7){if(_0x4001e7){var _0x4c7da2=_0x293bbd[_0x8f78('0x5b')][_0x8f78('0x50')]({'plain':!![]});if(!_0x4c7da2)throw new Error(_0x8f78('0x64'));if(_0x4c7da2[_0x8f78('0x65')]===_0x8f78('0x5b')){return db[_0x8f78('0x66')][_0x8f78('0x4c')]({'where':{'name':_0x8f78('0x67'),'userProfileId':_0x4c7da2[_0x8f78('0x68')]},'raw':!![]})[_0x8f78('0x29')](function(_0x1dd8d9){if(_0x1dd8d9){if(_0x1dd8d9[_0x8f78('0x69')]===0x0){return db['UserProfileResource']['findAll']({'where':{'sectionId':_0x1dd8d9['id']},'raw':!![]})[_0x8f78('0x29')](function(_0x1eba48){var _0x22b8b3=[];if(_[_0x8f78('0x6a')](_0x1eba48)){_0x22b8b3=_['filter'](_0x4001e7,function(_0x547940){if(_0x547940['role']!==_0x8f78('0x59')){return _0x547940;}});return _0x22b8b3;}else{for(var _0x97362b=0x0;_0x97362b<_0x4001e7[_0x8f78('0x3e')];_0x97362b++){var _0x4b844c=_0x4001e7[_0x97362b];if(_0x4b844c['role']===_0x8f78('0x59')){var _0x40ce8e=_[_0x8f78('0x4c')](_0x1eba48,function(_0x5e7c51){return _0x5e7c51['resourceId']===_0x4b844c['id'];});if(_0x40ce8e)_0x22b8b3[_0x8f78('0x6b')](_0x4b844c);}else{_0x22b8b3[_0x8f78('0x6b')](_0x4b844c);}}return _0x22b8b3;}});}else{return _0x4001e7;}}else{var _0x43d998=_['filter'](_0x4001e7,function(_0x4d9d46){if(_0x4d9d46[_0x8f78('0x65')]!==_0x8f78('0x59')){return _0x4d9d46;}});return _0x43d998;}});}else{return _0x4001e7;}}})['then'](function(_0xb6c07f){var _0x24a0ff=[];if(_0xb6c07f){_0x4066e4=_0xb6c07f;for(var _0xe557b1=0x0;_0xe557b1<_0xb6c07f[_0x8f78('0x3e')];_0xe557b1+=0x1){_0x4066e4[_0xe557b1][_0x8f78('0x51')]=[];_0x24a0ff[_0x8f78('0x6b')](db[_0x8f78('0x31')][_0x8f78('0x4c')]({'where':{'FromId':[_0x293bbd[_0x8f78('0x5b')]['id'],_0xb6c07f[_0xe557b1]['id']],'ToId':[_0x293bbd['user']['id'],_0xb6c07f[_0xe557b1]['id']],'ChatGroupId':null},'attributes':['id',_0x8f78('0x53'),_0x8f78('0x6c'),_0x8f78('0x6d'),_0x8f78('0x6e'),'body'],'order':qs['sort'](_0x8f78('0x6f')),'limit':0x1,'offset':0x0,'raw':!![]})['then'](wrapUser(_0xb6c07f[_0xe557b1])));}return BPromise['all'](_0x24a0ff);}return[];})['then'](function(_0x52b580){return{'rows':_0x52b580,'count':_0x52b580[_0x8f78('0x3e')]};})['then'](respondWithResult(_0x395384,null))[_0x8f78('0x49')](handleError(_0x395384,null));};