Built motion from commit (unavailable).|2.3.4
[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 _0x9f82=['query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','pick','filter','where','merge','type','key','field','options','includeAll','rows','show','params','rawAttributes','include','find','catch','create','body','get','messages','lastMessageAt','createdAt','format','YYYY-MM-DD\x20HH:mm:ss','lastMessage','getUsers','User','findAll','agent','admin','user','fullname','userpic','internal','online','lastLoginAt','allowmessenger','showWebBar','-online','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','Agents','userProfileId','UserProfileResource','isEmpty','resourceId','push','role','FromId','ToId','read','all','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','jsforce','papaparse','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/utils','redis','defaults','sendStatus','end','status','json','count','offset','set','Content-Range','reject','save','update','then','stack','name','send','index','ChatInternalMessage','model','keys'];(function(_0x36c01d,_0x554405){var _0x85df9f=function(_0x520f84){while(--_0x520f84){_0x36c01d['push'](_0x36c01d['shift']());}};_0x85df9f(++_0x554405);}(_0x9f82,0x111));var _0x29f8=function(_0x2b10c6,_0x185590){_0x2b10c6=_0x2b10c6-0x0;var _0x3789d2=_0x9f82[_0x2b10c6];return _0x3789d2;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x29f8('0x0'));var rimraf=require(_0x29f8('0x1'));var zipdir=require(_0x29f8('0x2'));var jsonpatch=require(_0x29f8('0x3'));var rp=require(_0x29f8('0x4'));var moment=require(_0x29f8('0x5'));var BPromise=require(_0x29f8('0x6'));var Mustache=require(_0x29f8('0x7'));var util=require(_0x29f8('0x8'));var path=require(_0x29f8('0x9'));var sox=require(_0x29f8('0xa'));var csv=require(_0x29f8('0xb'));var ejs=require(_0x29f8('0xc'));var fs=require('fs');var _=require(_0x29f8('0xd'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x29f8('0xe'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x29f8('0xf'));var authService=require('../../components/auth/service');var hardwareService=require('../../config/license/hardware');var Redis=require(_0x29f8('0x10'));var qs=require(_0x29f8('0x11'));var logger=require(_0x29f8('0x12'))(_0x29f8('0x13'));var utils=require(_0x29f8('0x14'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0x29f8('0x15')]=_[_0x29f8('0x16')](config[_0x29f8('0x15')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x29f8('0x15')]));require('./chatInternalMessage.socket')['register'](socket);function respondWithStatusCode(_0x1a41bd,_0x6e3a2c){_0x6e3a2c=_0x6e3a2c||0xcc;return function(_0x14335f){if(_0x14335f){return _0x1a41bd[_0x29f8('0x17')](_0x6e3a2c);}return _0x1a41bd['status'](_0x6e3a2c)[_0x29f8('0x18')]();};}function respondWithResult(_0x1990b2,_0x1ebd14){_0x1ebd14=_0x1ebd14||0xc8;return function(_0x4e8a69){if(_0x4e8a69){return _0x1990b2[_0x29f8('0x19')](_0x1ebd14)[_0x29f8('0x1a')](_0x4e8a69);}};}function respondWithFilteredResult(_0x1a362f,_0x307eaa){return function(_0x4706cb){if(_0x4706cb){var _0x795634=_0x4706cb[_0x29f8('0x1b')],_0x57f2e7=_0x307eaa['offset'],_0x32bc33=_0x307eaa[_0x29f8('0x1c')]+_0x307eaa['limit'],_0x2547e3;if(_0x32bc33>=_0x795634){_0x32bc33=_0x795634;_0x2547e3=0xc8;}else{_0x2547e3=0xce;}_0x1a362f[_0x29f8('0x19')](_0x2547e3);return _0x1a362f[_0x29f8('0x1d')](_0x29f8('0x1e'),_0x57f2e7+'-'+_0x32bc33+'/'+_0x795634)[_0x29f8('0x1a')](_0x4706cb);}return null;};}function patchUpdates(_0x21141e){return function(_0xb186a3){try{jsonpatch['apply'](_0xb186a3,_0x21141e,!![]);}catch(_0x568a3b){return BPromise[_0x29f8('0x1f')](_0x568a3b);}return _0xb186a3[_0x29f8('0x20')]();};}function saveUpdates(_0x24a14c,_0x86748c){return function(_0xbbe078){if(_0xbbe078){return _0xbbe078[_0x29f8('0x21')](_0x24a14c)[_0x29f8('0x22')](function(_0x9b175d){return _0x9b175d;});}return null;};}function removeEntity(_0x2703f7,_0x569a29){return function(_0x5672ad){if(_0x5672ad){return _0x5672ad['destroy']()[_0x29f8('0x22')](function(){_0x2703f7[_0x29f8('0x19')](0xcc)[_0x29f8('0x18')]();});}};}function handleEntityNotFound(_0x2d3199,_0x3b9633){return function(_0x1ea772){if(!_0x1ea772){_0x2d3199[_0x29f8('0x17')](0x194);}return _0x1ea772;};}function handleError(_0x285b32,_0x5486fe){_0x5486fe=_0x5486fe||0x1f4;return function(_0x5090ee){logger['error'](_0x5090ee[_0x29f8('0x23')]);if(_0x5090ee['name']){delete _0x5090ee[_0x29f8('0x24')];}_0x285b32['status'](_0x5486fe)[_0x29f8('0x25')](_0x5090ee);};}exports[_0x29f8('0x26')]=function(_0x8c1ef9,_0x9d255b){var _0x527644={},_0x4f9a99={},_0x3cd0aa={'count':0x0,'rows':[]};var _0x15e577=db[_0x29f8('0x27')]['rawAttributes'];_0x4f9a99[_0x29f8('0x28')]=_[_0x29f8('0x29')](_0x15e577);_0x4f9a99['query']=_[_0x29f8('0x29')](_0x8c1ef9[_0x29f8('0x2a')]);_0x4f9a99[_0x29f8('0x2b')]=_[_0x29f8('0x2c')](_0x4f9a99[_0x29f8('0x28')],_0x4f9a99[_0x29f8('0x2a')]);_0x527644[_0x29f8('0x2d')]=_[_0x29f8('0x2c')](_0x4f9a99[_0x29f8('0x28')],qs['fields'](_0x8c1ef9['query'][_0x29f8('0x2e')]));_0x527644[_0x29f8('0x2d')]=_0x527644[_0x29f8('0x2d')][_0x29f8('0x2f')]?_0x527644[_0x29f8('0x2d')]:_0x4f9a99[_0x29f8('0x28')];if(!_0x8c1ef9[_0x29f8('0x2a')][_0x29f8('0x30')](_0x29f8('0x31'))){_0x527644[_0x29f8('0x32')]=qs['limit'](_0x8c1ef9[_0x29f8('0x2a')][_0x29f8('0x32')]);_0x527644[_0x29f8('0x1c')]=qs['offset'](_0x8c1ef9[_0x29f8('0x2a')]['offset']);}_0x527644[_0x29f8('0x33')]=qs[_0x29f8('0x34')](_0x8c1ef9[_0x29f8('0x2a')][_0x29f8('0x34')]);_0x527644['where']=qs[_0x29f8('0x2b')](_[_0x29f8('0x35')](_0x8c1ef9[_0x29f8('0x2a')],_0x4f9a99[_0x29f8('0x2b')]));if(_0x8c1ef9['query'][_0x29f8('0x36')]){_0x527644[_0x29f8('0x37')]=_[_0x29f8('0x38')](_0x527644[_0x29f8('0x37')],{'$or':_['map'](_0x15e577,function(_0x23993d){if(_0x23993d[_0x29f8('0x39')][_0x29f8('0x3a')]!=='VIRTUAL'){var _0x32fc10={};_0x32fc10[_0x23993d[_0x29f8('0x3b')]]={'$like':'%'+_0x8c1ef9['query'][_0x29f8('0x36')]+'%'};return _0x32fc10;}})});}_0x527644=_[_0x29f8('0x38')]({},_0x527644,_0x8c1ef9[_0x29f8('0x3c')]);var _0x2c1ceb={'where':_0x527644[_0x29f8('0x37')]};return db[_0x29f8('0x27')][_0x29f8('0x1b')](_0x2c1ceb)[_0x29f8('0x22')](function(_0x19e417){_0x3cd0aa[_0x29f8('0x1b')]=_0x19e417;if(_0x8c1ef9[_0x29f8('0x2a')][_0x29f8('0x3d')]){_0x527644['include']=[{'all':!![]}];}return db[_0x29f8('0x27')]['findAll'](_0x527644);})[_0x29f8('0x22')](function(_0x4b8030){_0x3cd0aa[_0x29f8('0x3e')]=_0x4b8030;return _0x3cd0aa;})[_0x29f8('0x22')](respondWithFilteredResult(_0x9d255b,_0x527644))['catch'](handleError(_0x9d255b,null));};exports[_0x29f8('0x3f')]=function(_0x296ba5,_0x27f38d){var _0x48f664={'raw':!![],'where':{'id':_0x296ba5[_0x29f8('0x40')]['id']}},_0x11cba8={};_0x11cba8[_0x29f8('0x28')]=_['keys'](db[_0x29f8('0x27')][_0x29f8('0x41')]);_0x11cba8[_0x29f8('0x2a')]=_[_0x29f8('0x29')](_0x296ba5['query']);_0x11cba8['filters']=_['intersection'](_0x11cba8['model'],_0x11cba8['query']);_0x48f664[_0x29f8('0x2d')]=_[_0x29f8('0x2c')](_0x11cba8[_0x29f8('0x28')],qs[_0x29f8('0x2e')](_0x296ba5[_0x29f8('0x2a')][_0x29f8('0x2e')]));_0x48f664[_0x29f8('0x2d')]=_0x48f664[_0x29f8('0x2d')][_0x29f8('0x2f')]?_0x48f664[_0x29f8('0x2d')]:_0x11cba8[_0x29f8('0x28')];if(_0x296ba5[_0x29f8('0x2a')][_0x29f8('0x3d')]){_0x48f664[_0x29f8('0x42')]=[{'all':!![]}];}_0x48f664=_['merge']({},_0x48f664,_0x296ba5[_0x29f8('0x3c')]);return db[_0x29f8('0x27')][_0x29f8('0x43')](_0x48f664)[_0x29f8('0x22')](handleEntityNotFound(_0x27f38d,null))[_0x29f8('0x22')](respondWithResult(_0x27f38d,null))[_0x29f8('0x44')](handleError(_0x27f38d,null));};exports[_0x29f8('0x45')]=function(_0x154fe5,_0x108e7f){return db[_0x29f8('0x27')][_0x29f8('0x45')](_0x154fe5[_0x29f8('0x46')],{})[_0x29f8('0x22')](respondWithResult(_0x108e7f,0xc9))[_0x29f8('0x44')](handleError(_0x108e7f,null));};exports[_0x29f8('0x21')]=function(_0x5c92fd,_0x134964){if(_0x5c92fd[_0x29f8('0x46')]['id']){delete _0x5c92fd[_0x29f8('0x46')]['id'];}return db['ChatInternalMessage'][_0x29f8('0x43')]({'where':{'id':_0x5c92fd[_0x29f8('0x40')]['id']}})[_0x29f8('0x22')](handleEntityNotFound(_0x134964,null))[_0x29f8('0x22')](saveUpdates(_0x5c92fd[_0x29f8('0x46')],null))[_0x29f8('0x22')](respondWithResult(_0x134964,null))[_0x29f8('0x44')](handleError(_0x134964,null));};exports['destroy']=function(_0x65c532,_0x48f08b){return db[_0x29f8('0x27')][_0x29f8('0x43')]({'where':{'id':_0x65c532[_0x29f8('0x40')]['id']}})[_0x29f8('0x22')](handleEntityNotFound(_0x48f08b,null))['then'](removeEntity(_0x48f08b,null))[_0x29f8('0x44')](handleError(_0x48f08b,null));};exports['describe']=function(_0x4cdac6,_0x2d69e1){return db[_0x29f8('0x27')]['describe']()['then'](respondWithResult(_0x2d69e1,null))[_0x29f8('0x44')](handleError(_0x2d69e1,null));};function wrapUser(_0x604663){return function(_0x1284ea){var _0x52b73b=_0x604663[_0x29f8('0x47')]({'plain':!![]});_0x52b73b[_0x29f8('0x48')]=[];if(_0x1284ea){_0x52b73b[_0x29f8('0x49')]=_0x1284ea[_0x29f8('0x4a')];_0x52b73b['lastMessage']=_0x1284ea;}else{_0x52b73b[_0x29f8('0x49')]=moment(0x0)[_0x29f8('0x4b')](_0x29f8('0x4c'));_0x52b73b[_0x29f8('0x4d')]={};}return _0x52b73b;};}exports[_0x29f8('0x4e')]=function(_0x3ee10c,_0x174047,_0xcf3adf){var _0x1e2972=[];return db[_0x29f8('0x4f')][_0x29f8('0x50')]({'where':{'role':[_0x29f8('0x51'),_0x29f8('0x52'),_0x29f8('0x53')]},'attributes':['id',_0x29f8('0x54'),'name','role',_0x29f8('0x55'),_0x29f8('0x56'),_0x29f8('0x57'),_0x29f8('0x58'),_0x29f8('0x59'),_0x29f8('0x5a')],'order':qs[_0x29f8('0x34')](_0x29f8('0x5b')),'include':[{'model':db['VoiceQueue'],'as':'VoiceQueues','attributes':['id','name','type'],'through':{'attributes':[]}}]})['then'](function(_0xfc2c1e){if(_0xfc2c1e){var _0xcf01b1=_0x3ee10c[_0x29f8('0x53')]['get']({'plain':!![]});if(!_0xcf01b1)throw new Error(_0x29f8('0x5c'));if(_0xcf01b1['role']===_0x29f8('0x53')){return db[_0x29f8('0x5d')][_0x29f8('0x43')]({'where':{'name':_0x29f8('0x5e'),'userProfileId':_0xcf01b1[_0x29f8('0x5f')]},'raw':!![]})[_0x29f8('0x22')](function(_0x55b5ed){if(_0x55b5ed){if(_0x55b5ed['autoAssociation']===0x0){return db[_0x29f8('0x60')][_0x29f8('0x50')]({'where':{'sectionId':_0x55b5ed['id']},'raw':!![]})['then'](function(_0x146223){var _0x2724f9=[];if(_[_0x29f8('0x61')](_0x146223)){_0x2724f9=_[_0x29f8('0x36')](_0xfc2c1e,function(_0x30c20e){if(_0x30c20e['role']!==_0x29f8('0x51')){return _0x30c20e;}});return _0x2724f9;}else{for(var _0x2773ba=0x0;_0x2773ba<_0xfc2c1e[_0x29f8('0x2f')];_0x2773ba++){var _0x2287cf=_0xfc2c1e[_0x2773ba];if(_0x2287cf['role']===_0x29f8('0x51')){var _0x4c7d95=_[_0x29f8('0x43')](_0x146223,function(_0x14d99c){return _0x14d99c[_0x29f8('0x62')]===_0x2287cf['id'];});if(_0x4c7d95)_0x2724f9[_0x29f8('0x63')](_0x2287cf);}else{_0x2724f9['push'](_0x2287cf);}}return _0x2724f9;}});}else{return _0xfc2c1e;}}else{var _0x2a2b7b=_[_0x29f8('0x36')](_0xfc2c1e,function(_0x23ea06){if(_0x23ea06[_0x29f8('0x64')]!=='agent'){return _0x23ea06;}});return _0x2a2b7b;}});}else{return _0xfc2c1e;}}})['then'](function(_0x5a935a){var _0x42024c=[];if(_0x5a935a){_0x1e2972=_0x5a935a;for(var _0x4d595=0x0;_0x4d595<_0x5a935a[_0x29f8('0x2f')];_0x4d595+=0x1){_0x1e2972[_0x4d595][_0x29f8('0x48')]=[];_0x42024c[_0x29f8('0x63')](db[_0x29f8('0x27')][_0x29f8('0x43')]({'where':{'FromId':[_0x3ee10c[_0x29f8('0x53')]['id'],_0x5a935a[_0x4d595]['id']],'ToId':[_0x3ee10c['user']['id'],_0x5a935a[_0x4d595]['id']],'ChatGroupId':null},'attributes':['id','createdAt',_0x29f8('0x65'),_0x29f8('0x66'),_0x29f8('0x67'),_0x29f8('0x46')],'order':qs['sort']('-createdAt'),'limit':0x1,'offset':0x0,'raw':!![]})[_0x29f8('0x22')](wrapUser(_0x5a935a[_0x4d595])));}return BPromise[_0x29f8('0x68')](_0x42024c);}return[];})[_0x29f8('0x22')](function(_0x4b654a){return{'rows':_0x4b654a,'count':_0x4b654a[_0x29f8('0x2f')]};})[_0x29f8('0x22')](respondWithResult(_0x174047,null))['catch'](handleError(_0x174047,null));};