9ef6a7c09e82638bf5cf192e5df5e65095e61782
[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 _0xc74f=['show','params','length','includeAll','find','create','body','describe','messages','lastMessageAt','createdAt','lastMessage','format','YYYY-MM-DD\x20HH:mm:ss','getUsers','User','admin','user','fullname','role','userpic','online','lastLoginAt','showWebBar','-online','VoiceQueues','UserProfileSection','Agents','userProfileId','autoAssociation','isEmpty','agent','resourceId','push','FromId','ToId','-createdAt','all','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','to-csv','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','localhost','socket.io-emitter','register','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','update','then','destroy','error','stack','name','send','index','ChatInternalMessage','rawAttributes','fieldName','type','model','map','keys','query','filters','attributes','intersection','fields','hasOwnProperty','sort','where','pick','filter','merge','VIRTUAL','options','include','findAll','rows','catch'];(function(_0x5e35ef,_0x501395){var _0x30cdc0=function(_0xf1133f){while(--_0xf1133f){_0x5e35ef['push'](_0x5e35ef['shift']());}};_0x30cdc0(++_0x501395);}(_0xc74f,0x173));var _0xfc74=function(_0x5977ee,_0x206476){_0x5977ee=_0x5977ee-0x0;var _0x263a34=_0xc74f[_0x5977ee];return _0x263a34;};'use strict';var emlformat=require(_0xfc74('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xfc74('0x1'));var jsonpatch=require(_0xfc74('0x2'));var rp=require(_0xfc74('0x3'));var moment=require(_0xfc74('0x4'));var BPromise=require(_0xfc74('0x5'));var Mustache=require(_0xfc74('0x6'));var util=require(_0xfc74('0x7'));var path=require('path');var sox=require(_0xfc74('0x8'));var csv=require(_0xfc74('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xfc74('0xa'));var squel=require(_0xfc74('0xb'));var crypto=require(_0xfc74('0xc'));var jsforce=require(_0xfc74('0xd'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0xfc74('0xe'));var Papa=require(_0xfc74('0xf'));var Redis=require(_0xfc74('0x10'));var authService=require(_0xfc74('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0xfc74('0x12'));var hardwareService=require(_0xfc74('0x13'));var logger=require(_0xfc74('0x14'))(_0xfc74('0x15'));var utils=require('../../config/utils');var config=require(_0xfc74('0x16'));var licenseUtil=require(_0xfc74('0x17'));var db=require(_0xfc74('0x18'))['db'];config['redis']=_['defaults'](config[_0xfc74('0x19')],{'host':_0xfc74('0x1a'),'port':0x18eb});var socket=require(_0xfc74('0x1b'))(new Redis(config['redis']));require('./chatInternalMessage.socket')[_0xfc74('0x1c')](socket);function respondWithStatusCode(_0x506735,_0x27bce4){_0x27bce4=_0x27bce4||0xcc;return function(_0x655142){if(_0x655142){return _0x506735[_0xfc74('0x1d')](_0x27bce4);}return _0x506735[_0xfc74('0x1e')](_0x27bce4)[_0xfc74('0x1f')]();};}function respondWithResult(_0x3a4bac,_0x18a618){_0x18a618=_0x18a618||0xc8;return function(_0x1bd90f){if(_0x1bd90f){return _0x3a4bac[_0xfc74('0x1e')](_0x18a618)[_0xfc74('0x20')](_0x1bd90f);}};}function respondWithFilteredResult(_0x23535a,_0x4feec3){return function(_0x121516){if(_0x121516){var _0xd18a09=typeof _0x4feec3[_0xfc74('0x21')]===_0xfc74('0x22')&&typeof _0x4feec3[_0xfc74('0x23')]===_0xfc74('0x22');var _0x495859=_0x121516[_0xfc74('0x24')];var _0x576aaa=_0xd18a09?0x0:_0x4feec3[_0xfc74('0x21')];var _0xe27878=_0xd18a09?_0x121516['count']:_0x4feec3[_0xfc74('0x21')]+_0x4feec3[_0xfc74('0x23')];var _0x5ef034;if(_0xe27878>=_0x495859){_0xe27878=_0x495859;_0x5ef034=0xc8;}else{_0x5ef034=0xce;}_0x23535a['status'](_0x5ef034);return _0x23535a[_0xfc74('0x25')](_0xfc74('0x26'),_0x576aaa+'-'+_0xe27878+'/'+_0x495859)['json'](_0x121516);}return null;};}function patchUpdates(_0x255ee6){return function(_0x2d4af1){try{jsonpatch[_0xfc74('0x27')](_0x2d4af1,_0x255ee6,!![]);}catch(_0x2df28a){return BPromise[_0xfc74('0x28')](_0x2df28a);}return _0x2d4af1['save']();};}function saveUpdates(_0xb0148e,_0x4fcc76){return function(_0x2aeee9){if(_0x2aeee9){return _0x2aeee9[_0xfc74('0x29')](_0xb0148e)[_0xfc74('0x2a')](function(_0x22579b){return _0x22579b;});}return null;};}function removeEntity(_0x9a7984,_0x18b9db){return function(_0x2fd9f0){if(_0x2fd9f0){return _0x2fd9f0[_0xfc74('0x2b')]()[_0xfc74('0x2a')](function(){_0x9a7984[_0xfc74('0x1e')](0xcc)[_0xfc74('0x1f')]();});}};}function handleEntityNotFound(_0x379681,_0xfa3589){return function(_0x3dc4a2){if(!_0x3dc4a2){_0x379681[_0xfc74('0x1d')](0x194);}return _0x3dc4a2;};}function handleError(_0x580362,_0x3419db){_0x3419db=_0x3419db||0x1f4;return function(_0x2f7031){logger[_0xfc74('0x2c')](_0x2f7031[_0xfc74('0x2d')]);if(_0x2f7031['name']){delete _0x2f7031[_0xfc74('0x2e')];}_0x580362['status'](_0x3419db)[_0xfc74('0x2f')](_0x2f7031);};}exports[_0xfc74('0x30')]=function(_0x54eb68,_0xb483a){var _0x311e4c={},_0x5471f4={},_0x1798d6={'count':0x0,'rows':[]};var _0x52e22d=_['map'](db[_0xfc74('0x31')][_0xfc74('0x32')],function(_0x4ce959){return{'name':_0x4ce959[_0xfc74('0x33')],'type':_0x4ce959[_0xfc74('0x34')]['key']};});_0x5471f4[_0xfc74('0x35')]=_[_0xfc74('0x36')](_0x52e22d,_0xfc74('0x2e'));_0x5471f4['query']=_[_0xfc74('0x37')](_0x54eb68[_0xfc74('0x38')]);_0x5471f4[_0xfc74('0x39')]=_['intersection'](_0x5471f4['model'],_0x5471f4[_0xfc74('0x38')]);_0x311e4c[_0xfc74('0x3a')]=_[_0xfc74('0x3b')](_0x5471f4[_0xfc74('0x35')],qs[_0xfc74('0x3c')](_0x54eb68[_0xfc74('0x38')][_0xfc74('0x3c')]));_0x311e4c[_0xfc74('0x3a')]=_0x311e4c[_0xfc74('0x3a')]['length']?_0x311e4c[_0xfc74('0x3a')]:_0x5471f4[_0xfc74('0x35')];if(!_0x54eb68[_0xfc74('0x38')][_0xfc74('0x3d')]('nolimit')){_0x311e4c[_0xfc74('0x23')]=qs[_0xfc74('0x23')](_0x54eb68[_0xfc74('0x38')]['limit']);_0x311e4c[_0xfc74('0x21')]=qs[_0xfc74('0x21')](_0x54eb68[_0xfc74('0x38')][_0xfc74('0x21')]);}_0x311e4c['order']=qs[_0xfc74('0x3e')](_0x54eb68['query'][_0xfc74('0x3e')]);_0x311e4c[_0xfc74('0x3f')]=qs['filters'](_[_0xfc74('0x40')](_0x54eb68[_0xfc74('0x38')],_0x5471f4['filters']),_0x52e22d);if(_0x54eb68[_0xfc74('0x38')][_0xfc74('0x41')]){_0x311e4c[_0xfc74('0x3f')]=_[_0xfc74('0x42')](_0x311e4c[_0xfc74('0x3f')],{'$or':_['map'](_0x52e22d,function(_0x18d954){if(_0x18d954[_0xfc74('0x34')]!==_0xfc74('0x43')){var _0x506147={};_0x506147[_0x18d954[_0xfc74('0x2e')]]={'$like':'%'+_0x54eb68[_0xfc74('0x38')][_0xfc74('0x41')]+'%'};return _0x506147;}})});}_0x311e4c=_['merge']({},_0x311e4c,_0x54eb68[_0xfc74('0x44')]);var _0x479a44={'where':_0x311e4c[_0xfc74('0x3f')]};return db[_0xfc74('0x31')][_0xfc74('0x24')](_0x479a44)[_0xfc74('0x2a')](function(_0x39ab3c){_0x1798d6['count']=_0x39ab3c;if(_0x54eb68[_0xfc74('0x38')]['includeAll']){_0x311e4c[_0xfc74('0x45')]=[{'all':!![]}];}return db[_0xfc74('0x31')][_0xfc74('0x46')](_0x311e4c);})['then'](function(_0x390144){_0x1798d6[_0xfc74('0x47')]=_0x390144;return _0x1798d6;})[_0xfc74('0x2a')](respondWithFilteredResult(_0xb483a,_0x311e4c))[_0xfc74('0x48')](handleError(_0xb483a,null));};exports[_0xfc74('0x49')]=function(_0xcd4b83,_0x25aca5){var _0xf128fd={'raw':!![],'where':{'id':_0xcd4b83[_0xfc74('0x4a')]['id']}},_0x2e4e65={};_0x2e4e65[_0xfc74('0x35')]=_['keys'](db[_0xfc74('0x31')][_0xfc74('0x32')]);_0x2e4e65[_0xfc74('0x38')]=_['keys'](_0xcd4b83[_0xfc74('0x38')]);_0x2e4e65[_0xfc74('0x39')]=_['intersection'](_0x2e4e65[_0xfc74('0x35')],_0x2e4e65['query']);_0xf128fd[_0xfc74('0x3a')]=_[_0xfc74('0x3b')](_0x2e4e65[_0xfc74('0x35')],qs[_0xfc74('0x3c')](_0xcd4b83[_0xfc74('0x38')][_0xfc74('0x3c')]));_0xf128fd['attributes']=_0xf128fd[_0xfc74('0x3a')][_0xfc74('0x4b')]?_0xf128fd[_0xfc74('0x3a')]:_0x2e4e65[_0xfc74('0x35')];if(_0xcd4b83['query'][_0xfc74('0x4c')]){_0xf128fd[_0xfc74('0x45')]=[{'all':!![]}];}_0xf128fd=_['merge']({},_0xf128fd,_0xcd4b83[_0xfc74('0x44')]);return db['ChatInternalMessage'][_0xfc74('0x4d')](_0xf128fd)[_0xfc74('0x2a')](handleEntityNotFound(_0x25aca5,null))[_0xfc74('0x2a')](respondWithResult(_0x25aca5,null))[_0xfc74('0x48')](handleError(_0x25aca5,null));};exports[_0xfc74('0x4e')]=function(_0x3f7625,_0x4f5896){return db['ChatInternalMessage']['create'](_0x3f7625[_0xfc74('0x4f')],{})[_0xfc74('0x2a')](respondWithResult(_0x4f5896,0xc9))['catch'](handleError(_0x4f5896,null));};exports[_0xfc74('0x29')]=function(_0x590e40,_0x3d4e2d){if(_0x590e40[_0xfc74('0x4f')]['id']){delete _0x590e40[_0xfc74('0x4f')]['id'];}return db['ChatInternalMessage'][_0xfc74('0x4d')]({'where':{'id':_0x590e40[_0xfc74('0x4a')]['id']}})['then'](handleEntityNotFound(_0x3d4e2d,null))[_0xfc74('0x2a')](saveUpdates(_0x590e40[_0xfc74('0x4f')],null))[_0xfc74('0x2a')](respondWithResult(_0x3d4e2d,null))[_0xfc74('0x48')](handleError(_0x3d4e2d,null));};exports[_0xfc74('0x2b')]=function(_0x2aeb0f,_0x4cdf58){return db[_0xfc74('0x31')]['find']({'where':{'id':_0x2aeb0f[_0xfc74('0x4a')]['id']}})[_0xfc74('0x2a')](handleEntityNotFound(_0x4cdf58,null))[_0xfc74('0x2a')](removeEntity(_0x4cdf58,null))[_0xfc74('0x48')](handleError(_0x4cdf58,null));};exports[_0xfc74('0x50')]=function(_0x2c9119,_0x3220f5){return db[_0xfc74('0x31')][_0xfc74('0x50')]()[_0xfc74('0x2a')](respondWithResult(_0x3220f5,null))[_0xfc74('0x48')](handleError(_0x3220f5,null));};function wrapUser(_0x37b452){return function(_0x32ba08){var _0x141a1b=_0x37b452['get']({'plain':!![]});_0x141a1b[_0xfc74('0x51')]=[];if(_0x32ba08){_0x141a1b[_0xfc74('0x52')]=_0x32ba08[_0xfc74('0x53')];_0x141a1b[_0xfc74('0x54')]=_0x32ba08;}else{_0x141a1b[_0xfc74('0x52')]=moment(0x0)[_0xfc74('0x55')](_0xfc74('0x56'));_0x141a1b[_0xfc74('0x54')]={};}return _0x141a1b;};}exports[_0xfc74('0x57')]=function(_0x219c6a,_0x28b2d6,_0x5dbe28){var _0x25bb84=[];return db[_0xfc74('0x58')]['findAll']({'where':{'role':['agent',_0xfc74('0x59'),_0xfc74('0x5a')]},'attributes':['id',_0xfc74('0x5b'),_0xfc74('0x2e'),_0xfc74('0x5c'),_0xfc74('0x5d'),'internal',_0xfc74('0x5e'),_0xfc74('0x5f'),'allowmessenger',_0xfc74('0x60')],'order':qs[_0xfc74('0x3e')](_0xfc74('0x61')),'include':[{'model':db['VoiceQueue'],'as':_0xfc74('0x62'),'attributes':['id',_0xfc74('0x2e'),_0xfc74('0x34')],'through':{'attributes':[]}}]})[_0xfc74('0x2a')](function(_0x57d972){if(_0x57d972){var _0x174d74=_0x219c6a['user']['get']({'plain':!![]});if(!_0x174d74)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x174d74[_0xfc74('0x5c')]==='user'){return db[_0xfc74('0x63')][_0xfc74('0x4d')]({'where':{'name':_0xfc74('0x64'),'userProfileId':_0x174d74[_0xfc74('0x65')]},'raw':!![]})['then'](function(_0x4862f8){if(_0x4862f8){if(_0x4862f8[_0xfc74('0x66')]===0x0){return db['UserProfileResource'][_0xfc74('0x46')]({'where':{'sectionId':_0x4862f8['id']},'raw':!![]})[_0xfc74('0x2a')](function(_0x282b53){var _0x191000=[];if(_[_0xfc74('0x67')](_0x282b53)){_0x191000=_[_0xfc74('0x41')](_0x57d972,function(_0xd19011){if(_0xd19011[_0xfc74('0x5c')]!==_0xfc74('0x68')){return _0xd19011;}});return _0x191000;}else{for(var _0x44c667=0x0;_0x44c667<_0x57d972[_0xfc74('0x4b')];_0x44c667++){var _0x545633=_0x57d972[_0x44c667];if(_0x545633[_0xfc74('0x5c')]===_0xfc74('0x68')){var _0x1a0d30=_[_0xfc74('0x4d')](_0x282b53,function(_0x1f75e5){return _0x1f75e5[_0xfc74('0x69')]===_0x545633['id'];});if(_0x1a0d30)_0x191000[_0xfc74('0x6a')](_0x545633);}else{_0x191000[_0xfc74('0x6a')](_0x545633);}}return _0x191000;}});}else{return _0x57d972;}}else{var _0x234e21=_[_0xfc74('0x41')](_0x57d972,function(_0x1c394f){if(_0x1c394f['role']!==_0xfc74('0x68')){return _0x1c394f;}});return _0x234e21;}});}else{return _0x57d972;}}})[_0xfc74('0x2a')](function(_0x174e24){var _0x51e86f=[];if(_0x174e24){_0x25bb84=_0x174e24;for(var _0x368710=0x0;_0x368710<_0x174e24[_0xfc74('0x4b')];_0x368710+=0x1){_0x25bb84[_0x368710][_0xfc74('0x51')]=[];_0x51e86f[_0xfc74('0x6a')](db[_0xfc74('0x31')][_0xfc74('0x4d')]({'where':{'FromId':[_0x219c6a[_0xfc74('0x5a')]['id'],_0x174e24[_0x368710]['id']],'ToId':[_0x219c6a[_0xfc74('0x5a')]['id'],_0x174e24[_0x368710]['id']],'ChatGroupId':null},'attributes':['id',_0xfc74('0x53'),_0xfc74('0x6b'),_0xfc74('0x6c'),'read',_0xfc74('0x4f')],'order':qs[_0xfc74('0x3e')](_0xfc74('0x6d')),'limit':0x1,'offset':0x0,'raw':!![]})[_0xfc74('0x2a')](wrapUser(_0x174e24[_0x368710])));}return BPromise[_0xfc74('0x6e')](_0x51e86f);}return[];})[_0xfc74('0x2a')](function(_0x100155){return{'rows':_0x100155,'count':_0x100155[_0xfc74('0x4b')]};})[_0xfc74('0x2a')](respondWithResult(_0x28b2d6,null))[_0xfc74('0x48')](handleError(_0x28b2d6,null));};