67e478dae378396dcc91ed42fefc3fa3fe578de7
[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 _0x7bfa=['api','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./chatInternalMessage.socket','register','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','save','then','destroy','error','name','send','index','map','ChatInternalMessage','rawAttributes','model','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','where','VIRTUAL','filter','merge','options','includeAll','findAll','rows','show','params','keys','include','find','catch','create','body','describe','get','messages','lastMessage','lastMessageAt','format','YYYY-MM-DD\x20HH:mm:ss','getUsers','User','agent','admin','user','fullname','role','userpic','internal','online','lastLoginAt','allowmessenger','showWebBar','-online','VoiceQueue','VoiceQueues','type','UserProfileSection','Agents','UserProfileResource','resourceId','push','createdAt','FromId','ToId','read','-createdAt','all','html-pdf','eml-format','zip-dir','request-promise','moment','bluebird','mustache','path','sox','ejs','lodash','squel','crypto','jsforce','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware'];(function(_0x35d43c,_0x3b92af){var _0x54aaec=function(_0x52caaf){while(--_0x52caaf){_0x35d43c['push'](_0x35d43c['shift']());}};_0x54aaec(++_0x3b92af);}(_0x7bfa,0x1b6));var _0xa7bf=function(_0x1352f0,_0x4cd565){_0x1352f0=_0x1352f0-0x0;var _0x1c2a82=_0x7bfa[_0x1352f0];return _0x1c2a82;};'use strict';var pdf=require(_0xa7bf('0x0'));var emlformat=require(_0xa7bf('0x1'));var rimraf=require('rimraf');var zipdir=require(_0xa7bf('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xa7bf('0x3'));var moment=require(_0xa7bf('0x4'));var BPromise=require(_0xa7bf('0x5'));var Mustache=require(_0xa7bf('0x6'));var util=require('util');var path=require(_0xa7bf('0x7'));var sox=require(_0xa7bf('0x8'));var csv=require('to-csv');var ejs=require(_0xa7bf('0x9'));var fs=require('fs');var _=require(_0xa7bf('0xa'));var squel=require(_0xa7bf('0xb'));var crypto=require(_0xa7bf('0xc'));var jsforce=require(_0xa7bf('0xd'));var deskjs=require('desk.js');var toCsv=require(_0xa7bf('0xe'));var querystring=require(_0xa7bf('0xf'));var Papa=require(_0xa7bf('0x10'));var Redis=require(_0xa7bf('0x11'));var authService=require(_0xa7bf('0x12'));var qs=require(_0xa7bf('0x13'));var as=require(_0xa7bf('0x14'));var hardwareService=require(_0xa7bf('0x15'));var logger=require('../../config/logger')(_0xa7bf('0x16'));var utils=require(_0xa7bf('0x17'));var config=require(_0xa7bf('0x18'));var db=require(_0xa7bf('0x19'))['db'];config[_0xa7bf('0x1a')]=_[_0xa7bf('0x1b')](config[_0xa7bf('0x1a')],{'host':_0xa7bf('0x1c'),'port':0x18eb});var socket=require(_0xa7bf('0x1d'))(new Redis(config['redis']));require(_0xa7bf('0x1e'))[_0xa7bf('0x1f')](socket);function respondWithStatusCode(_0x1737c8,_0x552316){_0x552316=_0x552316||0xcc;return function(_0x953446){if(_0x953446){return _0x1737c8[_0xa7bf('0x20')](_0x552316);}return _0x1737c8[_0xa7bf('0x21')](_0x552316)[_0xa7bf('0x22')]();};}function respondWithResult(_0xaa23b9,_0x302c4c){_0x302c4c=_0x302c4c||0xc8;return function(_0x4adbf3){if(_0x4adbf3){return _0xaa23b9[_0xa7bf('0x21')](_0x302c4c)[_0xa7bf('0x23')](_0x4adbf3);}};}function respondWithFilteredResult(_0x4f030f,_0x314eeb){return function(_0x16226a){if(_0x16226a){var _0x573fcc=_0x16226a[_0xa7bf('0x24')],_0x489e39=_0x314eeb[_0xa7bf('0x25')],_0x37ac42=_0x314eeb[_0xa7bf('0x25')]+_0x314eeb[_0xa7bf('0x26')],_0x1e2cc7;if(_0x37ac42>=_0x573fcc){_0x37ac42=_0x573fcc;_0x1e2cc7=0xc8;}else{_0x1e2cc7=0xce;}_0x4f030f[_0xa7bf('0x21')](_0x1e2cc7);return _0x4f030f[_0xa7bf('0x27')](_0xa7bf('0x28'),_0x489e39+'-'+_0x37ac42+'/'+_0x573fcc)[_0xa7bf('0x23')](_0x16226a);}return null;};}function patchUpdates(_0x1abc59){return function(_0x590557){try{jsonpatch[_0xa7bf('0x29')](_0x590557,_0x1abc59,!![]);}catch(_0x3c3ac7){return BPromise['reject'](_0x3c3ac7);}return _0x590557[_0xa7bf('0x2a')]();};}function saveUpdates(_0x4c07e0,_0x2c0d75){return function(_0x2b2d05){if(_0x2b2d05){return _0x2b2d05['update'](_0x4c07e0)[_0xa7bf('0x2b')](function(_0x5b9b0b){return _0x5b9b0b;});}return null;};}function removeEntity(_0x5d3626,_0x10a0f0){return function(_0xe7ef72){if(_0xe7ef72){return _0xe7ef72[_0xa7bf('0x2c')]()['then'](function(){_0x5d3626[_0xa7bf('0x21')](0xcc)[_0xa7bf('0x22')]();});}};}function handleEntityNotFound(_0x33372e,_0x3dd0bc){return function(_0xf24f17){if(!_0xf24f17){_0x33372e[_0xa7bf('0x20')](0x194);}return _0xf24f17;};}function handleError(_0x6a5731,_0x4f546c){_0x4f546c=_0x4f546c||0x1f4;return function(_0x5a947b){logger[_0xa7bf('0x2d')](_0x5a947b['stack']);if(_0x5a947b['name']){delete _0x5a947b[_0xa7bf('0x2e')];}_0x6a5731[_0xa7bf('0x21')](_0x4f546c)[_0xa7bf('0x2f')](_0x5a947b);};}exports[_0xa7bf('0x30')]=function(_0x524243,_0x3877c3){var _0x35b540={},_0x99596a={},_0x57cdd5={'count':0x0,'rows':[]};var _0x163c2b=_[_0xa7bf('0x31')](db[_0xa7bf('0x32')][_0xa7bf('0x33')],function(_0x6770d0){return{'name':_0x6770d0['fieldName'],'type':_0x6770d0['type']['key']};});_0x99596a[_0xa7bf('0x34')]=_[_0xa7bf('0x31')](_0x163c2b,_0xa7bf('0x2e'));_0x99596a['query']=_['keys'](_0x524243[_0xa7bf('0x35')]);_0x99596a['filters']=_[_0xa7bf('0x36')](_0x99596a['model'],_0x99596a[_0xa7bf('0x35')]);_0x35b540[_0xa7bf('0x37')]=_[_0xa7bf('0x36')](_0x99596a['model'],qs[_0xa7bf('0x38')](_0x524243['query'][_0xa7bf('0x38')]));_0x35b540[_0xa7bf('0x37')]=_0x35b540[_0xa7bf('0x37')][_0xa7bf('0x39')]?_0x35b540[_0xa7bf('0x37')]:_0x99596a[_0xa7bf('0x34')];if(!_0x524243[_0xa7bf('0x35')][_0xa7bf('0x3a')](_0xa7bf('0x3b'))){_0x35b540[_0xa7bf('0x26')]=qs[_0xa7bf('0x26')](_0x524243[_0xa7bf('0x35')][_0xa7bf('0x26')]);_0x35b540[_0xa7bf('0x25')]=qs[_0xa7bf('0x25')](_0x524243[_0xa7bf('0x35')][_0xa7bf('0x25')]);}_0x35b540[_0xa7bf('0x3c')]=qs[_0xa7bf('0x3d')](_0x524243[_0xa7bf('0x35')][_0xa7bf('0x3d')]);_0x35b540['where']=qs[_0xa7bf('0x3e')](_[_0xa7bf('0x3f')](_0x524243[_0xa7bf('0x35')],_0x99596a['filters']));if(_0x524243[_0xa7bf('0x35')]['filter']){_0x35b540['where']=_['merge'](_0x35b540[_0xa7bf('0x40')],{'$or':_[_0xa7bf('0x31')](_0x163c2b,function(_0x1eb452){if(_0x1eb452['type']!==_0xa7bf('0x41')){var _0x375b57={};_0x375b57[_0x1eb452[_0xa7bf('0x2e')]]={'$like':'%'+_0x524243[_0xa7bf('0x35')][_0xa7bf('0x42')]+'%'};return _0x375b57;}})});}_0x35b540=_[_0xa7bf('0x43')]({},_0x35b540,_0x524243[_0xa7bf('0x44')]);var _0x30e76f={'where':_0x35b540[_0xa7bf('0x40')]};return db[_0xa7bf('0x32')][_0xa7bf('0x24')](_0x30e76f)[_0xa7bf('0x2b')](function(_0x1ca8a4){_0x57cdd5[_0xa7bf('0x24')]=_0x1ca8a4;if(_0x524243[_0xa7bf('0x35')][_0xa7bf('0x45')]){_0x35b540['include']=[{'all':!![]}];}return db[_0xa7bf('0x32')][_0xa7bf('0x46')](_0x35b540);})[_0xa7bf('0x2b')](function(_0x35d2f9){_0x57cdd5[_0xa7bf('0x47')]=_0x35d2f9;return _0x57cdd5;})[_0xa7bf('0x2b')](respondWithFilteredResult(_0x3877c3,_0x35b540))['catch'](handleError(_0x3877c3,null));};exports[_0xa7bf('0x48')]=function(_0x2f7381,_0x4847b3){var _0x30c72d={'raw':!![],'where':{'id':_0x2f7381[_0xa7bf('0x49')]['id']}},_0x38e657={};_0x38e657[_0xa7bf('0x34')]=_[_0xa7bf('0x4a')](db['ChatInternalMessage'][_0xa7bf('0x33')]);_0x38e657[_0xa7bf('0x35')]=_[_0xa7bf('0x4a')](_0x2f7381[_0xa7bf('0x35')]);_0x38e657[_0xa7bf('0x3e')]=_[_0xa7bf('0x36')](_0x38e657[_0xa7bf('0x34')],_0x38e657[_0xa7bf('0x35')]);_0x30c72d['attributes']=_[_0xa7bf('0x36')](_0x38e657[_0xa7bf('0x34')],qs[_0xa7bf('0x38')](_0x2f7381[_0xa7bf('0x35')][_0xa7bf('0x38')]));_0x30c72d[_0xa7bf('0x37')]=_0x30c72d['attributes'][_0xa7bf('0x39')]?_0x30c72d['attributes']:_0x38e657[_0xa7bf('0x34')];if(_0x2f7381[_0xa7bf('0x35')]['includeAll']){_0x30c72d[_0xa7bf('0x4b')]=[{'all':!![]}];}_0x30c72d=_[_0xa7bf('0x43')]({},_0x30c72d,_0x2f7381['options']);return db['ChatInternalMessage'][_0xa7bf('0x4c')](_0x30c72d)['then'](handleEntityNotFound(_0x4847b3,null))['then'](respondWithResult(_0x4847b3,null))[_0xa7bf('0x4d')](handleError(_0x4847b3,null));};exports[_0xa7bf('0x4e')]=function(_0x5155e7,_0xf18ffe){return db[_0xa7bf('0x32')][_0xa7bf('0x4e')](_0x5155e7['body'],{})[_0xa7bf('0x2b')](respondWithResult(_0xf18ffe,0xc9))[_0xa7bf('0x4d')](handleError(_0xf18ffe,null));};exports['update']=function(_0x1a3495,_0x13c1fc){if(_0x1a3495[_0xa7bf('0x4f')]['id']){delete _0x1a3495[_0xa7bf('0x4f')]['id'];}return db[_0xa7bf('0x32')][_0xa7bf('0x4c')]({'where':{'id':_0x1a3495[_0xa7bf('0x49')]['id']}})[_0xa7bf('0x2b')](handleEntityNotFound(_0x13c1fc,null))[_0xa7bf('0x2b')](saveUpdates(_0x1a3495[_0xa7bf('0x4f')],null))[_0xa7bf('0x2b')](respondWithResult(_0x13c1fc,null))[_0xa7bf('0x4d')](handleError(_0x13c1fc,null));};exports[_0xa7bf('0x2c')]=function(_0x1939de,_0x4457a7){return db[_0xa7bf('0x32')][_0xa7bf('0x4c')]({'where':{'id':_0x1939de[_0xa7bf('0x49')]['id']}})[_0xa7bf('0x2b')](handleEntityNotFound(_0x4457a7,null))[_0xa7bf('0x2b')](removeEntity(_0x4457a7,null))[_0xa7bf('0x4d')](handleError(_0x4457a7,null));};exports[_0xa7bf('0x50')]=function(_0x3c6ef7,_0x5b718b){return db[_0xa7bf('0x32')]['describe']()[_0xa7bf('0x2b')](respondWithResult(_0x5b718b,null))['catch'](handleError(_0x5b718b,null));};function wrapUser(_0x30f6d2){return function(_0x5a6a97){var _0x351230=_0x30f6d2[_0xa7bf('0x51')]({'plain':!![]});_0x351230[_0xa7bf('0x52')]=[];if(_0x5a6a97){_0x351230['lastMessageAt']=_0x5a6a97['createdAt'];_0x351230[_0xa7bf('0x53')]=_0x5a6a97;}else{_0x351230[_0xa7bf('0x54')]=moment(0x0)[_0xa7bf('0x55')](_0xa7bf('0x56'));_0x351230[_0xa7bf('0x53')]={};}return _0x351230;};}exports[_0xa7bf('0x57')]=function(_0x27dd9e,_0x2980cd,_0x22cb0e){var _0x45038d=[];return db[_0xa7bf('0x58')][_0xa7bf('0x46')]({'where':{'role':[_0xa7bf('0x59'),_0xa7bf('0x5a'),_0xa7bf('0x5b')]},'attributes':['id',_0xa7bf('0x5c'),'name',_0xa7bf('0x5d'),_0xa7bf('0x5e'),_0xa7bf('0x5f'),_0xa7bf('0x60'),_0xa7bf('0x61'),_0xa7bf('0x62'),_0xa7bf('0x63')],'order':qs['sort'](_0xa7bf('0x64')),'include':[{'model':db[_0xa7bf('0x65')],'as':_0xa7bf('0x66'),'attributes':['id',_0xa7bf('0x2e'),_0xa7bf('0x67')],'through':{'attributes':[]}}]})[_0xa7bf('0x2b')](function(_0x3691d3){if(_0x3691d3){var _0x5cd934=_0x27dd9e[_0xa7bf('0x5b')][_0xa7bf('0x51')]({'plain':!![]});if(!_0x5cd934)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x5cd934[_0xa7bf('0x5d')]===_0xa7bf('0x5b')){return db[_0xa7bf('0x68')][_0xa7bf('0x4c')]({'where':{'name':_0xa7bf('0x69'),'userProfileId':_0x5cd934['userProfileId']},'raw':!![]})['then'](function(_0x578e3c){if(_0x578e3c){if(_0x578e3c['autoAssociation']===0x0){return db[_0xa7bf('0x6a')]['findAll']({'where':{'sectionId':_0x578e3c['id']},'raw':!![]})[_0xa7bf('0x2b')](function(_0x164192){var _0x2bc442=[];if(_['isEmpty'](_0x164192)){_0x2bc442=_[_0xa7bf('0x42')](_0x3691d3,function(_0x436b7a){if(_0x436b7a[_0xa7bf('0x5d')]!==_0xa7bf('0x59')){return _0x436b7a;}});return _0x2bc442;}else{for(var _0x52cb92=0x0;_0x52cb92<_0x3691d3[_0xa7bf('0x39')];_0x52cb92++){var _0x2708bc=_0x3691d3[_0x52cb92];if(_0x2708bc[_0xa7bf('0x5d')]===_0xa7bf('0x59')){var _0x28fcfc=_[_0xa7bf('0x4c')](_0x164192,function(_0x2f5153){return _0x2f5153[_0xa7bf('0x6b')]===_0x2708bc['id'];});if(_0x28fcfc)_0x2bc442[_0xa7bf('0x6c')](_0x2708bc);}else{_0x2bc442[_0xa7bf('0x6c')](_0x2708bc);}}return _0x2bc442;}});}else{return _0x3691d3;}}else{var _0x52cba5=_[_0xa7bf('0x42')](_0x3691d3,function(_0x436a6c){if(_0x436a6c[_0xa7bf('0x5d')]!==_0xa7bf('0x59')){return _0x436a6c;}});return _0x52cba5;}});}else{return _0x3691d3;}}})[_0xa7bf('0x2b')](function(_0x4631d3){var _0x4ce661=[];if(_0x4631d3){_0x45038d=_0x4631d3;for(var _0x257606=0x0;_0x257606<_0x4631d3[_0xa7bf('0x39')];_0x257606+=0x1){_0x45038d[_0x257606][_0xa7bf('0x52')]=[];_0x4ce661[_0xa7bf('0x6c')](db[_0xa7bf('0x32')][_0xa7bf('0x4c')]({'where':{'FromId':[_0x27dd9e['user']['id'],_0x4631d3[_0x257606]['id']],'ToId':[_0x27dd9e['user']['id'],_0x4631d3[_0x257606]['id']],'ChatGroupId':null},'attributes':['id',_0xa7bf('0x6d'),_0xa7bf('0x6e'),_0xa7bf('0x6f'),_0xa7bf('0x70'),_0xa7bf('0x4f')],'order':qs[_0xa7bf('0x3d')](_0xa7bf('0x71')),'limit':0x1,'offset':0x0,'raw':!![]})['then'](wrapUser(_0x4631d3[_0x257606])));}return BPromise[_0xa7bf('0x72')](_0x4ce661);}return[];})[_0xa7bf('0x2b')](function(_0x327cab){return{'rows':_0x327cab,'count':_0x327cab[_0xa7bf('0x39')]};})['then'](respondWithResult(_0x2980cd,null))[_0xa7bf('0x4d')](handleError(_0x2980cd,null));};