Built motion from commit b0a9a629.|2.6.33
[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 _0x5b86=['status','end','json','offset','limit','undefined','set','Content-Range','reject','save','then','stack','name','send','index','map','ChatInternalMessage','fieldName','type','model','filters','intersection','query','fields','attributes','length','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','count','includeAll','findAll','rows','show','keys','find','catch','create','update','body','params','destroy','describe','get','messages','lastMessageAt','createdAt','format','lastMessage','agent','admin','user','fullname','role','userpic','internal','online','lastLoginAt','allowmessenger','showWebBar','-online','VoiceQueue','VoiceQueues','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Agents','userProfileId','autoAssociation','UserProfileResource','isEmpty','push','FromId','ToId','read','-createdAt','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','squel','crypto','desk.js','to-csv','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','defaults','redis','localhost','socket.io-emitter','register','sendStatus'];(function(_0x287537,_0x8e1eaf){var _0x390a32=function(_0x4c5032){while(--_0x4c5032){_0x287537['push'](_0x287537['shift']());}};_0x390a32(++_0x8e1eaf);}(_0x5b86,0x19e));var _0x65b8=function(_0x3f3523,_0x22f0c0){_0x3f3523=_0x3f3523-0x0;var _0x342469=_0x5b86[_0x3f3523];return _0x342469;};'use strict';var emlformat=require(_0x65b8('0x0'));var rimraf=require(_0x65b8('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x65b8('0x2'));var rp=require('request-promise');var moment=require(_0x65b8('0x3'));var BPromise=require(_0x65b8('0x4'));var Mustache=require(_0x65b8('0x5'));var util=require(_0x65b8('0x6'));var path=require(_0x65b8('0x7'));var sox=require(_0x65b8('0x8'));var csv=require('to-csv');var ejs=require(_0x65b8('0x9'));var fs=require('fs');var fs_extra=require(_0x65b8('0xa'));var _=require('lodash');var squel=require(_0x65b8('0xb'));var crypto=require(_0x65b8('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x65b8('0xd'));var toCsv=require(_0x65b8('0xe'));var querystring=require('querystring');var Papa=require(_0x65b8('0xf'));var Redis=require('ioredis');var authService=require(_0x65b8('0x10'));var qs=require(_0x65b8('0x11'));var as=require(_0x65b8('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x65b8('0x13'))(_0x65b8('0x14'));var utils=require(_0x65b8('0x15'));var config=require(_0x65b8('0x16'));var licenseUtil=require('../../config/license/util');var db=require(_0x65b8('0x17'))['db'];config['redis']=_[_0x65b8('0x18')](config[_0x65b8('0x19')],{'host':_0x65b8('0x1a'),'port':0x18eb});var socket=require(_0x65b8('0x1b'))(new Redis(config[_0x65b8('0x19')]));require('./chatInternalMessage.socket')[_0x65b8('0x1c')](socket);function respondWithStatusCode(_0x1c2697,_0x2a8b38){_0x2a8b38=_0x2a8b38||0xcc;return function(_0x4ac5fb){if(_0x4ac5fb){return _0x1c2697[_0x65b8('0x1d')](_0x2a8b38);}return _0x1c2697[_0x65b8('0x1e')](_0x2a8b38)[_0x65b8('0x1f')]();};}function respondWithResult(_0x436c77,_0x1a639c){_0x1a639c=_0x1a639c||0xc8;return function(_0x446925){if(_0x446925){return _0x436c77['status'](_0x1a639c)[_0x65b8('0x20')](_0x446925);}};}function respondWithFilteredResult(_0x22e5f1,_0x47a968){return function(_0x129b52){if(_0x129b52){var _0x23bbb9=typeof _0x47a968[_0x65b8('0x21')]==='undefined'&&typeof _0x47a968[_0x65b8('0x22')]===_0x65b8('0x23');var _0x171212=_0x129b52['count'];var _0x4cad9b=_0x23bbb9?0x0:_0x47a968['offset'];var _0x4f36de=_0x23bbb9?_0x129b52['count']:_0x47a968[_0x65b8('0x21')]+_0x47a968['limit'];var _0x3759b9;if(_0x4f36de>=_0x171212){_0x4f36de=_0x171212;_0x3759b9=0xc8;}else{_0x3759b9=0xce;}_0x22e5f1[_0x65b8('0x1e')](_0x3759b9);return _0x22e5f1[_0x65b8('0x24')](_0x65b8('0x25'),_0x4cad9b+'-'+_0x4f36de+'/'+_0x171212)[_0x65b8('0x20')](_0x129b52);}return null;};}function patchUpdates(_0x58658c){return function(_0x420475){try{jsonpatch['apply'](_0x420475,_0x58658c,!![]);}catch(_0xb46236){return BPromise[_0x65b8('0x26')](_0xb46236);}return _0x420475[_0x65b8('0x27')]();};}function saveUpdates(_0x37e564,_0x2909db){return function(_0x453faa){if(_0x453faa){return _0x453faa['update'](_0x37e564)[_0x65b8('0x28')](function(_0x24019f){return _0x24019f;});}return null;};}function removeEntity(_0x5deb9c,_0x525c9c){return function(_0x336bb3){if(_0x336bb3){return _0x336bb3['destroy']()[_0x65b8('0x28')](function(){_0x5deb9c[_0x65b8('0x1e')](0xcc)['end']();});}};}function handleEntityNotFound(_0x299a78,_0x2f7ddf){return function(_0x36197c){if(!_0x36197c){_0x299a78[_0x65b8('0x1d')](0x194);}return _0x36197c;};}function handleError(_0x671700,_0x38f0b8){_0x38f0b8=_0x38f0b8||0x1f4;return function(_0x7649f8){logger['error'](_0x7649f8[_0x65b8('0x29')]);if(_0x7649f8[_0x65b8('0x2a')]){delete _0x7649f8[_0x65b8('0x2a')];}_0x671700[_0x65b8('0x1e')](_0x38f0b8)[_0x65b8('0x2b')](_0x7649f8);};}exports[_0x65b8('0x2c')]=function(_0x513c01,_0x58e356){var _0x214e8c={},_0x2bc5e4={},_0x16b6e3={'count':0x0,'rows':[]};var _0x9bd583=_[_0x65b8('0x2d')](db[_0x65b8('0x2e')]['rawAttributes'],function(_0x152d71){return{'name':_0x152d71[_0x65b8('0x2f')],'type':_0x152d71[_0x65b8('0x30')]['key']};});_0x2bc5e4[_0x65b8('0x31')]=_[_0x65b8('0x2d')](_0x9bd583,_0x65b8('0x2a'));_0x2bc5e4['query']=_['keys'](_0x513c01['query']);_0x2bc5e4[_0x65b8('0x32')]=_[_0x65b8('0x33')](_0x2bc5e4['model'],_0x2bc5e4[_0x65b8('0x34')]);_0x214e8c['attributes']=_[_0x65b8('0x33')](_0x2bc5e4[_0x65b8('0x31')],qs[_0x65b8('0x35')](_0x513c01[_0x65b8('0x34')][_0x65b8('0x35')]));_0x214e8c['attributes']=_0x214e8c[_0x65b8('0x36')][_0x65b8('0x37')]?_0x214e8c[_0x65b8('0x36')]:_0x2bc5e4[_0x65b8('0x31')];if(!_0x513c01[_0x65b8('0x34')]['hasOwnProperty'](_0x65b8('0x38'))){_0x214e8c[_0x65b8('0x22')]=qs[_0x65b8('0x22')](_0x513c01[_0x65b8('0x34')][_0x65b8('0x22')]);_0x214e8c[_0x65b8('0x21')]=qs[_0x65b8('0x21')](_0x513c01[_0x65b8('0x34')][_0x65b8('0x21')]);}_0x214e8c[_0x65b8('0x39')]=qs[_0x65b8('0x3a')](_0x513c01[_0x65b8('0x34')][_0x65b8('0x3a')]);_0x214e8c[_0x65b8('0x3b')]=qs[_0x65b8('0x32')](_[_0x65b8('0x3c')](_0x513c01[_0x65b8('0x34')],_0x2bc5e4['filters']),_0x9bd583);if(_0x513c01[_0x65b8('0x34')]['filter']){_0x214e8c[_0x65b8('0x3b')]=_[_0x65b8('0x3d')](_0x214e8c['where'],{'$or':_[_0x65b8('0x2d')](_0x9bd583,function(_0x3a5c6e){if(_0x3a5c6e[_0x65b8('0x30')]!==_0x65b8('0x3e')){var _0x40a207={};_0x40a207[_0x3a5c6e[_0x65b8('0x2a')]]={'$like':'%'+_0x513c01['query'][_0x65b8('0x3f')]+'%'};return _0x40a207;}})});}_0x214e8c=_[_0x65b8('0x3d')]({},_0x214e8c,_0x513c01[_0x65b8('0x40')]);var _0x3f6e48={'where':_0x214e8c[_0x65b8('0x3b')]};return db[_0x65b8('0x2e')][_0x65b8('0x41')](_0x3f6e48)[_0x65b8('0x28')](function(_0xdcfe5e){_0x16b6e3[_0x65b8('0x41')]=_0xdcfe5e;if(_0x513c01[_0x65b8('0x34')][_0x65b8('0x42')]){_0x214e8c['include']=[{'all':!![]}];}return db[_0x65b8('0x2e')][_0x65b8('0x43')](_0x214e8c);})[_0x65b8('0x28')](function(_0x2b68c1){_0x16b6e3[_0x65b8('0x44')]=_0x2b68c1;return _0x16b6e3;})[_0x65b8('0x28')](respondWithFilteredResult(_0x58e356,_0x214e8c))['catch'](handleError(_0x58e356,null));};exports[_0x65b8('0x45')]=function(_0x45e950,_0x48fd07){var _0x3f95f7={'raw':!![],'where':{'id':_0x45e950['params']['id']}},_0x451257={};_0x451257[_0x65b8('0x31')]=_[_0x65b8('0x46')](db[_0x65b8('0x2e')]['rawAttributes']);_0x451257['query']=_[_0x65b8('0x46')](_0x45e950[_0x65b8('0x34')]);_0x451257[_0x65b8('0x32')]=_[_0x65b8('0x33')](_0x451257[_0x65b8('0x31')],_0x451257[_0x65b8('0x34')]);_0x3f95f7['attributes']=_[_0x65b8('0x33')](_0x451257[_0x65b8('0x31')],qs[_0x65b8('0x35')](_0x45e950['query'][_0x65b8('0x35')]));_0x3f95f7[_0x65b8('0x36')]=_0x3f95f7[_0x65b8('0x36')][_0x65b8('0x37')]?_0x3f95f7[_0x65b8('0x36')]:_0x451257[_0x65b8('0x31')];if(_0x45e950[_0x65b8('0x34')][_0x65b8('0x42')]){_0x3f95f7['include']=[{'all':!![]}];}_0x3f95f7=_[_0x65b8('0x3d')]({},_0x3f95f7,_0x45e950[_0x65b8('0x40')]);return db['ChatInternalMessage'][_0x65b8('0x47')](_0x3f95f7)['then'](handleEntityNotFound(_0x48fd07,null))[_0x65b8('0x28')](respondWithResult(_0x48fd07,null))[_0x65b8('0x48')](handleError(_0x48fd07,null));};exports[_0x65b8('0x49')]=function(_0x364b70,_0x4df5c4){return db['ChatInternalMessage'][_0x65b8('0x49')](_0x364b70['body'],{})['then'](respondWithResult(_0x4df5c4,0xc9))[_0x65b8('0x48')](handleError(_0x4df5c4,null));};exports[_0x65b8('0x4a')]=function(_0x5bbf8f,_0x2c3878){if(_0x5bbf8f[_0x65b8('0x4b')]['id']){delete _0x5bbf8f[_0x65b8('0x4b')]['id'];}return db['ChatInternalMessage'][_0x65b8('0x47')]({'where':{'id':_0x5bbf8f[_0x65b8('0x4c')]['id']}})[_0x65b8('0x28')](handleEntityNotFound(_0x2c3878,null))['then'](saveUpdates(_0x5bbf8f[_0x65b8('0x4b')],null))[_0x65b8('0x28')](respondWithResult(_0x2c3878,null))[_0x65b8('0x48')](handleError(_0x2c3878,null));};exports[_0x65b8('0x4d')]=function(_0x3e37f5,_0xab41a2){return db[_0x65b8('0x2e')][_0x65b8('0x47')]({'where':{'id':_0x3e37f5['params']['id']}})[_0x65b8('0x28')](handleEntityNotFound(_0xab41a2,null))[_0x65b8('0x28')](removeEntity(_0xab41a2,null))['catch'](handleError(_0xab41a2,null));};exports[_0x65b8('0x4e')]=function(_0x784c9c,_0x31d203){return db[_0x65b8('0x2e')][_0x65b8('0x4e')]()[_0x65b8('0x28')](respondWithResult(_0x31d203,null))['catch'](handleError(_0x31d203,null));};function wrapUser(_0xafaf44){return function(_0xd2551d){var _0x348662=_0xafaf44[_0x65b8('0x4f')]({'plain':!![]});_0x348662[_0x65b8('0x50')]=[];if(_0xd2551d){_0x348662[_0x65b8('0x51')]=_0xd2551d[_0x65b8('0x52')];_0x348662['lastMessage']=_0xd2551d;}else{_0x348662['lastMessageAt']=moment(0x0)[_0x65b8('0x53')]('YYYY-MM-DD\x20HH:mm:ss');_0x348662[_0x65b8('0x54')]={};}return _0x348662;};}exports['getUsers']=function(_0x318d06,_0x1ea222,_0x179ec8){var _0x5dbc47=[];return db['User'][_0x65b8('0x43')]({'where':{'role':[_0x65b8('0x55'),_0x65b8('0x56'),_0x65b8('0x57')]},'attributes':['id',_0x65b8('0x58'),_0x65b8('0x2a'),_0x65b8('0x59'),_0x65b8('0x5a'),_0x65b8('0x5b'),_0x65b8('0x5c'),_0x65b8('0x5d'),_0x65b8('0x5e'),_0x65b8('0x5f')],'order':qs['sort'](_0x65b8('0x60')),'include':[{'model':db[_0x65b8('0x61')],'as':_0x65b8('0x62'),'attributes':['id','name',_0x65b8('0x30')],'through':{'attributes':[]}}]})[_0x65b8('0x28')](function(_0x12ad24){if(_0x12ad24){var _0x4016b1=_0x318d06[_0x65b8('0x57')]['get']({'plain':!![]});if(!_0x4016b1)throw new Error(_0x65b8('0x63'));if(_0x4016b1['role']===_0x65b8('0x57')){return db[_0x65b8('0x64')][_0x65b8('0x47')]({'where':{'name':_0x65b8('0x65'),'userProfileId':_0x4016b1[_0x65b8('0x66')]},'raw':!![]})['then'](function(_0x4030b9){if(_0x4030b9){if(_0x4030b9[_0x65b8('0x67')]===0x0){return db[_0x65b8('0x68')][_0x65b8('0x43')]({'where':{'sectionId':_0x4030b9['id']},'raw':!![]})[_0x65b8('0x28')](function(_0x75de22){var _0x17da4f=[];if(_[_0x65b8('0x69')](_0x75de22)){_0x17da4f=_[_0x65b8('0x3f')](_0x12ad24,function(_0x42d4ad){if(_0x42d4ad[_0x65b8('0x59')]!==_0x65b8('0x55')){return _0x42d4ad;}});return _0x17da4f;}else{for(var _0x1634ef=0x0;_0x1634ef<_0x12ad24[_0x65b8('0x37')];_0x1634ef++){var _0x1a0db3=_0x12ad24[_0x1634ef];if(_0x1a0db3['role']===_0x65b8('0x55')){var _0x5b4af8=_['find'](_0x75de22,function(_0x5abdac){return _0x5abdac['resourceId']===_0x1a0db3['id'];});if(_0x5b4af8)_0x17da4f[_0x65b8('0x6a')](_0x1a0db3);}else{_0x17da4f[_0x65b8('0x6a')](_0x1a0db3);}}return _0x17da4f;}});}else{return _0x12ad24;}}else{var _0x586efc=_[_0x65b8('0x3f')](_0x12ad24,function(_0x30fec3){if(_0x30fec3['role']!==_0x65b8('0x55')){return _0x30fec3;}});return _0x586efc;}});}else{return _0x12ad24;}}})[_0x65b8('0x28')](function(_0x4427c1){var _0x30101f=[];if(_0x4427c1){_0x5dbc47=_0x4427c1;for(var _0x18ad8d=0x0;_0x18ad8d<_0x4427c1[_0x65b8('0x37')];_0x18ad8d+=0x1){_0x5dbc47[_0x18ad8d][_0x65b8('0x50')]=[];_0x30101f[_0x65b8('0x6a')](db[_0x65b8('0x2e')][_0x65b8('0x47')]({'where':{'FromId':[_0x318d06['user']['id'],_0x4427c1[_0x18ad8d]['id']],'ToId':[_0x318d06[_0x65b8('0x57')]['id'],_0x4427c1[_0x18ad8d]['id']],'ChatGroupId':null},'attributes':['id',_0x65b8('0x52'),_0x65b8('0x6b'),_0x65b8('0x6c'),_0x65b8('0x6d'),_0x65b8('0x4b')],'order':qs[_0x65b8('0x3a')](_0x65b8('0x6e')),'limit':0x1,'offset':0x0,'raw':!![]})[_0x65b8('0x28')](wrapUser(_0x4427c1[_0x18ad8d])));}return BPromise['all'](_0x30101f);}return[];})[_0x65b8('0x28')](function(_0x4f5757){return{'rows':_0x4f5757,'count':_0x4f5757['length']};})[_0x65b8('0x28')](respondWithResult(_0x1ea222,null))['catch'](handleError(_0x1ea222,null));};