0f0267d36e16356e1ba63a745007af8b681cba11
[motion-next.git] / server / api / chatInternalMessage / chatInternalMessage.controller.js
1 const a73_0xbeff=['lastMessage','findOne','rows','error','describe','createdAt','get','fieldName','../../mysqldb','filters','allowmessenger','lodash','intersection','map','VoiceQueues','moment','length','autoAssociation','agent','UserProfileSection','count','fullname','order','sort','push','like','User','role','limit','params','UserProfileResource','merge','update','type','VoiceQueue','query','end','lastMessageAt','Content-Range','getUsers','rawAttributes','name','undefined','../../rediscache','Agents','then','key','send','fields','../../components/parsers/qs','read','includeAll','user','-createdAt','status','destroy','nolimit','stack','catch','findAll','../../config/logger','create','offset','keys','internal','ChatInternalMessage','lastLoginAt','sendStatus','json','format','filter','body','model','resourceId','set','attributes','where'];(function(_0x2cf48c,_0xbeff80){const _0x2d2e8e=function(_0x2ac7cb){while(--_0x2ac7cb){_0x2cf48c['push'](_0x2cf48c['shift']());}};_0x2d2e8e(++_0xbeff80);}(a73_0xbeff,0x1a6));const a73_0x2d2e=function(_0x2cf48c,_0xbeff80){_0x2cf48c=_0x2cf48c-0x0;let _0x2d2e8e=a73_0xbeff[_0x2cf48c];return _0x2d2e8e;};const _0x1c086a=a73_0x2d2e;'use strict';const moment=require(_0x1c086a('0x37')),_=require(_0x1c086a('0x33')),qs=require(_0x1c086a('0xc')),logger=require(_0x1c086a('0x17'))('api'),{getSocket}=require(_0x1c086a('0x6')),db=require(_0x1c086a('0x30'))['db'],{Op}=require('sequelize'),socket=getSocket();require('./chatInternalMessage.socket')['register'](socket);function respondWithResult(_0x2ac7cb,_0x10e125){return _0x10e125=_0x10e125||0xc8,function(_0x4626d0){const _0x373c53=a73_0x2d2e;if(_0x4626d0)return _0x2ac7cb[_0x373c53('0x11')](_0x10e125)[_0x373c53('0x1f')](_0x4626d0);};}function respondWithFilteredResult(_0x2d8231,_0x224bbf){return function(_0xc0000f){const _0x32bc98=a73_0x2d2e;if(_0xc0000f){const _0x2105fb=_0xc0000f[_0x32bc98('0x3c')],_0x236c0b=_0x224bbf[_0x32bc98('0x19')];let _0x36629e=_0x224bbf[_0x32bc98('0x19')]+_0x224bbf[_0x32bc98('0x44')],_0x1530c4;return _0x36629e>=_0x2105fb?(_0x36629e=_0x2105fb,_0x1530c4=0xc8):_0x1530c4=0xce,_0x2d8231[_0x32bc98('0x11')](_0x1530c4),_0x2d8231[_0x32bc98('0x25')](_0x32bc98('0x1'),_0x236c0b+'-'+_0x36629e+'/'+_0x2105fb)[_0x32bc98('0x1f')](_0xc0000f);}return null;};}function saveUpdates(_0x1d6df1){return function(_0x525397){const _0x97d9e4=a73_0x2d2e;if(_0x525397)return _0x525397['update'](_0x1d6df1)[_0x97d9e4('0x8')](function(_0x154b37){return _0x154b37;});return null;};}function removeEntity(_0x1fe4a1){return function(_0x520808){const _0x516475=a73_0x2d2e;if(_0x520808)return _0x520808[_0x516475('0x12')]()['then'](function(){const _0x1038b0=_0x516475;_0x1fe4a1['status'](0xcc)[_0x1038b0('0x4c')]();});};}function handleEntityNotFound(_0x100e84){return function(_0x119a97){const _0x3d8ee0=a73_0x2d2e;return!_0x119a97&&_0x100e84[_0x3d8ee0('0x1e')](0x194),_0x119a97;};}function handleError(_0x461409,_0x5c0037){return _0x5c0037=_0x5c0037||0x1f4,function(_0x2b2b93){const _0x532540=a73_0x2d2e;logger[_0x532540('0x2b')](_0x2b2b93[_0x532540('0x14')]),_0x2b2b93[_0x532540('0x4')]&&delete _0x2b2b93[_0x532540('0x4')],_0x461409[_0x532540('0x11')](_0x5c0037)[_0x532540('0xa')](_0x2b2b93);};}exports['index']=function(_0x506d8c,_0x4d3893){const _0x4f9cac=_0x1c086a;let _0x5b870a={};const _0x117416={},_0x475bfc={'count':0x0,'rows':[]},_0x36de00=_[_0x4f9cac('0x35')](db['ChatInternalMessage'][_0x4f9cac('0x3')],function(_0x5722e6){const _0x52bbe0=_0x4f9cac;return{'name':_0x5722e6[_0x52bbe0('0x2f')],'type':_0x5722e6['type'][_0x52bbe0('0x9')]};});_0x117416[_0x4f9cac('0x23')]=_[_0x4f9cac('0x35')](_0x36de00,'name'),_0x117416[_0x4f9cac('0x4b')]=_[_0x4f9cac('0x1a')](_0x506d8c[_0x4f9cac('0x4b')]),_0x117416[_0x4f9cac('0x31')]=_[_0x4f9cac('0x34')](_0x117416[_0x4f9cac('0x23')],_0x117416[_0x4f9cac('0x4b')]),_0x5b870a[_0x4f9cac('0x26')]=_[_0x4f9cac('0x34')](_0x117416[_0x4f9cac('0x23')],qs[_0x4f9cac('0xb')](_0x506d8c[_0x4f9cac('0x4b')][_0x4f9cac('0xb')])),_0x5b870a[_0x4f9cac('0x26')]=_0x5b870a[_0x4f9cac('0x26')][_0x4f9cac('0x38')]?_0x5b870a[_0x4f9cac('0x26')]:_0x117416['model'];typeof _0x506d8c[_0x4f9cac('0x4b')][_0x4f9cac('0x13')]===_0x4f9cac('0x5')&&(_0x5b870a[_0x4f9cac('0x44')]=qs[_0x4f9cac('0x44')](_0x506d8c[_0x4f9cac('0x4b')][_0x4f9cac('0x44')]),_0x5b870a[_0x4f9cac('0x19')]=qs[_0x4f9cac('0x19')](_0x506d8c[_0x4f9cac('0x4b')][_0x4f9cac('0x19')]));_0x5b870a[_0x4f9cac('0x3e')]=qs[_0x4f9cac('0x3f')](_0x506d8c[_0x4f9cac('0x4b')]['sort']),_0x5b870a['where']=qs[_0x4f9cac('0x31')](_['pick'](_0x506d8c[_0x4f9cac('0x4b')],_0x117416['filters']),_0x36de00);_0x506d8c[_0x4f9cac('0x4b')]['filter']&&(_0x5b870a[_0x4f9cac('0x27')]=_[_0x4f9cac('0x47')](_0x5b870a[_0x4f9cac('0x27')],{[Op['or']]:_['map'](_0x36de00,function(_0x5037e3){const _0x382e9a=_0x4f9cac;if(_0x5037e3[_0x382e9a('0x49')]!=='VIRTUAL'){const _0x4e9498={};return _0x4e9498[_0x5037e3[_0x382e9a('0x4')]]={[Op[_0x382e9a('0x41')]]:'%'+_0x506d8c[_0x382e9a('0x4b')][_0x382e9a('0x21')]+'%'},_0x4e9498;}})}));_0x5b870a=_[_0x4f9cac('0x47')]({},_0x5b870a,_0x506d8c['options']);const _0x434f74={'where':_0x5b870a[_0x4f9cac('0x27')]};return db[_0x4f9cac('0x1c')][_0x4f9cac('0x3c')](_0x434f74)['then'](function(_0x25a99a){const _0x24052b=_0x4f9cac;return _0x475bfc[_0x24052b('0x3c')]=_0x25a99a,_0x506d8c[_0x24052b('0x4b')]['includeAll']&&(_0x5b870a['include']=[{'all':!![]}]),db[_0x24052b('0x1c')][_0x24052b('0x16')](_0x5b870a);})[_0x4f9cac('0x8')](function(_0x2712f6){const _0x32c033=_0x4f9cac;return _0x475bfc[_0x32c033('0x2a')]=_0x2712f6,_0x475bfc;})['then'](respondWithFilteredResult(_0x4d3893,_0x5b870a))[_0x4f9cac('0x15')](handleError(_0x4d3893,null));},exports['show']=function(_0x103a19,_0x21ebb4){const _0x3db1a9=_0x1c086a;let _0x43d3bf={'raw':!![],'where':{'id':_0x103a19[_0x3db1a9('0x45')]['id']}};const _0x3f18c7={};return _0x3f18c7[_0x3db1a9('0x23')]=_[_0x3db1a9('0x1a')](db['ChatInternalMessage']['rawAttributes']),_0x3f18c7['query']=_[_0x3db1a9('0x1a')](_0x103a19[_0x3db1a9('0x4b')]),_0x3f18c7[_0x3db1a9('0x31')]=_[_0x3db1a9('0x34')](_0x3f18c7[_0x3db1a9('0x23')],_0x3f18c7[_0x3db1a9('0x4b')]),_0x43d3bf[_0x3db1a9('0x26')]=_[_0x3db1a9('0x34')](_0x3f18c7[_0x3db1a9('0x23')],qs['fields'](_0x103a19[_0x3db1a9('0x4b')]['fields'])),_0x43d3bf[_0x3db1a9('0x26')]=_0x43d3bf['attributes'][_0x3db1a9('0x38')]?_0x43d3bf[_0x3db1a9('0x26')]:_0x3f18c7[_0x3db1a9('0x23')],_0x103a19[_0x3db1a9('0x4b')][_0x3db1a9('0xe')]&&(_0x43d3bf['include']=[{'all':!![]}]),_0x43d3bf=_[_0x3db1a9('0x47')]({},_0x43d3bf,_0x103a19['options']),db[_0x3db1a9('0x1c')][_0x3db1a9('0x29')](_0x43d3bf)[_0x3db1a9('0x8')](handleEntityNotFound(_0x21ebb4,null))[_0x3db1a9('0x8')](respondWithResult(_0x21ebb4,null))[_0x3db1a9('0x15')](handleError(_0x21ebb4,null));},exports[_0x1c086a('0x18')]=function(_0x3b6cd8,_0x384d5c){const _0x17ad60=_0x1c086a;return db[_0x17ad60('0x1c')]['create'](_0x3b6cd8[_0x17ad60('0x22')],{})['then'](respondWithResult(_0x384d5c,0xc9))['catch'](handleError(_0x384d5c,null));},exports[_0x1c086a('0x48')]=function(_0x3d44da,_0x5d6944){const _0x8a5fc8=_0x1c086a;return _0x3d44da[_0x8a5fc8('0x22')]['id']&&delete _0x3d44da[_0x8a5fc8('0x22')]['id'],db[_0x8a5fc8('0x1c')][_0x8a5fc8('0x29')]({'where':{'id':_0x3d44da[_0x8a5fc8('0x45')]['id']}})['then'](handleEntityNotFound(_0x5d6944,null))[_0x8a5fc8('0x8')](saveUpdates(_0x3d44da[_0x8a5fc8('0x22')],null))[_0x8a5fc8('0x8')](respondWithResult(_0x5d6944,null))[_0x8a5fc8('0x15')](handleError(_0x5d6944,null));},exports[_0x1c086a('0x12')]=function(_0x2629a1,_0xfb922a){const _0x555e9b=_0x1c086a;return db[_0x555e9b('0x1c')][_0x555e9b('0x29')]({'where':{'id':_0x2629a1[_0x555e9b('0x45')]['id']}})[_0x555e9b('0x8')](handleEntityNotFound(_0xfb922a,null))[_0x555e9b('0x8')](removeEntity(_0xfb922a,null))['catch'](handleError(_0xfb922a,null));},exports[_0x1c086a('0x2c')]=function(_0x46a989,_0x4525c5){const _0x13fef0=_0x1c086a;return db[_0x13fef0('0x1c')]['describe']()[_0x13fef0('0x8')](respondWithResult(_0x4525c5,null))['catch'](handleError(_0x4525c5,null));};function wrapUser(_0x2cb163){return function(_0x2249c4){const _0x222d45=a73_0x2d2e,_0x5bca69=_0x2cb163[_0x222d45('0x2e')]({'plain':!![]});return _0x5bca69['messages']=[],_0x2249c4?(_0x5bca69[_0x222d45('0x0')]=_0x2249c4['createdAt'],_0x5bca69[_0x222d45('0x28')]=_0x2249c4):(_0x5bca69[_0x222d45('0x0')]=moment(0x0)[_0x222d45('0x20')]('YYYY-MM-DD\x20HH:mm:ss'),_0x5bca69[_0x222d45('0x28')]={}),_0x5bca69;};}exports[_0x1c086a('0x2')]=function(_0x4918f1,_0x332d4f){const _0x118753=_0x1c086a;let _0x34c42a=[];return db[_0x118753('0x42')][_0x118753('0x16')]({'where':{'role':[_0x118753('0x3a'),'admin',_0x118753('0xf')]},'attributes':['id',_0x118753('0x3d'),_0x118753('0x4'),_0x118753('0x43'),'userpic',_0x118753('0x1b'),'online',_0x118753('0x1d'),_0x118753('0x32'),'showWebBar'],'order':qs[_0x118753('0x3f')]('-online'),'include':[{'model':db[_0x118753('0x4a')],'as':_0x118753('0x36'),'attributes':['id',_0x118753('0x4'),_0x118753('0x49')],'through':{'attributes':[]}}]})[_0x118753('0x8')](function(_0x392a7f){const _0x45a67f=_0x118753;if(_0x392a7f){const _0x572f37=_0x4918f1[_0x45a67f('0xf')]['get']({'plain':!![]});if(!_0x572f37)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');return _0x572f37[_0x45a67f('0x43')]==='user'?db[_0x45a67f('0x3b')]['findOne']({'where':{'name':_0x45a67f('0x7'),'userProfileId':_0x572f37['userProfileId']},'raw':!![]})[_0x45a67f('0x8')](function(_0x474f2a){const _0x175b2a=_0x45a67f;if(_0x474f2a)return _0x474f2a[_0x175b2a('0x39')]===0x0?db[_0x175b2a('0x46')][_0x175b2a('0x16')]({'where':{'sectionId':_0x474f2a['id']},'raw':!![]})[_0x175b2a('0x8')](function(_0x3379d3){const _0x56b872=_0x175b2a;let _0x22eaa8=[];if(_['isEmpty'](_0x3379d3))return _0x22eaa8=_['filter'](_0x392a7f,function(_0x5de7d5){const _0x440784=a73_0x2d2e;if(_0x5de7d5[_0x440784('0x43')]!==_0x440784('0x3a'))return _0x5de7d5;}),_0x22eaa8;else{for(let _0xa792ce=0x0;_0xa792ce<_0x392a7f[_0x56b872('0x38')];_0xa792ce++){const _0x32f7c9=_0x392a7f[_0xa792ce];if(_0x32f7c9[_0x56b872('0x43')]===_0x56b872('0x3a')){const _0x309682=_['find'](_0x3379d3,function(_0x398c38){const _0x25f859=_0x56b872;return _0x398c38[_0x25f859('0x24')]===_0x32f7c9['id'];});if(_0x309682)_0x22eaa8[_0x56b872('0x40')](_0x32f7c9);}else _0x22eaa8[_0x56b872('0x40')](_0x32f7c9);}return _0x22eaa8;}}):_0x392a7f;else{const _0x16fe60=_[_0x175b2a('0x21')](_0x392a7f,function(_0xba30f5){const _0x48164a=_0x175b2a;if(_0xba30f5[_0x48164a('0x43')]!=='agent')return _0xba30f5;});return _0x16fe60;}}):_0x392a7f;}})[_0x118753('0x8')](function(_0x3e2fb4){const _0xe508e4=_0x118753,_0xa99e81=[];if(_0x3e2fb4){_0x34c42a=_0x3e2fb4;for(let _0x1a96b3=0x0;_0x1a96b3<_0x3e2fb4['length'];_0x1a96b3+=0x1){_0x34c42a[_0x1a96b3]['messages']=[],_0xa99e81[_0xe508e4('0x40')](db[_0xe508e4('0x1c')]['findOne']({'where':{'FromId':[_0x4918f1[_0xe508e4('0xf')]['id'],_0x3e2fb4[_0x1a96b3]['id']],'ToId':[_0x4918f1[_0xe508e4('0xf')]['id'],_0x3e2fb4[_0x1a96b3]['id']],'ChatGroupId':null},'attributes':['id',_0xe508e4('0x2d'),'FromId','ToId',_0xe508e4('0xd'),'body'],'order':qs[_0xe508e4('0x3f')](_0xe508e4('0x10')),'limit':0x1,'offset':0x0,'raw':!![]})[_0xe508e4('0x8')](wrapUser(_0x3e2fb4[_0x1a96b3])));}return Promise['all'](_0xa99e81);}return[];})[_0x118753('0x8')](function(_0x313bd7){const _0x64a060=_0x118753;return{'rows':_0x313bd7,'count':_0x313bd7[_0x64a060('0x38')]};})['then'](respondWithResult(_0x332d4f,null))[_0x118753('0x15')](handleError(_0x332d4f,null));};