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 _0xe5d4=['create','body','find','describe','get','messages','lastMessageAt','lastMessage','format','YYYY-MM-DD\x20HH:mm:ss','getUsers','agent','admin','user','fullname','userpic','internal','online','allowmessenger','showWebBar','-online','VoiceQueue','VoiceQueues','role','UserProfileSection','Agents','autoAssociation','UserProfileResource','findAll','isEmpty','resourceId','push','createdAt','FromId','ToId','-createdAt','all','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','to-csv','ejs','fs-extra','lodash','squel','crypto','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','defaults','redis','localhost','./chatInternalMessage.socket','register','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','reject','update','then','destroy','error','stack','name','send','index','map','ChatInternalMessage','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','includeAll','include','rows','catch','show'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0xe5d4,0x1f1));var _0x4e5d=function(_0x5d6456,_0x58ecc7){_0x5d6456=_0x5d6456-0x0;var _0x4d66c1=_0xe5d4[_0x5d6456];return _0x4d66c1;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x4e5d('0x0'));var jsonpatch=require(_0x4e5d('0x1'));var rp=require(_0x4e5d('0x2'));var moment=require(_0x4e5d('0x3'));var BPromise=require(_0x4e5d('0x4'));var Mustache=require(_0x4e5d('0x5'));var util=require(_0x4e5d('0x6'));var path=require('path');var sox=require('sox');var csv=require(_0x4e5d('0x7'));var ejs=require(_0x4e5d('0x8'));var fs=require('fs');var fs_extra=require(_0x4e5d('0x9'));var _=require(_0x4e5d('0xa'));var squel=require(_0x4e5d('0xb'));var crypto=require(_0x4e5d('0xc'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x4e5d('0xd'));var Redis=require(_0x4e5d('0xe'));var authService=require(_0x4e5d('0xf'));var qs=require(_0x4e5d('0x10'));var as=require(_0x4e5d('0x11'));var hardwareService=require(_0x4e5d('0x12'));var logger=require(_0x4e5d('0x13'))(_0x4e5d('0x14'));var utils=require(_0x4e5d('0x15'));var config=require(_0x4e5d('0x16'));var licenseUtil=require(_0x4e5d('0x17'));var db=require(_0x4e5d('0x18'))['db'];config['redis']=_[_0x4e5d('0x19')](config[_0x4e5d('0x1a')],{'host':_0x4e5d('0x1b'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x4e5d('0x1a')]));require(_0x4e5d('0x1c'))[_0x4e5d('0x1d')](socket);function respondWithStatusCode(_0x2857d7,_0x5ed124){_0x5ed124=_0x5ed124||0xcc;return function(_0x19c146){if(_0x19c146){return _0x2857d7[_0x4e5d('0x1e')](_0x5ed124);}return _0x2857d7['status'](_0x5ed124)[_0x4e5d('0x1f')]();};}function respondWithResult(_0x36fb2e,_0x3f35df){_0x3f35df=_0x3f35df||0xc8;return function(_0x52fdb9){if(_0x52fdb9){return _0x36fb2e[_0x4e5d('0x20')](_0x3f35df)[_0x4e5d('0x21')](_0x52fdb9);}};}function respondWithFilteredResult(_0x5c1ed2,_0x5fc978){return function(_0x183197){if(_0x183197){var _0x29bd9f=typeof _0x5fc978[_0x4e5d('0x22')]===_0x4e5d('0x23')&&typeof _0x5fc978[_0x4e5d('0x24')]===_0x4e5d('0x23');var _0x2ef16b=_0x183197[_0x4e5d('0x25')];var _0x28386a=_0x29bd9f?0x0:_0x5fc978[_0x4e5d('0x22')];var _0x3cee79=_0x29bd9f?_0x183197[_0x4e5d('0x25')]:_0x5fc978[_0x4e5d('0x22')]+_0x5fc978[_0x4e5d('0x24')];var _0x918fce;if(_0x3cee79>=_0x2ef16b){_0x3cee79=_0x2ef16b;_0x918fce=0xc8;}else{_0x918fce=0xce;}_0x5c1ed2[_0x4e5d('0x20')](_0x918fce);return _0x5c1ed2[_0x4e5d('0x26')](_0x4e5d('0x27'),_0x28386a+'-'+_0x3cee79+'/'+_0x2ef16b)[_0x4e5d('0x21')](_0x183197);}return null;};}function patchUpdates(_0x8aae26){return function(_0x2795fc){try{jsonpatch['apply'](_0x2795fc,_0x8aae26,!![]);}catch(_0x117a88){return BPromise[_0x4e5d('0x28')](_0x117a88);}return _0x2795fc['save']();};}function saveUpdates(_0x4892ef,_0x5a1aef){return function(_0x3ae38d){if(_0x3ae38d){return _0x3ae38d[_0x4e5d('0x29')](_0x4892ef)[_0x4e5d('0x2a')](function(_0x3c73b2){return _0x3c73b2;});}return null;};}function removeEntity(_0x219df1,_0x21f724){return function(_0xe2b53b){if(_0xe2b53b){return _0xe2b53b[_0x4e5d('0x2b')]()['then'](function(){_0x219df1['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x4eb4e8,_0x3fb767){return function(_0xc82a42){if(!_0xc82a42){_0x4eb4e8[_0x4e5d('0x1e')](0x194);}return _0xc82a42;};}function handleError(_0x4d8191,_0x2c5025){_0x2c5025=_0x2c5025||0x1f4;return function(_0x37e72d){logger[_0x4e5d('0x2c')](_0x37e72d[_0x4e5d('0x2d')]);if(_0x37e72d[_0x4e5d('0x2e')]){delete _0x37e72d[_0x4e5d('0x2e')];}_0x4d8191[_0x4e5d('0x20')](_0x2c5025)[_0x4e5d('0x2f')](_0x37e72d);};}exports[_0x4e5d('0x30')]=function(_0x5bff47,_0x188f70){var _0x5f2118={},_0x444935={},_0x485d0e={'count':0x0,'rows':[]};var _0x277968=_[_0x4e5d('0x31')](db[_0x4e5d('0x32')][_0x4e5d('0x33')],function(_0x5b4d14){return{'name':_0x5b4d14[_0x4e5d('0x34')],'type':_0x5b4d14[_0x4e5d('0x35')][_0x4e5d('0x36')]};});_0x444935[_0x4e5d('0x37')]=_[_0x4e5d('0x31')](_0x277968,_0x4e5d('0x2e'));_0x444935[_0x4e5d('0x38')]=_[_0x4e5d('0x39')](_0x5bff47[_0x4e5d('0x38')]);_0x444935[_0x4e5d('0x3a')]=_[_0x4e5d('0x3b')](_0x444935[_0x4e5d('0x37')],_0x444935['query']);_0x5f2118['attributes']=_[_0x4e5d('0x3b')](_0x444935[_0x4e5d('0x37')],qs[_0x4e5d('0x3c')](_0x5bff47[_0x4e5d('0x38')][_0x4e5d('0x3c')]));_0x5f2118[_0x4e5d('0x3d')]=_0x5f2118[_0x4e5d('0x3d')][_0x4e5d('0x3e')]?_0x5f2118[_0x4e5d('0x3d')]:_0x444935['model'];if(!_0x5bff47[_0x4e5d('0x38')][_0x4e5d('0x3f')](_0x4e5d('0x40'))){_0x5f2118['limit']=qs[_0x4e5d('0x24')](_0x5bff47[_0x4e5d('0x38')]['limit']);_0x5f2118[_0x4e5d('0x22')]=qs['offset'](_0x5bff47[_0x4e5d('0x38')]['offset']);}_0x5f2118[_0x4e5d('0x41')]=qs[_0x4e5d('0x42')](_0x5bff47[_0x4e5d('0x38')][_0x4e5d('0x42')]);_0x5f2118[_0x4e5d('0x43')]=qs['filters'](_[_0x4e5d('0x44')](_0x5bff47[_0x4e5d('0x38')],_0x444935[_0x4e5d('0x3a')]),_0x277968);if(_0x5bff47[_0x4e5d('0x38')]['filter']){_0x5f2118[_0x4e5d('0x43')]=_[_0x4e5d('0x45')](_0x5f2118['where'],{'$or':_['map'](_0x277968,function(_0x4f2461){if(_0x4f2461[_0x4e5d('0x35')]!==_0x4e5d('0x46')){var _0x21b93e={};_0x21b93e[_0x4f2461['name']]={'$like':'%'+_0x5bff47[_0x4e5d('0x38')][_0x4e5d('0x47')]+'%'};return _0x21b93e;}})});}_0x5f2118=_[_0x4e5d('0x45')]({},_0x5f2118,_0x5bff47[_0x4e5d('0x48')]);var _0x57d4bd={'where':_0x5f2118['where']};return db['ChatInternalMessage']['count'](_0x57d4bd)[_0x4e5d('0x2a')](function(_0x5cc450){_0x485d0e[_0x4e5d('0x25')]=_0x5cc450;if(_0x5bff47['query'][_0x4e5d('0x49')]){_0x5f2118[_0x4e5d('0x4a')]=[{'all':!![]}];}return db[_0x4e5d('0x32')]['findAll'](_0x5f2118);})[_0x4e5d('0x2a')](function(_0x3bb352){_0x485d0e[_0x4e5d('0x4b')]=_0x3bb352;return _0x485d0e;})[_0x4e5d('0x2a')](respondWithFilteredResult(_0x188f70,_0x5f2118))[_0x4e5d('0x4c')](handleError(_0x188f70,null));};exports[_0x4e5d('0x4d')]=function(_0x283357,_0x48d057){var _0x27c695={'raw':!![],'where':{'id':_0x283357['params']['id']}},_0x52486f={};_0x52486f[_0x4e5d('0x37')]=_[_0x4e5d('0x39')](db[_0x4e5d('0x32')]['rawAttributes']);_0x52486f[_0x4e5d('0x38')]=_[_0x4e5d('0x39')](_0x283357[_0x4e5d('0x38')]);_0x52486f[_0x4e5d('0x3a')]=_[_0x4e5d('0x3b')](_0x52486f['model'],_0x52486f['query']);_0x27c695[_0x4e5d('0x3d')]=_[_0x4e5d('0x3b')](_0x52486f['model'],qs[_0x4e5d('0x3c')](_0x283357['query']['fields']));_0x27c695[_0x4e5d('0x3d')]=_0x27c695[_0x4e5d('0x3d')][_0x4e5d('0x3e')]?_0x27c695[_0x4e5d('0x3d')]:_0x52486f[_0x4e5d('0x37')];if(_0x283357['query'][_0x4e5d('0x49')]){_0x27c695[_0x4e5d('0x4a')]=[{'all':!![]}];}_0x27c695=_['merge']({},_0x27c695,_0x283357[_0x4e5d('0x48')]);return db['ChatInternalMessage']['find'](_0x27c695)[_0x4e5d('0x2a')](handleEntityNotFound(_0x48d057,null))[_0x4e5d('0x2a')](respondWithResult(_0x48d057,null))['catch'](handleError(_0x48d057,null));};exports[_0x4e5d('0x4e')]=function(_0x262ecc,_0x5bebc0){return db[_0x4e5d('0x32')]['create'](_0x262ecc[_0x4e5d('0x4f')],{})[_0x4e5d('0x2a')](respondWithResult(_0x5bebc0,0xc9))[_0x4e5d('0x4c')](handleError(_0x5bebc0,null));};exports[_0x4e5d('0x29')]=function(_0x2d794d,_0x5f4ccf){if(_0x2d794d[_0x4e5d('0x4f')]['id']){delete _0x2d794d[_0x4e5d('0x4f')]['id'];}return db[_0x4e5d('0x32')][_0x4e5d('0x50')]({'where':{'id':_0x2d794d['params']['id']}})[_0x4e5d('0x2a')](handleEntityNotFound(_0x5f4ccf,null))[_0x4e5d('0x2a')](saveUpdates(_0x2d794d['body'],null))[_0x4e5d('0x2a')](respondWithResult(_0x5f4ccf,null))[_0x4e5d('0x4c')](handleError(_0x5f4ccf,null));};exports[_0x4e5d('0x2b')]=function(_0xdb4aab,_0x2bcc0d){return db['ChatInternalMessage'][_0x4e5d('0x50')]({'where':{'id':_0xdb4aab['params']['id']}})['then'](handleEntityNotFound(_0x2bcc0d,null))[_0x4e5d('0x2a')](removeEntity(_0x2bcc0d,null))['catch'](handleError(_0x2bcc0d,null));};exports[_0x4e5d('0x51')]=function(_0xba3bf2,_0x213251){return db['ChatInternalMessage'][_0x4e5d('0x51')]()[_0x4e5d('0x2a')](respondWithResult(_0x213251,null))[_0x4e5d('0x4c')](handleError(_0x213251,null));};function wrapUser(_0x17209c){return function(_0x1a7491){var _0x18e656=_0x17209c[_0x4e5d('0x52')]({'plain':!![]});_0x18e656[_0x4e5d('0x53')]=[];if(_0x1a7491){_0x18e656[_0x4e5d('0x54')]=_0x1a7491['createdAt'];_0x18e656[_0x4e5d('0x55')]=_0x1a7491;}else{_0x18e656['lastMessageAt']=moment(0x0)[_0x4e5d('0x56')](_0x4e5d('0x57'));_0x18e656[_0x4e5d('0x55')]={};}return _0x18e656;};}exports[_0x4e5d('0x58')]=function(_0x32d88b,_0x239121,_0x526aa3){var _0x5ce0ab=[];return db['User']['findAll']({'where':{'role':[_0x4e5d('0x59'),_0x4e5d('0x5a'),_0x4e5d('0x5b')]},'attributes':['id',_0x4e5d('0x5c'),_0x4e5d('0x2e'),'role',_0x4e5d('0x5d'),_0x4e5d('0x5e'),_0x4e5d('0x5f'),'lastLoginAt',_0x4e5d('0x60'),_0x4e5d('0x61')],'order':qs[_0x4e5d('0x42')](_0x4e5d('0x62')),'include':[{'model':db[_0x4e5d('0x63')],'as':_0x4e5d('0x64'),'attributes':['id',_0x4e5d('0x2e'),'type'],'through':{'attributes':[]}}]})[_0x4e5d('0x2a')](function(_0x1f88bf){if(_0x1f88bf){var _0x245cef=_0x32d88b[_0x4e5d('0x5b')][_0x4e5d('0x52')]({'plain':!![]});if(!_0x245cef)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x245cef[_0x4e5d('0x65')]===_0x4e5d('0x5b')){return db[_0x4e5d('0x66')][_0x4e5d('0x50')]({'where':{'name':_0x4e5d('0x67'),'userProfileId':_0x245cef['userProfileId']},'raw':!![]})[_0x4e5d('0x2a')](function(_0x30082){if(_0x30082){if(_0x30082[_0x4e5d('0x68')]===0x0){return db[_0x4e5d('0x69')][_0x4e5d('0x6a')]({'where':{'sectionId':_0x30082['id']},'raw':!![]})[_0x4e5d('0x2a')](function(_0x4796ea){var _0x573e20=[];if(_[_0x4e5d('0x6b')](_0x4796ea)){_0x573e20=_[_0x4e5d('0x47')](_0x1f88bf,function(_0x535436){if(_0x535436[_0x4e5d('0x65')]!=='agent'){return _0x535436;}});return _0x573e20;}else{for(var _0x8024ff=0x0;_0x8024ff<_0x1f88bf[_0x4e5d('0x3e')];_0x8024ff++){var _0x38dd41=_0x1f88bf[_0x8024ff];if(_0x38dd41[_0x4e5d('0x65')]===_0x4e5d('0x59')){var _0x451312=_[_0x4e5d('0x50')](_0x4796ea,function(_0x27c3bb){return _0x27c3bb[_0x4e5d('0x6c')]===_0x38dd41['id'];});if(_0x451312)_0x573e20[_0x4e5d('0x6d')](_0x38dd41);}else{_0x573e20[_0x4e5d('0x6d')](_0x38dd41);}}return _0x573e20;}});}else{return _0x1f88bf;}}else{var _0x244029=_['filter'](_0x1f88bf,function(_0x8dac1a){if(_0x8dac1a[_0x4e5d('0x65')]!==_0x4e5d('0x59')){return _0x8dac1a;}});return _0x244029;}});}else{return _0x1f88bf;}}})[_0x4e5d('0x2a')](function(_0x1ab69d){var _0x2826d7=[];if(_0x1ab69d){_0x5ce0ab=_0x1ab69d;for(var _0x3732bb=0x0;_0x3732bb<_0x1ab69d['length'];_0x3732bb+=0x1){_0x5ce0ab[_0x3732bb]['messages']=[];_0x2826d7[_0x4e5d('0x6d')](db[_0x4e5d('0x32')][_0x4e5d('0x50')]({'where':{'FromId':[_0x32d88b[_0x4e5d('0x5b')]['id'],_0x1ab69d[_0x3732bb]['id']],'ToId':[_0x32d88b[_0x4e5d('0x5b')]['id'],_0x1ab69d[_0x3732bb]['id']],'ChatGroupId':null},'attributes':['id',_0x4e5d('0x6e'),_0x4e5d('0x6f'),_0x4e5d('0x70'),'read',_0x4e5d('0x4f')],'order':qs[_0x4e5d('0x42')](_0x4e5d('0x71')),'limit':0x1,'offset':0x0,'raw':!![]})[_0x4e5d('0x2a')](wrapUser(_0x1ab69d[_0x3732bb])));}return BPromise[_0x4e5d('0x72')](_0x2826d7);}return[];})[_0x4e5d('0x2a')](function(_0x5beb2c){return{'rows':_0x5beb2c,'count':_0x5beb2c[_0x4e5d('0x3e')]};})['then'](respondWithResult(_0x239121,null))[_0x4e5d('0x4c')](handleError(_0x239121,null));};