Built motion from commit (unavailable).|2.5.21
[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 _0x5f22=['catch','sendStatus','status','json','count','offset','limit','set','Content-Range','apply','reject','update','destroy','name','send','index','map','ChatMessage','rawAttributes','fieldName','key','model','keys','query','filters','intersection','attributes','fields','length','nolimit','order','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','findAll','rows','show','params','include','find','create','body','describe','accept','chat','read','readAt','format','YYYY-MM-DD\x20HH:mm:ss','UserId','user','agent','CmContact','get','ChatInteraction','ChatInteractionId','isNil','read1stAt','interaction','[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','AcceptMessage','interface','channel','event','EventManager','acceptmessage','parse','chat-interactions','RejectMessage','rejectMessage','SIP/%s','rejectmessage','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','path','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','defaults','localhost','socket.io-emitter','./chatMessage.socket','register','jayson/promise','client','http','request','then','info','request\x20sent','debug','ChatMessage,\x20%s,\x20%s,\x20%s','stringify','error','ChatMessage,\x20%s,\x20%s','message'];(function(_0x587eeb,_0x437854){var _0x4791ec=function(_0x4f44e0){while(--_0x4f44e0){_0x587eeb['push'](_0x587eeb['shift']());}};_0x4791ec(++_0x437854);}(_0x5f22,0x144));var _0x25f2=function(_0x2df724,_0x411382){_0x2df724=_0x2df724-0x0;var _0x1b2504=_0x5f22[_0x2df724];return _0x1b2504;};'use strict';var pdf=require(_0x25f2('0x0'));var emlformat=require(_0x25f2('0x1'));var rimraf=require(_0x25f2('0x2'));var zipdir=require(_0x25f2('0x3'));var jsonpatch=require(_0x25f2('0x4'));var rp=require('request-promise');var moment=require(_0x25f2('0x5'));var BPromise=require(_0x25f2('0x6'));var Mustache=require('mustache');var util=require('util');var path=require(_0x25f2('0x7'));var sox=require('sox');var csv=require(_0x25f2('0x8'));var ejs=require(_0x25f2('0x9'));var fs=require('fs');var _=require('lodash');var squel=require(_0x25f2('0xa'));var crypto=require(_0x25f2('0xb'));var jsforce=require(_0x25f2('0xc'));var deskjs=require(_0x25f2('0xd'));var toCsv=require('to-csv');var querystring=require(_0x25f2('0xe'));var Papa=require(_0x25f2('0xf'));var Redis=require(_0x25f2('0x10'));var authService=require(_0x25f2('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x25f2('0x12'));var hardwareService=require(_0x25f2('0x13'));var logger=require(_0x25f2('0x14'))('api');var utils=require(_0x25f2('0x15'));var config=require(_0x25f2('0x16'));var licenseUtil=require(_0x25f2('0x17'));var db=require('../../mysqldb')['db'];config['redis']=_[_0x25f2('0x18')](config['redis'],{'host':_0x25f2('0x19'),'port':0x18eb});var socket=require(_0x25f2('0x1a'))(new Redis(config['redis']));require(_0x25f2('0x1b'))[_0x25f2('0x1c')](socket);var jayson=require(_0x25f2('0x1d'));var client=jayson[_0x25f2('0x1e')][_0x25f2('0x1f')]({'port':0x232c});var client9002=jayson[_0x25f2('0x1e')][_0x25f2('0x1f')]({'port':0x232a});function respondWithRpcPromise(_0x9329a6,_0x822fa8,_0x1e9d54,_0x3db9ab){return new BPromise(function(_0x5d97df,_0x3e8b61){var _0x4fd6bb=_0x3db9ab||client;return _0x4fd6bb[_0x25f2('0x20')](_0x9329a6,_0x1e9d54)[_0x25f2('0x21')](function(_0x421829){logger[_0x25f2('0x22')]('ChatMessage,\x20%s,\x20%s',_0x822fa8,_0x25f2('0x23'));logger[_0x25f2('0x24')](_0x25f2('0x25'),_0x822fa8,_0x25f2('0x23'),JSON[_0x25f2('0x26')](_0x421829));if(_0x421829[_0x25f2('0x27')]){if(_0x421829[_0x25f2('0x27')]['code']===0x1f4){logger[_0x25f2('0x27')](_0x25f2('0x28'),_0x822fa8,_0x421829[_0x25f2('0x27')]['message']);return _0x3e8b61(_0x421829[_0x25f2('0x27')][_0x25f2('0x29')]);}logger[_0x25f2('0x27')](_0x25f2('0x28'),_0x822fa8,_0x421829['error'][_0x25f2('0x29')]);return _0x5d97df(_0x421829[_0x25f2('0x27')][_0x25f2('0x29')]);}else{logger[_0x25f2('0x22')](_0x25f2('0x28'),_0x822fa8,_0x25f2('0x23'));_0x5d97df(_0x421829['result'][_0x25f2('0x29')]);}})[_0x25f2('0x2a')](function(_0x7bec3e){logger['error']('ChatMessage,\x20%s,\x20%s',_0x822fa8,_0x7bec3e);_0x3e8b61(_0x7bec3e);});});}function respondWithStatusCode(_0x23d5fe,_0x376cab){_0x376cab=_0x376cab||0xcc;return function(_0x4d7207){if(_0x4d7207){return _0x23d5fe[_0x25f2('0x2b')](_0x376cab);}return _0x23d5fe[_0x25f2('0x2c')](_0x376cab)['end']();};}function respondWithResult(_0x569495,_0x2751b6){_0x2751b6=_0x2751b6||0xc8;return function(_0x36b073){if(_0x36b073){return _0x569495[_0x25f2('0x2c')](_0x2751b6)[_0x25f2('0x2d')](_0x36b073);}};}function respondWithFilteredResult(_0x1886a8,_0x180cfa){return function(_0x47fe33){if(_0x47fe33){var _0x344b56=_0x47fe33[_0x25f2('0x2e')],_0x2f5e29=_0x180cfa[_0x25f2('0x2f')],_0x5c28a4=_0x180cfa['offset']+_0x180cfa[_0x25f2('0x30')],_0x1dc046;if(_0x5c28a4>=_0x344b56){_0x5c28a4=_0x344b56;_0x1dc046=0xc8;}else{_0x1dc046=0xce;}_0x1886a8[_0x25f2('0x2c')](_0x1dc046);return _0x1886a8[_0x25f2('0x31')](_0x25f2('0x32'),_0x2f5e29+'-'+_0x5c28a4+'/'+_0x344b56)[_0x25f2('0x2d')](_0x47fe33);}return null;};}function patchUpdates(_0x9173cf){return function(_0x58c816){try{jsonpatch[_0x25f2('0x33')](_0x58c816,_0x9173cf,!![]);}catch(_0x3028ac){return BPromise[_0x25f2('0x34')](_0x3028ac);}return _0x58c816['save']();};}function saveUpdates(_0xae3a27,_0x3d172a){return function(_0xca881b){if(_0xca881b){return _0xca881b[_0x25f2('0x35')](_0xae3a27)[_0x25f2('0x21')](function(_0x306002){return _0x306002;});}return null;};}function removeEntity(_0xffb468,_0x131f11){return function(_0x28a42b){if(_0x28a42b){return _0x28a42b[_0x25f2('0x36')]()[_0x25f2('0x21')](function(){_0xffb468['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x5b4465,_0x5688bc){return function(_0xa690c1){if(!_0xa690c1){_0x5b4465[_0x25f2('0x2b')](0x194);}return _0xa690c1;};}function handleError(_0x3a94c6,_0x5b8787){_0x5b8787=_0x5b8787||0x1f4;return function(_0x2d3673){logger['error'](_0x2d3673['stack']);if(_0x2d3673[_0x25f2('0x37')]){delete _0x2d3673['name'];}_0x3a94c6[_0x25f2('0x2c')](_0x5b8787)[_0x25f2('0x38')](_0x2d3673);};}exports[_0x25f2('0x39')]=function(_0x52650a,_0x14e1a6){var _0x13dc43={},_0x1ac24a={},_0x1e33d5={'count':0x0,'rows':[]};var _0x47139a=_[_0x25f2('0x3a')](db[_0x25f2('0x3b')][_0x25f2('0x3c')],function(_0x1f8156){return{'name':_0x1f8156[_0x25f2('0x3d')],'type':_0x1f8156['type'][_0x25f2('0x3e')]};});_0x1ac24a[_0x25f2('0x3f')]=_[_0x25f2('0x3a')](_0x47139a,_0x25f2('0x37'));_0x1ac24a['query']=_[_0x25f2('0x40')](_0x52650a[_0x25f2('0x41')]);_0x1ac24a[_0x25f2('0x42')]=_[_0x25f2('0x43')](_0x1ac24a[_0x25f2('0x3f')],_0x1ac24a[_0x25f2('0x41')]);_0x13dc43[_0x25f2('0x44')]=_[_0x25f2('0x43')](_0x1ac24a['model'],qs[_0x25f2('0x45')](_0x52650a[_0x25f2('0x41')][_0x25f2('0x45')]));_0x13dc43[_0x25f2('0x44')]=_0x13dc43[_0x25f2('0x44')][_0x25f2('0x46')]?_0x13dc43[_0x25f2('0x44')]:_0x1ac24a[_0x25f2('0x3f')];if(!_0x52650a['query']['hasOwnProperty'](_0x25f2('0x47'))){_0x13dc43[_0x25f2('0x30')]=qs['limit'](_0x52650a[_0x25f2('0x41')]['limit']);_0x13dc43[_0x25f2('0x2f')]=qs['offset'](_0x52650a['query']['offset']);}_0x13dc43[_0x25f2('0x48')]=qs[_0x25f2('0x49')](_0x52650a[_0x25f2('0x41')]['sort']);_0x13dc43[_0x25f2('0x4a')]=qs[_0x25f2('0x42')](_[_0x25f2('0x4b')](_0x52650a[_0x25f2('0x41')],_0x1ac24a['filters']),_0x47139a);if(_0x52650a[_0x25f2('0x41')][_0x25f2('0x4c')]){_0x13dc43[_0x25f2('0x4a')]=_[_0x25f2('0x4d')](_0x13dc43[_0x25f2('0x4a')],{'$or':_['map'](_0x47139a,function(_0x27308b){if(_0x27308b[_0x25f2('0x4e')]!==_0x25f2('0x4f')){var _0x27ba46={};_0x27ba46[_0x27308b[_0x25f2('0x37')]]={'$like':'%'+_0x52650a['query'][_0x25f2('0x4c')]+'%'};return _0x27ba46;}})});}_0x13dc43=_[_0x25f2('0x4d')]({},_0x13dc43,_0x52650a[_0x25f2('0x50')]);var _0x2ce1f0={'where':_0x13dc43[_0x25f2('0x4a')]};return db['ChatMessage'][_0x25f2('0x2e')](_0x2ce1f0)[_0x25f2('0x21')](function(_0x2c7971){_0x1e33d5[_0x25f2('0x2e')]=_0x2c7971;if(_0x52650a[_0x25f2('0x41')][_0x25f2('0x51')]){_0x13dc43['include']=[{'all':!![]}];}return db[_0x25f2('0x3b')][_0x25f2('0x52')](_0x13dc43);})[_0x25f2('0x21')](function(_0x26a88d){_0x1e33d5[_0x25f2('0x53')]=_0x26a88d;return _0x1e33d5;})[_0x25f2('0x21')](respondWithFilteredResult(_0x14e1a6,_0x13dc43))['catch'](handleError(_0x14e1a6,null));};exports[_0x25f2('0x54')]=function(_0x17df86,_0x112588){var _0x41dddf={'raw':!![],'where':{'id':_0x17df86[_0x25f2('0x55')]['id']}},_0x610025={};_0x610025[_0x25f2('0x3f')]=_[_0x25f2('0x40')](db[_0x25f2('0x3b')][_0x25f2('0x3c')]);_0x610025[_0x25f2('0x41')]=_[_0x25f2('0x40')](_0x17df86[_0x25f2('0x41')]);_0x610025[_0x25f2('0x42')]=_['intersection'](_0x610025['model'],_0x610025[_0x25f2('0x41')]);_0x41dddf[_0x25f2('0x44')]=_[_0x25f2('0x43')](_0x610025[_0x25f2('0x3f')],qs[_0x25f2('0x45')](_0x17df86[_0x25f2('0x41')][_0x25f2('0x45')]));_0x41dddf[_0x25f2('0x44')]=_0x41dddf[_0x25f2('0x44')]['length']?_0x41dddf[_0x25f2('0x44')]:_0x610025[_0x25f2('0x3f')];if(_0x17df86[_0x25f2('0x41')][_0x25f2('0x51')]){_0x41dddf[_0x25f2('0x56')]=[{'all':!![]}];}_0x41dddf=_[_0x25f2('0x4d')]({},_0x41dddf,_0x17df86[_0x25f2('0x50')]);return db[_0x25f2('0x3b')][_0x25f2('0x57')](_0x41dddf)[_0x25f2('0x21')](handleEntityNotFound(_0x112588,null))[_0x25f2('0x21')](respondWithResult(_0x112588,null))[_0x25f2('0x2a')](handleError(_0x112588,null));};exports['create']=function(_0x51f497,_0x35cb47){return db[_0x25f2('0x3b')][_0x25f2('0x58')](_0x51f497[_0x25f2('0x59')],{})[_0x25f2('0x21')](respondWithResult(_0x35cb47,0xc9))['catch'](handleError(_0x35cb47,null));};exports[_0x25f2('0x35')]=function(_0x316e6a,_0x126f51){if(_0x316e6a[_0x25f2('0x59')]['id']){delete _0x316e6a[_0x25f2('0x59')]['id'];}return db['ChatMessage'][_0x25f2('0x57')]({'where':{'id':_0x316e6a[_0x25f2('0x55')]['id']}})['then'](handleEntityNotFound(_0x126f51,null))[_0x25f2('0x21')](saveUpdates(_0x316e6a[_0x25f2('0x59')],null))['then'](respondWithResult(_0x126f51,null))[_0x25f2('0x2a')](handleError(_0x126f51,null));};exports['destroy']=function(_0x5c82dc,_0x145f58){return db[_0x25f2('0x3b')][_0x25f2('0x57')]({'where':{'id':_0x5c82dc[_0x25f2('0x55')]['id']}})['then'](handleEntityNotFound(_0x145f58,null))[_0x25f2('0x21')](removeEntity(_0x145f58,null))[_0x25f2('0x2a')](handleError(_0x145f58,null));};exports[_0x25f2('0x5a')]=function(_0x530126,_0x580af6){return db[_0x25f2('0x3b')][_0x25f2('0x5a')]()[_0x25f2('0x21')](respondWithResult(_0x580af6,null))[_0x25f2('0x2a')](handleError(_0x580af6,null));};var interaction_log=require(_0x25f2('0x14'))('chat-interactions');exports[_0x25f2('0x5b')]=function(_0x3ef774,_0x14ee49,_0x2dcee1){var _0xb73254={'agent':{},'channel':_0x25f2('0x5c')};if(_0x3ef774[_0x25f2('0x59')]['id']){delete _0x3ef774['body']['id'];}_0x3ef774[_0x25f2('0x59')][_0x25f2('0x5d')]=!![];_0x3ef774[_0x25f2('0x59')][_0x25f2('0x5e')]=moment()[_0x25f2('0x5f')](_0x25f2('0x60'));_0x3ef774[_0x25f2('0x59')][_0x25f2('0x61')]=_0x3ef774[_0x25f2('0x59')][_0x25f2('0x61')]||_0x3ef774[_0x25f2('0x62')]['id'];_0xb73254[_0x25f2('0x63')]['id']=_0x3ef774['body'][_0x25f2('0x61')];return db[_0x25f2('0x3b')]['find']({'where':{'id':_0x3ef774[_0x25f2('0x55')]['id'],'UserId':null},'include':[{'model':db[_0x25f2('0x64')],'as':'Contact'}]})[_0x25f2('0x21')](handleEntityNotFound(_0x14ee49,null))[_0x25f2('0x21')](saveUpdates(_0x3ef774[_0x25f2('0x59')],null))[_0x25f2('0x21')](function(_0x3163b0){if(_0x3163b0){_0xb73254['message']=_0x3163b0[_0x25f2('0x65')]({'plain':!![]});return db[_0x25f2('0x66')][_0x25f2('0x57')]({'where':{'id':_0x3163b0[_0x25f2('0x67')]}});}return null;})[_0x25f2('0x21')](handleEntityNotFound(_0x14ee49,null))[_0x25f2('0x21')](function(_0x4a5ee2){if(_0x4a5ee2){return _0x4a5ee2[_0x25f2('0x35')]({'UserId':_0x3ef774[_0x25f2('0x59')][_0x25f2('0x61')],'read1stAt':_[_0x25f2('0x68')](_0x4a5ee2[_0x25f2('0x69')])?moment()['format'](_0x25f2('0x60')):undefined});}return null;})['then'](function(_0x39090e){if(_0x39090e){_0xb73254[_0x25f2('0x6a')]=_0x39090e[_0x25f2('0x65')]({'plain':!![]});interaction_log['info'](_0x25f2('0x6b'),_0x3ef774[_0x25f2('0x62')]['id'],_0x3ef774[_0x25f2('0x62')][_0x25f2('0x37')],_0x3ef774[_0x25f2('0x62')][_0x25f2('0x6c')],_0xb73254[_0x25f2('0x6a')]['id'],_0x3ef774[_0x25f2('0x59')]?JSON[_0x25f2('0x26')](_0x3ef774[_0x25f2('0x59')]):_0x25f2('0x6d'));return respondWithRpcPromise(_0x25f2('0x6e'),'acceptMessage',_0xb73254);}return null;})[_0x25f2('0x21')](function(_0x27f462){if(_0x27f462){if(_0x3ef774['body']['manual']){_0xb73254['message'][_0x25f2('0x6f')]=util[_0x25f2('0x5f')]('SIP/%s',_0x3ef774['user'][_0x25f2('0x37')]);_0xb73254['message'][_0x25f2('0x70')]=_0xb73254['channel'];_0xb73254['message'][_0x25f2('0x71')]=_0x25f2('0x5b');return respondWithRpcPromise('EventManager',_0x25f2('0x72'),{'event':_0x25f2('0x73'),'message':_[_0x25f2('0x4d')](_0xb73254[_0x25f2('0x29')],JSON[_0x25f2('0x74')](_0xb73254[_0x25f2('0x6a')]['formData']))},client9002)[_0x25f2('0x21')](function(){return _0xb73254;});}return _0xb73254;}return null;})[_0x25f2('0x21')](respondWithResult(_0x14ee49,null))[_0x25f2('0x2a')](handleError(_0x14ee49,null));};var interaction_log=require(_0x25f2('0x14'))(_0x25f2('0x75'));exports['reject']=function(_0x5932c6,_0x1f87cc,_0x121642){var _0x2cd705={'agent':{},'channel':_0x25f2('0x5c')};if(_0x5932c6[_0x25f2('0x59')]['id']){delete _0x5932c6[_0x25f2('0x59')]['id'];}_0x5932c6['body'][_0x25f2('0x61')]=_0x5932c6[_0x25f2('0x59')][_0x25f2('0x61')]||_0x5932c6[_0x25f2('0x62')]['id'];_0x2cd705[_0x25f2('0x63')]['id']=_0x5932c6[_0x25f2('0x59')]['UserId'];return db['ChatMessage'][_0x25f2('0x57')]({'where':{'id':_0x5932c6[_0x25f2('0x55')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x1f87cc,null))['then'](function(_0x3122fd){if(_0x3122fd){_0x2cd705[_0x25f2('0x29')]=_0x3122fd[_0x25f2('0x65')]({'plain':!![]});return db[_0x25f2('0x66')][_0x25f2('0x57')]({'where':{'id':_0x3122fd[_0x25f2('0x67')]}});}return null;})['then'](handleEntityNotFound(_0x1f87cc,null))[_0x25f2('0x21')](function(_0x249909){if(_0x249909){_0x2cd705[_0x25f2('0x6a')]=_0x249909['get']({'plain':!![]});interaction_log[_0x25f2('0x22')]('[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x5932c6[_0x25f2('0x62')]['id'],_0x5932c6[_0x25f2('0x62')][_0x25f2('0x37')],_0x5932c6[_0x25f2('0x62')][_0x25f2('0x6c')],_0x2cd705[_0x25f2('0x6a')]['id'],_0x5932c6[_0x25f2('0x59')]?JSON[_0x25f2('0x26')](_0x5932c6[_0x25f2('0x59')]):_0x25f2('0x6d'));return respondWithRpcPromise(_0x25f2('0x76'),_0x25f2('0x77'),_0x2cd705);}return null;})[_0x25f2('0x21')](function(_0x57a519){if(_0x57a519){_0x57a519['message'][_0x25f2('0x6f')]=util['format'](_0x25f2('0x78'),_0x5932c6['user'][_0x25f2('0x37')]);_0x57a519[_0x25f2('0x29')][_0x25f2('0x70')]=_0x2cd705['channel'];_0x57a519[_0x25f2('0x29')][_0x25f2('0x71')]=_0x25f2('0x34');respondWithRpcPromise(_0x25f2('0x72'),'EventManager',{'event':_0x25f2('0x79'),'message':_0x57a519['message']},client9002);return _0x2cd705;}return null;})['then'](respondWithResult(_0x1f87cc,null))[_0x25f2('0x2a')](handleError(_0x1f87cc,null));};