Built motion from commit 449495f6.|2.6.27
[motion2.git] / server / services / routing / server / applications / autoreply.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 _0x771c=['isNil','interaction','created','[%s][AUTOREPLY]\x20%s','autoreplyExecuted','One\x20Time\x20already\x20sent','next','contact','YYYY-MM-DD\x20HH:mm:ss','out','getMustacheRender','message','from','phone','read','subject','startsWith','Re:','Re:\x20','ChatWebsiteId','account','upperFirst','Message','inReplyTo','messageId','html','<div\x20id=\x22xm-mail-content\x22>','replace','<div\x20id=\x22xm-mail-content\x22','<div','</div>','headers','auto-replied','smtpOptions','[%s][AUTOREPLY]\x20SMTP\x20SENT','create','then','stringify','catch','error','[RPC][%s][AUTOREPLY]\x20%s','finally','resolve','Interaction','update','[%s][AUTOREPLY][autoreplyExecuted]\x20%s','[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s','stop','lodash','moment','./utils','../../../../config/logger','routing','../../rpc/chatMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/chatInteraction','../../rpc/openchannelInteraction','../../rpc/mailInteraction','../../rpc/smsInteraction','../../rpc/faxInteraction','../../rpc/whatsappInteraction','../../mailRpc','consequence','channel','mail','body','isAutoreply','info'];(function(_0x32a767,_0x4e3e76){var _0x2b4e6d=function(_0x5e0702){while(--_0x5e0702){_0x32a767['push'](_0x32a767['shift']());}};_0x2b4e6d(++_0x4e3e76);}(_0x771c,0x189));var _0xc771=function(_0x4b3f84,_0x4aed97){_0x4b3f84=_0x4b3f84-0x0;var _0x21b380=_0x771c[_0x4b3f84];return _0x21b380;};'use strict';var _=require(_0xc771('0x0'));var moment=require(_0xc771('0x1'));var utils=require(_0xc771('0x2'));var logger=require(_0xc771('0x3'))(_0xc771('0x4'));var rpc={'chatMessage':require(_0xc771('0x5')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require('../../rpc/mailMessage'),'smsMessage':require(_0xc771('0x6')),'faxMessage':require(_0xc771('0x7')),'whatsappMessage':require('../../rpc/whatsappMessage'),'chatInteraction':require(_0xc771('0x8')),'openchannelInteraction':require(_0xc771('0x9')),'mailInteraction':require(_0xc771('0xa')),'smsInteraction':require(_0xc771('0xb')),'faxInteraction':require(_0xc771('0xc')),'whatsappInteraction':require(_0xc771('0xd'))};var mailRpc=require(_0xc771('0xe'));exports[_0xc771('0xf')]=function(_0x5aba07,_0x198c4c,_0x7bcd33,_0x1634bc){return function(_0x4d6475){try{var _0x29b83c=this[_0xc771('0x10')];if(_0x29b83c===_0xc771('0x11')&&_0x1634bc[_0xc771('0x12')][_0xc771('0x13')]){logger[_0xc771('0x14')]('[%s][AUTOREPLY]\x20%s',_0x5aba07,'Autoreply\x20not\x20executed\x20(message\x20is\x20autoreply)');return _0x4d6475['next']();}if(_0x198c4c[0x0]==='1'&&(_[_0xc771('0x15')](this['interaction']['created'])||!this[_0xc771('0x16')][_0xc771('0x17')])){logger[_0xc771('0x14')](_0xc771('0x18'),_0x5aba07,'Autoreply\x20already\x20sent');return _0x4d6475['next']();}if(_0x198c4c[0x0]==='2'&&this['interaction'][_0xc771('0x19')]){logger[_0xc771('0x14')]('[%s][AUTOREPLY][autoreplyExecuted]\x20%s',_0x5aba07,_0xc771('0x1a'));return _0x4d6475[_0xc771('0x1b')]();}var _0x1a6caf={'ContactId':this[_0xc771('0x1c')]['id'],'sentAt':moment()['format'](_0xc771('0x1d')),'direction':_0xc771('0x1e')};if(this[_0xc771('0x10')]==='mail'){_0x1a6caf['body']=utils[_0xc771('0x1f')](_0x198c4c[0x2],this);}else{_0x1a6caf['body']=utils[_0xc771('0x1f')](_0x198c4c[0x1],this);}if(this[_0xc771('0x20')]){_0x1a6caf['to']=this[_0xc771('0x20')][_0xc771('0x21')]||'';if(this[_0xc771('0x10')]===_0xc771('0x11')&&_0x198c4c[0x1]==='1'){_0x1a6caf['cc']=this[_0xc771('0x20')]['cc']||'';}_0x1a6caf[_0xc771('0x21')]=this[_0xc771('0x20')]['to']||'';_0x1a6caf[_0xc771('0x22')]=this[_0xc771('0x20')][_0xc771('0x22')]||'';_0x1a6caf[_0xc771('0x23')]=!![];if(this[_0xc771('0x20')][_0xc771('0x24')]){_0x1a6caf[_0xc771('0x24')]='';_0x1a6caf[_0xc771('0x24')]+=_[_0xc771('0x25')](this[_0xc771('0x20')][_0xc771('0x24')],_0xc771('0x26'))?'':_0xc771('0x27');_0x1a6caf[_0xc771('0x24')]+=this['message'][_0xc771('0x24')];}}if(this[_0xc771('0x10')]==='chat'){_0x1a6caf[_0xc771('0x28')]=this['account']['id'];}else{_0x1a6caf[_['upperFirst'](this[_0xc771('0x10')])+'AccountId']=this[_0xc771('0x29')]['id'];}_0x1a6caf[_[_0xc771('0x2a')](this[_0xc771('0x10')])+'InteractionId']=this['interaction']['id'];var _0x2da424=this['interaction'];if(rpc[this[_0xc771('0x10')]+_0xc771('0x2b')]){if(this['channel']==='mail'){_0x1a6caf[_0xc771('0x2c')]=this[_0xc771('0x20')][_0xc771('0x2d')];_0x1a6caf[_0xc771('0x2e')]=_0xc771('0x2f')+_[_0xc771('0x30')](_0x1a6caf[_0xc771('0x12')],_0xc771('0x31'),_0xc771('0x32'))+_0xc771('0x33');_0x1a6caf[_0xc771('0x34')]={'Auto-Submitted':_0xc771('0x35'),'X-Auto-Response-Suppress':'All','Precedence':'auto_reply'};return mailRpc['sendMailMessage'](this[_0xc771('0x29')][_0xc771('0x36')],_0x1a6caf)['then'](function(_0x2c1b79){logger['info'](_0xc771('0x37'),_0x5aba07);_0x1a6caf['messageId']=_0x2c1b79['message'][_0xc771('0x2d')];return rpc[_0x29b83c+_0xc771('0x2b')][_0xc771('0x38')](_0x1a6caf);})[_0xc771('0x39')](function(_0x382249){logger[_0xc771('0x14')](_0xc771('0x18'),_0x5aba07,JSON[_0xc771('0x3a')](_0x382249));})[_0xc771('0x3b')](function(_0x3019d3){logger[_0xc771('0x3c')](_0xc771('0x3d'),_0x5aba07,JSON[_0xc771('0x3a')](_0x3019d3));})[_0xc771('0x3e')](function(){Promise[_0xc771('0x3f')]()[_0xc771('0x39')](function(){if(_0x198c4c[0x0]!=='2'||_0x2da424[_0xc771('0x19')])return;return rpc[_0x29b83c+_0xc771('0x40')][_0xc771('0x41')](_0x2da424['id'],{'autoreplyExecuted':!![]})[_0xc771('0x39')](function(_0xfb0edf){logger[_0xc771('0x14')](_0xc771('0x42'),_0x5aba07,JSON[_0xc771('0x3a')](_0xfb0edf));})[_0xc771('0x3b')](function(_0x2db3b5){logger['error'](_0xc771('0x43'),_0x5aba07,JSON[_0xc771('0x3a')](_0x2db3b5));});})[_0xc771('0x39')](function(){_0x4d6475[_0xc771('0x1b')]();});});}else{return rpc[this[_0xc771('0x10')]+_0xc771('0x2b')][_0xc771('0x38')](_0x1a6caf)['then'](function(){logger[_0xc771('0x14')]('[%s][AUTOREPLY]\x20%s',_0x5aba07,_0x1a6caf[_0xc771('0x12')]);})[_0xc771('0x3b')](function(_0x5bb2de){logger[_0xc771('0x3c')](_0xc771('0x18'),_0x5aba07,JSON[_0xc771('0x3a')](_0x5bb2de));})['finally'](function(){Promise[_0xc771('0x3f')]()[_0xc771('0x39')](function(){if(_0x198c4c[0x0]!=='2'||_0x2da424[_0xc771('0x19')])return;return rpc[_0x29b83c+_0xc771('0x40')][_0xc771('0x41')](_0x2da424['id'],{'autoreplyExecuted':!![]})[_0xc771('0x39')](function(_0x4ec5e4){logger[_0xc771('0x14')](_0xc771('0x42'),_0x5aba07,JSON[_0xc771('0x3a')](_0x4ec5e4));})[_0xc771('0x3b')](function(_0x187870){logger[_0xc771('0x3c')]('[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s',_0x5aba07,JSON['stringify'](_0x187870));});})[_0xc771('0x39')](function(){_0x4d6475['next']();});});}}}catch(_0x3a3de1){logger[_0xc771('0x3c')]('[%s][AUTOREPLY]\x20%s',_0x5aba07,JSON[_0xc771('0x3a')](_0x3a3de1));_0x4d6475[_0xc771('0x44')]();}};};