bf8ddb9d40f201bc1fc8a5743457b107926cb3ef
[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 _0xb0b8=['show','params','keys','find','create','body','update','describe','get','lastMessageAt','createdAt','lastMessage','YYYY-MM-DD\x20HH:mm:ss','getUsers','agent','user','fullname','role','userpic','showWebBar','sort','-online','VoiceQueue','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Agents','autoAssociation','isEmpty','resourceId','push','messages','read','-createdAt','all','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','querystring','papaparse','ioredis','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','socket.io-emitter','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','then','destroy','end','error','name','send','index','map','ChatInternalMessage','rawAttributes','fieldName','type','query','filters','intersection','model','attributes','fields','length','pick','merge','filter','options','where','includeAll','include','findAll','rows','catch'];(function(_0x14f6ed,_0xbc064){var _0x40a672=function(_0x351a34){while(--_0x351a34){_0x14f6ed['push'](_0x14f6ed['shift']());}};_0x40a672(++_0xbc064);}(_0xb0b8,0xf2));var _0x8b0b=function(_0x3a7634,_0x2db384){_0x3a7634=_0x3a7634-0x0;var _0x23bbe9=_0xb0b8[_0x3a7634];return _0x23bbe9;};'use strict';var emlformat=require(_0x8b0b('0x0'));var rimraf=require(_0x8b0b('0x1'));var zipdir=require(_0x8b0b('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x8b0b('0x3'));var moment=require(_0x8b0b('0x4'));var BPromise=require(_0x8b0b('0x5'));var Mustache=require(_0x8b0b('0x6'));var util=require(_0x8b0b('0x7'));var path=require('path');var sox=require(_0x8b0b('0x8'));var csv=require(_0x8b0b('0x9'));var ejs=require(_0x8b0b('0xa'));var fs=require('fs');var fs_extra=require(_0x8b0b('0xb'));var _=require(_0x8b0b('0xc'));var squel=require(_0x8b0b('0xd'));var crypto=require('crypto');var jsforce=require(_0x8b0b('0xe'));var deskjs=require('desk.js');var toCsv=require(_0x8b0b('0x9'));var querystring=require(_0x8b0b('0xf'));var Papa=require(_0x8b0b('0x10'));var Redis=require(_0x8b0b('0x11'));var authService=require('../../components/auth/service');var qs=require(_0x8b0b('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x8b0b('0x13'));var logger=require(_0x8b0b('0x14'))(_0x8b0b('0x15'));var utils=require(_0x8b0b('0x16'));var config=require(_0x8b0b('0x17'));var licenseUtil=require(_0x8b0b('0x18'));var db=require(_0x8b0b('0x19'))['db'];config[_0x8b0b('0x1a')]=_[_0x8b0b('0x1b')](config[_0x8b0b('0x1a')],{'host':'localhost','port':0x18eb});var socket=require(_0x8b0b('0x1c'))(new Redis(config[_0x8b0b('0x1a')]));require('./chatInternalMessage.socket')['register'](socket);function respondWithStatusCode(_0x36299a,_0xa55b34){_0xa55b34=_0xa55b34||0xcc;return function(_0x5032fc){if(_0x5032fc){return _0x36299a[_0x8b0b('0x1d')](_0xa55b34);}return _0x36299a[_0x8b0b('0x1e')](_0xa55b34)['end']();};}function respondWithResult(_0x584f7f,_0x1ec091){_0x1ec091=_0x1ec091||0xc8;return function(_0x2ef906){if(_0x2ef906){return _0x584f7f[_0x8b0b('0x1e')](_0x1ec091)[_0x8b0b('0x1f')](_0x2ef906);}};}function respondWithFilteredResult(_0x53b77e,_0x31e09d){return function(_0x29df14){if(_0x29df14){var _0x96f65e=typeof _0x31e09d[_0x8b0b('0x20')]===_0x8b0b('0x21')&&typeof _0x31e09d[_0x8b0b('0x22')]===_0x8b0b('0x21');var _0x4407e5=_0x29df14[_0x8b0b('0x23')];var _0x1c9029=_0x96f65e?0x0:_0x31e09d[_0x8b0b('0x20')];var _0x5ad4a9=_0x96f65e?_0x29df14['count']:_0x31e09d[_0x8b0b('0x20')]+_0x31e09d['limit'];var _0x49b967;if(_0x5ad4a9>=_0x4407e5){_0x5ad4a9=_0x4407e5;_0x49b967=0xc8;}else{_0x49b967=0xce;}_0x53b77e[_0x8b0b('0x1e')](_0x49b967);return _0x53b77e[_0x8b0b('0x24')](_0x8b0b('0x25'),_0x1c9029+'-'+_0x5ad4a9+'/'+_0x4407e5)['json'](_0x29df14);}return null;};}function patchUpdates(_0x33c788){return function(_0x44bbcc){try{jsonpatch[_0x8b0b('0x26')](_0x44bbcc,_0x33c788,!![]);}catch(_0x688c20){return BPromise[_0x8b0b('0x27')](_0x688c20);}return _0x44bbcc[_0x8b0b('0x28')]();};}function saveUpdates(_0x462d3a,_0x1b2530){return function(_0x58eba7){if(_0x58eba7){return _0x58eba7['update'](_0x462d3a)[_0x8b0b('0x29')](function(_0x2b59cd){return _0x2b59cd;});}return null;};}function removeEntity(_0x1fe051,_0x13a4d3){return function(_0x121bdc){if(_0x121bdc){return _0x121bdc[_0x8b0b('0x2a')]()[_0x8b0b('0x29')](function(){_0x1fe051['status'](0xcc)[_0x8b0b('0x2b')]();});}};}function handleEntityNotFound(_0x38d4b3,_0x1f09b6){return function(_0x10d6a1){if(!_0x10d6a1){_0x38d4b3[_0x8b0b('0x1d')](0x194);}return _0x10d6a1;};}function handleError(_0x270a13,_0x22faf1){_0x22faf1=_0x22faf1||0x1f4;return function(_0x53fb9c){logger[_0x8b0b('0x2c')](_0x53fb9c['stack']);if(_0x53fb9c[_0x8b0b('0x2d')]){delete _0x53fb9c['name'];}_0x270a13[_0x8b0b('0x1e')](_0x22faf1)[_0x8b0b('0x2e')](_0x53fb9c);};}exports[_0x8b0b('0x2f')]=function(_0x1a1292,_0x46f212){var _0x52c8d2={},_0x20ef3b={},_0x3248f4={'count':0x0,'rows':[]};var _0x298a76=_[_0x8b0b('0x30')](db[_0x8b0b('0x31')][_0x8b0b('0x32')],function(_0x17ee11){return{'name':_0x17ee11[_0x8b0b('0x33')],'type':_0x17ee11[_0x8b0b('0x34')]['key']};});_0x20ef3b['model']=_[_0x8b0b('0x30')](_0x298a76,_0x8b0b('0x2d'));_0x20ef3b[_0x8b0b('0x35')]=_['keys'](_0x1a1292[_0x8b0b('0x35')]);_0x20ef3b[_0x8b0b('0x36')]=_[_0x8b0b('0x37')](_0x20ef3b[_0x8b0b('0x38')],_0x20ef3b[_0x8b0b('0x35')]);_0x52c8d2[_0x8b0b('0x39')]=_[_0x8b0b('0x37')](_0x20ef3b[_0x8b0b('0x38')],qs['fields'](_0x1a1292['query'][_0x8b0b('0x3a')]));_0x52c8d2['attributes']=_0x52c8d2[_0x8b0b('0x39')][_0x8b0b('0x3b')]?_0x52c8d2[_0x8b0b('0x39')]:_0x20ef3b['model'];if(!_0x1a1292[_0x8b0b('0x35')]['hasOwnProperty']('nolimit')){_0x52c8d2[_0x8b0b('0x22')]=qs['limit'](_0x1a1292[_0x8b0b('0x35')][_0x8b0b('0x22')]);_0x52c8d2[_0x8b0b('0x20')]=qs[_0x8b0b('0x20')](_0x1a1292[_0x8b0b('0x35')]['offset']);}_0x52c8d2['order']=qs['sort'](_0x1a1292[_0x8b0b('0x35')]['sort']);_0x52c8d2['where']=qs[_0x8b0b('0x36')](_[_0x8b0b('0x3c')](_0x1a1292[_0x8b0b('0x35')],_0x20ef3b['filters']),_0x298a76);if(_0x1a1292[_0x8b0b('0x35')]['filter']){_0x52c8d2['where']=_[_0x8b0b('0x3d')](_0x52c8d2['where'],{'$or':_[_0x8b0b('0x30')](_0x298a76,function(_0x1afd4d){if(_0x1afd4d[_0x8b0b('0x34')]!=='VIRTUAL'){var _0x503d91={};_0x503d91[_0x1afd4d[_0x8b0b('0x2d')]]={'$like':'%'+_0x1a1292['query'][_0x8b0b('0x3e')]+'%'};return _0x503d91;}})});}_0x52c8d2=_['merge']({},_0x52c8d2,_0x1a1292[_0x8b0b('0x3f')]);var _0x2b4ff9={'where':_0x52c8d2[_0x8b0b('0x40')]};return db['ChatInternalMessage']['count'](_0x2b4ff9)[_0x8b0b('0x29')](function(_0x1348cd){_0x3248f4[_0x8b0b('0x23')]=_0x1348cd;if(_0x1a1292['query'][_0x8b0b('0x41')]){_0x52c8d2[_0x8b0b('0x42')]=[{'all':!![]}];}return db['ChatInternalMessage'][_0x8b0b('0x43')](_0x52c8d2);})[_0x8b0b('0x29')](function(_0x2cfda5){_0x3248f4[_0x8b0b('0x44')]=_0x2cfda5;return _0x3248f4;})['then'](respondWithFilteredResult(_0x46f212,_0x52c8d2))[_0x8b0b('0x45')](handleError(_0x46f212,null));};exports[_0x8b0b('0x46')]=function(_0x48620f,_0x312925){var _0x4698c7={'raw':!![],'where':{'id':_0x48620f[_0x8b0b('0x47')]['id']}},_0x924f91={};_0x924f91['model']=_['keys'](db['ChatInternalMessage']['rawAttributes']);_0x924f91[_0x8b0b('0x35')]=_[_0x8b0b('0x48')](_0x48620f[_0x8b0b('0x35')]);_0x924f91[_0x8b0b('0x36')]=_[_0x8b0b('0x37')](_0x924f91[_0x8b0b('0x38')],_0x924f91[_0x8b0b('0x35')]);_0x4698c7['attributes']=_[_0x8b0b('0x37')](_0x924f91[_0x8b0b('0x38')],qs[_0x8b0b('0x3a')](_0x48620f[_0x8b0b('0x35')][_0x8b0b('0x3a')]));_0x4698c7['attributes']=_0x4698c7[_0x8b0b('0x39')]['length']?_0x4698c7[_0x8b0b('0x39')]:_0x924f91[_0x8b0b('0x38')];if(_0x48620f[_0x8b0b('0x35')][_0x8b0b('0x41')]){_0x4698c7['include']=[{'all':!![]}];}_0x4698c7=_[_0x8b0b('0x3d')]({},_0x4698c7,_0x48620f['options']);return db['ChatInternalMessage'][_0x8b0b('0x49')](_0x4698c7)[_0x8b0b('0x29')](handleEntityNotFound(_0x312925,null))[_0x8b0b('0x29')](respondWithResult(_0x312925,null))[_0x8b0b('0x45')](handleError(_0x312925,null));};exports[_0x8b0b('0x4a')]=function(_0x2b03fe,_0x3cc9e0){return db[_0x8b0b('0x31')][_0x8b0b('0x4a')](_0x2b03fe[_0x8b0b('0x4b')],{})[_0x8b0b('0x29')](respondWithResult(_0x3cc9e0,0xc9))['catch'](handleError(_0x3cc9e0,null));};exports[_0x8b0b('0x4c')]=function(_0xb26e05,_0x37396c){if(_0xb26e05['body']['id']){delete _0xb26e05[_0x8b0b('0x4b')]['id'];}return db['ChatInternalMessage']['find']({'where':{'id':_0xb26e05[_0x8b0b('0x47')]['id']}})[_0x8b0b('0x29')](handleEntityNotFound(_0x37396c,null))[_0x8b0b('0x29')](saveUpdates(_0xb26e05[_0x8b0b('0x4b')],null))['then'](respondWithResult(_0x37396c,null))[_0x8b0b('0x45')](handleError(_0x37396c,null));};exports[_0x8b0b('0x2a')]=function(_0x2c7a07,_0x2c1e86){return db[_0x8b0b('0x31')][_0x8b0b('0x49')]({'where':{'id':_0x2c7a07['params']['id']}})[_0x8b0b('0x29')](handleEntityNotFound(_0x2c1e86,null))['then'](removeEntity(_0x2c1e86,null))[_0x8b0b('0x45')](handleError(_0x2c1e86,null));};exports[_0x8b0b('0x4d')]=function(_0x3afc35,_0x394e4a){return db[_0x8b0b('0x31')][_0x8b0b('0x4d')]()['then'](respondWithResult(_0x394e4a,null))[_0x8b0b('0x45')](handleError(_0x394e4a,null));};function wrapUser(_0x547398){return function(_0x3631fe){var _0x3dec59=_0x547398[_0x8b0b('0x4e')]({'plain':!![]});_0x3dec59['messages']=[];if(_0x3631fe){_0x3dec59[_0x8b0b('0x4f')]=_0x3631fe[_0x8b0b('0x50')];_0x3dec59[_0x8b0b('0x51')]=_0x3631fe;}else{_0x3dec59[_0x8b0b('0x4f')]=moment(0x0)['format'](_0x8b0b('0x52'));_0x3dec59[_0x8b0b('0x51')]={};}return _0x3dec59;};}exports[_0x8b0b('0x53')]=function(_0x3b2cb5,_0x1089dd,_0x4f40e8){var _0x2fa9de=[];return db['User'][_0x8b0b('0x43')]({'where':{'role':[_0x8b0b('0x54'),'admin',_0x8b0b('0x55')]},'attributes':['id',_0x8b0b('0x56'),_0x8b0b('0x2d'),_0x8b0b('0x57'),_0x8b0b('0x58'),'internal','online','lastLoginAt','allowmessenger',_0x8b0b('0x59')],'order':qs[_0x8b0b('0x5a')](_0x8b0b('0x5b')),'include':[{'model':db[_0x8b0b('0x5c')],'as':'VoiceQueues','attributes':['id',_0x8b0b('0x2d'),'type'],'through':{'attributes':[]}}]})[_0x8b0b('0x29')](function(_0x248b0a){if(_0x248b0a){var _0x239de9=_0x3b2cb5[_0x8b0b('0x55')][_0x8b0b('0x4e')]({'plain':!![]});if(!_0x239de9)throw new Error(_0x8b0b('0x5d'));if(_0x239de9[_0x8b0b('0x57')]==='user'){return db[_0x8b0b('0x5e')][_0x8b0b('0x49')]({'where':{'name':_0x8b0b('0x5f'),'userProfileId':_0x239de9['userProfileId']},'raw':!![]})[_0x8b0b('0x29')](function(_0x241b71){if(_0x241b71){if(_0x241b71[_0x8b0b('0x60')]===0x0){return db['UserProfileResource'][_0x8b0b('0x43')]({'where':{'sectionId':_0x241b71['id']},'raw':!![]})[_0x8b0b('0x29')](function(_0x120659){var _0x28fc1e=[];if(_[_0x8b0b('0x61')](_0x120659)){_0x28fc1e=_[_0x8b0b('0x3e')](_0x248b0a,function(_0x3acdf3){if(_0x3acdf3['role']!==_0x8b0b('0x54')){return _0x3acdf3;}});return _0x28fc1e;}else{for(var _0x20de9e=0x0;_0x20de9e<_0x248b0a[_0x8b0b('0x3b')];_0x20de9e++){var _0x189790=_0x248b0a[_0x20de9e];if(_0x189790[_0x8b0b('0x57')]==='agent'){var _0x362ad3=_['find'](_0x120659,function(_0x3f5f4a){return _0x3f5f4a[_0x8b0b('0x62')]===_0x189790['id'];});if(_0x362ad3)_0x28fc1e[_0x8b0b('0x63')](_0x189790);}else{_0x28fc1e['push'](_0x189790);}}return _0x28fc1e;}});}else{return _0x248b0a;}}else{var _0x203e7a=_[_0x8b0b('0x3e')](_0x248b0a,function(_0x12a88d){if(_0x12a88d[_0x8b0b('0x57')]!==_0x8b0b('0x54')){return _0x12a88d;}});return _0x203e7a;}});}else{return _0x248b0a;}}})['then'](function(_0x462585){var _0x4529ea=[];if(_0x462585){_0x2fa9de=_0x462585;for(var _0x39151f=0x0;_0x39151f<_0x462585[_0x8b0b('0x3b')];_0x39151f+=0x1){_0x2fa9de[_0x39151f][_0x8b0b('0x64')]=[];_0x4529ea[_0x8b0b('0x63')](db[_0x8b0b('0x31')][_0x8b0b('0x49')]({'where':{'FromId':[_0x3b2cb5[_0x8b0b('0x55')]['id'],_0x462585[_0x39151f]['id']],'ToId':[_0x3b2cb5[_0x8b0b('0x55')]['id'],_0x462585[_0x39151f]['id']],'ChatGroupId':null},'attributes':['id',_0x8b0b('0x50'),'FromId','ToId',_0x8b0b('0x65'),_0x8b0b('0x4b')],'order':qs[_0x8b0b('0x5a')](_0x8b0b('0x66')),'limit':0x1,'offset':0x0,'raw':!![]})[_0x8b0b('0x29')](wrapUser(_0x462585[_0x39151f])));}return BPromise[_0x8b0b('0x67')](_0x4529ea);}return[];})['then'](function(_0x4a2a4c){return{'rows':_0x4a2a4c,'count':_0x4a2a4c['length']};})[_0x8b0b('0x29')](respondWithResult(_0x1089dd,null))[_0x8b0b('0x45')](handleError(_0x1089dd,null));};