Built motion from commit 9efc8423.|2.6.31
[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 _0xb04f=['Content-Range','update','then','destroy','end','sendStatus','stack','name','send','index','key','query','keys','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','sort','pick','filter','merge','where','map','type','ChatMessage','includeAll','include','findAll','rows','show','params','rawAttributes','find','create','body','describe','accept','chat','acceptMessage','reject','UserId','user','agent','ChatInteraction','ChatInteractionId','interaction','get','[CHATMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','null','RejectMessage','rejectMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','sequelize','SELECT','contact','User','fullname','internal','interface','format','SIP/%s','channel','motionChannel','event','EventManager','rejectmessage','parse','formData','util','lodash','ioredis','../../components/interaction/service','../../config/logger','api','redis','localhost','socket.io-emitter','./chatMessage.socket','register','client','http','chat-interactions','request','info','ChatMessage,\x20%s,\x20%s','request\x20sent','debug','ChatMessage,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','catch','status','json','offset','undefined','limit','count','set'];(function(_0x2f3ff8,_0x5f0185){var _0x3edbfc=function(_0x504618){while(--_0x504618){_0x2f3ff8['push'](_0x2f3ff8['shift']());}};_0x3edbfc(++_0x5f0185);}(_0xb04f,0xb1));var _0xfb04=function(_0xb093b4,_0x2544c0){_0xb093b4=_0xb093b4-0x0;var _0x3a3c3b=_0xb04f[_0xb093b4];return _0x3a3c3b;};'use strict';var util=require(_0xfb04('0x0'));var _=require(_0xfb04('0x1'));var Redis=require(_0xfb04('0x2'));var interactionService=require(_0xfb04('0x3'));var qs=require('../../components/parsers/qs');var logger=require(_0xfb04('0x4'))(_0xfb04('0x5'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0xfb04('0x6')]=_['defaults'](config[_0xfb04('0x6')],{'host':_0xfb04('0x7'),'port':0x18eb});var socket=require(_0xfb04('0x8'))(new Redis(config[_0xfb04('0x6')]));require(_0xfb04('0x9'))[_0xfb04('0xa')](socket);var jayson=require('jayson/promise');var client=jayson[_0xfb04('0xb')][_0xfb04('0xc')]({'port':0x232c});var client9002=jayson[_0xfb04('0xb')][_0xfb04('0xc')]({'port':0x232a});var interaction_log=require(_0xfb04('0x4'))(_0xfb04('0xd'));function respondWithRpcPromise(_0xf8cc7c,_0x1f8a17,_0x56fcdd,_0x290bb9){return new Promise(function(_0x59aecf,_0x545735){var _0x23af45=_0x290bb9||client;return _0x23af45[_0xfb04('0xe')](_0xf8cc7c,_0x56fcdd)['then'](function(_0x24a4bf){logger[_0xfb04('0xf')](_0xfb04('0x10'),_0x1f8a17,_0xfb04('0x11'));logger[_0xfb04('0x12')](_0xfb04('0x13'),_0x1f8a17,'request\x20sent',JSON[_0xfb04('0x14')](_0x24a4bf));if(_0x24a4bf[_0xfb04('0x15')]){if(_0x24a4bf[_0xfb04('0x15')][_0xfb04('0x16')]===0x1f4){logger[_0xfb04('0x15')]('ChatMessage,\x20%s,\x20%s',_0x1f8a17,_0x24a4bf[_0xfb04('0x15')]['message']);return _0x545735(_0x24a4bf[_0xfb04('0x15')]['message']);}logger[_0xfb04('0x15')](_0xfb04('0x10'),_0x1f8a17,_0x24a4bf['error'][_0xfb04('0x17')]);return _0x59aecf(_0x24a4bf['error']['message']);}else{logger[_0xfb04('0xf')](_0xfb04('0x10'),_0x1f8a17,_0xfb04('0x11'));_0x59aecf(_0x24a4bf[_0xfb04('0x18')][_0xfb04('0x17')]);}})[_0xfb04('0x19')](function(_0x1b07cd){logger[_0xfb04('0x15')](_0xfb04('0x10'),_0x1f8a17,_0x1b07cd);_0x545735(_0x1b07cd);});});}function respondWithResult(_0x65615,_0x15ee60){_0x15ee60=_0x15ee60||0xc8;return function(_0x3cc7e6){if(_0x3cc7e6){return _0x65615[_0xfb04('0x1a')](_0x15ee60)[_0xfb04('0x1b')](_0x3cc7e6);}};}function respondWithFilteredResult(_0x5ed972,_0x106db2){return function(_0x3160fa){if(_0x3160fa){var _0x249b1d=typeof _0x106db2[_0xfb04('0x1c')]===_0xfb04('0x1d')&&typeof _0x106db2[_0xfb04('0x1e')]===_0xfb04('0x1d');var _0xe38ae1=_0x3160fa[_0xfb04('0x1f')];var _0x2f5fd5=_0x249b1d?0x0:_0x106db2[_0xfb04('0x1c')];var _0x1664ef=_0x249b1d?_0x3160fa[_0xfb04('0x1f')]:_0x106db2[_0xfb04('0x1c')]+_0x106db2[_0xfb04('0x1e')];var _0x117139;if(_0x1664ef>=_0xe38ae1){_0x1664ef=_0xe38ae1;_0x117139=0xc8;}else{_0x117139=0xce;}_0x5ed972[_0xfb04('0x1a')](_0x117139);return _0x5ed972[_0xfb04('0x20')](_0xfb04('0x21'),_0x2f5fd5+'-'+_0x1664ef+'/'+_0xe38ae1)[_0xfb04('0x1b')](_0x3160fa);}return null;};}function saveUpdates(_0x2aa308){return function(_0x4a6df5){if(_0x4a6df5){return _0x4a6df5[_0xfb04('0x22')](_0x2aa308)[_0xfb04('0x23')](function(_0x3e9e87){return _0x3e9e87;});}return null;};}function removeEntity(_0x27b2d4){return function(_0x1524d6){if(_0x1524d6){return _0x1524d6[_0xfb04('0x24')]()['then'](function(){_0x27b2d4[_0xfb04('0x1a')](0xcc)[_0xfb04('0x25')]();});}};}function handleEntityNotFound(_0x221f68){return function(_0x44054d){if(!_0x44054d){_0x221f68[_0xfb04('0x26')](0x194);}return _0x44054d;};}function handleError(_0x2f25d1,_0x25b0a8){_0x25b0a8=_0x25b0a8||0x1f4;return function(_0x41cbd8){logger[_0xfb04('0x15')](_0x41cbd8[_0xfb04('0x27')]);if(_0x41cbd8[_0xfb04('0x28')]){delete _0x41cbd8[_0xfb04('0x28')];}_0x2f25d1[_0xfb04('0x1a')](_0x25b0a8)[_0xfb04('0x29')](_0x41cbd8);};}exports[_0xfb04('0x2a')]=function(_0x285782,_0x1ec273){var _0x18300c={},_0x35a8c8={},_0x5911b7={'count':0x0,'rows':[]};var _0x25360e=_['map'](db['ChatMessage']['rawAttributes'],function(_0x2823fb){return{'name':_0x2823fb['fieldName'],'type':_0x2823fb['type'][_0xfb04('0x2b')]};});_0x35a8c8['model']=_['map'](_0x25360e,'name');_0x35a8c8[_0xfb04('0x2c')]=_[_0xfb04('0x2d')](_0x285782[_0xfb04('0x2c')]);_0x35a8c8[_0xfb04('0x2e')]=_[_0xfb04('0x2f')](_0x35a8c8[_0xfb04('0x30')],_0x35a8c8[_0xfb04('0x2c')]);_0x18300c['attributes']=_[_0xfb04('0x2f')](_0x35a8c8['model'],qs[_0xfb04('0x31')](_0x285782[_0xfb04('0x2c')][_0xfb04('0x31')]));_0x18300c['attributes']=_0x18300c[_0xfb04('0x32')][_0xfb04('0x33')]?_0x18300c[_0xfb04('0x32')]:_0x35a8c8[_0xfb04('0x30')];if(!_0x285782[_0xfb04('0x2c')][_0xfb04('0x34')](_0xfb04('0x35'))){_0x18300c[_0xfb04('0x1e')]=qs[_0xfb04('0x1e')](_0x285782[_0xfb04('0x2c')][_0xfb04('0x1e')]);_0x18300c[_0xfb04('0x1c')]=qs[_0xfb04('0x1c')](_0x285782[_0xfb04('0x2c')][_0xfb04('0x1c')]);}_0x18300c['order']=qs['sort'](_0x285782[_0xfb04('0x2c')][_0xfb04('0x36')]);_0x18300c['where']=qs['filters'](_[_0xfb04('0x37')](_0x285782[_0xfb04('0x2c')],_0x35a8c8[_0xfb04('0x2e')]),_0x25360e);if(_0x285782['query'][_0xfb04('0x38')]){_0x18300c['where']=_[_0xfb04('0x39')](_0x18300c[_0xfb04('0x3a')],{'$or':_[_0xfb04('0x3b')](_0x25360e,function(_0x4ea37b){if(_0x4ea37b[_0xfb04('0x3c')]!=='VIRTUAL'){var _0x496f83={};_0x496f83[_0x4ea37b['name']]={'$like':'%'+_0x285782['query']['filter']+'%'};return _0x496f83;}})});}_0x18300c=_[_0xfb04('0x39')]({},_0x18300c,_0x285782['options']);var _0x2f783c={'where':_0x18300c[_0xfb04('0x3a')]};return db[_0xfb04('0x3d')]['count'](_0x2f783c)[_0xfb04('0x23')](function(_0xffba8b){_0x5911b7[_0xfb04('0x1f')]=_0xffba8b;if(_0x285782[_0xfb04('0x2c')][_0xfb04('0x3e')]){_0x18300c[_0xfb04('0x3f')]=[{'all':!![]}];}return db[_0xfb04('0x3d')][_0xfb04('0x40')](_0x18300c);})[_0xfb04('0x23')](function(_0x5a7783){_0x5911b7[_0xfb04('0x41')]=_0x5a7783;return _0x5911b7;})[_0xfb04('0x23')](respondWithFilteredResult(_0x1ec273,_0x18300c))['catch'](handleError(_0x1ec273,null));};exports[_0xfb04('0x42')]=function(_0x45a893,_0x142b20){var _0x2a7447={'raw':!![],'where':{'id':_0x45a893[_0xfb04('0x43')]['id']}},_0x649b61={};_0x649b61[_0xfb04('0x30')]=_['keys'](db[_0xfb04('0x3d')][_0xfb04('0x44')]);_0x649b61['query']=_[_0xfb04('0x2d')](_0x45a893['query']);_0x649b61[_0xfb04('0x2e')]=_['intersection'](_0x649b61[_0xfb04('0x30')],_0x649b61[_0xfb04('0x2c')]);_0x2a7447[_0xfb04('0x32')]=_[_0xfb04('0x2f')](_0x649b61[_0xfb04('0x30')],qs['fields'](_0x45a893[_0xfb04('0x2c')][_0xfb04('0x31')]));_0x2a7447['attributes']=_0x2a7447[_0xfb04('0x32')][_0xfb04('0x33')]?_0x2a7447[_0xfb04('0x32')]:_0x649b61[_0xfb04('0x30')];if(_0x45a893['query']['includeAll']){_0x2a7447[_0xfb04('0x3f')]=[{'all':!![]}];}_0x2a7447=_['merge']({},_0x2a7447,_0x45a893['options']);return db[_0xfb04('0x3d')][_0xfb04('0x45')](_0x2a7447)[_0xfb04('0x23')](handleEntityNotFound(_0x142b20,null))[_0xfb04('0x23')](respondWithResult(_0x142b20,null))[_0xfb04('0x19')](handleError(_0x142b20,null));};exports[_0xfb04('0x46')]=function(_0x277a8d,_0x48f53d){return db['ChatMessage'][_0xfb04('0x46')](_0x277a8d[_0xfb04('0x47')],{})[_0xfb04('0x23')](respondWithResult(_0x48f53d,0xc9))['catch'](handleError(_0x48f53d,null));};exports[_0xfb04('0x22')]=function(_0x31ae35,_0x486b87){if(_0x31ae35[_0xfb04('0x47')]['id']){delete _0x31ae35['body']['id'];}return db['ChatMessage'][_0xfb04('0x45')]({'where':{'id':_0x31ae35[_0xfb04('0x43')]['id']}})['then'](handleEntityNotFound(_0x486b87,null))[_0xfb04('0x23')](saveUpdates(_0x31ae35[_0xfb04('0x47')],null))[_0xfb04('0x23')](respondWithResult(_0x486b87,null))[_0xfb04('0x19')](handleError(_0x486b87,null));};exports['destroy']=function(_0x3fa159,_0x196c08){return db[_0xfb04('0x3d')][_0xfb04('0x45')]({'where':{'id':_0x3fa159[_0xfb04('0x43')]['id']}})[_0xfb04('0x23')](handleEntityNotFound(_0x196c08,null))['then'](removeEntity(_0x196c08,null))[_0xfb04('0x19')](handleError(_0x196c08,null));};exports[_0xfb04('0x48')]=function(_0x24ff23,_0x55932){return db[_0xfb04('0x3d')]['describe']()[_0xfb04('0x23')](respondWithResult(_0x55932,null))[_0xfb04('0x19')](handleError(_0x55932,null));};exports[_0xfb04('0x49')]=function(_0x3412ee,_0x5a9abf){var _0x21d5a6=_0xfb04('0x4a');return interactionService[_0xfb04('0x4b')](_0x3412ee,_0x21d5a6)['then'](respondWithResult(_0x5a9abf,null))[_0xfb04('0x19')](handleError(_0x5a9abf,null));};exports[_0xfb04('0x4c')]=function(_0x9e7739,_0x50b9a9){var _0x47d8e1={'agent':{},'channel':_0xfb04('0x4a')};if(_0x9e7739[_0xfb04('0x47')]['id']){delete _0x9e7739['body']['id'];}_0x9e7739[_0xfb04('0x47')]['UserId']=_0x9e7739[_0xfb04('0x47')][_0xfb04('0x4d')]||_0x9e7739[_0xfb04('0x4e')]['id'];_0x47d8e1[_0xfb04('0x4f')]['id']=_0x9e7739[_0xfb04('0x47')][_0xfb04('0x4d')];return db[_0xfb04('0x3d')][_0xfb04('0x45')]({'where':{'id':_0x9e7739['params']['id'],'UserId':null}})[_0xfb04('0x23')](handleEntityNotFound(_0x50b9a9,null))[_0xfb04('0x23')](function(_0x158a9c){if(_0x158a9c){_0x47d8e1[_0xfb04('0x17')]=_0x158a9c['get']({'plain':!![]});return db[_0xfb04('0x50')][_0xfb04('0x45')]({'where':{'id':_0x158a9c[_0xfb04('0x51')]}});}return null;})['then'](handleEntityNotFound(_0x50b9a9,null))[_0xfb04('0x23')](function(_0x2b52e3){if(_0x2b52e3){_0x47d8e1[_0xfb04('0x52')]=_0x2b52e3[_0xfb04('0x53')]({'plain':!![]});interaction_log[_0xfb04('0xf')](_0xfb04('0x54'),_0x9e7739[_0xfb04('0x4e')]['id'],_0x9e7739[_0xfb04('0x4e')]['name'],_0x9e7739['user']['role'],_0x47d8e1[_0xfb04('0x52')]['id'],_0x9e7739[_0xfb04('0x47')]?JSON['stringify'](_0x9e7739[_0xfb04('0x47')]):_0xfb04('0x55'));return respondWithRpcPromise(_0xfb04('0x56'),_0xfb04('0x57'),_0x47d8e1);}return null;})[_0xfb04('0x23')](function(_0x44c70e){var _0x21ac53=_0xfb04('0x58')+_0x44c70e['message']['ContactId'];return db[_0xfb04('0x59')][_0xfb04('0x2c')](_0x21ac53,{'type':db['Sequelize']['QueryTypes'][_0xfb04('0x5a')],'raw':!![]})['then'](function(_0x311cd7){return _0x311cd7;});})[_0xfb04('0x23')](function(_0x43571b){if(_0x43571b){_0x47d8e1[_0xfb04('0x17')][_0xfb04('0x5b')]=_0x43571b[0x0];if(!_0x9e7739[_0xfb04('0x47')][_0xfb04('0x4d')])return;return db[_0xfb04('0x5c')]['find']({'where':{'id':_0x9e7739[_0xfb04('0x47')][_0xfb04('0x4d')],'role':_0xfb04('0x4f')},'attributes':['id',_0xfb04('0x28'),_0xfb04('0x5d'),'email',_0xfb04('0x5e')],'raw':!![]});}return null;})[_0xfb04('0x23')](function(_0x8e964e){if(!_0x8e964e)return null;_0x47d8e1[_0xfb04('0x17')][_0xfb04('0x4f')]=_0x8e964e;return _0x47d8e1;})[_0xfb04('0x23')](function(_0x1c6a47){if(_0x1c6a47){_0x1c6a47[_0xfb04('0x17')][_0xfb04('0x5f')]=util[_0xfb04('0x60')](_0xfb04('0x61'),_0x9e7739[_0xfb04('0x4e')][_0xfb04('0x28')]);_0x1c6a47[_0xfb04('0x17')][_0xfb04('0x62')]=_0x47d8e1[_0xfb04('0x62')];_0x1c6a47[_0xfb04('0x17')][_0xfb04('0x63')]=_0x47d8e1[_0xfb04('0x62')];_0x1c6a47[_0xfb04('0x17')][_0xfb04('0x64')]=_0xfb04('0x4c');respondWithRpcPromise('EventManager',_0xfb04('0x65'),{'event':_0xfb04('0x66'),'message':_[_0xfb04('0x39')](_0x47d8e1[_0xfb04('0x17')],JSON[_0xfb04('0x67')](_0x47d8e1[_0xfb04('0x52')][_0xfb04('0x68')]))},client9002);return _0x47d8e1;}return null;})[_0xfb04('0x23')](respondWithResult(_0x50b9a9,null))['catch'](handleError(_0x50b9a9,null));};