Built motion from commit (unavailable).|2.5.22
[motion2.git] / server / api / chatMessage / chatMessage.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 _0x638f=['sox','to-csv','ejs','lodash','crypto','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','redis','localhost','./chatMessage.socket','register','jayson/promise','client','http','ChatMessage,\x20%s,\x20%s','request\x20sent','debug','ChatMessage,\x20%s,\x20%s,\x20%s','error','code','message','info','result','catch','sendStatus','end','status','count','offset','limit','Content-Range','apply','reject','save','destroy','then','stack','name','send','index','ChatMessage','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','pick','where','merge','VIRTUAL','filter','options','includeAll','include','findAll','rows','params','find','create','body','update','describe','accept','read','readAt','format','YYYY-MM-DD\x20HH:mm:ss','user','agent','CmContact','get','ChatInteractionId','UserId','read1stAt','interaction','stringify','null','acceptMessage','manual','interface','SIP/%s','channel','event','EventManager','acceptmessage','parse','formData','chat-interactions','chat','ChatInteraction','html-pdf','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util'];(function(_0x389cda,_0x31155c){var _0x5889bc=function(_0x32f473){while(--_0x32f473){_0x389cda['push'](_0x389cda['shift']());}};_0x5889bc(++_0x31155c);}(_0x638f,0xe5));var _0xf638=function(_0xabca41,_0xbfdf4a){_0xabca41=_0xabca41-0x0;var _0x404ccb=_0x638f[_0xabca41];return _0x404ccb;};'use strict';var pdf=require(_0xf638('0x0'));var emlformat=require(_0xf638('0x1'));var rimraf=require(_0xf638('0x2'));var zipdir=require(_0xf638('0x3'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf638('0x4'));var moment=require(_0xf638('0x5'));var BPromise=require(_0xf638('0x6'));var Mustache=require(_0xf638('0x7'));var util=require(_0xf638('0x8'));var path=require('path');var sox=require(_0xf638('0x9'));var csv=require(_0xf638('0xa'));var ejs=require(_0xf638('0xb'));var fs=require('fs');var _=require(_0xf638('0xc'));var squel=require('squel');var crypto=require(_0xf638('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xf638('0xe'));var toCsv=require(_0xf638('0xa'));var querystring=require(_0xf638('0xf'));var Papa=require(_0xf638('0x10'));var Redis=require('ioredis');var authService=require(_0xf638('0x11'));var qs=require(_0xf638('0x12'));var as=require(_0xf638('0x13'));var hardwareService=require(_0xf638('0x14'));var logger=require(_0xf638('0x15'))(_0xf638('0x16'));var utils=require(_0xf638('0x17'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xf638('0x18'))['db'];config[_0xf638('0x19')]=_['defaults'](config['redis'],{'host':_0xf638('0x1a'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf638('0x19')]));require(_0xf638('0x1b'))[_0xf638('0x1c')](socket);var jayson=require(_0xf638('0x1d'));var client=jayson[_0xf638('0x1e')][_0xf638('0x1f')]({'port':0x232c});var client9002=jayson[_0xf638('0x1e')][_0xf638('0x1f')]({'port':0x232a});function respondWithRpcPromise(_0x5d79f1,_0x1d7740,_0x5741f2,_0x57956a){return new BPromise(function(_0x127d60,_0x88fd3){var _0x17dd2f=_0x57956a||client;return _0x17dd2f['request'](_0x5d79f1,_0x5741f2)['then'](function(_0x481f0d){logger['info'](_0xf638('0x20'),_0x1d7740,_0xf638('0x21'));logger[_0xf638('0x22')](_0xf638('0x23'),_0x1d7740,_0xf638('0x21'),JSON['stringify'](_0x481f0d));if(_0x481f0d['error']){if(_0x481f0d[_0xf638('0x24')][_0xf638('0x25')]===0x1f4){logger['error'](_0xf638('0x20'),_0x1d7740,_0x481f0d[_0xf638('0x24')][_0xf638('0x26')]);return _0x88fd3(_0x481f0d[_0xf638('0x24')][_0xf638('0x26')]);}logger[_0xf638('0x24')](_0xf638('0x20'),_0x1d7740,_0x481f0d[_0xf638('0x24')]['message']);return _0x127d60(_0x481f0d[_0xf638('0x24')][_0xf638('0x26')]);}else{logger[_0xf638('0x27')]('ChatMessage,\x20%s,\x20%s',_0x1d7740,_0xf638('0x21'));_0x127d60(_0x481f0d[_0xf638('0x28')]['message']);}})[_0xf638('0x29')](function(_0x395d5b){logger[_0xf638('0x24')](_0xf638('0x20'),_0x1d7740,_0x395d5b);_0x88fd3(_0x395d5b);});});}function respondWithStatusCode(_0x2fbd7a,_0x57431f){_0x57431f=_0x57431f||0xcc;return function(_0x3cb9eb){if(_0x3cb9eb){return _0x2fbd7a[_0xf638('0x2a')](_0x57431f);}return _0x2fbd7a['status'](_0x57431f)[_0xf638('0x2b')]();};}function respondWithResult(_0x23a969,_0xcf68a1){_0xcf68a1=_0xcf68a1||0xc8;return function(_0x2c2e42){if(_0x2c2e42){return _0x23a969[_0xf638('0x2c')](_0xcf68a1)['json'](_0x2c2e42);}};}function respondWithFilteredResult(_0x49fd8b,_0x248e1b){return function(_0x14cbda){if(_0x14cbda){var _0x2a2d6d=_0x14cbda[_0xf638('0x2d')],_0x11f04b=_0x248e1b[_0xf638('0x2e')],_0x5f4200=_0x248e1b['offset']+_0x248e1b[_0xf638('0x2f')],_0x50cd79;if(_0x5f4200>=_0x2a2d6d){_0x5f4200=_0x2a2d6d;_0x50cd79=0xc8;}else{_0x50cd79=0xce;}_0x49fd8b['status'](_0x50cd79);return _0x49fd8b['set'](_0xf638('0x30'),_0x11f04b+'-'+_0x5f4200+'/'+_0x2a2d6d)['json'](_0x14cbda);}return null;};}function patchUpdates(_0xdbf6aa){return function(_0x16a9ef){try{jsonpatch[_0xf638('0x31')](_0x16a9ef,_0xdbf6aa,!![]);}catch(_0x3ad03b){return BPromise[_0xf638('0x32')](_0x3ad03b);}return _0x16a9ef[_0xf638('0x33')]();};}function saveUpdates(_0x55c0c4,_0x80c264){return function(_0x5a1e37){if(_0x5a1e37){return _0x5a1e37['update'](_0x55c0c4)['then'](function(_0x1444f4){return _0x1444f4;});}return null;};}function removeEntity(_0x3e7849,_0x3485d0){return function(_0x56b90e){if(_0x56b90e){return _0x56b90e[_0xf638('0x34')]()[_0xf638('0x35')](function(){_0x3e7849[_0xf638('0x2c')](0xcc)[_0xf638('0x2b')]();});}};}function handleEntityNotFound(_0x2696a2,_0xa495fd){return function(_0x2288f2){if(!_0x2288f2){_0x2696a2[_0xf638('0x2a')](0x194);}return _0x2288f2;};}function handleError(_0xf0ee96,_0x55c468){_0x55c468=_0x55c468||0x1f4;return function(_0x4e7cf8){logger['error'](_0x4e7cf8[_0xf638('0x36')]);if(_0x4e7cf8[_0xf638('0x37')]){delete _0x4e7cf8[_0xf638('0x37')];}_0xf0ee96['status'](_0x55c468)[_0xf638('0x38')](_0x4e7cf8);};}exports[_0xf638('0x39')]=function(_0x14b022,_0x1371e2){var _0x13c57e={},_0x78fe25={},_0x13ea6c={'count':0x0,'rows':[]};var _0x452f74=_['map'](db[_0xf638('0x3a')][_0xf638('0x3b')],function(_0x5d4b2f){return{'name':_0x5d4b2f[_0xf638('0x3c')],'type':_0x5d4b2f[_0xf638('0x3d')][_0xf638('0x3e')]};});_0x78fe25[_0xf638('0x3f')]=_[_0xf638('0x40')](_0x452f74,'name');_0x78fe25[_0xf638('0x41')]=_[_0xf638('0x42')](_0x14b022['query']);_0x78fe25[_0xf638('0x43')]=_[_0xf638('0x44')](_0x78fe25[_0xf638('0x3f')],_0x78fe25['query']);_0x13c57e[_0xf638('0x45')]=_['intersection'](_0x78fe25[_0xf638('0x3f')],qs[_0xf638('0x46')](_0x14b022[_0xf638('0x41')][_0xf638('0x46')]));_0x13c57e[_0xf638('0x45')]=_0x13c57e[_0xf638('0x45')][_0xf638('0x47')]?_0x13c57e[_0xf638('0x45')]:_0x78fe25[_0xf638('0x3f')];if(!_0x14b022[_0xf638('0x41')][_0xf638('0x48')]('nolimit')){_0x13c57e[_0xf638('0x2f')]=qs[_0xf638('0x2f')](_0x14b022[_0xf638('0x41')]['limit']);_0x13c57e[_0xf638('0x2e')]=qs[_0xf638('0x2e')](_0x14b022[_0xf638('0x41')]['offset']);}_0x13c57e[_0xf638('0x49')]=qs[_0xf638('0x4a')](_0x14b022[_0xf638('0x41')][_0xf638('0x4a')]);_0x13c57e['where']=qs[_0xf638('0x43')](_[_0xf638('0x4b')](_0x14b022['query'],_0x78fe25[_0xf638('0x43')]),_0x452f74);if(_0x14b022['query']['filter']){_0x13c57e[_0xf638('0x4c')]=_[_0xf638('0x4d')](_0x13c57e[_0xf638('0x4c')],{'$or':_['map'](_0x452f74,function(_0x584381){if(_0x584381[_0xf638('0x3d')]!==_0xf638('0x4e')){var _0x4c5783={};_0x4c5783[_0x584381[_0xf638('0x37')]]={'$like':'%'+_0x14b022['query'][_0xf638('0x4f')]+'%'};return _0x4c5783;}})});}_0x13c57e=_['merge']({},_0x13c57e,_0x14b022[_0xf638('0x50')]);var _0x5b9c13={'where':_0x13c57e[_0xf638('0x4c')]};return db[_0xf638('0x3a')][_0xf638('0x2d')](_0x5b9c13)['then'](function(_0x7b4b34){_0x13ea6c['count']=_0x7b4b34;if(_0x14b022[_0xf638('0x41')][_0xf638('0x51')]){_0x13c57e[_0xf638('0x52')]=[{'all':!![]}];}return db[_0xf638('0x3a')][_0xf638('0x53')](_0x13c57e);})[_0xf638('0x35')](function(_0xabb7c6){_0x13ea6c[_0xf638('0x54')]=_0xabb7c6;return _0x13ea6c;})[_0xf638('0x35')](respondWithFilteredResult(_0x1371e2,_0x13c57e))[_0xf638('0x29')](handleError(_0x1371e2,null));};exports['show']=function(_0x518306,_0x89025d){var _0xf62d7d={'raw':!![],'where':{'id':_0x518306[_0xf638('0x55')]['id']}},_0x22416b={};_0x22416b[_0xf638('0x3f')]=_[_0xf638('0x42')](db['ChatMessage'][_0xf638('0x3b')]);_0x22416b[_0xf638('0x41')]=_[_0xf638('0x42')](_0x518306['query']);_0x22416b['filters']=_[_0xf638('0x44')](_0x22416b[_0xf638('0x3f')],_0x22416b[_0xf638('0x41')]);_0xf62d7d[_0xf638('0x45')]=_[_0xf638('0x44')](_0x22416b[_0xf638('0x3f')],qs[_0xf638('0x46')](_0x518306[_0xf638('0x41')][_0xf638('0x46')]));_0xf62d7d['attributes']=_0xf62d7d[_0xf638('0x45')][_0xf638('0x47')]?_0xf62d7d[_0xf638('0x45')]:_0x22416b['model'];if(_0x518306[_0xf638('0x41')][_0xf638('0x51')]){_0xf62d7d['include']=[{'all':!![]}];}_0xf62d7d=_[_0xf638('0x4d')]({},_0xf62d7d,_0x518306['options']);return db[_0xf638('0x3a')][_0xf638('0x56')](_0xf62d7d)['then'](handleEntityNotFound(_0x89025d,null))[_0xf638('0x35')](respondWithResult(_0x89025d,null))[_0xf638('0x29')](handleError(_0x89025d,null));};exports[_0xf638('0x57')]=function(_0x2917ed,_0x4cf69e){return db[_0xf638('0x3a')]['create'](_0x2917ed[_0xf638('0x58')],{})[_0xf638('0x35')](respondWithResult(_0x4cf69e,0xc9))[_0xf638('0x29')](handleError(_0x4cf69e,null));};exports[_0xf638('0x59')]=function(_0x379cfa,_0x5b8f89){if(_0x379cfa[_0xf638('0x58')]['id']){delete _0x379cfa[_0xf638('0x58')]['id'];}return db[_0xf638('0x3a')][_0xf638('0x56')]({'where':{'id':_0x379cfa[_0xf638('0x55')]['id']}})[_0xf638('0x35')](handleEntityNotFound(_0x5b8f89,null))['then'](saveUpdates(_0x379cfa['body'],null))[_0xf638('0x35')](respondWithResult(_0x5b8f89,null))[_0xf638('0x29')](handleError(_0x5b8f89,null));};exports['destroy']=function(_0x1bca61,_0x553680){return db['ChatMessage'][_0xf638('0x56')]({'where':{'id':_0x1bca61[_0xf638('0x55')]['id']}})['then'](handleEntityNotFound(_0x553680,null))[_0xf638('0x35')](removeEntity(_0x553680,null))['catch'](handleError(_0x553680,null));};exports[_0xf638('0x5a')]=function(_0x19027e,_0x421640){return db[_0xf638('0x3a')][_0xf638('0x5a')]()['then'](respondWithResult(_0x421640,null))['catch'](handleError(_0x421640,null));};var interaction_log=require(_0xf638('0x15'))('chat-interactions');exports[_0xf638('0x5b')]=function(_0x19d956,_0x5d282f,_0x5a7dda){var _0x52b79c={'agent':{},'channel':'chat'};if(_0x19d956[_0xf638('0x58')]['id']){delete _0x19d956[_0xf638('0x58')]['id'];}_0x19d956[_0xf638('0x58')][_0xf638('0x5c')]=!![];_0x19d956['body'][_0xf638('0x5d')]=moment()[_0xf638('0x5e')](_0xf638('0x5f'));_0x19d956[_0xf638('0x58')]['UserId']=_0x19d956[_0xf638('0x58')]['UserId']||_0x19d956[_0xf638('0x60')]['id'];_0x52b79c[_0xf638('0x61')]['id']=_0x19d956[_0xf638('0x58')]['UserId'];return db[_0xf638('0x3a')][_0xf638('0x56')]({'where':{'id':_0x19d956[_0xf638('0x55')]['id'],'UserId':null},'include':[{'model':db[_0xf638('0x62')],'as':'Contact'}]})['then'](handleEntityNotFound(_0x5d282f,null))[_0xf638('0x35')](saveUpdates(_0x19d956[_0xf638('0x58')],null))[_0xf638('0x35')](function(_0x305e92){if(_0x305e92){_0x52b79c[_0xf638('0x26')]=_0x305e92[_0xf638('0x63')]({'plain':!![]});return db['ChatInteraction']['find']({'where':{'id':_0x305e92[_0xf638('0x64')]}});}return null;})[_0xf638('0x35')](handleEntityNotFound(_0x5d282f,null))[_0xf638('0x35')](function(_0x1934de){if(_0x1934de){return _0x1934de[_0xf638('0x59')]({'UserId':_0x19d956[_0xf638('0x58')][_0xf638('0x65')],'read1stAt':_['isNil'](_0x1934de[_0xf638('0x66')])?moment()[_0xf638('0x5e')](_0xf638('0x5f')):undefined});}return null;})[_0xf638('0x35')](function(_0x1da7eb){if(_0x1da7eb){_0x52b79c[_0xf638('0x67')]=_0x1da7eb[_0xf638('0x63')]({'plain':!![]});interaction_log['info']('[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x19d956['user']['id'],_0x19d956[_0xf638('0x60')]['name'],_0x19d956[_0xf638('0x60')]['role'],_0x52b79c['interaction']['id'],_0x19d956[_0xf638('0x58')]?JSON[_0xf638('0x68')](_0x19d956[_0xf638('0x58')]):_0xf638('0x69'));return respondWithRpcPromise('AcceptMessage',_0xf638('0x6a'),_0x52b79c);}return null;})[_0xf638('0x35')](function(_0x320e7b){if(_0x320e7b){if(_0x19d956['body'][_0xf638('0x6b')]){_0x52b79c[_0xf638('0x26')][_0xf638('0x6c')]=util['format'](_0xf638('0x6d'),_0x19d956[_0xf638('0x60')][_0xf638('0x37')]);_0x52b79c[_0xf638('0x26')][_0xf638('0x6e')]=_0x52b79c[_0xf638('0x6e')];_0x52b79c[_0xf638('0x26')][_0xf638('0x6f')]='accept';return respondWithRpcPromise(_0xf638('0x70'),'EventManager',{'event':_0xf638('0x71'),'message':_['merge'](_0x52b79c['message'],JSON[_0xf638('0x72')](_0x52b79c[_0xf638('0x67')][_0xf638('0x73')]))},client9002)[_0xf638('0x35')](function(){return _0x52b79c;});}return _0x52b79c;}return null;})[_0xf638('0x35')](respondWithResult(_0x5d282f,null))['catch'](handleError(_0x5d282f,null));};var interaction_log=require(_0xf638('0x15'))(_0xf638('0x74'));exports[_0xf638('0x32')]=function(_0xc413a3,_0x2d681a,_0x2bcd54){var _0x5c4402={'agent':{},'channel':_0xf638('0x75')};if(_0xc413a3[_0xf638('0x58')]['id']){delete _0xc413a3[_0xf638('0x58')]['id'];}_0xc413a3[_0xf638('0x58')][_0xf638('0x65')]=_0xc413a3[_0xf638('0x58')][_0xf638('0x65')]||_0xc413a3[_0xf638('0x60')]['id'];_0x5c4402[_0xf638('0x61')]['id']=_0xc413a3[_0xf638('0x58')][_0xf638('0x65')];return db[_0xf638('0x3a')]['find']({'where':{'id':_0xc413a3['params']['id'],'UserId':null}})[_0xf638('0x35')](handleEntityNotFound(_0x2d681a,null))[_0xf638('0x35')](function(_0x3d759a){if(_0x3d759a){_0x5c4402[_0xf638('0x26')]=_0x3d759a[_0xf638('0x63')]({'plain':!![]});return db[_0xf638('0x76')][_0xf638('0x56')]({'where':{'id':_0x3d759a['ChatInteractionId']}});}return null;})[_0xf638('0x35')](handleEntityNotFound(_0x2d681a,null))[_0xf638('0x35')](function(_0x11981a){if(_0x11981a){_0x5c4402[_0xf638('0x67')]=_0x11981a[_0xf638('0x63')]({'plain':!![]});interaction_log[_0xf638('0x27')]('[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0xc413a3['user']['id'],_0xc413a3[_0xf638('0x60')][_0xf638('0x37')],_0xc413a3['user']['role'],_0x5c4402[_0xf638('0x67')]['id'],_0xc413a3[_0xf638('0x58')]?JSON['stringify'](_0xc413a3['body']):_0xf638('0x69'));return respondWithRpcPromise('RejectMessage','rejectMessage',_0x5c4402);}return null;})[_0xf638('0x35')](function(_0x3ebe35){if(_0x3ebe35){_0x3ebe35[_0xf638('0x26')][_0xf638('0x6c')]=util[_0xf638('0x5e')]('SIP/%s',_0xc413a3[_0xf638('0x60')][_0xf638('0x37')]);_0x3ebe35[_0xf638('0x26')]['channel']=_0x5c4402[_0xf638('0x6e')];_0x3ebe35[_0xf638('0x26')]['event']=_0xf638('0x32');respondWithRpcPromise(_0xf638('0x70'),'EventManager',{'event':'rejectmessage','message':_0x3ebe35[_0xf638('0x26')]},client9002);return _0x5c4402;}return null;})['then'](respondWithResult(_0x2d681a,null))[_0xf638('0x29')](handleError(_0x2d681a,null));};