Built motion from commit (unavailable).|2.5.30
[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 _0x9f43=['[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','null','RejectMessage','rejectMessage','interface','channel','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','path','sox','to-csv','ejs','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost','./chatMessage.socket','register','client','http','request','then','info','ChatMessage,\x20%s,\x20%s','debug','ChatMessage,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','end','json','offset','limit','undefined','count','set','apply','reject','save','update','destroy','status','sendStatus','stack','name','send','map','ChatMessage','fieldName','type','key','query','filters','intersection','model','fields','length','attributes','nolimit','sort','pick','filter','where','VIRTUAL','merge','options','includeAll','findAll','show','params','rawAttributes','keys','include','find','create','body','describe','../../config/logger','chat-interactions','accept','chat','read','readAt','UserId','user','agent','CmContact','Contact','get','ChatInteraction','isNil','read1stAt','format','YYYY-MM-DD\x20HH:mm:ss','[CHATMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','acceptMessage','manual','SIP/%s','event','EventManager','acceptmessage','parse','ChatInteractionId','interaction'];(function(_0x115b7f,_0x3e5fcf){var _0x36974f=function(_0x80e6bb){while(--_0x80e6bb){_0x115b7f['push'](_0x115b7f['shift']());}};_0x36974f(++_0x3e5fcf);}(_0x9f43,0x81));var _0x39f4=function(_0x3a9635,_0x1ab817){_0x3a9635=_0x3a9635-0x0;var _0x10d08c=_0x9f43[_0x3a9635];return _0x10d08c;};'use strict';var emlformat=require(_0x39f4('0x0'));var rimraf=require(_0x39f4('0x1'));var zipdir=require(_0x39f4('0x2'));var jsonpatch=require(_0x39f4('0x3'));var rp=require(_0x39f4('0x4'));var moment=require(_0x39f4('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x39f4('0x6'));var util=require('util');var path=require(_0x39f4('0x7'));var sox=require(_0x39f4('0x8'));var csv=require(_0x39f4('0x9'));var ejs=require(_0x39f4('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x39f4('0xb'));var deskjs=require(_0x39f4('0xc'));var toCsv=require(_0x39f4('0x9'));var querystring=require('querystring');var Papa=require(_0x39f4('0xd'));var Redis=require(_0x39f4('0xe'));var authService=require(_0x39f4('0xf'));var qs=require(_0x39f4('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x39f4('0x11'));var logger=require('../../config/logger')('api');var utils=require(_0x39f4('0x12'));var config=require('../../config/environment');var licenseUtil=require(_0x39f4('0x13'));var db=require(_0x39f4('0x14'))['db'];config[_0x39f4('0x15')]=_[_0x39f4('0x16')](config[_0x39f4('0x15')],{'host':_0x39f4('0x17'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require(_0x39f4('0x18'))[_0x39f4('0x19')](socket);var jayson=require('jayson/promise');var client=jayson[_0x39f4('0x1a')][_0x39f4('0x1b')]({'port':0x232c});var client9002=jayson['client'][_0x39f4('0x1b')]({'port':0x232a});function respondWithRpcPromise(_0x2fd629,_0x5d6364,_0x1a21a0,_0x58fdd9){return new BPromise(function(_0x14098f,_0x13a009){var _0x54c444=_0x58fdd9||client;return _0x54c444[_0x39f4('0x1c')](_0x2fd629,_0x1a21a0)[_0x39f4('0x1d')](function(_0x5c4536){logger[_0x39f4('0x1e')](_0x39f4('0x1f'),_0x5d6364,'request\x20sent');logger[_0x39f4('0x20')](_0x39f4('0x21'),_0x5d6364,_0x39f4('0x22'),JSON[_0x39f4('0x23')](_0x5c4536));if(_0x5c4536[_0x39f4('0x24')]){if(_0x5c4536[_0x39f4('0x24')][_0x39f4('0x25')]===0x1f4){logger[_0x39f4('0x24')](_0x39f4('0x1f'),_0x5d6364,_0x5c4536[_0x39f4('0x24')]['message']);return _0x13a009(_0x5c4536[_0x39f4('0x24')][_0x39f4('0x26')]);}logger['error']('ChatMessage,\x20%s,\x20%s',_0x5d6364,_0x5c4536['error']['message']);return _0x14098f(_0x5c4536[_0x39f4('0x24')]['message']);}else{logger['info'](_0x39f4('0x1f'),_0x5d6364,'request\x20sent');_0x14098f(_0x5c4536[_0x39f4('0x27')]['message']);}})[_0x39f4('0x28')](function(_0x32e8d7){logger[_0x39f4('0x24')](_0x39f4('0x1f'),_0x5d6364,_0x32e8d7);_0x13a009(_0x32e8d7);});});}function respondWithStatusCode(_0x5644f6,_0x5cba2c){_0x5cba2c=_0x5cba2c||0xcc;return function(_0x22fbcf){if(_0x22fbcf){return _0x5644f6['sendStatus'](_0x5cba2c);}return _0x5644f6['status'](_0x5cba2c)[_0x39f4('0x29')]();};}function respondWithResult(_0x15d050,_0x2cc7f5){_0x2cc7f5=_0x2cc7f5||0xc8;return function(_0x3b2f44){if(_0x3b2f44){return _0x15d050['status'](_0x2cc7f5)[_0x39f4('0x2a')](_0x3b2f44);}};}function respondWithFilteredResult(_0x5e96f8,_0x327cc9){return function(_0x5a5d40){if(_0x5a5d40){var _0x5d838e=typeof _0x327cc9[_0x39f4('0x2b')]==='undefined'&&typeof _0x327cc9[_0x39f4('0x2c')]===_0x39f4('0x2d');var _0x17c123=_0x5a5d40[_0x39f4('0x2e')];var _0x5687f2=_0x5d838e?0x0:_0x327cc9[_0x39f4('0x2b')];var _0x4a816e=_0x5d838e?_0x5a5d40['count']:_0x327cc9['offset']+_0x327cc9['limit'];var _0x37a698;if(_0x4a816e>=_0x17c123){_0x4a816e=_0x17c123;_0x37a698=0xc8;}else{_0x37a698=0xce;}_0x5e96f8['status'](_0x37a698);return _0x5e96f8[_0x39f4('0x2f')]('Content-Range',_0x5687f2+'-'+_0x4a816e+'/'+_0x17c123)['json'](_0x5a5d40);}return null;};}function patchUpdates(_0xef339d){return function(_0x2ab3e7){try{jsonpatch[_0x39f4('0x30')](_0x2ab3e7,_0xef339d,!![]);}catch(_0x304ea4){return BPromise[_0x39f4('0x31')](_0x304ea4);}return _0x2ab3e7[_0x39f4('0x32')]();};}function saveUpdates(_0x1ee4aa,_0xc6f3be){return function(_0x23618b){if(_0x23618b){return _0x23618b[_0x39f4('0x33')](_0x1ee4aa)[_0x39f4('0x1d')](function(_0x19e0be){return _0x19e0be;});}return null;};}function removeEntity(_0x4bebaa,_0x12a99b){return function(_0x5ce321){if(_0x5ce321){return _0x5ce321[_0x39f4('0x34')]()[_0x39f4('0x1d')](function(){_0x4bebaa[_0x39f4('0x35')](0xcc)[_0x39f4('0x29')]();});}};}function handleEntityNotFound(_0x5d0477,_0x2aec5c){return function(_0xce2a7f){if(!_0xce2a7f){_0x5d0477[_0x39f4('0x36')](0x194);}return _0xce2a7f;};}function handleError(_0x1cfe2c,_0x223b0b){_0x223b0b=_0x223b0b||0x1f4;return function(_0x49cf58){logger['error'](_0x49cf58[_0x39f4('0x37')]);if(_0x49cf58[_0x39f4('0x38')]){delete _0x49cf58[_0x39f4('0x38')];}_0x1cfe2c['status'](_0x223b0b)[_0x39f4('0x39')](_0x49cf58);};}exports['index']=function(_0x16efcd,_0x90c65b){var _0xe11322={},_0x18ce59={},_0x3ddc92={'count':0x0,'rows':[]};var _0x5d0da6=_[_0x39f4('0x3a')](db[_0x39f4('0x3b')]['rawAttributes'],function(_0x594f0c){return{'name':_0x594f0c[_0x39f4('0x3c')],'type':_0x594f0c[_0x39f4('0x3d')][_0x39f4('0x3e')]};});_0x18ce59['model']=_['map'](_0x5d0da6,_0x39f4('0x38'));_0x18ce59[_0x39f4('0x3f')]=_['keys'](_0x16efcd[_0x39f4('0x3f')]);_0x18ce59[_0x39f4('0x40')]=_[_0x39f4('0x41')](_0x18ce59[_0x39f4('0x42')],_0x18ce59['query']);_0xe11322['attributes']=_['intersection'](_0x18ce59[_0x39f4('0x42')],qs[_0x39f4('0x43')](_0x16efcd[_0x39f4('0x3f')]['fields']));_0xe11322['attributes']=_0xe11322['attributes'][_0x39f4('0x44')]?_0xe11322[_0x39f4('0x45')]:_0x18ce59[_0x39f4('0x42')];if(!_0x16efcd[_0x39f4('0x3f')]['hasOwnProperty'](_0x39f4('0x46'))){_0xe11322[_0x39f4('0x2c')]=qs[_0x39f4('0x2c')](_0x16efcd[_0x39f4('0x3f')][_0x39f4('0x2c')]);_0xe11322[_0x39f4('0x2b')]=qs[_0x39f4('0x2b')](_0x16efcd['query'][_0x39f4('0x2b')]);}_0xe11322['order']=qs[_0x39f4('0x47')](_0x16efcd[_0x39f4('0x3f')]['sort']);_0xe11322['where']=qs['filters'](_[_0x39f4('0x48')](_0x16efcd[_0x39f4('0x3f')],_0x18ce59['filters']),_0x5d0da6);if(_0x16efcd[_0x39f4('0x3f')][_0x39f4('0x49')]){_0xe11322[_0x39f4('0x4a')]=_['merge'](_0xe11322[_0x39f4('0x4a')],{'$or':_[_0x39f4('0x3a')](_0x5d0da6,function(_0x453d90){if(_0x453d90[_0x39f4('0x3d')]!==_0x39f4('0x4b')){var _0x56d01c={};_0x56d01c[_0x453d90[_0x39f4('0x38')]]={'$like':'%'+_0x16efcd[_0x39f4('0x3f')][_0x39f4('0x49')]+'%'};return _0x56d01c;}})});}_0xe11322=_[_0x39f4('0x4c')]({},_0xe11322,_0x16efcd[_0x39f4('0x4d')]);var _0x5852fd={'where':_0xe11322['where']};return db[_0x39f4('0x3b')][_0x39f4('0x2e')](_0x5852fd)['then'](function(_0x3d8561){_0x3ddc92['count']=_0x3d8561;if(_0x16efcd[_0x39f4('0x3f')][_0x39f4('0x4e')]){_0xe11322['include']=[{'all':!![]}];}return db[_0x39f4('0x3b')][_0x39f4('0x4f')](_0xe11322);})['then'](function(_0x474fe3){_0x3ddc92['rows']=_0x474fe3;return _0x3ddc92;})[_0x39f4('0x1d')](respondWithFilteredResult(_0x90c65b,_0xe11322))[_0x39f4('0x28')](handleError(_0x90c65b,null));};exports[_0x39f4('0x50')]=function(_0x1dcefb,_0x1a4d18){var _0x34ed01={'raw':!![],'where':{'id':_0x1dcefb[_0x39f4('0x51')]['id']}},_0x3d54ce={};_0x3d54ce[_0x39f4('0x42')]=_['keys'](db[_0x39f4('0x3b')][_0x39f4('0x52')]);_0x3d54ce[_0x39f4('0x3f')]=_[_0x39f4('0x53')](_0x1dcefb[_0x39f4('0x3f')]);_0x3d54ce[_0x39f4('0x40')]=_['intersection'](_0x3d54ce['model'],_0x3d54ce[_0x39f4('0x3f')]);_0x34ed01['attributes']=_[_0x39f4('0x41')](_0x3d54ce[_0x39f4('0x42')],qs[_0x39f4('0x43')](_0x1dcefb['query'][_0x39f4('0x43')]));_0x34ed01['attributes']=_0x34ed01['attributes'][_0x39f4('0x44')]?_0x34ed01[_0x39f4('0x45')]:_0x3d54ce[_0x39f4('0x42')];if(_0x1dcefb[_0x39f4('0x3f')][_0x39f4('0x4e')]){_0x34ed01[_0x39f4('0x54')]=[{'all':!![]}];}_0x34ed01=_[_0x39f4('0x4c')]({},_0x34ed01,_0x1dcefb[_0x39f4('0x4d')]);return db[_0x39f4('0x3b')][_0x39f4('0x55')](_0x34ed01)[_0x39f4('0x1d')](handleEntityNotFound(_0x1a4d18,null))['then'](respondWithResult(_0x1a4d18,null))[_0x39f4('0x28')](handleError(_0x1a4d18,null));};exports['create']=function(_0x411d41,_0x33875e){return db['ChatMessage'][_0x39f4('0x56')](_0x411d41[_0x39f4('0x57')],{})[_0x39f4('0x1d')](respondWithResult(_0x33875e,0xc9))[_0x39f4('0x28')](handleError(_0x33875e,null));};exports['update']=function(_0x37ecd2,_0x4702ca){if(_0x37ecd2['body']['id']){delete _0x37ecd2[_0x39f4('0x57')]['id'];}return db[_0x39f4('0x3b')][_0x39f4('0x55')]({'where':{'id':_0x37ecd2[_0x39f4('0x51')]['id']}})[_0x39f4('0x1d')](handleEntityNotFound(_0x4702ca,null))[_0x39f4('0x1d')](saveUpdates(_0x37ecd2[_0x39f4('0x57')],null))[_0x39f4('0x1d')](respondWithResult(_0x4702ca,null))['catch'](handleError(_0x4702ca,null));};exports[_0x39f4('0x34')]=function(_0x3b0824,_0x563418){return db[_0x39f4('0x3b')][_0x39f4('0x55')]({'where':{'id':_0x3b0824[_0x39f4('0x51')]['id']}})[_0x39f4('0x1d')](handleEntityNotFound(_0x563418,null))['then'](removeEntity(_0x563418,null))[_0x39f4('0x28')](handleError(_0x563418,null));};exports[_0x39f4('0x58')]=function(_0x4a9e55,_0x419fa3){return db[_0x39f4('0x3b')][_0x39f4('0x58')]()[_0x39f4('0x1d')](respondWithResult(_0x419fa3,null))['catch'](handleError(_0x419fa3,null));};var interaction_log=require(_0x39f4('0x59'))(_0x39f4('0x5a'));exports[_0x39f4('0x5b')]=function(_0xb7dd00,_0x2eedcf,_0x4aeb0b){var _0x21ba49={'agent':{},'channel':_0x39f4('0x5c')};if(_0xb7dd00[_0x39f4('0x57')]['id']){delete _0xb7dd00[_0x39f4('0x57')]['id'];}_0xb7dd00[_0x39f4('0x57')][_0x39f4('0x5d')]=!![];_0xb7dd00['body'][_0x39f4('0x5e')]=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');_0xb7dd00[_0x39f4('0x57')][_0x39f4('0x5f')]=_0xb7dd00[_0x39f4('0x57')][_0x39f4('0x5f')]||_0xb7dd00[_0x39f4('0x60')]['id'];_0x21ba49[_0x39f4('0x61')]['id']=_0xb7dd00[_0x39f4('0x57')][_0x39f4('0x5f')];return db[_0x39f4('0x3b')][_0x39f4('0x55')]({'where':{'id':_0xb7dd00[_0x39f4('0x51')]['id'],'UserId':null},'include':[{'model':db[_0x39f4('0x62')],'as':_0x39f4('0x63')}]})[_0x39f4('0x1d')](handleEntityNotFound(_0x2eedcf,null))[_0x39f4('0x1d')](saveUpdates(_0xb7dd00['body'],null))[_0x39f4('0x1d')](function(_0x225585){if(_0x225585){_0x21ba49[_0x39f4('0x26')]=_0x225585[_0x39f4('0x64')]({'plain':!![]});return db[_0x39f4('0x65')][_0x39f4('0x55')]({'where':{'id':_0x225585['ChatInteractionId']}});}return null;})['then'](handleEntityNotFound(_0x2eedcf,null))[_0x39f4('0x1d')](function(_0x49f34e){if(_0x49f34e){return _0x49f34e['update']({'UserId':_0xb7dd00['body'][_0x39f4('0x5f')],'read1stAt':_[_0x39f4('0x66')](_0x49f34e[_0x39f4('0x67')])?moment()[_0x39f4('0x68')](_0x39f4('0x69')):undefined});}return null;})[_0x39f4('0x1d')](function(_0x37d030){if(_0x37d030){_0x21ba49['interaction']=_0x37d030[_0x39f4('0x64')]({'plain':!![]});interaction_log[_0x39f4('0x1e')](_0x39f4('0x6a'),_0xb7dd00['user']['id'],_0xb7dd00[_0x39f4('0x60')][_0x39f4('0x38')],_0xb7dd00[_0x39f4('0x60')][_0x39f4('0x6b')],_0x21ba49['interaction']['id'],_0xb7dd00[_0x39f4('0x57')]?JSON[_0x39f4('0x23')](_0xb7dd00[_0x39f4('0x57')]):'null');return respondWithRpcPromise('AcceptMessage',_0x39f4('0x6c'),_0x21ba49);}return null;})[_0x39f4('0x1d')](function(_0x18a4bc){if(_0x18a4bc){if(_0xb7dd00[_0x39f4('0x57')][_0x39f4('0x6d')]){_0x21ba49['message']['interface']=util['format'](_0x39f4('0x6e'),_0xb7dd00['user'][_0x39f4('0x38')]);_0x21ba49['message']['channel']=_0x21ba49['channel'];_0x21ba49[_0x39f4('0x26')][_0x39f4('0x6f')]=_0x39f4('0x5b');return respondWithRpcPromise('EventManager',_0x39f4('0x70'),{'event':_0x39f4('0x71'),'message':_[_0x39f4('0x4c')](_0x21ba49[_0x39f4('0x26')],JSON[_0x39f4('0x72')](_0x21ba49['interaction']['formData']))},client9002)['then'](function(){return _0x21ba49;});}return _0x21ba49;}return null;})['then'](respondWithResult(_0x2eedcf,null))[_0x39f4('0x28')](handleError(_0x2eedcf,null));};var interaction_log=require(_0x39f4('0x59'))(_0x39f4('0x5a'));exports[_0x39f4('0x31')]=function(_0x478b9e,_0x38c6c5,_0x5cf737){var _0x2ccb5a={'agent':{},'channel':_0x39f4('0x5c')};if(_0x478b9e[_0x39f4('0x57')]['id']){delete _0x478b9e[_0x39f4('0x57')]['id'];}_0x478b9e[_0x39f4('0x57')][_0x39f4('0x5f')]=_0x478b9e[_0x39f4('0x57')][_0x39f4('0x5f')]||_0x478b9e[_0x39f4('0x60')]['id'];_0x2ccb5a['agent']['id']=_0x478b9e[_0x39f4('0x57')][_0x39f4('0x5f')];return db[_0x39f4('0x3b')][_0x39f4('0x55')]({'where':{'id':_0x478b9e[_0x39f4('0x51')]['id'],'UserId':null}})[_0x39f4('0x1d')](handleEntityNotFound(_0x38c6c5,null))[_0x39f4('0x1d')](function(_0x3e11c5){if(_0x3e11c5){_0x2ccb5a['message']=_0x3e11c5[_0x39f4('0x64')]({'plain':!![]});return db[_0x39f4('0x65')][_0x39f4('0x55')]({'where':{'id':_0x3e11c5[_0x39f4('0x73')]}});}return null;})[_0x39f4('0x1d')](handleEntityNotFound(_0x38c6c5,null))[_0x39f4('0x1d')](function(_0x4e9990){if(_0x4e9990){_0x2ccb5a[_0x39f4('0x74')]=_0x4e9990[_0x39f4('0x64')]({'plain':!![]});interaction_log['info'](_0x39f4('0x75'),_0x478b9e['user']['id'],_0x478b9e[_0x39f4('0x60')][_0x39f4('0x38')],_0x478b9e[_0x39f4('0x60')][_0x39f4('0x6b')],_0x2ccb5a[_0x39f4('0x74')]['id'],_0x478b9e['body']?JSON[_0x39f4('0x23')](_0x478b9e[_0x39f4('0x57')]):_0x39f4('0x76'));return respondWithRpcPromise(_0x39f4('0x77'),_0x39f4('0x78'),_0x2ccb5a);}return null;})['then'](function(_0x232bae){if(_0x232bae){_0x232bae[_0x39f4('0x26')][_0x39f4('0x79')]=util['format'](_0x39f4('0x6e'),_0x478b9e[_0x39f4('0x60')][_0x39f4('0x38')]);_0x232bae[_0x39f4('0x26')][_0x39f4('0x7a')]=_0x2ccb5a[_0x39f4('0x7a')];_0x232bae[_0x39f4('0x26')][_0x39f4('0x6f')]=_0x39f4('0x31');respondWithRpcPromise(_0x39f4('0x70'),'EventManager',{'event':'rejectmessage','message':_0x232bae['message']},client9002);return _0x2ccb5a;}return null;})['then'](respondWithResult(_0x38c6c5,null))[_0x39f4('0x28')](handleError(_0x38c6c5,null));};