001b30f9be2611eba7821562a670948e85ed3807
[motion2.git] / server / components / interaction / accept-message.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 _0xfe0d=['interaction','formData','EventManager','sendAmiAcceptEvent','AcceptMessage','acceptMessage','sendRoutingAcceptEvent','newinteraction','opened','sendInteractionOpenedEvent','lodash','moment','jayson/promise','../../mysqldb','../../errors/api','../../config/logger','api','http','then','info','%s,\x20%s,\x20%s','request\x20sent','%s,\x20%s,\x20%s,\x20%s','stringify','error','code','message','params','user','read','format','UserId','getAcceptParams','capitalize','findOne','Message\x20with\x20id\x20','\x20not\x20found','update','get','read1stAt','YYYY-MM-DD\x20HH:mm:ss','updateInteractionById','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','sequelize','QueryTypes','SELECT','Contact\x20with\x20id\x20','findContactById','agent','fullname','internal','findAgentById','Message','chat','ChatInteractionId','fax','FaxInteractionId','mail','MailInteractionId','openchannel','OpenchannelInteractionId','sms','SmsInteractionId','whatsapp','WhatsappInteractionId','col','isNewInteraction','assign','parse'];(function(_0x5d4607,_0x6b5e7b){var _0x5e4a86=function(_0x46891a){while(--_0x46891a){_0x5d4607['push'](_0x5d4607['shift']());}};_0x5e4a86(++_0x6b5e7b);}(_0xfe0d,0x1a8));var _0xdfe0=function(_0x432682,_0x253044){_0x432682=_0x432682-0x0;var _0x463d05=_0xfe0d[_0x432682];return _0x463d05;};'use strict';var _=require(_0xdfe0('0x0'));var moment=require(_0xdfe0('0x1'));var jayson=require(_0xdfe0('0x2'));var db=require(_0xdfe0('0x3'))['db'];var APINotFoundError=require(_0xdfe0('0x4'))['APINotFoundError'];var logger=require(_0xdfe0('0x5'))(_0xdfe0('0x6'));var rpcAmiClient=jayson['client'][_0xdfe0('0x7')]({'port':0x232a});var rpcRoutingClient=jayson['client'][_0xdfe0('0x7')]({'port':0x232c});function respondWithRpcPromise(_0x34b622,_0x13e1d4,_0x23b27d,_0x31eb99,_0x2d3b54){return new Promise(function(_0x391561,_0x4fb32d){return _0x2d3b54['request'](_0x13e1d4,_0x31eb99)[_0xdfe0('0x8')](function(_0x4a7821){logger[_0xdfe0('0x9')](_0xdfe0('0xa'),_0x34b622,_0x23b27d,_0xdfe0('0xb'));logger['debug'](_0xdfe0('0xc'),_0x34b622,_0x23b27d,_0xdfe0('0xb'),JSON[_0xdfe0('0xd')](_0x4a7821));if(_0x4a7821[_0xdfe0('0xe')]){if(_0x4a7821[_0xdfe0('0xe')][_0xdfe0('0xf')]===0x1f4){logger[_0xdfe0('0xe')](_0xdfe0('0xa'),_0x34b622,_0x23b27d,_0x4a7821[_0xdfe0('0xe')][_0xdfe0('0x10')]);return _0x4fb32d(_0x4a7821[_0xdfe0('0xe')][_0xdfe0('0x10')]);}logger[_0xdfe0('0xe')](_0xdfe0('0xa'),_0x34b622,_0x23b27d,_0x4a7821[_0xdfe0('0xe')]['message']);return _0x391561(_0x4a7821[_0xdfe0('0xe')][_0xdfe0('0x10')]);}else{logger[_0xdfe0('0x9')]('%s,\x20%s,\x20%s',_0x34b622,_0x23b27d,_0xdfe0('0xb'));_0x391561(_0x4a7821['result'][_0xdfe0('0x10')]);}})['catch'](function(_0x13fc4f){logger[_0xdfe0('0xe')](_0xdfe0('0xa'),_0x34b622,_0x23b27d,_0x13fc4f);_0x4fb32d(_0x13fc4f);});});}function getAcceptParams(_0xad24c5){var _0x18b46c=_0xad24c5[_0xdfe0('0x11')]['id'];var _0x249ed7=_0xad24c5['body'];delete _0x249ed7['id'];var _0x44af08=_0xad24c5[_0xdfe0('0x12')]['get']({'plain':!![]});_0x249ed7[_0xdfe0('0x13')]=!![];_0x249ed7['readAt']=moment()[_0xdfe0('0x14')]('YYYY-MM-DD\x20HH:mm:ss');_0x249ed7[_0xdfe0('0x15')]=_0x249ed7['UserId']||_0x44af08['id'];return{'messageId':_0x18b46c,'payload':_0x249ed7,'user':_0x44af08};}exports[_0xdfe0('0x16')]=getAcceptParams;function updateMessageById(_0x4d4da8,_0x35ca9e,_0x3a792f){var _0x8a0bcc=_[_0xdfe0('0x17')](_0x4d4da8)+'Message';return db[_0x8a0bcc][_0xdfe0('0x18')]({'where':{'id':_0x35ca9e,'UserId':null}})[_0xdfe0('0x8')](function(_0x5c1080){if(!_0x5c1080){throw new APINotFoundError(_0xdfe0('0x19')+_0x35ca9e+_0xdfe0('0x1a'));}return _0x5c1080[_0xdfe0('0x1b')](_0x3a792f);})['then'](function(_0x4778fa){return _0x4778fa[_0xdfe0('0x1c')]({'plain':!![]});});}exports['updateMessageById']=updateMessageById;function updateInteractionById(_0x1ca93e,_0xf74951,_0x2add80){var _0x4faaa1=_[_0xdfe0('0x17')](_0x1ca93e)+'Interaction';return db[_0x4faaa1][_0xdfe0('0x18')]({'where':{'id':_0xf74951}})[_0xdfe0('0x8')](function(_0xe9f8b6){if(!_0xe9f8b6){throw new APINotFoundError('Interaction\x20with\x20id\x20'+_0xf74951+_0xdfe0('0x1a'));}var _0x3e7422=_0xe9f8b6[_0xdfe0('0x1d')]||moment()['format'](_0xdfe0('0x1e'));return _0xe9f8b6['update']({'UserId':_0x2add80,'read1stAt':_0x3e7422});})[_0xdfe0('0x8')](function(_0x5a18eb){return _0x5a18eb[_0xdfe0('0x1c')]({'plain':!![]});});}exports[_0xdfe0('0x1f')]=updateInteractionById;function findContactById(_0x3e4b4c){var _0x3e25c9=_0xdfe0('0x20')+_0x3e4b4c;return db['sequelize']['query'](_0x3e25c9,{'type':db[_0xdfe0('0x21')][_0xdfe0('0x22')][_0xdfe0('0x23')],'raw':!![]})[_0xdfe0('0x8')](function(_0x3f33df){if(_0x3f33df['length']===0x0){throw new APINotFoundError(_0xdfe0('0x24')+_0x3e4b4c+'\x20not\x20found');}return _0x3f33df[0x0];});}exports[_0xdfe0('0x25')]=findContactById;function findAgentById(_0x15f3da){return db['User']['findOne']({'where':{'id':_0x15f3da,'role':_0xdfe0('0x26')},'attributes':['id','name',_0xdfe0('0x27'),'email',_0xdfe0('0x28')],'raw':!![]});}exports[_0xdfe0('0x29')]=findAgentById;function isNewInteraction(_0x2274ac,_0x188863,_0x1c73b8){var _0x46d336=_[_0xdfe0('0x17')](_0x2274ac)+_0xdfe0('0x2a');var _0x389acb={};switch(_0x2274ac){case _0xdfe0('0x2b'):_0x389acb[_0xdfe0('0x2c')]=_0x188863;break;case _0xdfe0('0x2d'):_0x389acb[_0xdfe0('0x2e')]=_0x188863;break;case _0xdfe0('0x2f'):_0x389acb[_0xdfe0('0x30')]=_0x188863;break;case _0xdfe0('0x31'):_0x389acb[_0xdfe0('0x32')]=_0x188863;break;case _0xdfe0('0x33'):_0x389acb[_0xdfe0('0x34')]=_0x188863;break;case _0xdfe0('0x35'):_0x389acb[_0xdfe0('0x36')]=_0x188863;break;}return db[_0x46d336][_0xdfe0('0x18')]({'where':_0x389acb,'attributes':[[db[_0xdfe0('0x21')]['fn']('min',db['sequelize'][_0xdfe0('0x37')]('id')),'id']],'raw':!![]})[_0xdfe0('0x8')](function(_0x5c8ed6){return _0x1c73b8===_0x5c8ed6['id'];});}exports[_0xdfe0('0x38')]=isNewInteraction;function sendAmiAcceptEvent(_0x158ddc,_0xfca09d){var _0x430f0a=_['capitalize'](_0x158ddc)+'Message';var _0x445bbd=_0xfca09d[_0xdfe0('0x10')];if(_0x158ddc===_0xdfe0('0x2b')){Object[_0xdfe0('0x39')](_0x445bbd,JSON[_0xdfe0('0x3a')](_0xfca09d[_0xdfe0('0x3b')][_0xdfe0('0x3c')]));}return respondWithRpcPromise(_0x430f0a,'EventManager',_0xdfe0('0x3d'),{'event':'acceptmessage','message':_0x445bbd},rpcAmiClient);}exports[_0xdfe0('0x3e')]=sendAmiAcceptEvent;function sendRoutingAcceptEvent(_0x17dc4f,_0x1018e3){var _0x70df30=_[_0xdfe0('0x17')](_0x17dc4f)+_0xdfe0('0x2a');return respondWithRpcPromise(_0x70df30,_0xdfe0('0x3f'),_0xdfe0('0x40'),_0x1018e3,rpcRoutingClient);}exports[_0xdfe0('0x41')]=sendRoutingAcceptEvent;function sendInteractionOpenedEvent(_0x43b89a,_0x27bffb){var _0x31fdb5=_[_0xdfe0('0x17')](_0x43b89a)+_0xdfe0('0x2a');return respondWithRpcPromise(_0x31fdb5,_0xdfe0('0x3d'),'EventManager',{'event':_0xdfe0('0x42'),'message':Object[_0xdfe0('0x39')]({},_0x27bffb,{'event':_0xdfe0('0x43')})},rpcAmiClient);}exports[_0xdfe0('0x44')]=sendInteractionOpenedEvent;