4aee290debb7c9d2f037dc9082d753cf7da66534
[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 _0x5cfb=['squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../mysqldb','redis','socket.io-emitter','./chatMessage.socket','jayson/promise','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','ChatMessage,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','sendStatus','status','end','json','count','offset','limit','reject','save','then','stack','send','index','map','ChatMessage','rawAttributes','fieldName','key','model','name','query','keys','filters','attributes','intersection','fields','length','order','sort','where','merge','type','filter','options','includeAll','params','create','body','find','describe','../../config/logger','accept','chat','read','format','YYYY-MM-DD\x20HH:mm:ss','UserId','user','CmContact','get','ChatInteraction','ChatInteractionId','interaction','[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','manual','interface','channel','event','EventManager','acceptmessage','parse','formData','agent','[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','null','RejectMessage','rejectMessage','SIP/%s','rejectmessage','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','ejs'];(function(_0x583f72,_0x22e8d3){var _0x1ef581=function(_0x1e58f6){while(--_0x1e58f6){_0x583f72['push'](_0x583f72['shift']());}};_0x1ef581(++_0x22e8d3);}(_0x5cfb,0x1b8));var _0xb5cf=function(_0x2d7832,_0x2ada44){_0x2d7832=_0x2d7832-0x0;var _0x2e852b=_0x5cfb[_0x2d7832];return _0x2e852b;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xb5cf('0x0'));var rimraf=require(_0xb5cf('0x1'));var zipdir=require(_0xb5cf('0x2'));var jsonpatch=require(_0xb5cf('0x3'));var rp=require(_0xb5cf('0x4'));var moment=require(_0xb5cf('0x5'));var BPromise=require(_0xb5cf('0x6'));var Mustache=require('mustache');var util=require(_0xb5cf('0x7'));var path=require(_0xb5cf('0x8'));var sox=require(_0xb5cf('0x9'));var csv=require(_0xb5cf('0xa'));var ejs=require(_0xb5cf('0xb'));var fs=require('fs');var _=require('lodash');var squel=require(_0xb5cf('0xc'));var crypto=require('crypto');var jsforce=require(_0xb5cf('0xd'));var deskjs=require(_0xb5cf('0xe'));var toCsv=require('to-csv');var querystring=require(_0xb5cf('0xf'));var Papa=require(_0xb5cf('0x10'));var Redis=require(_0xb5cf('0x11'));var authService=require(_0xb5cf('0x12'));var qs=require(_0xb5cf('0x13'));var as=require(_0xb5cf('0x14'));var hardwareService=require(_0xb5cf('0x15'));var logger=require('../../config/logger')(_0xb5cf('0x16'));var utils=require('../../config/utils');var config=require(_0xb5cf('0x17'));var db=require(_0xb5cf('0x18'))['db'];config[_0xb5cf('0x19')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xb5cf('0x1a'))(new Redis(config['redis']));require(_0xb5cf('0x1b'))['register'](socket);var jayson=require(_0xb5cf('0x1c'));var client=jayson[_0xb5cf('0x1d')][_0xb5cf('0x1e')]({'port':0x232c});var client9002=jayson[_0xb5cf('0x1d')][_0xb5cf('0x1e')]({'port':0x232a});function checkPasswordPattern(_0x1df26f,_0x17dddb,_0x5b575a){if(_0x5b575a){var _0x20652e=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x20652e[_0xb5cf('0x1f')](_0x17dddb)){return _0x1df26f;}else{throw new db['Sequelize']['ValidationError'](_0xb5cf('0x20'));}}else{return _0x1df26f;}}function respondWithRpcPromise(_0x30fc8d,_0x49e068,_0x1edb6d,_0x7f2c10){return new BPromise(function(_0x405f12,_0x1cd574){var _0x476987=_0x7f2c10||client;return _0x476987[_0xb5cf('0x21')](_0x30fc8d,_0x1edb6d)['then'](function(_0x30548d){logger[_0xb5cf('0x22')](_0xb5cf('0x23'),_0x49e068,_0xb5cf('0x24'));logger['debug'](_0xb5cf('0x25'),_0x49e068,_0xb5cf('0x24'),JSON[_0xb5cf('0x26')](_0x30548d));if(_0x30548d[_0xb5cf('0x27')]){if(_0x30548d[_0xb5cf('0x27')]['code']===0x1f4){logger['error'](_0xb5cf('0x23'),_0x49e068,_0x30548d[_0xb5cf('0x27')][_0xb5cf('0x28')]);return _0x1cd574(_0x30548d[_0xb5cf('0x27')][_0xb5cf('0x28')]);}logger[_0xb5cf('0x27')](_0xb5cf('0x23'),_0x49e068,_0x30548d[_0xb5cf('0x27')][_0xb5cf('0x28')]);return _0x405f12(_0x30548d['error'][_0xb5cf('0x28')]);}else{logger[_0xb5cf('0x22')](_0xb5cf('0x23'),_0x49e068,_0xb5cf('0x24'));_0x405f12(_0x30548d[_0xb5cf('0x29')][_0xb5cf('0x28')]);}})[_0xb5cf('0x2a')](function(_0x222790){logger[_0xb5cf('0x27')]('ChatMessage,\x20%s,\x20%s',_0x49e068,_0x222790);_0x1cd574(_0x222790);});});}function respondWithStatusCode(_0xd3c468,_0x29ca20){_0x29ca20=_0x29ca20||0xcc;return function(_0x27f00a){if(_0x27f00a){return _0xd3c468[_0xb5cf('0x2b')](_0x29ca20);}return _0xd3c468[_0xb5cf('0x2c')](_0x29ca20)[_0xb5cf('0x2d')]();};}function respondWithResult(_0x2e67ed,_0x1eecac){_0x1eecac=_0x1eecac||0xc8;return function(_0x39ad1f){if(_0x39ad1f){return _0x2e67ed['status'](_0x1eecac)[_0xb5cf('0x2e')](_0x39ad1f);}};}function respondWithFilteredResult(_0x5b7aab,_0x151397){return function(_0x337f61){if(_0x337f61){var _0x3dc096=_0x337f61[_0xb5cf('0x2f')],_0x5b9c0a=_0x151397[_0xb5cf('0x30')],_0x33b4b5=_0x151397['offset']+_0x151397[_0xb5cf('0x31')],_0x5e2b54;if(_0x33b4b5>=_0x3dc096){_0x33b4b5=_0x3dc096;_0x5e2b54=0xc8;}else{_0x5e2b54=0xce;}_0x5b7aab[_0xb5cf('0x2c')](_0x5e2b54);return _0x5b7aab['set']('Content-Range',_0x5b9c0a+'-'+_0x33b4b5+'/'+_0x3dc096)[_0xb5cf('0x2e')](_0x337f61);}return null;};}function patchUpdates(_0x2d7629){return function(_0x4aa25e){try{jsonpatch['apply'](_0x4aa25e,_0x2d7629,!![]);}catch(_0x5d291b){return BPromise[_0xb5cf('0x32')](_0x5d291b);}return _0x4aa25e[_0xb5cf('0x33')]();};}function saveUpdates(_0x1c50b7,_0x3fde54){return function(_0x2e8de0){if(_0x2e8de0){return _0x2e8de0['update'](_0x1c50b7)[_0xb5cf('0x34')](function(_0x55a866){return _0x55a866;});}return null;};}function removeEntity(_0x12409b,_0x55cabc){return function(_0x1c2cab){if(_0x1c2cab){return _0x1c2cab['destroy']()[_0xb5cf('0x34')](function(){_0x12409b[_0xb5cf('0x2c')](0xcc)[_0xb5cf('0x2d')]();});}};}function handleEntityNotFound(_0x26ed3c,_0x4d5823){return function(_0x14e65f){if(!_0x14e65f){_0x26ed3c[_0xb5cf('0x2b')](0x194);}return _0x14e65f;};}function handleError(_0x1b5aa0,_0x5cd506){_0x5cd506=_0x5cd506||0x1f4;return function(_0x57613b){logger[_0xb5cf('0x27')](_0x57613b[_0xb5cf('0x35')]);if(_0x57613b['name']){delete _0x57613b['name'];}_0x1b5aa0[_0xb5cf('0x2c')](_0x5cd506)[_0xb5cf('0x36')](_0x57613b);};}exports[_0xb5cf('0x37')]=function(_0x2569d6,_0x48e916){var _0x1825e9={},_0x2e6b58={},_0x527110={'count':0x0,'rows':[]};var _0x5378a6=_[_0xb5cf('0x38')](db[_0xb5cf('0x39')][_0xb5cf('0x3a')],function(_0x4d53bb){return{'name':_0x4d53bb[_0xb5cf('0x3b')],'type':_0x4d53bb['type'][_0xb5cf('0x3c')]};});_0x2e6b58[_0xb5cf('0x3d')]=_[_0xb5cf('0x38')](_0x5378a6,_0xb5cf('0x3e'));_0x2e6b58[_0xb5cf('0x3f')]=_[_0xb5cf('0x40')](_0x2569d6[_0xb5cf('0x3f')]);_0x2e6b58[_0xb5cf('0x41')]=_['intersection'](_0x2e6b58[_0xb5cf('0x3d')],_0x2e6b58[_0xb5cf('0x3f')]);_0x1825e9[_0xb5cf('0x42')]=_[_0xb5cf('0x43')](_0x2e6b58[_0xb5cf('0x3d')],qs['fields'](_0x2569d6[_0xb5cf('0x3f')][_0xb5cf('0x44')]));_0x1825e9[_0xb5cf('0x42')]=_0x1825e9[_0xb5cf('0x42')][_0xb5cf('0x45')]?_0x1825e9['attributes']:_0x2e6b58[_0xb5cf('0x3d')];if(!_0x2569d6[_0xb5cf('0x3f')]['hasOwnProperty']('nolimit')){_0x1825e9[_0xb5cf('0x31')]=qs[_0xb5cf('0x31')](_0x2569d6[_0xb5cf('0x3f')][_0xb5cf('0x31')]);_0x1825e9['offset']=qs[_0xb5cf('0x30')](_0x2569d6['query']['offset']);}_0x1825e9[_0xb5cf('0x46')]=qs[_0xb5cf('0x47')](_0x2569d6[_0xb5cf('0x3f')][_0xb5cf('0x47')]);_0x1825e9[_0xb5cf('0x48')]=qs[_0xb5cf('0x41')](_['pick'](_0x2569d6[_0xb5cf('0x3f')],_0x2e6b58[_0xb5cf('0x41')]),_0x5378a6);if(_0x2569d6['query']['filter']){_0x1825e9[_0xb5cf('0x48')]=_[_0xb5cf('0x49')](_0x1825e9['where'],{'$or':_[_0xb5cf('0x38')](_0x5378a6,function(_0xc051b7){if(_0xc051b7[_0xb5cf('0x4a')]!=='VIRTUAL'){var _0x4cd7cf={};_0x4cd7cf[_0xc051b7[_0xb5cf('0x3e')]]={'$like':'%'+_0x2569d6[_0xb5cf('0x3f')][_0xb5cf('0x4b')]+'%'};return _0x4cd7cf;}})});}_0x1825e9=_['merge']({},_0x1825e9,_0x2569d6[_0xb5cf('0x4c')]);var _0x2d4531={'where':_0x1825e9['where']};return db[_0xb5cf('0x39')][_0xb5cf('0x2f')](_0x2d4531)[_0xb5cf('0x34')](function(_0x5db8cd){_0x527110['count']=_0x5db8cd;if(_0x2569d6[_0xb5cf('0x3f')][_0xb5cf('0x4d')]){_0x1825e9['include']=[{'all':!![]}];}return db[_0xb5cf('0x39')]['findAll'](_0x1825e9);})[_0xb5cf('0x34')](function(_0x37d0e1){_0x527110['rows']=_0x37d0e1;return _0x527110;})[_0xb5cf('0x34')](respondWithFilteredResult(_0x48e916,_0x1825e9))[_0xb5cf('0x2a')](handleError(_0x48e916,null));};exports['show']=function(_0x3dc946,_0x4e41fe){var _0x5ba44c={'raw':!![],'where':{'id':_0x3dc946[_0xb5cf('0x4e')]['id']}},_0x2be3d7={};_0x2be3d7[_0xb5cf('0x3d')]=_[_0xb5cf('0x40')](db[_0xb5cf('0x39')][_0xb5cf('0x3a')]);_0x2be3d7[_0xb5cf('0x3f')]=_[_0xb5cf('0x40')](_0x3dc946[_0xb5cf('0x3f')]);_0x2be3d7[_0xb5cf('0x41')]=_[_0xb5cf('0x43')](_0x2be3d7[_0xb5cf('0x3d')],_0x2be3d7[_0xb5cf('0x3f')]);_0x5ba44c[_0xb5cf('0x42')]=_[_0xb5cf('0x43')](_0x2be3d7['model'],qs[_0xb5cf('0x44')](_0x3dc946[_0xb5cf('0x3f')][_0xb5cf('0x44')]));_0x5ba44c[_0xb5cf('0x42')]=_0x5ba44c[_0xb5cf('0x42')]['length']?_0x5ba44c[_0xb5cf('0x42')]:_0x2be3d7[_0xb5cf('0x3d')];if(_0x3dc946[_0xb5cf('0x3f')]['includeAll']){_0x5ba44c['include']=[{'all':!![]}];}_0x5ba44c=_[_0xb5cf('0x49')]({},_0x5ba44c,_0x3dc946['options']);return db[_0xb5cf('0x39')]['find'](_0x5ba44c)[_0xb5cf('0x34')](handleEntityNotFound(_0x4e41fe,null))[_0xb5cf('0x34')](respondWithResult(_0x4e41fe,null))[_0xb5cf('0x2a')](handleError(_0x4e41fe,null));};exports[_0xb5cf('0x4f')]=function(_0x1077b3,_0x338fa3){return db['ChatMessage'][_0xb5cf('0x4f')](_0x1077b3[_0xb5cf('0x50')],{})['then'](respondWithResult(_0x338fa3,0xc9))['catch'](handleError(_0x338fa3,null));};exports['update']=function(_0x36691c,_0x4d949c){if(_0x36691c[_0xb5cf('0x50')]['id']){delete _0x36691c[_0xb5cf('0x50')]['id'];}return db['ChatMessage'][_0xb5cf('0x51')]({'where':{'id':_0x36691c[_0xb5cf('0x4e')]['id']}})[_0xb5cf('0x34')](handleEntityNotFound(_0x4d949c,null))[_0xb5cf('0x34')](saveUpdates(_0x36691c[_0xb5cf('0x50')],null))[_0xb5cf('0x34')](respondWithResult(_0x4d949c,null))[_0xb5cf('0x2a')](handleError(_0x4d949c,null));};exports['destroy']=function(_0xdf5d74,_0x268bd8){return db[_0xb5cf('0x39')][_0xb5cf('0x51')]({'where':{'id':_0xdf5d74[_0xb5cf('0x4e')]['id']}})[_0xb5cf('0x34')](handleEntityNotFound(_0x268bd8,null))[_0xb5cf('0x34')](removeEntity(_0x268bd8,null))['catch'](handleError(_0x268bd8,null));};exports['describe']=function(_0x114cf2,_0x4979d6){return db[_0xb5cf('0x39')][_0xb5cf('0x52')]()[_0xb5cf('0x34')](respondWithResult(_0x4979d6,null))[_0xb5cf('0x2a')](handleError(_0x4979d6,null));};var interaction_log=require(_0xb5cf('0x53'))('chat-interactions');exports[_0xb5cf('0x54')]=function(_0x16d187,_0x4e5357,_0x25884e){var _0x43058d={'channel':_0xb5cf('0x55')};if(_0x16d187[_0xb5cf('0x50')]['id']){delete _0x16d187[_0xb5cf('0x50')]['id'];}_0x16d187[_0xb5cf('0x50')][_0xb5cf('0x56')]=!![];_0x16d187[_0xb5cf('0x50')]['readAt']=moment()[_0xb5cf('0x57')](_0xb5cf('0x58'));_0x16d187['body'][_0xb5cf('0x59')]=_0x16d187[_0xb5cf('0x50')][_0xb5cf('0x59')]||_0x16d187[_0xb5cf('0x5a')]['id'];return db[_0xb5cf('0x39')][_0xb5cf('0x51')]({'where':{'id':_0x16d187['params']['id'],'UserId':null},'include':[{'model':db[_0xb5cf('0x5b')],'as':'Contact'}]})[_0xb5cf('0x34')](handleEntityNotFound(_0x4e5357,null))[_0xb5cf('0x34')](saveUpdates(_0x16d187[_0xb5cf('0x50')],null))['then'](function(_0x2a69db){if(_0x2a69db){_0x43058d[_0xb5cf('0x28')]=_0x2a69db[_0xb5cf('0x5c')]({'plain':!![]});return db[_0xb5cf('0x5d')]['find']({'where':{'id':_0x2a69db[_0xb5cf('0x5e')]}});}return null;})[_0xb5cf('0x34')](handleEntityNotFound(_0x4e5357,null))['then'](function(_0x111cf3){if(_0x111cf3){return _0x111cf3['update']({'UserId':_0x16d187[_0xb5cf('0x50')][_0xb5cf('0x59')],'read1stAt':_['isNil'](_0x111cf3['read1stAt'])?moment()[_0xb5cf('0x57')](_0xb5cf('0x58')):undefined});}return null;})[_0xb5cf('0x34')](function(_0x264fd2){if(_0x264fd2){_0x43058d[_0xb5cf('0x5f')]=_0x264fd2[_0xb5cf('0x5c')]({'plain':!![]});interaction_log[_0xb5cf('0x22')](_0xb5cf('0x60'),_0x16d187[_0xb5cf('0x5a')]['id'],_0x16d187[_0xb5cf('0x5a')][_0xb5cf('0x3e')],_0x16d187[_0xb5cf('0x5a')][_0xb5cf('0x61')],_0x43058d[_0xb5cf('0x5f')]['id'],_0x16d187[_0xb5cf('0x50')]?JSON[_0xb5cf('0x26')](_0x16d187[_0xb5cf('0x50')]):'null');return respondWithRpcPromise('AcceptMessage','acceptMessage',_0x43058d);}return null;})[_0xb5cf('0x34')](function(_0x160204){if(_0x160204){if(_0x16d187['body'][_0xb5cf('0x62')]){_0x43058d[_0xb5cf('0x28')][_0xb5cf('0x63')]=util[_0xb5cf('0x57')]('SIP/%s',_0x16d187[_0xb5cf('0x5a')][_0xb5cf('0x3e')]);_0x43058d[_0xb5cf('0x28')][_0xb5cf('0x64')]=_0x43058d[_0xb5cf('0x64')];_0x43058d[_0xb5cf('0x28')][_0xb5cf('0x65')]='accept';return respondWithRpcPromise(_0xb5cf('0x66'),'EventManager',{'event':_0xb5cf('0x67'),'message':_[_0xb5cf('0x49')](_0x43058d[_0xb5cf('0x28')],JSON[_0xb5cf('0x68')](_0x43058d['interaction'][_0xb5cf('0x69')]))},client9002)[_0xb5cf('0x34')](function(){return _0x43058d;});}return _0x43058d;}return null;})[_0xb5cf('0x34')](respondWithResult(_0x4e5357,null))['catch'](handleError(_0x4e5357,null));};var interaction_log=require(_0xb5cf('0x53'))('chat-interactions');exports[_0xb5cf('0x32')]=function(_0x1af255,_0x15d458,_0x3f37f6){var _0x4707d7={'agent':{},'channel':_0xb5cf('0x55')};if(_0x1af255['body']['id']){delete _0x1af255[_0xb5cf('0x50')]['id'];}_0x1af255['body'][_0xb5cf('0x59')]=_0x1af255[_0xb5cf('0x50')][_0xb5cf('0x59')]||_0x1af255['user']['id'];_0x4707d7[_0xb5cf('0x6a')]['id']=_0x1af255[_0xb5cf('0x50')][_0xb5cf('0x59')];return db['ChatMessage'][_0xb5cf('0x51')]({'where':{'id':_0x1af255[_0xb5cf('0x4e')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x15d458,null))['then'](function(_0x42d87b){if(_0x42d87b){_0x4707d7[_0xb5cf('0x28')]=_0x42d87b[_0xb5cf('0x5c')]({'plain':!![]});return db['ChatInteraction']['find']({'where':{'id':_0x42d87b['ChatInteractionId']}});}return null;})[_0xb5cf('0x34')](handleEntityNotFound(_0x15d458,null))[_0xb5cf('0x34')](function(_0x295c54){if(_0x295c54){_0x4707d7['interaction']=_0x295c54['get']({'plain':!![]});interaction_log[_0xb5cf('0x22')](_0xb5cf('0x6b'),_0x1af255[_0xb5cf('0x5a')]['id'],_0x1af255[_0xb5cf('0x5a')][_0xb5cf('0x3e')],_0x1af255[_0xb5cf('0x5a')]['role'],_0x4707d7['interaction']['id'],_0x1af255[_0xb5cf('0x50')]?JSON[_0xb5cf('0x26')](_0x1af255[_0xb5cf('0x50')]):_0xb5cf('0x6c'));return respondWithRpcPromise(_0xb5cf('0x6d'),_0xb5cf('0x6e'),_0x4707d7);}return null;})[_0xb5cf('0x34')](function(_0x3f84a0){if(_0x3f84a0){_0x3f84a0[_0xb5cf('0x28')]['interface']=util[_0xb5cf('0x57')](_0xb5cf('0x6f'),_0x1af255[_0xb5cf('0x5a')][_0xb5cf('0x3e')]);_0x3f84a0[_0xb5cf('0x28')][_0xb5cf('0x64')]=_0x4707d7[_0xb5cf('0x64')];_0x3f84a0[_0xb5cf('0x28')][_0xb5cf('0x65')]=_0xb5cf('0x32');respondWithRpcPromise(_0xb5cf('0x66'),'EventManager',{'event':_0xb5cf('0x70'),'message':_0x3f84a0[_0xb5cf('0x28')]},client9002);return _0x4707d7;}return null;})[_0xb5cf('0x34')](respondWithResult(_0x15d458,null))[_0xb5cf('0x2a')](handleError(_0x15d458,null));};