fefc494339c6f35705cf15b377222c8e860bfb1b
[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 _0x5e4a=['params','rawAttributes','catch','create','body','find','describe','messages','lastMessage','lastMessageAt','YYYY-MM-DD\x20HH:mm:ss','getUsers','agent','user','fullname','role','userpic','online','allowmessenger','showWebBar','VoiceQueue','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Agents','userProfileId','autoAssociation','UserProfileResource','isEmpty','push','createdAt','FromId','ToId','-createdAt','eml-format','rimraf','zip-dir','request-promise','moment','mustache','util','sox','to-csv','fs-extra','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','defaults','redis','localhost','socket.io-emitter','register','status','json','undefined','limit','count','offset','Content-Range','apply','reject','save','update','destroy','then','end','sendStatus','error','stack','name','send','index','fieldName','type','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','options','includeAll','include','ChatInternalMessage','findAll','rows'];(function(_0xe503ff,_0xc9e5e4){var _0x345c46=function(_0x3f6bc7){while(--_0x3f6bc7){_0xe503ff['push'](_0xe503ff['shift']());}};_0x345c46(++_0xc9e5e4);}(_0x5e4a,0xf3));var _0xa5e4=function(_0x5a42b2,_0x49e23f){_0x5a42b2=_0x5a42b2-0x0;var _0x6fa38d=_0x5e4a[_0x5a42b2];return _0x6fa38d;};'use strict';var emlformat=require(_0xa5e4('0x0'));var rimraf=require(_0xa5e4('0x1'));var zipdir=require(_0xa5e4('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xa5e4('0x3'));var moment=require(_0xa5e4('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xa5e4('0x5'));var util=require(_0xa5e4('0x6'));var path=require('path');var sox=require(_0xa5e4('0x7'));var csv=require(_0xa5e4('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xa5e4('0x9'));var _=require('lodash');var squel=require('squel');var crypto=require(_0xa5e4('0xa'));var jsforce=require(_0xa5e4('0xb'));var deskjs=require(_0xa5e4('0xc'));var toCsv=require(_0xa5e4('0x8'));var querystring=require(_0xa5e4('0xd'));var Papa=require('papaparse');var Redis=require(_0xa5e4('0xe'));var authService=require(_0xa5e4('0xf'));var qs=require(_0xa5e4('0x10'));var as=require(_0xa5e4('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xa5e4('0x12'));var utils=require(_0xa5e4('0x13'));var config=require(_0xa5e4('0x14'));var licenseUtil=require(_0xa5e4('0x15'));var db=require(_0xa5e4('0x16'))['db'];config['redis']=_[_0xa5e4('0x17')](config[_0xa5e4('0x18')],{'host':_0xa5e4('0x19'),'port':0x18eb});var socket=require(_0xa5e4('0x1a'))(new Redis(config['redis']));require('./chatInternalMessage.socket')[_0xa5e4('0x1b')](socket);function respondWithStatusCode(_0xe84edd,_0x2a6440){_0x2a6440=_0x2a6440||0xcc;return function(_0x4a79bc){if(_0x4a79bc){return _0xe84edd['sendStatus'](_0x2a6440);}return _0xe84edd[_0xa5e4('0x1c')](_0x2a6440)['end']();};}function respondWithResult(_0x533a52,_0x4a5416){_0x4a5416=_0x4a5416||0xc8;return function(_0x3bd692){if(_0x3bd692){return _0x533a52[_0xa5e4('0x1c')](_0x4a5416)[_0xa5e4('0x1d')](_0x3bd692);}};}function respondWithFilteredResult(_0x1901f6,_0x165fa3){return function(_0x164a21){if(_0x164a21){var _0x51ebee=typeof _0x165fa3['offset']===_0xa5e4('0x1e')&&typeof _0x165fa3[_0xa5e4('0x1f')]===_0xa5e4('0x1e');var _0x176b21=_0x164a21[_0xa5e4('0x20')];var _0x1d3622=_0x51ebee?0x0:_0x165fa3[_0xa5e4('0x21')];var _0x2c957a=_0x51ebee?_0x164a21[_0xa5e4('0x20')]:_0x165fa3[_0xa5e4('0x21')]+_0x165fa3['limit'];var _0x26185b;if(_0x2c957a>=_0x176b21){_0x2c957a=_0x176b21;_0x26185b=0xc8;}else{_0x26185b=0xce;}_0x1901f6[_0xa5e4('0x1c')](_0x26185b);return _0x1901f6['set'](_0xa5e4('0x22'),_0x1d3622+'-'+_0x2c957a+'/'+_0x176b21)[_0xa5e4('0x1d')](_0x164a21);}return null;};}function patchUpdates(_0x3fe2d8){return function(_0x297820){try{jsonpatch[_0xa5e4('0x23')](_0x297820,_0x3fe2d8,!![]);}catch(_0x21e677){return BPromise[_0xa5e4('0x24')](_0x21e677);}return _0x297820[_0xa5e4('0x25')]();};}function saveUpdates(_0x200ce1,_0x584631){return function(_0x3eb5ad){if(_0x3eb5ad){return _0x3eb5ad[_0xa5e4('0x26')](_0x200ce1)['then'](function(_0x51b027){return _0x51b027;});}return null;};}function removeEntity(_0x2ed137,_0x43311a){return function(_0x426967){if(_0x426967){return _0x426967[_0xa5e4('0x27')]()[_0xa5e4('0x28')](function(){_0x2ed137[_0xa5e4('0x1c')](0xcc)[_0xa5e4('0x29')]();});}};}function handleEntityNotFound(_0x3a5d08,_0xe4e21f){return function(_0xbc8455){if(!_0xbc8455){_0x3a5d08[_0xa5e4('0x2a')](0x194);}return _0xbc8455;};}function handleError(_0x59204b,_0x2290cc){_0x2290cc=_0x2290cc||0x1f4;return function(_0x4d0859){logger[_0xa5e4('0x2b')](_0x4d0859[_0xa5e4('0x2c')]);if(_0x4d0859[_0xa5e4('0x2d')]){delete _0x4d0859[_0xa5e4('0x2d')];}_0x59204b['status'](_0x2290cc)[_0xa5e4('0x2e')](_0x4d0859);};}exports[_0xa5e4('0x2f')]=function(_0x5a6ba8,_0xf2498a){var _0x2ca8d6={},_0x34d2d4={},_0x4e4df6={'count':0x0,'rows':[]};var _0x4fcb5c=_['map'](db['ChatInternalMessage']['rawAttributes'],function(_0x52ee6b){return{'name':_0x52ee6b[_0xa5e4('0x30')],'type':_0x52ee6b[_0xa5e4('0x31')][_0xa5e4('0x32')]};});_0x34d2d4[_0xa5e4('0x33')]=_['map'](_0x4fcb5c,_0xa5e4('0x2d'));_0x34d2d4[_0xa5e4('0x34')]=_[_0xa5e4('0x35')](_0x5a6ba8['query']);_0x34d2d4[_0xa5e4('0x36')]=_[_0xa5e4('0x37')](_0x34d2d4['model'],_0x34d2d4[_0xa5e4('0x34')]);_0x2ca8d6['attributes']=_[_0xa5e4('0x37')](_0x34d2d4[_0xa5e4('0x33')],qs[_0xa5e4('0x38')](_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x38')]));_0x2ca8d6[_0xa5e4('0x39')]=_0x2ca8d6['attributes'][_0xa5e4('0x3a')]?_0x2ca8d6[_0xa5e4('0x39')]:_0x34d2d4['model'];if(!_0x5a6ba8['query'][_0xa5e4('0x3b')](_0xa5e4('0x3c'))){_0x2ca8d6[_0xa5e4('0x1f')]=qs[_0xa5e4('0x1f')](_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x1f')]);_0x2ca8d6['offset']=qs[_0xa5e4('0x21')](_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x21')]);}_0x2ca8d6['order']=qs['sort'](_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x3d')]);_0x2ca8d6[_0xa5e4('0x3e')]=qs['filters'](_[_0xa5e4('0x3f')](_0x5a6ba8['query'],_0x34d2d4[_0xa5e4('0x36')]),_0x4fcb5c);if(_0x5a6ba8['query'][_0xa5e4('0x40')]){_0x2ca8d6['where']=_[_0xa5e4('0x41')](_0x2ca8d6[_0xa5e4('0x3e')],{'$or':_['map'](_0x4fcb5c,function(_0x4a2881){if(_0x4a2881[_0xa5e4('0x31')]!=='VIRTUAL'){var _0x2cfc51={};_0x2cfc51[_0x4a2881[_0xa5e4('0x2d')]]={'$like':'%'+_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x40')]+'%'};return _0x2cfc51;}})});}_0x2ca8d6=_['merge']({},_0x2ca8d6,_0x5a6ba8[_0xa5e4('0x42')]);var _0x388b41={'where':_0x2ca8d6['where']};return db['ChatInternalMessage']['count'](_0x388b41)['then'](function(_0x40126b){_0x4e4df6[_0xa5e4('0x20')]=_0x40126b;if(_0x5a6ba8[_0xa5e4('0x34')][_0xa5e4('0x43')]){_0x2ca8d6[_0xa5e4('0x44')]=[{'all':!![]}];}return db[_0xa5e4('0x45')][_0xa5e4('0x46')](_0x2ca8d6);})[_0xa5e4('0x28')](function(_0x294939){_0x4e4df6[_0xa5e4('0x47')]=_0x294939;return _0x4e4df6;})['then'](respondWithFilteredResult(_0xf2498a,_0x2ca8d6))['catch'](handleError(_0xf2498a,null));};exports['show']=function(_0x4ffd64,_0x3689b5){var _0x52740c={'raw':!![],'where':{'id':_0x4ffd64[_0xa5e4('0x48')]['id']}},_0x5f16cd={};_0x5f16cd[_0xa5e4('0x33')]=_['keys'](db[_0xa5e4('0x45')][_0xa5e4('0x49')]);_0x5f16cd[_0xa5e4('0x34')]=_[_0xa5e4('0x35')](_0x4ffd64[_0xa5e4('0x34')]);_0x5f16cd['filters']=_['intersection'](_0x5f16cd[_0xa5e4('0x33')],_0x5f16cd[_0xa5e4('0x34')]);_0x52740c[_0xa5e4('0x39')]=_[_0xa5e4('0x37')](_0x5f16cd['model'],qs[_0xa5e4('0x38')](_0x4ffd64[_0xa5e4('0x34')][_0xa5e4('0x38')]));_0x52740c[_0xa5e4('0x39')]=_0x52740c[_0xa5e4('0x39')][_0xa5e4('0x3a')]?_0x52740c[_0xa5e4('0x39')]:_0x5f16cd['model'];if(_0x4ffd64[_0xa5e4('0x34')][_0xa5e4('0x43')]){_0x52740c[_0xa5e4('0x44')]=[{'all':!![]}];}_0x52740c=_['merge']({},_0x52740c,_0x4ffd64['options']);return db['ChatInternalMessage']['find'](_0x52740c)[_0xa5e4('0x28')](handleEntityNotFound(_0x3689b5,null))[_0xa5e4('0x28')](respondWithResult(_0x3689b5,null))[_0xa5e4('0x4a')](handleError(_0x3689b5,null));};exports[_0xa5e4('0x4b')]=function(_0x312695,_0x85e375){return db[_0xa5e4('0x45')][_0xa5e4('0x4b')](_0x312695[_0xa5e4('0x4c')],{})[_0xa5e4('0x28')](respondWithResult(_0x85e375,0xc9))[_0xa5e4('0x4a')](handleError(_0x85e375,null));};exports[_0xa5e4('0x26')]=function(_0x3ded6c,_0x217afd){if(_0x3ded6c[_0xa5e4('0x4c')]['id']){delete _0x3ded6c[_0xa5e4('0x4c')]['id'];}return db['ChatInternalMessage']['find']({'where':{'id':_0x3ded6c['params']['id']}})['then'](handleEntityNotFound(_0x217afd,null))[_0xa5e4('0x28')](saveUpdates(_0x3ded6c[_0xa5e4('0x4c')],null))[_0xa5e4('0x28')](respondWithResult(_0x217afd,null))[_0xa5e4('0x4a')](handleError(_0x217afd,null));};exports[_0xa5e4('0x27')]=function(_0x2a1a58,_0x30bafe){return db['ChatInternalMessage'][_0xa5e4('0x4d')]({'where':{'id':_0x2a1a58[_0xa5e4('0x48')]['id']}})[_0xa5e4('0x28')](handleEntityNotFound(_0x30bafe,null))[_0xa5e4('0x28')](removeEntity(_0x30bafe,null))[_0xa5e4('0x4a')](handleError(_0x30bafe,null));};exports[_0xa5e4('0x4e')]=function(_0x35bd0c,_0x35e1f8){return db[_0xa5e4('0x45')][_0xa5e4('0x4e')]()[_0xa5e4('0x28')](respondWithResult(_0x35e1f8,null))[_0xa5e4('0x4a')](handleError(_0x35e1f8,null));};function wrapUser(_0x34794c){return function(_0x237413){var _0x4f8480=_0x34794c['get']({'plain':!![]});_0x4f8480[_0xa5e4('0x4f')]=[];if(_0x237413){_0x4f8480['lastMessageAt']=_0x237413['createdAt'];_0x4f8480[_0xa5e4('0x50')]=_0x237413;}else{_0x4f8480[_0xa5e4('0x51')]=moment(0x0)['format'](_0xa5e4('0x52'));_0x4f8480['lastMessage']={};}return _0x4f8480;};}exports[_0xa5e4('0x53')]=function(_0x3ec843,_0x4dbc21,_0x12d70a){var _0x1dc79a=[];return db['User'][_0xa5e4('0x46')]({'where':{'role':[_0xa5e4('0x54'),'admin',_0xa5e4('0x55')]},'attributes':['id',_0xa5e4('0x56'),_0xa5e4('0x2d'),_0xa5e4('0x57'),_0xa5e4('0x58'),'internal',_0xa5e4('0x59'),'lastLoginAt',_0xa5e4('0x5a'),_0xa5e4('0x5b')],'order':qs[_0xa5e4('0x3d')]('-online'),'include':[{'model':db[_0xa5e4('0x5c')],'as':'VoiceQueues','attributes':['id',_0xa5e4('0x2d'),'type'],'through':{'attributes':[]}}]})[_0xa5e4('0x28')](function(_0x48fe88){if(_0x48fe88){var _0x1014df=_0x3ec843[_0xa5e4('0x55')]['get']({'plain':!![]});if(!_0x1014df)throw new Error(_0xa5e4('0x5d'));if(_0x1014df[_0xa5e4('0x57')]===_0xa5e4('0x55')){return db[_0xa5e4('0x5e')][_0xa5e4('0x4d')]({'where':{'name':_0xa5e4('0x5f'),'userProfileId':_0x1014df[_0xa5e4('0x60')]},'raw':!![]})[_0xa5e4('0x28')](function(_0x2282bf){if(_0x2282bf){if(_0x2282bf[_0xa5e4('0x61')]===0x0){return db[_0xa5e4('0x62')][_0xa5e4('0x46')]({'where':{'sectionId':_0x2282bf['id']},'raw':!![]})['then'](function(_0x2fe9be){var _0x1b9abc=[];if(_[_0xa5e4('0x63')](_0x2fe9be)){_0x1b9abc=_[_0xa5e4('0x40')](_0x48fe88,function(_0x18359b){if(_0x18359b[_0xa5e4('0x57')]!=='agent'){return _0x18359b;}});return _0x1b9abc;}else{for(var _0x400218=0x0;_0x400218<_0x48fe88['length'];_0x400218++){var _0x5418df=_0x48fe88[_0x400218];if(_0x5418df['role']===_0xa5e4('0x54')){var _0x46e582=_[_0xa5e4('0x4d')](_0x2fe9be,function(_0xfb472f){return _0xfb472f['resourceId']===_0x5418df['id'];});if(_0x46e582)_0x1b9abc['push'](_0x5418df);}else{_0x1b9abc['push'](_0x5418df);}}return _0x1b9abc;}});}else{return _0x48fe88;}}else{var _0x525212=_[_0xa5e4('0x40')](_0x48fe88,function(_0x21e68d){if(_0x21e68d[_0xa5e4('0x57')]!=='agent'){return _0x21e68d;}});return _0x525212;}});}else{return _0x48fe88;}}})[_0xa5e4('0x28')](function(_0x3b3bb8){var _0x56ee2a=[];if(_0x3b3bb8){_0x1dc79a=_0x3b3bb8;for(var _0x25ca2b=0x0;_0x25ca2b<_0x3b3bb8[_0xa5e4('0x3a')];_0x25ca2b+=0x1){_0x1dc79a[_0x25ca2b][_0xa5e4('0x4f')]=[];_0x56ee2a[_0xa5e4('0x64')](db[_0xa5e4('0x45')][_0xa5e4('0x4d')]({'where':{'FromId':[_0x3ec843[_0xa5e4('0x55')]['id'],_0x3b3bb8[_0x25ca2b]['id']],'ToId':[_0x3ec843[_0xa5e4('0x55')]['id'],_0x3b3bb8[_0x25ca2b]['id']],'ChatGroupId':null},'attributes':['id',_0xa5e4('0x65'),_0xa5e4('0x66'),_0xa5e4('0x67'),'read',_0xa5e4('0x4c')],'order':qs[_0xa5e4('0x3d')](_0xa5e4('0x68')),'limit':0x1,'offset':0x0,'raw':!![]})[_0xa5e4('0x28')](wrapUser(_0x3b3bb8[_0x25ca2b])));}return BPromise['all'](_0x56ee2a);}return[];})[_0xa5e4('0x28')](function(_0x9a5f3f){return{'rows':_0x9a5f3f,'count':_0x9a5f3f[_0xa5e4('0x3a')]};})[_0xa5e4('0x28')](respondWithResult(_0x4dbc21,null))['catch'](handleError(_0x4dbc21,null));};