fd1fa800c84157a78f53f4f360c27e7cb8fcc562
[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 _0xf782=['eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','lodash','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','redis','localhost','./chatMessage.socket','client','http','test','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','request','info','ChatMessage,\x20%s,\x20%s','request\x20sent','debug','stringify','error','code','message','result','catch','sendStatus','status','offset','limit','set','Content-Range','apply','reject','update','then','destroy','end','name','send','ChatMessage','rawAttributes','fieldName','key','model','map','keys','query','intersection','attributes','length','hasOwnProperty','order','sort','where','pick','filters','merge','type','filter','options','count','include','findAll','rows','show','params','fields','includeAll','find','body','chat-interactions','read','format','YYYY-MM-DD\x20HH:mm:ss','UserId','user','CmContact','Contact','ChatInteraction','isNil','get','interaction','null','AcceptMessage','acceptMessage','manual','interface','SIP/%s','channel','event','accept','EventManager','acceptmessage','parse','formData','agent','ChatInteractionId','[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','RejectMessage','rejectMessage','rejectmessage','html-pdf'];(function(_0x1c3ff6,_0x5a480e){var _0x5663fe=function(_0x35ad98){while(--_0x35ad98){_0x1c3ff6['push'](_0x1c3ff6['shift']());}};_0x5663fe(++_0x5a480e);}(_0xf782,0x1e3));var _0x2f78=function(_0x26dd9a,_0x28a8e1){_0x26dd9a=_0x26dd9a-0x0;var _0x32c5f8=_0xf782[_0x26dd9a];return _0x32c5f8;};'use strict';var pdf=require(_0x2f78('0x0'));var emlformat=require(_0x2f78('0x1'));var rimraf=require(_0x2f78('0x2'));var zipdir=require(_0x2f78('0x3'));var jsonpatch=require(_0x2f78('0x4'));var rp=require(_0x2f78('0x5'));var moment=require(_0x2f78('0x6'));var BPromise=require(_0x2f78('0x7'));var Mustache=require(_0x2f78('0x8'));var util=require(_0x2f78('0x9'));var path=require(_0x2f78('0xa'));var sox=require('sox');var csv=require(_0x2f78('0xb'));var ejs=require(_0x2f78('0xc'));var fs=require('fs');var _=require(_0x2f78('0xd'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x2f78('0xe'));var deskjs=require(_0x2f78('0xf'));var toCsv=require(_0x2f78('0xb'));var querystring=require(_0x2f78('0x10'));var Papa=require('papaparse');var Redis=require(_0x2f78('0x11'));var authService=require(_0x2f78('0x12'));var qs=require(_0x2f78('0x13'));var as=require(_0x2f78('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x2f78('0x15'))(_0x2f78('0x16'));var utils=require(_0x2f78('0x17'));var config=require(_0x2f78('0x18'));var db=require(_0x2f78('0x19'))['db'];config[_0x2f78('0x1a')]=_['defaults'](config['redis'],{'host':_0x2f78('0x1b'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x2f78('0x1a')]));require(_0x2f78('0x1c'))['register'](socket);var jayson=require('jayson/promise');var client=jayson[_0x2f78('0x1d')][_0x2f78('0x1e')]({'port':0x232c});var client9002=jayson[_0x2f78('0x1d')][_0x2f78('0x1e')]({'port':0x232a});function checkPasswordPattern(_0x36f07b,_0x3059b1,_0x2c42bc){if(_0x2c42bc){var _0x1da315=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x1da315[_0x2f78('0x1f')](_0x3059b1)){return _0x36f07b;}else{throw new db['Sequelize']['ValidationError'](_0x2f78('0x20'));}}else{return _0x36f07b;}}function respondWithRpcPromise(_0x33506a,_0x199b98,_0x468026,_0x344e1e){return new BPromise(function(_0xe40e25,_0x395c39){var _0x32bbe9=_0x344e1e||client;return _0x32bbe9[_0x2f78('0x21')](_0x33506a,_0x468026)['then'](function(_0x1e6bf3){logger[_0x2f78('0x22')](_0x2f78('0x23'),_0x199b98,_0x2f78('0x24'));logger[_0x2f78('0x25')]('ChatMessage,\x20%s,\x20%s,\x20%s',_0x199b98,'request\x20sent',JSON[_0x2f78('0x26')](_0x1e6bf3));if(_0x1e6bf3[_0x2f78('0x27')]){if(_0x1e6bf3[_0x2f78('0x27')][_0x2f78('0x28')]===0x1f4){logger[_0x2f78('0x27')](_0x2f78('0x23'),_0x199b98,_0x1e6bf3['error'][_0x2f78('0x29')]);return _0x395c39(_0x1e6bf3[_0x2f78('0x27')][_0x2f78('0x29')]);}logger[_0x2f78('0x27')]('ChatMessage,\x20%s,\x20%s',_0x199b98,_0x1e6bf3[_0x2f78('0x27')][_0x2f78('0x29')]);return _0xe40e25(_0x1e6bf3[_0x2f78('0x27')][_0x2f78('0x29')]);}else{logger[_0x2f78('0x22')](_0x2f78('0x23'),_0x199b98,_0x2f78('0x24'));_0xe40e25(_0x1e6bf3[_0x2f78('0x2a')][_0x2f78('0x29')]);}})[_0x2f78('0x2b')](function(_0x3d0ca1){logger[_0x2f78('0x27')](_0x2f78('0x23'),_0x199b98,_0x3d0ca1);_0x395c39(_0x3d0ca1);});});}function respondWithStatusCode(_0x4ab906,_0x2f8584){_0x2f8584=_0x2f8584||0xcc;return function(_0x44e460){if(_0x44e460){return _0x4ab906[_0x2f78('0x2c')](_0x2f8584);}return _0x4ab906[_0x2f78('0x2d')](_0x2f8584)['end']();};}function respondWithResult(_0x2fe496,_0x5d3f6f){_0x5d3f6f=_0x5d3f6f||0xc8;return function(_0x2cb62e){if(_0x2cb62e){return _0x2fe496['status'](_0x5d3f6f)['json'](_0x2cb62e);}};}function respondWithFilteredResult(_0x5a1fd5,_0x45b997){return function(_0x51ffb1){if(_0x51ffb1){var _0x2185ec=_0x51ffb1['count'],_0x16cdd2=_0x45b997[_0x2f78('0x2e')],_0x327afc=_0x45b997['offset']+_0x45b997[_0x2f78('0x2f')],_0x5e2b82;if(_0x327afc>=_0x2185ec){_0x327afc=_0x2185ec;_0x5e2b82=0xc8;}else{_0x5e2b82=0xce;}_0x5a1fd5[_0x2f78('0x2d')](_0x5e2b82);return _0x5a1fd5[_0x2f78('0x30')](_0x2f78('0x31'),_0x16cdd2+'-'+_0x327afc+'/'+_0x2185ec)['json'](_0x51ffb1);}return null;};}function patchUpdates(_0x5f29d4){return function(_0x87154b){try{jsonpatch[_0x2f78('0x32')](_0x87154b,_0x5f29d4,!![]);}catch(_0x32e9c4){return BPromise[_0x2f78('0x33')](_0x32e9c4);}return _0x87154b['save']();};}function saveUpdates(_0x2de6fe,_0x5c766e){return function(_0x5d90ce){if(_0x5d90ce){return _0x5d90ce[_0x2f78('0x34')](_0x2de6fe)[_0x2f78('0x35')](function(_0x8f5f82){return _0x8f5f82;});}return null;};}function removeEntity(_0xe65e46,_0x10ac9c){return function(_0x4878a1){if(_0x4878a1){return _0x4878a1[_0x2f78('0x36')]()['then'](function(){_0xe65e46[_0x2f78('0x2d')](0xcc)[_0x2f78('0x37')]();});}};}function handleEntityNotFound(_0x16a572,_0x3c2250){return function(_0x37056e){if(!_0x37056e){_0x16a572[_0x2f78('0x2c')](0x194);}return _0x37056e;};}function handleError(_0xb48a4e,_0x42d04a){_0x42d04a=_0x42d04a||0x1f4;return function(_0x55e822){logger[_0x2f78('0x27')](_0x55e822['stack']);if(_0x55e822[_0x2f78('0x38')]){delete _0x55e822[_0x2f78('0x38')];}_0xb48a4e[_0x2f78('0x2d')](_0x42d04a)[_0x2f78('0x39')](_0x55e822);};}exports['index']=function(_0x69675,_0x37d007){var _0x2437b7={},_0x5ad207={},_0x173d01={'count':0x0,'rows':[]};var _0x3a2836=_['map'](db[_0x2f78('0x3a')][_0x2f78('0x3b')],function(_0x2ebcf6){return{'name':_0x2ebcf6[_0x2f78('0x3c')],'type':_0x2ebcf6['type'][_0x2f78('0x3d')]};});_0x5ad207[_0x2f78('0x3e')]=_[_0x2f78('0x3f')](_0x3a2836,_0x2f78('0x38'));_0x5ad207['query']=_[_0x2f78('0x40')](_0x69675[_0x2f78('0x41')]);_0x5ad207['filters']=_['intersection'](_0x5ad207[_0x2f78('0x3e')],_0x5ad207[_0x2f78('0x41')]);_0x2437b7['attributes']=_[_0x2f78('0x42')](_0x5ad207[_0x2f78('0x3e')],qs['fields'](_0x69675['query']['fields']));_0x2437b7['attributes']=_0x2437b7[_0x2f78('0x43')][_0x2f78('0x44')]?_0x2437b7['attributes']:_0x5ad207[_0x2f78('0x3e')];if(!_0x69675[_0x2f78('0x41')][_0x2f78('0x45')]('nolimit')){_0x2437b7[_0x2f78('0x2f')]=qs[_0x2f78('0x2f')](_0x69675['query'][_0x2f78('0x2f')]);_0x2437b7[_0x2f78('0x2e')]=qs[_0x2f78('0x2e')](_0x69675[_0x2f78('0x41')][_0x2f78('0x2e')]);}_0x2437b7[_0x2f78('0x46')]=qs['sort'](_0x69675[_0x2f78('0x41')][_0x2f78('0x47')]);_0x2437b7[_0x2f78('0x48')]=qs['filters'](_[_0x2f78('0x49')](_0x69675[_0x2f78('0x41')],_0x5ad207[_0x2f78('0x4a')]),_0x3a2836);if(_0x69675['query']['filter']){_0x2437b7[_0x2f78('0x48')]=_[_0x2f78('0x4b')](_0x2437b7[_0x2f78('0x48')],{'$or':_[_0x2f78('0x3f')](_0x3a2836,function(_0x57f1c8){if(_0x57f1c8[_0x2f78('0x4c')]!=='VIRTUAL'){var _0x514257={};_0x514257[_0x57f1c8[_0x2f78('0x38')]]={'$like':'%'+_0x69675[_0x2f78('0x41')][_0x2f78('0x4d')]+'%'};return _0x514257;}})});}_0x2437b7=_['merge']({},_0x2437b7,_0x69675[_0x2f78('0x4e')]);var _0x1e2e50={'where':_0x2437b7['where']};return db[_0x2f78('0x3a')][_0x2f78('0x4f')](_0x1e2e50)[_0x2f78('0x35')](function(_0x127e8c){_0x173d01[_0x2f78('0x4f')]=_0x127e8c;if(_0x69675['query']['includeAll']){_0x2437b7[_0x2f78('0x50')]=[{'all':!![]}];}return db[_0x2f78('0x3a')][_0x2f78('0x51')](_0x2437b7);})[_0x2f78('0x35')](function(_0x3e90c9){_0x173d01[_0x2f78('0x52')]=_0x3e90c9;return _0x173d01;})['then'](respondWithFilteredResult(_0x37d007,_0x2437b7))[_0x2f78('0x2b')](handleError(_0x37d007,null));};exports[_0x2f78('0x53')]=function(_0x3c0b06,_0x3f97ba){var _0x1a1f24={'raw':!![],'where':{'id':_0x3c0b06[_0x2f78('0x54')]['id']}},_0x2dd351={};_0x2dd351[_0x2f78('0x3e')]=_[_0x2f78('0x40')](db[_0x2f78('0x3a')][_0x2f78('0x3b')]);_0x2dd351[_0x2f78('0x41')]=_['keys'](_0x3c0b06[_0x2f78('0x41')]);_0x2dd351[_0x2f78('0x4a')]=_[_0x2f78('0x42')](_0x2dd351[_0x2f78('0x3e')],_0x2dd351[_0x2f78('0x41')]);_0x1a1f24['attributes']=_[_0x2f78('0x42')](_0x2dd351['model'],qs[_0x2f78('0x55')](_0x3c0b06[_0x2f78('0x41')]['fields']));_0x1a1f24[_0x2f78('0x43')]=_0x1a1f24['attributes'][_0x2f78('0x44')]?_0x1a1f24['attributes']:_0x2dd351[_0x2f78('0x3e')];if(_0x3c0b06[_0x2f78('0x41')][_0x2f78('0x56')]){_0x1a1f24['include']=[{'all':!![]}];}_0x1a1f24=_[_0x2f78('0x4b')]({},_0x1a1f24,_0x3c0b06[_0x2f78('0x4e')]);return db[_0x2f78('0x3a')][_0x2f78('0x57')](_0x1a1f24)[_0x2f78('0x35')](handleEntityNotFound(_0x3f97ba,null))[_0x2f78('0x35')](respondWithResult(_0x3f97ba,null))[_0x2f78('0x2b')](handleError(_0x3f97ba,null));};exports['create']=function(_0x3658cb,_0x189ef1){return db[_0x2f78('0x3a')]['create'](_0x3658cb['body'],{})['then'](respondWithResult(_0x189ef1,0xc9))[_0x2f78('0x2b')](handleError(_0x189ef1,null));};exports[_0x2f78('0x34')]=function(_0x69ef77,_0x3baa8f){if(_0x69ef77['body']['id']){delete _0x69ef77['body']['id'];}return db['ChatMessage'][_0x2f78('0x57')]({'where':{'id':_0x69ef77[_0x2f78('0x54')]['id']}})[_0x2f78('0x35')](handleEntityNotFound(_0x3baa8f,null))[_0x2f78('0x35')](saveUpdates(_0x69ef77[_0x2f78('0x58')],null))[_0x2f78('0x35')](respondWithResult(_0x3baa8f,null))['catch'](handleError(_0x3baa8f,null));};exports['destroy']=function(_0x4fcaf5,_0x150578){return db['ChatMessage'][_0x2f78('0x57')]({'where':{'id':_0x4fcaf5['params']['id']}})[_0x2f78('0x35')](handleEntityNotFound(_0x150578,null))['then'](removeEntity(_0x150578,null))[_0x2f78('0x2b')](handleError(_0x150578,null));};exports['describe']=function(_0x150e59,_0x16c642){return db[_0x2f78('0x3a')]['describe']()[_0x2f78('0x35')](respondWithResult(_0x16c642,null))[_0x2f78('0x2b')](handleError(_0x16c642,null));};var interaction_log=require(_0x2f78('0x15'))(_0x2f78('0x59'));exports['accept']=function(_0xe1f2d5,_0x50e763,_0x234302){var _0x235b66={'channel':'chat'};if(_0xe1f2d5[_0x2f78('0x58')]['id']){delete _0xe1f2d5['body']['id'];}_0xe1f2d5[_0x2f78('0x58')][_0x2f78('0x5a')]=!![];_0xe1f2d5[_0x2f78('0x58')]['readAt']=moment()[_0x2f78('0x5b')](_0x2f78('0x5c'));_0xe1f2d5[_0x2f78('0x58')][_0x2f78('0x5d')]=_0xe1f2d5[_0x2f78('0x58')][_0x2f78('0x5d')]||_0xe1f2d5[_0x2f78('0x5e')]['id'];return db[_0x2f78('0x3a')][_0x2f78('0x57')]({'where':{'id':_0xe1f2d5[_0x2f78('0x54')]['id'],'UserId':null},'include':[{'model':db[_0x2f78('0x5f')],'as':_0x2f78('0x60')}]})[_0x2f78('0x35')](handleEntityNotFound(_0x50e763,null))[_0x2f78('0x35')](saveUpdates(_0xe1f2d5['body'],null))[_0x2f78('0x35')](function(_0x13876b){if(_0x13876b){_0x235b66['message']=_0x13876b['get']({'plain':!![]});return db[_0x2f78('0x61')]['find']({'where':{'id':_0x13876b['ChatInteractionId']}});}return null;})['then'](handleEntityNotFound(_0x50e763,null))[_0x2f78('0x35')](function(_0x2153cb){if(_0x2153cb){return _0x2153cb[_0x2f78('0x34')]({'UserId':_0xe1f2d5[_0x2f78('0x58')][_0x2f78('0x5d')],'read1stAt':_[_0x2f78('0x62')](_0x2153cb['read1stAt'])?moment()[_0x2f78('0x5b')](_0x2f78('0x5c')):undefined});}return null;})[_0x2f78('0x35')](function(_0x10e530){if(_0x10e530){_0x235b66['interaction']=_0x10e530[_0x2f78('0x63')]({'plain':!![]});interaction_log[_0x2f78('0x22')]('[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0xe1f2d5[_0x2f78('0x5e')]['id'],_0xe1f2d5[_0x2f78('0x5e')]['name'],_0xe1f2d5[_0x2f78('0x5e')]['role'],_0x235b66[_0x2f78('0x64')]['id'],_0xe1f2d5['body']?JSON[_0x2f78('0x26')](_0xe1f2d5[_0x2f78('0x58')]):_0x2f78('0x65'));return respondWithRpcPromise(_0x2f78('0x66'),_0x2f78('0x67'),_0x235b66);}return null;})[_0x2f78('0x35')](function(_0x4d11bd){if(_0x4d11bd){if(_0xe1f2d5['body'][_0x2f78('0x68')]){_0x235b66[_0x2f78('0x29')][_0x2f78('0x69')]=util['format'](_0x2f78('0x6a'),_0xe1f2d5[_0x2f78('0x5e')][_0x2f78('0x38')]);_0x235b66['message']['channel']=_0x235b66[_0x2f78('0x6b')];_0x235b66[_0x2f78('0x29')][_0x2f78('0x6c')]=_0x2f78('0x6d');return respondWithRpcPromise(_0x2f78('0x6e'),_0x2f78('0x6e'),{'event':_0x2f78('0x6f'),'message':_[_0x2f78('0x4b')](_0x235b66[_0x2f78('0x29')],JSON[_0x2f78('0x70')](_0x235b66[_0x2f78('0x64')][_0x2f78('0x71')]))},client9002)[_0x2f78('0x35')](function(){return _0x235b66;});}return _0x235b66;}return null;})['then'](respondWithResult(_0x50e763,null))[_0x2f78('0x2b')](handleError(_0x50e763,null));};var interaction_log=require('../../config/logger')(_0x2f78('0x59'));exports[_0x2f78('0x33')]=function(_0x269e8b,_0x297287,_0x442cf0){var _0x150488={'agent':{},'channel':'chat'};if(_0x269e8b['body']['id']){delete _0x269e8b[_0x2f78('0x58')]['id'];}_0x269e8b[_0x2f78('0x58')][_0x2f78('0x5d')]=_0x269e8b[_0x2f78('0x58')][_0x2f78('0x5d')]||_0x269e8b['user']['id'];_0x150488[_0x2f78('0x72')]['id']=_0x269e8b[_0x2f78('0x58')][_0x2f78('0x5d')];return db[_0x2f78('0x3a')][_0x2f78('0x57')]({'where':{'id':_0x269e8b[_0x2f78('0x54')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x297287,null))[_0x2f78('0x35')](function(_0x2c1975){if(_0x2c1975){_0x150488[_0x2f78('0x29')]=_0x2c1975[_0x2f78('0x63')]({'plain':!![]});return db[_0x2f78('0x61')][_0x2f78('0x57')]({'where':{'id':_0x2c1975[_0x2f78('0x73')]}});}return null;})[_0x2f78('0x35')](handleEntityNotFound(_0x297287,null))[_0x2f78('0x35')](function(_0x55dce2){if(_0x55dce2){_0x150488['interaction']=_0x55dce2[_0x2f78('0x63')]({'plain':!![]});interaction_log['info'](_0x2f78('0x74'),_0x269e8b['user']['id'],_0x269e8b['user'][_0x2f78('0x38')],_0x269e8b[_0x2f78('0x5e')][_0x2f78('0x75')],_0x150488[_0x2f78('0x64')]['id'],_0x269e8b[_0x2f78('0x58')]?JSON['stringify'](_0x269e8b[_0x2f78('0x58')]):_0x2f78('0x65'));return respondWithRpcPromise(_0x2f78('0x76'),_0x2f78('0x77'),_0x150488);}return null;})[_0x2f78('0x35')](function(_0x32f77b){if(_0x32f77b){_0x32f77b['message']['interface']=util[_0x2f78('0x5b')]('SIP/%s',_0x269e8b['user'][_0x2f78('0x38')]);_0x32f77b['message'][_0x2f78('0x6b')]=_0x150488['channel'];_0x32f77b[_0x2f78('0x29')]['event']=_0x2f78('0x33');respondWithRpcPromise('EventManager','EventManager',{'event':_0x2f78('0x78'),'message':_0x32f77b[_0x2f78('0x29')]},client9002);return _0x150488;}return null;})[_0x2f78('0x35')](respondWithResult(_0x297287,null))[_0x2f78('0x2b')](handleError(_0x297287,null));};