Built motion from commit (unavailable).|2.5.25
[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 _0x402f=['sox','to-csv','ejs','lodash','squel','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','redis','socket.io-emitter','./chatMessage.socket','register','jayson/promise','client','request','then','info','ChatMessage,\x20%s,\x20%s','request\x20sent','debug','stringify','error','code','message','result','catch','end','status','offset','limit','Content-Range','json','apply','reject','save','update','sendStatus','stack','name','send','ChatMessage','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','attributes','fields','length','nolimit','order','sort','where','pick','filter','VIRTUAL','options','count','include','findAll','rows','show','merge','find','create','body','destroy','chat-interactions','accept','chat','read','format','UserId','user','agent','params','CmContact','Contact','get','ChatInteraction','ChatInteractionId','isNil','read1stAt','YYYY-MM-DD\x20HH:mm:ss','interaction','[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','AcceptMessage','manual','interface','SIP/%s','channel','event','EventManager','acceptmessage','formData','[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','RejectMessage','rejectMessage','rejectmessage','html-pdf','rimraf','request-promise','moment','bluebird','util'];(function(_0x5830d6,_0x4a4a51){var _0x57debb=function(_0x1ef169){while(--_0x1ef169){_0x5830d6['push'](_0x5830d6['shift']());}};_0x57debb(++_0x4a4a51);}(_0x402f,0x6f));var _0xf402=function(_0x4852ff,_0x35118d){_0x4852ff=_0x4852ff-0x0;var _0x3ee92e=_0x402f[_0x4852ff];return _0x3ee92e;};'use strict';var pdf=require(_0xf402('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xf402('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0xf402('0x2'));var moment=require(_0xf402('0x3'));var BPromise=require(_0xf402('0x4'));var Mustache=require('mustache');var util=require(_0xf402('0x5'));var path=require('path');var sox=require(_0xf402('0x6'));var csv=require(_0xf402('0x7'));var ejs=require(_0xf402('0x8'));var fs=require('fs');var _=require(_0xf402('0x9'));var squel=require(_0xf402('0xa'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xf402('0xb'));var toCsv=require(_0xf402('0x7'));var querystring=require(_0xf402('0xc'));var Papa=require(_0xf402('0xd'));var Redis=require(_0xf402('0xe'));var authService=require(_0xf402('0xf'));var qs=require(_0xf402('0x10'));var as=require(_0xf402('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf402('0x12'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];config[_0xf402('0x13')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0xf402('0x14'))(new Redis(config['redis']));require(_0xf402('0x15'))[_0xf402('0x16')](socket);var jayson=require(_0xf402('0x17'));var client=jayson[_0xf402('0x18')]['http']({'port':0x232c});var client9002=jayson[_0xf402('0x18')]['http']({'port':0x232a});function respondWithRpcPromise(_0x3af9e3,_0x5e38d8,_0x5cc240,_0x545d1a){return new BPromise(function(_0xb45d00,_0x1e66a3){var _0xd406bf=_0x545d1a||client;return _0xd406bf[_0xf402('0x19')](_0x3af9e3,_0x5cc240)[_0xf402('0x1a')](function(_0x5d7236){logger[_0xf402('0x1b')](_0xf402('0x1c'),_0x5e38d8,_0xf402('0x1d'));logger[_0xf402('0x1e')]('ChatMessage,\x20%s,\x20%s,\x20%s',_0x5e38d8,_0xf402('0x1d'),JSON[_0xf402('0x1f')](_0x5d7236));if(_0x5d7236[_0xf402('0x20')]){if(_0x5d7236[_0xf402('0x20')][_0xf402('0x21')]===0x1f4){logger[_0xf402('0x20')](_0xf402('0x1c'),_0x5e38d8,_0x5d7236[_0xf402('0x20')][_0xf402('0x22')]);return _0x1e66a3(_0x5d7236['error'][_0xf402('0x22')]);}logger['error'](_0xf402('0x1c'),_0x5e38d8,_0x5d7236[_0xf402('0x20')][_0xf402('0x22')]);return _0xb45d00(_0x5d7236[_0xf402('0x20')][_0xf402('0x22')]);}else{logger[_0xf402('0x1b')](_0xf402('0x1c'),_0x5e38d8,_0xf402('0x1d'));_0xb45d00(_0x5d7236[_0xf402('0x23')][_0xf402('0x22')]);}})[_0xf402('0x24')](function(_0xfad629){logger['error'](_0xf402('0x1c'),_0x5e38d8,_0xfad629);_0x1e66a3(_0xfad629);});});}function respondWithStatusCode(_0x16c21c,_0x28fc75){_0x28fc75=_0x28fc75||0xcc;return function(_0x35543b){if(_0x35543b){return _0x16c21c['sendStatus'](_0x28fc75);}return _0x16c21c['status'](_0x28fc75)[_0xf402('0x25')]();};}function respondWithResult(_0x3f837e,_0x3a6a8d){_0x3a6a8d=_0x3a6a8d||0xc8;return function(_0xb7c8e4){if(_0xb7c8e4){return _0x3f837e[_0xf402('0x26')](_0x3a6a8d)['json'](_0xb7c8e4);}};}function respondWithFilteredResult(_0x39a38a,_0x47c0fb){return function(_0x1574e3){if(_0x1574e3){var _0xe3dc2f=_0x1574e3['count'],_0x45ead3=_0x47c0fb[_0xf402('0x27')],_0xb28d88=_0x47c0fb[_0xf402('0x27')]+_0x47c0fb[_0xf402('0x28')],_0xd82d61;if(_0xb28d88>=_0xe3dc2f){_0xb28d88=_0xe3dc2f;_0xd82d61=0xc8;}else{_0xd82d61=0xce;}_0x39a38a['status'](_0xd82d61);return _0x39a38a['set'](_0xf402('0x29'),_0x45ead3+'-'+_0xb28d88+'/'+_0xe3dc2f)[_0xf402('0x2a')](_0x1574e3);}return null;};}function patchUpdates(_0x1ecc55){return function(_0x221d6d){try{jsonpatch[_0xf402('0x2b')](_0x221d6d,_0x1ecc55,!![]);}catch(_0x4a593b){return BPromise[_0xf402('0x2c')](_0x4a593b);}return _0x221d6d[_0xf402('0x2d')]();};}function saveUpdates(_0x44aa26,_0x1f2cc4){return function(_0x176b17){if(_0x176b17){return _0x176b17[_0xf402('0x2e')](_0x44aa26)['then'](function(_0x69b6ff){return _0x69b6ff;});}return null;};}function removeEntity(_0x209eae,_0x1af6a5){return function(_0x53d725){if(_0x53d725){return _0x53d725['destroy']()[_0xf402('0x1a')](function(){_0x209eae[_0xf402('0x26')](0xcc)[_0xf402('0x25')]();});}};}function handleEntityNotFound(_0x5cf566,_0x291636){return function(_0x335b34){if(!_0x335b34){_0x5cf566[_0xf402('0x2f')](0x194);}return _0x335b34;};}function handleError(_0x29b86a,_0xa90acd){_0xa90acd=_0xa90acd||0x1f4;return function(_0x2c4217){logger['error'](_0x2c4217[_0xf402('0x30')]);if(_0x2c4217[_0xf402('0x31')]){delete _0x2c4217[_0xf402('0x31')];}_0x29b86a[_0xf402('0x26')](_0xa90acd)[_0xf402('0x32')](_0x2c4217);};}exports['index']=function(_0x330d85,_0x55d87d){var _0x40e9e4={},_0x50a430={},_0x5ac297={'count':0x0,'rows':[]};var _0x66f414=_['map'](db[_0xf402('0x33')][_0xf402('0x34')],function(_0x5dc99b){return{'name':_0x5dc99b[_0xf402('0x35')],'type':_0x5dc99b[_0xf402('0x36')][_0xf402('0x37')]};});_0x50a430[_0xf402('0x38')]=_[_0xf402('0x39')](_0x66f414,_0xf402('0x31'));_0x50a430[_0xf402('0x3a')]=_[_0xf402('0x3b')](_0x330d85['query']);_0x50a430[_0xf402('0x3c')]=_[_0xf402('0x3d')](_0x50a430[_0xf402('0x38')],_0x50a430[_0xf402('0x3a')]);_0x40e9e4[_0xf402('0x3e')]=_['intersection'](_0x50a430[_0xf402('0x38')],qs['fields'](_0x330d85[_0xf402('0x3a')][_0xf402('0x3f')]));_0x40e9e4[_0xf402('0x3e')]=_0x40e9e4[_0xf402('0x3e')][_0xf402('0x40')]?_0x40e9e4[_0xf402('0x3e')]:_0x50a430[_0xf402('0x38')];if(!_0x330d85['query']['hasOwnProperty'](_0xf402('0x41'))){_0x40e9e4[_0xf402('0x28')]=qs[_0xf402('0x28')](_0x330d85[_0xf402('0x3a')][_0xf402('0x28')]);_0x40e9e4[_0xf402('0x27')]=qs['offset'](_0x330d85[_0xf402('0x3a')][_0xf402('0x27')]);}_0x40e9e4[_0xf402('0x42')]=qs[_0xf402('0x43')](_0x330d85['query'][_0xf402('0x43')]);_0x40e9e4[_0xf402('0x44')]=qs[_0xf402('0x3c')](_[_0xf402('0x45')](_0x330d85[_0xf402('0x3a')],_0x50a430[_0xf402('0x3c')]),_0x66f414);if(_0x330d85['query'][_0xf402('0x46')]){_0x40e9e4[_0xf402('0x44')]=_['merge'](_0x40e9e4['where'],{'$or':_[_0xf402('0x39')](_0x66f414,function(_0x3ac553){if(_0x3ac553['type']!==_0xf402('0x47')){var _0x1152c8={};_0x1152c8[_0x3ac553[_0xf402('0x31')]]={'$like':'%'+_0x330d85['query'][_0xf402('0x46')]+'%'};return _0x1152c8;}})});}_0x40e9e4=_['merge']({},_0x40e9e4,_0x330d85[_0xf402('0x48')]);var _0x4b72ba={'where':_0x40e9e4[_0xf402('0x44')]};return db[_0xf402('0x33')]['count'](_0x4b72ba)['then'](function(_0x32b863){_0x5ac297[_0xf402('0x49')]=_0x32b863;if(_0x330d85['query']['includeAll']){_0x40e9e4[_0xf402('0x4a')]=[{'all':!![]}];}return db[_0xf402('0x33')][_0xf402('0x4b')](_0x40e9e4);})[_0xf402('0x1a')](function(_0x20a1cd){_0x5ac297[_0xf402('0x4c')]=_0x20a1cd;return _0x5ac297;})[_0xf402('0x1a')](respondWithFilteredResult(_0x55d87d,_0x40e9e4))[_0xf402('0x24')](handleError(_0x55d87d,null));};exports[_0xf402('0x4d')]=function(_0x4816f9,_0x1f128d){var _0x71825e={'raw':!![],'where':{'id':_0x4816f9['params']['id']}},_0x29f869={};_0x29f869[_0xf402('0x38')]=_[_0xf402('0x3b')](db[_0xf402('0x33')][_0xf402('0x34')]);_0x29f869['query']=_[_0xf402('0x3b')](_0x4816f9[_0xf402('0x3a')]);_0x29f869[_0xf402('0x3c')]=_[_0xf402('0x3d')](_0x29f869[_0xf402('0x38')],_0x29f869[_0xf402('0x3a')]);_0x71825e[_0xf402('0x3e')]=_[_0xf402('0x3d')](_0x29f869[_0xf402('0x38')],qs[_0xf402('0x3f')](_0x4816f9[_0xf402('0x3a')][_0xf402('0x3f')]));_0x71825e[_0xf402('0x3e')]=_0x71825e['attributes']['length']?_0x71825e[_0xf402('0x3e')]:_0x29f869['model'];if(_0x4816f9['query']['includeAll']){_0x71825e[_0xf402('0x4a')]=[{'all':!![]}];}_0x71825e=_[_0xf402('0x4e')]({},_0x71825e,_0x4816f9['options']);return db[_0xf402('0x33')][_0xf402('0x4f')](_0x71825e)[_0xf402('0x1a')](handleEntityNotFound(_0x1f128d,null))[_0xf402('0x1a')](respondWithResult(_0x1f128d,null))[_0xf402('0x24')](handleError(_0x1f128d,null));};exports[_0xf402('0x50')]=function(_0x14c159,_0x138a56){return db['ChatMessage']['create'](_0x14c159[_0xf402('0x51')],{})[_0xf402('0x1a')](respondWithResult(_0x138a56,0xc9))['catch'](handleError(_0x138a56,null));};exports[_0xf402('0x2e')]=function(_0x2ee779,_0x49b1d6){if(_0x2ee779['body']['id']){delete _0x2ee779[_0xf402('0x51')]['id'];}return db['ChatMessage'][_0xf402('0x4f')]({'where':{'id':_0x2ee779['params']['id']}})[_0xf402('0x1a')](handleEntityNotFound(_0x49b1d6,null))[_0xf402('0x1a')](saveUpdates(_0x2ee779['body'],null))['then'](respondWithResult(_0x49b1d6,null))[_0xf402('0x24')](handleError(_0x49b1d6,null));};exports[_0xf402('0x52')]=function(_0x4e9b13,_0x13cd79){return db[_0xf402('0x33')][_0xf402('0x4f')]({'where':{'id':_0x4e9b13['params']['id']}})[_0xf402('0x1a')](handleEntityNotFound(_0x13cd79,null))[_0xf402('0x1a')](removeEntity(_0x13cd79,null))[_0xf402('0x24')](handleError(_0x13cd79,null));};exports['describe']=function(_0x82e852,_0x7938cc){return db[_0xf402('0x33')]['describe']()[_0xf402('0x1a')](respondWithResult(_0x7938cc,null))[_0xf402('0x24')](handleError(_0x7938cc,null));};var interaction_log=require('../../config/logger')(_0xf402('0x53'));exports[_0xf402('0x54')]=function(_0x470b41,_0x40cec0,_0x4b9ab1){var _0x3d21b2={'agent':{},'channel':_0xf402('0x55')};if(_0x470b41['body']['id']){delete _0x470b41[_0xf402('0x51')]['id'];}_0x470b41[_0xf402('0x51')][_0xf402('0x56')]=!![];_0x470b41[_0xf402('0x51')]['readAt']=moment()[_0xf402('0x57')]('YYYY-MM-DD\x20HH:mm:ss');_0x470b41[_0xf402('0x51')][_0xf402('0x58')]=_0x470b41[_0xf402('0x51')]['UserId']||_0x470b41[_0xf402('0x59')]['id'];_0x3d21b2[_0xf402('0x5a')]['id']=_0x470b41['body']['UserId'];return db['ChatMessage']['find']({'where':{'id':_0x470b41[_0xf402('0x5b')]['id'],'UserId':null},'include':[{'model':db[_0xf402('0x5c')],'as':_0xf402('0x5d')}]})[_0xf402('0x1a')](handleEntityNotFound(_0x40cec0,null))['then'](saveUpdates(_0x470b41[_0xf402('0x51')],null))[_0xf402('0x1a')](function(_0x385a95){if(_0x385a95){_0x3d21b2[_0xf402('0x22')]=_0x385a95[_0xf402('0x5e')]({'plain':!![]});return db[_0xf402('0x5f')][_0xf402('0x4f')]({'where':{'id':_0x385a95[_0xf402('0x60')]}});}return null;})[_0xf402('0x1a')](handleEntityNotFound(_0x40cec0,null))[_0xf402('0x1a')](function(_0x624e79){if(_0x624e79){return _0x624e79['update']({'UserId':_0x470b41[_0xf402('0x51')][_0xf402('0x58')],'read1stAt':_[_0xf402('0x61')](_0x624e79[_0xf402('0x62')])?moment()[_0xf402('0x57')](_0xf402('0x63')):undefined});}return null;})[_0xf402('0x1a')](function(_0xc313cc){if(_0xc313cc){_0x3d21b2[_0xf402('0x64')]=_0xc313cc['get']({'plain':!![]});interaction_log[_0xf402('0x1b')](_0xf402('0x65'),_0x470b41[_0xf402('0x59')]['id'],_0x470b41[_0xf402('0x59')]['name'],_0x470b41['user'][_0xf402('0x66')],_0x3d21b2['interaction']['id'],_0x470b41['body']?JSON[_0xf402('0x1f')](_0x470b41[_0xf402('0x51')]):_0xf402('0x67'));return respondWithRpcPromise(_0xf402('0x68'),'acceptMessage',_0x3d21b2);}return null;})['then'](function(_0x4b67fa){if(_0x4b67fa){if(_0x470b41[_0xf402('0x51')][_0xf402('0x69')]){_0x3d21b2[_0xf402('0x22')][_0xf402('0x6a')]=util['format'](_0xf402('0x6b'),_0x470b41[_0xf402('0x59')][_0xf402('0x31')]);_0x3d21b2[_0xf402('0x22')][_0xf402('0x6c')]=_0x3d21b2[_0xf402('0x6c')];_0x3d21b2[_0xf402('0x22')][_0xf402('0x6d')]='accept';return respondWithRpcPromise('EventManager',_0xf402('0x6e'),{'event':_0xf402('0x6f'),'message':_[_0xf402('0x4e')](_0x3d21b2[_0xf402('0x22')],JSON['parse'](_0x3d21b2[_0xf402('0x64')][_0xf402('0x70')]))},client9002)[_0xf402('0x1a')](function(){return _0x3d21b2;});}return _0x3d21b2;}return null;})[_0xf402('0x1a')](respondWithResult(_0x40cec0,null))[_0xf402('0x24')](handleError(_0x40cec0,null));};var interaction_log=require(_0xf402('0x12'))(_0xf402('0x53'));exports['reject']=function(_0x20956b,_0x57641c,_0x470455){var _0x511f35={'agent':{},'channel':_0xf402('0x55')};if(_0x20956b[_0xf402('0x51')]['id']){delete _0x20956b[_0xf402('0x51')]['id'];}_0x20956b[_0xf402('0x51')][_0xf402('0x58')]=_0x20956b[_0xf402('0x51')]['UserId']||_0x20956b[_0xf402('0x59')]['id'];_0x511f35[_0xf402('0x5a')]['id']=_0x20956b['body'][_0xf402('0x58')];return db[_0xf402('0x33')][_0xf402('0x4f')]({'where':{'id':_0x20956b[_0xf402('0x5b')]['id'],'UserId':null}})['then'](handleEntityNotFound(_0x57641c,null))['then'](function(_0x2705e8){if(_0x2705e8){_0x511f35['message']=_0x2705e8[_0xf402('0x5e')]({'plain':!![]});return db['ChatInteraction']['find']({'where':{'id':_0x2705e8[_0xf402('0x60')]}});}return null;})[_0xf402('0x1a')](handleEntityNotFound(_0x57641c,null))[_0xf402('0x1a')](function(_0x2f2177){if(_0x2f2177){_0x511f35[_0xf402('0x64')]=_0x2f2177[_0xf402('0x5e')]({'plain':!![]});interaction_log[_0xf402('0x1b')](_0xf402('0x71'),_0x20956b[_0xf402('0x59')]['id'],_0x20956b['user'][_0xf402('0x31')],_0x20956b[_0xf402('0x59')][_0xf402('0x66')],_0x511f35[_0xf402('0x64')]['id'],_0x20956b[_0xf402('0x51')]?JSON[_0xf402('0x1f')](_0x20956b[_0xf402('0x51')]):_0xf402('0x67'));return respondWithRpcPromise(_0xf402('0x72'),_0xf402('0x73'),_0x511f35);}return null;})[_0xf402('0x1a')](function(_0x5060fd){if(_0x5060fd){_0x5060fd['message'][_0xf402('0x6a')]=util['format'](_0xf402('0x6b'),_0x20956b[_0xf402('0x59')][_0xf402('0x31')]);_0x5060fd['message']['channel']=_0x511f35[_0xf402('0x6c')];_0x5060fd[_0xf402('0x22')][_0xf402('0x6d')]='reject';respondWithRpcPromise('EventManager',_0xf402('0x6e'),{'event':_0xf402('0x74'),'message':_0x5060fd['message']},client9002);return _0x511f35;}return null;})[_0xf402('0x1a')](respondWithResult(_0x57641c,null))[_0xf402('0x24')](handleError(_0x57641c,null));};