472890db4e7f62839877d83b78f0ada923cd97c2
[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 _0xf323=['../../../../config/logger','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/chatInteraction','../../rpc/openchannelInteraction','../../rpc/mailInteraction','../../rpc/smsInteraction','../../mailRpc','consequence','mail','body','isAutoreply','[%s][AUTOREPLY]\x20%s','Autoreply\x20not\x20executed\x20(message\x20is\x20autoreply)','next','isNil','interaction','created','Autoreply\x20already\x20sent','info','[%s][AUTOREPLY][autoreplyExecuted]\x20%s','One\x20Time\x20already\x20sent','format','YYYY-MM-DD\x20HH:mm:ss','out','getMustacheRender','message','from','channel','phone','subject','Re:\x20','chat','ChatWebsiteId','account','upperFirst','AccountId','Message','inReplyTo','messageId','html','<div\x20id=\x22xm-mail-content\x22>','replace','<div','</div>','headers','auto-replied','smtpOptions','then','[%s][AUTOREPLY]\x20SMTP\x20SENT','create','stringify','catch','[RPC][%s][AUTOREPLY]\x20%s','finally','resolve','autoreplyExecuted','update','error','[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s','stop','./utils'];(function(_0x1beef3,_0x57c4b8){var _0x5af964=function(_0x36f463){while(--_0x36f463){_0x1beef3['push'](_0x1beef3['shift']());}};_0x5af964(++_0x57c4b8);}(_0xf323,0x1c6));var _0x3f32=function(_0x13c23f,_0x1a48b8){_0x13c23f=_0x13c23f-0x0;var _0x3675a0=_0xf323[_0x13c23f];return _0x3675a0;};'use strict';var _=require('lodash');var moment=require('moment');var utils=require(_0x3f32('0x0'));var logger=require(_0x3f32('0x1'))('routing');var rpc={'chatMessage':require(_0x3f32('0x2')),'openchannelMessage':require(_0x3f32('0x3')),'mailMessage':require(_0x3f32('0x4')),'smsMessage':require(_0x3f32('0x5')),'faxMessage':require(_0x3f32('0x6')),'whatsappMessage':require('../../rpc/whatsappMessage'),'chatInteraction':require(_0x3f32('0x7')),'openchannelInteraction':require(_0x3f32('0x8')),'mailInteraction':require(_0x3f32('0x9')),'smsInteraction':require(_0x3f32('0xa')),'faxInteraction':require('../../rpc/faxInteraction'),'whatsappInteraction':require('../../rpc/whatsappInteraction')};var mailRpc=require(_0x3f32('0xb'));exports[_0x3f32('0xc')]=function(_0x1c6cb8,_0x34c78d,_0x22a9d3,_0x1c3257){return function(_0x228ac0){try{var _0x952bb4=this['channel'];if(_0x952bb4===_0x3f32('0xd')&&_0x1c3257[_0x3f32('0xe')][_0x3f32('0xf')]){logger['info'](_0x3f32('0x10'),_0x1c6cb8,_0x3f32('0x11'));return _0x228ac0[_0x3f32('0x12')]();}if(_0x34c78d[0x0]==='1'&&(_[_0x3f32('0x13')](this['interaction']['created'])||!this[_0x3f32('0x14')][_0x3f32('0x15')])){logger['info'](_0x3f32('0x10'),_0x1c6cb8,_0x3f32('0x16'));return _0x228ac0[_0x3f32('0x12')]();}if(_0x34c78d[0x0]==='2'&&this['interaction']['autoreplyExecuted']){logger[_0x3f32('0x17')](_0x3f32('0x18'),_0x1c6cb8,_0x3f32('0x19'));return _0x228ac0[_0x3f32('0x12')]();}var _0x54eb23={'ContactId':this['contact']['id'],'sentAt':moment()[_0x3f32('0x1a')](_0x3f32('0x1b')),'direction':_0x3f32('0x1c')};if(this['channel']===_0x3f32('0xd')){_0x54eb23[_0x3f32('0xe')]=utils[_0x3f32('0x1d')](_0x34c78d[0x2],this);}else{_0x54eb23[_0x3f32('0xe')]=utils[_0x3f32('0x1d')](_0x34c78d[0x1],this);}if(this[_0x3f32('0x1e')]){_0x54eb23['to']=this['message'][_0x3f32('0x1f')]||'';if(this[_0x3f32('0x20')]===_0x3f32('0xd')&&_0x34c78d[0x1]==='1'){_0x54eb23['cc']=this[_0x3f32('0x1e')]['cc']||'';}_0x54eb23[_0x3f32('0x1f')]=this[_0x3f32('0x1e')]['to']||'';_0x54eb23['phone']=this[_0x3f32('0x1e')][_0x3f32('0x21')]||'';_0x54eb23['read']=!![];if(this[_0x3f32('0x1e')][_0x3f32('0x22')]){_0x54eb23[_0x3f32('0x22')]='';_0x54eb23[_0x3f32('0x22')]+=_['startsWith'](this[_0x3f32('0x1e')][_0x3f32('0x22')],'Re:')?'':_0x3f32('0x23');_0x54eb23[_0x3f32('0x22')]+=this[_0x3f32('0x1e')][_0x3f32('0x22')];}}if(this[_0x3f32('0x20')]===_0x3f32('0x24')){_0x54eb23[_0x3f32('0x25')]=this[_0x3f32('0x26')]['id'];}else{_0x54eb23[_[_0x3f32('0x27')](this[_0x3f32('0x20')])+_0x3f32('0x28')]=this['account']['id'];}_0x54eb23[_[_0x3f32('0x27')](this[_0x3f32('0x20')])+'InteractionId']=this[_0x3f32('0x14')]['id'];var _0x5547e7=this['interaction'];if(rpc[this[_0x3f32('0x20')]+_0x3f32('0x29')]){if(this[_0x3f32('0x20')]===_0x3f32('0xd')){_0x54eb23[_0x3f32('0x2a')]=this[_0x3f32('0x1e')][_0x3f32('0x2b')];_0x54eb23[_0x3f32('0x2c')]=_0x3f32('0x2d')+_[_0x3f32('0x2e')](_0x54eb23[_0x3f32('0xe')],'<div\x20id=\x22xm-mail-content\x22',_0x3f32('0x2f'))+_0x3f32('0x30');_0x54eb23[_0x3f32('0x31')]={'Auto-Submitted':_0x3f32('0x32'),'X-Auto-Response-Suppress':'All','Precedence':'auto_reply'};return mailRpc['sendMailMessage'](this[_0x3f32('0x26')][_0x3f32('0x33')],_0x54eb23)[_0x3f32('0x34')](function(_0x43fe5b){logger[_0x3f32('0x17')](_0x3f32('0x35'),_0x1c6cb8);_0x54eb23[_0x3f32('0x2b')]=_0x43fe5b[_0x3f32('0x1e')][_0x3f32('0x2b')];return rpc[_0x952bb4+'Message'][_0x3f32('0x36')](_0x54eb23);})[_0x3f32('0x34')](function(_0x550fab){logger['info'](_0x3f32('0x10'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x550fab));})[_0x3f32('0x38')](function(_0x59e8a2){logger['error'](_0x3f32('0x39'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x59e8a2));})[_0x3f32('0x3a')](function(){Promise[_0x3f32('0x3b')]()['then'](function(){if(_0x34c78d[0x0]!=='2'||_0x5547e7[_0x3f32('0x3c')])return;return rpc[_0x952bb4+'Interaction'][_0x3f32('0x3d')](_0x5547e7['id'],{'autoreplyExecuted':!![]})[_0x3f32('0x34')](function(_0x47a4ee){logger[_0x3f32('0x17')](_0x3f32('0x18'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x47a4ee));})[_0x3f32('0x38')](function(_0x2d3ac0){logger[_0x3f32('0x3e')](_0x3f32('0x3f'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x2d3ac0));});})['then'](function(){_0x228ac0['next']();});});}else{return rpc[this[_0x3f32('0x20')]+_0x3f32('0x29')][_0x3f32('0x36')](_0x54eb23)[_0x3f32('0x34')](function(){logger[_0x3f32('0x17')](_0x3f32('0x10'),_0x1c6cb8,_0x54eb23[_0x3f32('0xe')]);})[_0x3f32('0x38')](function(_0x15b975){logger['error'](_0x3f32('0x10'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x15b975));})[_0x3f32('0x3a')](function(){Promise['resolve']()[_0x3f32('0x34')](function(){if(_0x34c78d[0x0]!=='2'||_0x5547e7[_0x3f32('0x3c')])return;return rpc[_0x952bb4+'Interaction'][_0x3f32('0x3d')](_0x5547e7['id'],{'autoreplyExecuted':!![]})[_0x3f32('0x34')](function(_0xf600f1){logger[_0x3f32('0x17')](_0x3f32('0x18'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0xf600f1));})[_0x3f32('0x38')](function(_0x55cacd){logger['error'](_0x3f32('0x3f'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x55cacd));});})['then'](function(){_0x228ac0[_0x3f32('0x12')]();});});}}}catch(_0x35701f){logger[_0x3f32('0x3e')](_0x3f32('0x10'),_0x1c6cb8,JSON[_0x3f32('0x37')](_0x35701f));_0x228ac0[_0x3f32('0x40')]();}};};