Built motion from commit (unavailable).|2.5.31
[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 _0x4c4e=['../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','defaults','redis','socket.io-emitter','register','jayson/promise','http','client','request','then','info','ChatMessage,\x20%s,\x20%s','debug','ChatMessage,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','json','offset','limit','undefined','count','status','set','save','update','destroy','end','sendStatus','stack','name','send','index','map','ChatMessage','fieldName','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','type','options','includeAll','include','show','params','body','find','describe','chat-interactions','accept','chat','readAt','format','agent','UserId','CmContact','Contact','get','ChatInteraction','ChatInteractionId','read1stAt','YYYY-MM-DD\x20HH:mm:ss','interaction','[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','user','role','null','manual','interface','SIP/%s','channel','EventManager','acceptmessage','reject','RejectMessage','rejectMessage','event','rejectmessage','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger'];(function(_0x253201,_0x55488a){var _0x4a479a=function(_0x219cda){while(--_0x219cda){_0x253201['push'](_0x253201['shift']());}};_0x4a479a(++_0x55488a);}(_0x4c4e,0x152));var _0xe4c4=function(_0x4ab25a,_0x552a42){_0x4ab25a=_0x4ab25a-0x0;var _0x3639f9=_0x4c4e[_0x4ab25a];return _0x3639f9;};'use strict';var emlformat=require(_0xe4c4('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xe4c4('0x1'));var jsonpatch=require(_0xe4c4('0x2'));var rp=require(_0xe4c4('0x3'));var moment=require('moment');var BPromise=require(_0xe4c4('0x4'));var Mustache=require(_0xe4c4('0x5'));var util=require(_0xe4c4('0x6'));var path=require('path');var sox=require(_0xe4c4('0x7'));var csv=require('to-csv');var ejs=require(_0xe4c4('0x8'));var fs=require('fs');var fs_extra=require(_0xe4c4('0x9'));var _=require(_0xe4c4('0xa'));var squel=require(_0xe4c4('0xb'));var crypto=require(_0xe4c4('0xc'));var jsforce=require(_0xe4c4('0xd'));var deskjs=require(_0xe4c4('0xe'));var toCsv=require(_0xe4c4('0xf'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xe4c4('0x10'));var authService=require(_0xe4c4('0x11'));var qs=require(_0xe4c4('0x12'));var as=require(_0xe4c4('0x13'));var hardwareService=require(_0xe4c4('0x14'));var logger=require(_0xe4c4('0x15'))('api');var utils=require(_0xe4c4('0x16'));var config=require(_0xe4c4('0x17'));var licenseUtil=require(_0xe4c4('0x18'));var db=require(_0xe4c4('0x19'))['db'];config['redis']=_[_0xe4c4('0x1a')](config[_0xe4c4('0x1b')],{'host':'localhost','port':0x18eb});var socket=require(_0xe4c4('0x1c'))(new Redis(config[_0xe4c4('0x1b')]));require('./chatMessage.socket')[_0xe4c4('0x1d')](socket);var jayson=require(_0xe4c4('0x1e'));var client=jayson['client'][_0xe4c4('0x1f')]({'port':0x232c});var client9002=jayson[_0xe4c4('0x20')][_0xe4c4('0x1f')]({'port':0x232a});function respondWithRpcPromise(_0x40a36e,_0x19e59b,_0x882be2,_0x1f637e){return new BPromise(function(_0x1038cb,_0x34ecbe){var _0x36b10c=_0x1f637e||client;return _0x36b10c[_0xe4c4('0x21')](_0x40a36e,_0x882be2)[_0xe4c4('0x22')](function(_0x3820c7){logger[_0xe4c4('0x23')](_0xe4c4('0x24'),_0x19e59b,'request\x20sent');logger[_0xe4c4('0x25')](_0xe4c4('0x26'),_0x19e59b,_0xe4c4('0x27'),JSON[_0xe4c4('0x28')](_0x3820c7));if(_0x3820c7['error']){if(_0x3820c7[_0xe4c4('0x29')][_0xe4c4('0x2a')]===0x1f4){logger[_0xe4c4('0x29')](_0xe4c4('0x24'),_0x19e59b,_0x3820c7[_0xe4c4('0x29')][_0xe4c4('0x2b')]);return _0x34ecbe(_0x3820c7[_0xe4c4('0x29')][_0xe4c4('0x2b')]);}logger[_0xe4c4('0x29')](_0xe4c4('0x24'),_0x19e59b,_0x3820c7[_0xe4c4('0x29')][_0xe4c4('0x2b')]);return _0x1038cb(_0x3820c7[_0xe4c4('0x29')][_0xe4c4('0x2b')]);}else{logger[_0xe4c4('0x23')]('ChatMessage,\x20%s,\x20%s',_0x19e59b,'request\x20sent');_0x1038cb(_0x3820c7[_0xe4c4('0x2c')]['message']);}})[_0xe4c4('0x2d')](function(_0x5374fd){logger[_0xe4c4('0x29')]('ChatMessage,\x20%s,\x20%s',_0x19e59b,_0x5374fd);_0x34ecbe(_0x5374fd);});});}function respondWithStatusCode(_0x34d667,_0x583393){_0x583393=_0x583393||0xcc;return function(_0xba53ef){if(_0xba53ef){return _0x34d667['sendStatus'](_0x583393);}return _0x34d667['status'](_0x583393)['end']();};}function respondWithResult(_0x5d66ab,_0x3221cd){_0x3221cd=_0x3221cd||0xc8;return function(_0x48cfe0){if(_0x48cfe0){return _0x5d66ab['status'](_0x3221cd)[_0xe4c4('0x2e')](_0x48cfe0);}};}function respondWithFilteredResult(_0x11deab,_0x4bb911){return function(_0x57ba43){if(_0x57ba43){var _0x7428ad=typeof _0x4bb911[_0xe4c4('0x2f')]==='undefined'&&typeof _0x4bb911[_0xe4c4('0x30')]===_0xe4c4('0x31');var _0x3f60fc=_0x57ba43['count'];var _0x5d49fd=_0x7428ad?0x0:_0x4bb911[_0xe4c4('0x2f')];var _0x2c5652=_0x7428ad?_0x57ba43[_0xe4c4('0x32')]:_0x4bb911[_0xe4c4('0x2f')]+_0x4bb911['limit'];var _0x49c913;if(_0x2c5652>=_0x3f60fc){_0x2c5652=_0x3f60fc;_0x49c913=0xc8;}else{_0x49c913=0xce;}_0x11deab[_0xe4c4('0x33')](_0x49c913);return _0x11deab[_0xe4c4('0x34')]('Content-Range',_0x5d49fd+'-'+_0x2c5652+'/'+_0x3f60fc)[_0xe4c4('0x2e')](_0x57ba43);}return null;};}function patchUpdates(_0x1ad3e6){return function(_0x835e8c){try{jsonpatch['apply'](_0x835e8c,_0x1ad3e6,!![]);}catch(_0x137f40){return BPromise['reject'](_0x137f40);}return _0x835e8c[_0xe4c4('0x35')]();};}function saveUpdates(_0x4239ba,_0x3bb6cf){return function(_0x35717a){if(_0x35717a){return _0x35717a[_0xe4c4('0x36')](_0x4239ba)[_0xe4c4('0x22')](function(_0xb801c8){return _0xb801c8;});}return null;};}function removeEntity(_0x20a3b0,_0x2213c9){return function(_0x18b2a8){if(_0x18b2a8){return _0x18b2a8[_0xe4c4('0x37')]()[_0xe4c4('0x22')](function(){_0x20a3b0['status'](0xcc)[_0xe4c4('0x38')]();});}};}function handleEntityNotFound(_0x25eb81,_0x3d598a){return function(_0x518018){if(!_0x518018){_0x25eb81[_0xe4c4('0x39')](0x194);}return _0x518018;};}function handleError(_0x22e32c,_0x38644c){_0x38644c=_0x38644c||0x1f4;return function(_0x53eb5b){logger[_0xe4c4('0x29')](_0x53eb5b[_0xe4c4('0x3a')]);if(_0x53eb5b[_0xe4c4('0x3b')]){delete _0x53eb5b[_0xe4c4('0x3b')];}_0x22e32c[_0xe4c4('0x33')](_0x38644c)[_0xe4c4('0x3c')](_0x53eb5b);};}exports[_0xe4c4('0x3d')]=function(_0x44f4eb,_0x3d5ecb){var _0x8b5bf3={},_0x5bb519={},_0x153df2={'count':0x0,'rows':[]};var _0x299528=_[_0xe4c4('0x3e')](db[_0xe4c4('0x3f')]['rawAttributes'],function(_0x2362d6){return{'name':_0x2362d6[_0xe4c4('0x40')],'type':_0x2362d6['type'][_0xe4c4('0x41')]};});_0x5bb519[_0xe4c4('0x42')]=_['map'](_0x299528,_0xe4c4('0x3b'));_0x5bb519[_0xe4c4('0x43')]=_[_0xe4c4('0x44')](_0x44f4eb[_0xe4c4('0x43')]);_0x5bb519[_0xe4c4('0x45')]=_[_0xe4c4('0x46')](_0x5bb519[_0xe4c4('0x42')],_0x5bb519[_0xe4c4('0x43')]);_0x8b5bf3[_0xe4c4('0x47')]=_[_0xe4c4('0x46')](_0x5bb519[_0xe4c4('0x42')],qs[_0xe4c4('0x48')](_0x44f4eb['query'][_0xe4c4('0x48')]));_0x8b5bf3[_0xe4c4('0x47')]=_0x8b5bf3[_0xe4c4('0x47')][_0xe4c4('0x49')]?_0x8b5bf3[_0xe4c4('0x47')]:_0x5bb519[_0xe4c4('0x42')];if(!_0x44f4eb['query'][_0xe4c4('0x4a')]('nolimit')){_0x8b5bf3[_0xe4c4('0x30')]=qs[_0xe4c4('0x30')](_0x44f4eb[_0xe4c4('0x43')][_0xe4c4('0x30')]);_0x8b5bf3[_0xe4c4('0x2f')]=qs['offset'](_0x44f4eb['query'][_0xe4c4('0x2f')]);}_0x8b5bf3[_0xe4c4('0x4b')]=qs[_0xe4c4('0x4c')](_0x44f4eb['query'][_0xe4c4('0x4c')]);_0x8b5bf3[_0xe4c4('0x4d')]=qs[_0xe4c4('0x45')](_[_0xe4c4('0x4e')](_0x44f4eb['query'],_0x5bb519[_0xe4c4('0x45')]),_0x299528);if(_0x44f4eb[_0xe4c4('0x43')][_0xe4c4('0x4f')]){_0x8b5bf3[_0xe4c4('0x4d')]=_[_0xe4c4('0x50')](_0x8b5bf3[_0xe4c4('0x4d')],{'$or':_[_0xe4c4('0x3e')](_0x299528,function(_0x176712){if(_0x176712[_0xe4c4('0x51')]!=='VIRTUAL'){var _0x2757fd={};_0x2757fd[_0x176712['name']]={'$like':'%'+_0x44f4eb[_0xe4c4('0x43')][_0xe4c4('0x4f')]+'%'};return _0x2757fd;}})});}_0x8b5bf3=_[_0xe4c4('0x50')]({},_0x8b5bf3,_0x44f4eb[_0xe4c4('0x52')]);var _0x2b9f7b={'where':_0x8b5bf3[_0xe4c4('0x4d')]};return db[_0xe4c4('0x3f')][_0xe4c4('0x32')](_0x2b9f7b)[_0xe4c4('0x22')](function(_0x18fb82){_0x153df2[_0xe4c4('0x32')]=_0x18fb82;if(_0x44f4eb[_0xe4c4('0x43')][_0xe4c4('0x53')]){_0x8b5bf3[_0xe4c4('0x54')]=[{'all':!![]}];}return db['ChatMessage']['findAll'](_0x8b5bf3);})[_0xe4c4('0x22')](function(_0x198961){_0x153df2['rows']=_0x198961;return _0x153df2;})[_0xe4c4('0x22')](respondWithFilteredResult(_0x3d5ecb,_0x8b5bf3))[_0xe4c4('0x2d')](handleError(_0x3d5ecb,null));};exports[_0xe4c4('0x55')]=function(_0x10e8df,_0x1f6d78){var _0x5587b2={'raw':!![],'where':{'id':_0x10e8df[_0xe4c4('0x56')]['id']}},_0x360f7d={};_0x360f7d['model']=_[_0xe4c4('0x44')](db[_0xe4c4('0x3f')]['rawAttributes']);_0x360f7d[_0xe4c4('0x43')]=_['keys'](_0x10e8df[_0xe4c4('0x43')]);_0x360f7d[_0xe4c4('0x45')]=_[_0xe4c4('0x46')](_0x360f7d[_0xe4c4('0x42')],_0x360f7d[_0xe4c4('0x43')]);_0x5587b2['attributes']=_[_0xe4c4('0x46')](_0x360f7d[_0xe4c4('0x42')],qs[_0xe4c4('0x48')](_0x10e8df[_0xe4c4('0x43')][_0xe4c4('0x48')]));_0x5587b2[_0xe4c4('0x47')]=_0x5587b2[_0xe4c4('0x47')][_0xe4c4('0x49')]?_0x5587b2[_0xe4c4('0x47')]:_0x360f7d[_0xe4c4('0x42')];if(_0x10e8df[_0xe4c4('0x43')][_0xe4c4('0x53')]){_0x5587b2[_0xe4c4('0x54')]=[{'all':!![]}];}_0x5587b2=_[_0xe4c4('0x50')]({},_0x5587b2,_0x10e8df[_0xe4c4('0x52')]);return db[_0xe4c4('0x3f')]['find'](_0x5587b2)[_0xe4c4('0x22')](handleEntityNotFound(_0x1f6d78,null))[_0xe4c4('0x22')](respondWithResult(_0x1f6d78,null))[_0xe4c4('0x2d')](handleError(_0x1f6d78,null));};exports['create']=function(_0x3c6632,_0xb7e4da){return db[_0xe4c4('0x3f')]['create'](_0x3c6632[_0xe4c4('0x57')],{})['then'](respondWithResult(_0xb7e4da,0xc9))[_0xe4c4('0x2d')](handleError(_0xb7e4da,null));};exports[_0xe4c4('0x36')]=function(_0x1cef48,_0x5b7194){if(_0x1cef48['body']['id']){delete _0x1cef48[_0xe4c4('0x57')]['id'];}return db[_0xe4c4('0x3f')][_0xe4c4('0x58')]({'where':{'id':_0x1cef48['params']['id']}})[_0xe4c4('0x22')](handleEntityNotFound(_0x5b7194,null))['then'](saveUpdates(_0x1cef48['body'],null))['then'](respondWithResult(_0x5b7194,null))['catch'](handleError(_0x5b7194,null));};exports[_0xe4c4('0x37')]=function(_0x3a06da,_0x1d89ff){return db[_0xe4c4('0x3f')][_0xe4c4('0x58')]({'where':{'id':_0x3a06da[_0xe4c4('0x56')]['id']}})['then'](handleEntityNotFound(_0x1d89ff,null))[_0xe4c4('0x22')](removeEntity(_0x1d89ff,null))[_0xe4c4('0x2d')](handleError(_0x1d89ff,null));};exports[_0xe4c4('0x59')]=function(_0x125652,_0x261341){return db[_0xe4c4('0x3f')]['describe']()['then'](respondWithResult(_0x261341,null))[_0xe4c4('0x2d')](handleError(_0x261341,null));};var interaction_log=require(_0xe4c4('0x15'))(_0xe4c4('0x5a'));exports[_0xe4c4('0x5b')]=function(_0x3ccf42,_0x2a575d,_0x1dfc2d){var _0x24e1a2={'agent':{},'channel':_0xe4c4('0x5c')};if(_0x3ccf42['body']['id']){delete _0x3ccf42['body']['id'];}_0x3ccf42[_0xe4c4('0x57')]['read']=!![];_0x3ccf42[_0xe4c4('0x57')][_0xe4c4('0x5d')]=moment()[_0xe4c4('0x5e')]('YYYY-MM-DD\x20HH:mm:ss');_0x3ccf42[_0xe4c4('0x57')]['UserId']=_0x3ccf42[_0xe4c4('0x57')]['UserId']||_0x3ccf42['user']['id'];_0x24e1a2[_0xe4c4('0x5f')]['id']=_0x3ccf42[_0xe4c4('0x57')][_0xe4c4('0x60')];return db['ChatMessage']['find']({'where':{'id':_0x3ccf42['params']['id'],'UserId':null},'include':[{'model':db[_0xe4c4('0x61')],'as':_0xe4c4('0x62')}]})['then'](handleEntityNotFound(_0x2a575d,null))[_0xe4c4('0x22')](saveUpdates(_0x3ccf42[_0xe4c4('0x57')],null))[_0xe4c4('0x22')](function(_0x5c389b){if(_0x5c389b){_0x24e1a2['message']=_0x5c389b[_0xe4c4('0x63')]({'plain':!![]});return db[_0xe4c4('0x64')][_0xe4c4('0x58')]({'where':{'id':_0x5c389b[_0xe4c4('0x65')]}});}return null;})[_0xe4c4('0x22')](handleEntityNotFound(_0x2a575d,null))[_0xe4c4('0x22')](function(_0x4b9879){if(_0x4b9879){return _0x4b9879[_0xe4c4('0x36')]({'UserId':_0x3ccf42[_0xe4c4('0x57')][_0xe4c4('0x60')],'read1stAt':_['isNil'](_0x4b9879[_0xe4c4('0x66')])?moment()[_0xe4c4('0x5e')](_0xe4c4('0x67')):undefined});}return null;})['then'](function(_0x5bb0cf){if(_0x5bb0cf){_0x24e1a2[_0xe4c4('0x68')]=_0x5bb0cf['get']({'plain':!![]});interaction_log['info'](_0xe4c4('0x69'),_0x3ccf42[_0xe4c4('0x6a')]['id'],_0x3ccf42[_0xe4c4('0x6a')][_0xe4c4('0x3b')],_0x3ccf42[_0xe4c4('0x6a')][_0xe4c4('0x6b')],_0x24e1a2[_0xe4c4('0x68')]['id'],_0x3ccf42[_0xe4c4('0x57')]?JSON['stringify'](_0x3ccf42[_0xe4c4('0x57')]):_0xe4c4('0x6c'));return respondWithRpcPromise('AcceptMessage','acceptMessage',_0x24e1a2);}return null;})[_0xe4c4('0x22')](function(_0x2a7a5a){if(_0x2a7a5a){if(_0x3ccf42[_0xe4c4('0x57')][_0xe4c4('0x6d')]){_0x24e1a2['message'][_0xe4c4('0x6e')]=util[_0xe4c4('0x5e')](_0xe4c4('0x6f'),_0x3ccf42['user']['name']);_0x24e1a2[_0xe4c4('0x2b')][_0xe4c4('0x70')]=_0x24e1a2[_0xe4c4('0x70')];_0x24e1a2[_0xe4c4('0x2b')]['event']='accept';return respondWithRpcPromise(_0xe4c4('0x71'),_0xe4c4('0x71'),{'event':_0xe4c4('0x72'),'message':_[_0xe4c4('0x50')](_0x24e1a2[_0xe4c4('0x2b')],JSON['parse'](_0x24e1a2[_0xe4c4('0x68')]['formData']))},client9002)[_0xe4c4('0x22')](function(){return _0x24e1a2;});}return _0x24e1a2;}return null;})[_0xe4c4('0x22')](respondWithResult(_0x2a575d,null))['catch'](handleError(_0x2a575d,null));};var interaction_log=require('../../config/logger')(_0xe4c4('0x5a'));exports[_0xe4c4('0x73')]=function(_0x250bb8,_0x1f880f,_0x4270ab){var _0x5b41e6={'agent':{},'channel':'chat'};if(_0x250bb8['body']['id']){delete _0x250bb8[_0xe4c4('0x57')]['id'];}_0x250bb8[_0xe4c4('0x57')]['UserId']=_0x250bb8[_0xe4c4('0x57')][_0xe4c4('0x60')]||_0x250bb8[_0xe4c4('0x6a')]['id'];_0x5b41e6[_0xe4c4('0x5f')]['id']=_0x250bb8['body']['UserId'];return db[_0xe4c4('0x3f')][_0xe4c4('0x58')]({'where':{'id':_0x250bb8[_0xe4c4('0x56')]['id'],'UserId':null}})[_0xe4c4('0x22')](handleEntityNotFound(_0x1f880f,null))[_0xe4c4('0x22')](function(_0x10dc38){if(_0x10dc38){_0x5b41e6[_0xe4c4('0x2b')]=_0x10dc38['get']({'plain':!![]});return db[_0xe4c4('0x64')][_0xe4c4('0x58')]({'where':{'id':_0x10dc38[_0xe4c4('0x65')]}});}return null;})[_0xe4c4('0x22')](handleEntityNotFound(_0x1f880f,null))['then'](function(_0x3f02ed){if(_0x3f02ed){_0x5b41e6[_0xe4c4('0x68')]=_0x3f02ed[_0xe4c4('0x63')]({'plain':!![]});interaction_log[_0xe4c4('0x23')]('[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x250bb8[_0xe4c4('0x6a')]['id'],_0x250bb8[_0xe4c4('0x6a')]['name'],_0x250bb8['user'][_0xe4c4('0x6b')],_0x5b41e6[_0xe4c4('0x68')]['id'],_0x250bb8[_0xe4c4('0x57')]?JSON[_0xe4c4('0x28')](_0x250bb8[_0xe4c4('0x57')]):_0xe4c4('0x6c'));return respondWithRpcPromise(_0xe4c4('0x74'),_0xe4c4('0x75'),_0x5b41e6);}return null;})['then'](function(_0x5ae9f0){if(_0x5ae9f0){_0x5ae9f0[_0xe4c4('0x2b')][_0xe4c4('0x6e')]=util[_0xe4c4('0x5e')](_0xe4c4('0x6f'),_0x250bb8[_0xe4c4('0x6a')]['name']);_0x5ae9f0[_0xe4c4('0x2b')][_0xe4c4('0x70')]=_0x5b41e6[_0xe4c4('0x70')];_0x5ae9f0[_0xe4c4('0x2b')][_0xe4c4('0x76')]=_0xe4c4('0x73');respondWithRpcPromise(_0xe4c4('0x71'),_0xe4c4('0x71'),{'event':_0xe4c4('0x77'),'message':_0x5ae9f0[_0xe4c4('0x2b')]},client9002);return _0x5b41e6;}return null;})[_0xe4c4('0x22')](respondWithResult(_0x1f880f,null))[_0xe4c4('0x2d')](handleError(_0x1f880f,null));};