8ad20d900411fe3bd23695346f8cf2adb1e4d985
[motion2.git] / server / components / interaction / update-interaction.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 _0x066f=['closed','disposition','UserId','total','MailMessage','findOne','-id','then','plainBody','lastMsgText','trimStart','Users','TagIds','Tags','catch','lodash','../../mysqldb','map','split','forEach','push','waiting','talking','unmanaged','abandoned'];(function(_0x4b9978,_0x58eb71){var _0x42f0e5=function(_0x4ff5dc){while(--_0x4ff5dc){_0x4b9978['push'](_0x4b9978['shift']());}};_0x42f0e5(++_0x58eb71);}(_0x066f,0x19f));var _0xf066=function(_0x369ce0,_0x584c10){_0x369ce0=_0x369ce0-0x0;var _0x1ac9d3=_0x066f[_0x369ce0];return _0x1ac9d3;};'use strict';var _=require(_0xf066('0x0'));var db=require(_0xf066('0x1'))['db'];function getInteractionUsers(_0x32304a,_0x270432){return new Promise(function(_0x534f46,_0x927212){try{var _0x473426=[];if(_0x270432[_0x32304a]){_0x473426=_0x270432[_0x32304a][_0xf066('0x2')](function(_0x57b3ae){return{'id':_0x57b3ae['id'],'fullname':_0x57b3ae['fullname']};});}return _0x534f46(_0x473426);}catch(_0x1cc72b){_0x927212(_0x1cc72b);}});}function getInteractionTags(_0x3caa8d,_0x5259db){var _0x157231=[];if(_0x3caa8d){_0x3caa8d[_0xf066('0x3')](',')[_0xf066('0x4')](function(_0x5478eb){_0x157231[_0xf066('0x5')](_0x5259db[_0x5478eb]);});}return _0x157231;}function updateWaitingCounter(_0xdb4792){if(_0xdb4792[_0xf066('0x6')]){_0xdb4792[_0xf066('0x6')]-=0x1;}}function updateInteractionCounters(_0x2f768d){_0x2f768d[_0xf066('0x6')]=0x0;_0x2f768d[_0xf066('0x7')]=0x0;_0x2f768d['answered']=0x0;_0x2f768d[_0xf066('0x8')]=0x0;_0x2f768d[_0xf066('0x9')]=0x0;_0x2f768d['total']=0x0;if(_0x2f768d[_0xf066('0xa')]){if(_0x2f768d[_0xf066('0xb')]==='abandoned'){_0x2f768d[_0xf066('0x9')]+=0x1;updateWaitingCounter(_0x2f768d);}if(_0x2f768d[_0xf066('0xc')]){_0x2f768d['answered']+=0x1;if(_0x2f768d[_0xf066('0x7')]){_0x2f768d[_0xf066('0x7')]-=0x1;}}else{if(_0x2f768d[_0xf066('0xb')]){_0x2f768d[_0xf066('0x8')]+=0x1;updateWaitingCounter(_0x2f768d);}}}else{if(!_0x2f768d[_0xf066('0xc')]){_0x2f768d['waiting']+=0x1;}if(_0x2f768d[_0xf066('0xc')]){_0x2f768d[_0xf066('0x7')]+=0x1;updateWaitingCounter(_0x2f768d);}}_0x2f768d[_0xf066('0xd')]+=0x1;return _0x2f768d;}function getLastMessageText(_0x40e89f){return db[_0xf066('0xe')][_0xf066('0xf')]({'where':{'$and':[{'MailInteractionId':_0x40e89f['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':'out'},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':['plainBody'],'order':_0xf066('0x10'),'raw':!![]})[_0xf066('0x11')](function(_0x5509be){if(_0x5509be&&_0x5509be[_0xf066('0x12')]){_0x40e89f[_0xf066('0x13')]=_[_0xf066('0x14')](_0x5509be[_0xf066('0x12')]);}return _0x40e89f;})['catch'](function(_0x4594a2){throw _0x4594a2;});}exports['updateInteraction']=function(_0xfed8e5,_0x189a10,_0x5e7b4d,_0x384d40){return new Promise(function(_0x17283d,_0x77ee38){return getInteractionUsers(_0xfed8e5['id'],_0x5e7b4d)[_0xf066('0x11')](function(_0x2c6215){_0xfed8e5[_0xf066('0x15')]=_0x2c6215;return getInteractionTags(_0xfed8e5[_0xf066('0x16')],_0x189a10);})['then'](function(_0xd6084e){_0xfed8e5[_0xf066('0x17')]=_0xd6084e;delete _0xfed8e5['TagIds'];return updateInteractionCounters(_0xfed8e5);})['then'](function(_0x565941){if(_0x384d40){return getLastMessageText(_0x565941);}else{return _0x565941;}})[_0xf066('0x11')](function(_0x4900c3){_0x17283d(_0x4900c3);})[_0xf066('0x18')](function(_0x554fb9){_0x77ee38(_0x554fb9);});});};