1a9aae245399a91e1a27cf5842f69214690e2033
[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 _0xde5f=['talking','answered','unmanaged','disposition','abandoned','UserId','total','MailMessage','findOne','plainBody','-id','then','lastMsgText','Users','TagIds','Tags','catch','lodash','map','fullname','split','waiting'];(function(_0x47fae6,_0x15b921){var _0x1faa74=function(_0x32ac6c){while(--_0x32ac6c){_0x47fae6['push'](_0x47fae6['shift']());}};_0x1faa74(++_0x15b921);}(_0xde5f,0x187));var _0xfde5=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xde5f[_0x3dd15e];return _0x231fd0;};'use strict';var _=require(_0xfde5('0x0'));var db=require('../../mysqldb')['db'];function getInteractionUsers(_0x647c72,_0x43e244){return new Promise(function(_0x29c534,_0x5dafed){try{var _0x489750=[];if(_0x43e244[_0x647c72]){_0x489750=_0x43e244[_0x647c72][_0xfde5('0x1')](function(_0x1f1484){return{'id':_0x1f1484['id'],'fullname':_0x1f1484[_0xfde5('0x2')]};});}return _0x29c534(_0x489750);}catch(_0x435705){_0x5dafed(_0x435705);}});}function getInteractionTags(_0x52564a,_0x136bdd){var _0x5b30f3=[];if(_0x52564a){_0x52564a[_0xfde5('0x3')](',')['forEach'](function(_0x21460d){_0x5b30f3['push'](_0x136bdd[_0x21460d]);});}return _0x5b30f3;}function updateWaitingCounter(_0x3bb8e7){if(_0x3bb8e7[_0xfde5('0x4')]){_0x3bb8e7[_0xfde5('0x4')]-=0x1;}}function updateInteractionCounters(_0x31eeff){_0x31eeff[_0xfde5('0x4')]=0x0;_0x31eeff[_0xfde5('0x5')]=0x0;_0x31eeff[_0xfde5('0x6')]=0x0;_0x31eeff[_0xfde5('0x7')]=0x0;_0x31eeff['abandoned']=0x0;_0x31eeff['total']=0x0;if(_0x31eeff['closed']){if(_0x31eeff[_0xfde5('0x8')]===_0xfde5('0x9')){_0x31eeff['abandoned']+=0x1;updateWaitingCounter(_0x31eeff);}if(_0x31eeff['UserId']){_0x31eeff['answered']+=0x1;if(_0x31eeff[_0xfde5('0x5')]){_0x31eeff[_0xfde5('0x5')]-=0x1;}}else{if(_0x31eeff[_0xfde5('0x8')]){_0x31eeff[_0xfde5('0x7')]+=0x1;updateWaitingCounter(_0x31eeff);}}}else{if(!_0x31eeff[_0xfde5('0xa')]){_0x31eeff[_0xfde5('0x4')]+=0x1;}if(_0x31eeff[_0xfde5('0xa')]){_0x31eeff[_0xfde5('0x5')]+=0x1;updateWaitingCounter(_0x31eeff);}}_0x31eeff[_0xfde5('0xb')]+=0x1;return _0x31eeff;}function getLastMessageText(_0x33664f){return db[_0xfde5('0xc')][_0xfde5('0xd')]({'where':{'$and':[{'MailInteractionId':_0x33664f['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':'out'},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':[_0xfde5('0xe')],'order':_0xfde5('0xf'),'raw':!![]})[_0xfde5('0x10')](function(_0x349071){if(_0x349071&&_0x349071[_0xfde5('0xe')]){_0x33664f[_0xfde5('0x11')]=_['trimStart'](_0x349071[_0xfde5('0xe')]);}return _0x33664f;})['catch'](function(_0x1d663a){throw _0x1d663a;});}exports['updateInteraction']=function(_0x15d3bf,_0x38dffe,_0x250cc3,_0x3f30aa){return new Promise(function(_0x4d7f04,_0x416213){return getInteractionUsers(_0x15d3bf['id'],_0x250cc3)[_0xfde5('0x10')](function(_0x3417f1){_0x15d3bf[_0xfde5('0x12')]=_0x3417f1;return getInteractionTags(_0x15d3bf[_0xfde5('0x13')],_0x38dffe);})[_0xfde5('0x10')](function(_0xcc3e3c){_0x15d3bf[_0xfde5('0x14')]=_0xcc3e3c;delete _0x15d3bf[_0xfde5('0x13')];return updateInteractionCounters(_0x15d3bf);})[_0xfde5('0x10')](function(_0x510ac5){if(_0x3f30aa){return getLastMessageText(_0x510ac5);}else{return _0x510ac5;}})['then'](function(_0x143712){_0x4d7f04(_0x143712);})[_0xfde5('0x15')](function(_0x4e3032){_0x416213(_0x4e3032);});});};