6577fff9a140b06b44a37e2140b26756ed156391
[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 _0x6ffb=['talking','answered','unmanaged','total','disposition','abandoned','UserId','MailMessage','findOne','out','-id','then','plainBody','updateInteraction','Users','TagIds','Tags','lodash','../../mysqldb','map','fullname','split','forEach','waiting'];(function(_0x5b8b97,_0x6b4dfa){var _0x1fdf84=function(_0x2ced4c){while(--_0x2ced4c){_0x5b8b97['push'](_0x5b8b97['shift']());}};_0x1fdf84(++_0x6b4dfa);}(_0x6ffb,0x1a9));var _0xb6ff=function(_0x48a331,_0x2ee259){_0x48a331=_0x48a331-0x0;var _0x47c60f=_0x6ffb[_0x48a331];return _0x47c60f;};'use strict';var _=require(_0xb6ff('0x0'));var db=require(_0xb6ff('0x1'))['db'];function getInteractionUsers(_0x129d19,_0x97e03){return new Promise(function(_0x543cd6,_0x26e0a2){try{var _0x303f42=[];if(_0x97e03[_0x129d19]){_0x303f42=_0x97e03[_0x129d19][_0xb6ff('0x2')](function(_0x26bdab){return{'id':_0x26bdab['id'],'fullname':_0x26bdab[_0xb6ff('0x3')]};});}return _0x543cd6(_0x303f42);}catch(_0x114888){_0x26e0a2(_0x114888);}});}function getInteractionTags(_0x5c1a4b,_0x532850){var _0x4c29cf=[];if(_0x5c1a4b){_0x5c1a4b[_0xb6ff('0x4')](',')[_0xb6ff('0x5')](function(_0x365917){_0x4c29cf['push'](_0x532850[_0x365917]);});}return _0x4c29cf;}function updateWaitingCounter(_0x240854){if(_0x240854[_0xb6ff('0x6')]){_0x240854[_0xb6ff('0x6')]-=0x1;}}function updateInteractionCounters(_0x22071d){_0x22071d[_0xb6ff('0x6')]=0x0;_0x22071d[_0xb6ff('0x7')]=0x0;_0x22071d[_0xb6ff('0x8')]=0x0;_0x22071d[_0xb6ff('0x9')]=0x0;_0x22071d['abandoned']=0x0;_0x22071d[_0xb6ff('0xa')]=0x0;if(_0x22071d['closed']){if(_0x22071d[_0xb6ff('0xb')]===_0xb6ff('0xc')){_0x22071d[_0xb6ff('0xc')]+=0x1;updateWaitingCounter(_0x22071d);}if(_0x22071d[_0xb6ff('0xd')]){_0x22071d[_0xb6ff('0x8')]+=0x1;if(_0x22071d[_0xb6ff('0x7')]){_0x22071d['talking']-=0x1;}}else{if(_0x22071d[_0xb6ff('0xb')]){_0x22071d['unmanaged']+=0x1;updateWaitingCounter(_0x22071d);}}}else{if(!_0x22071d['UserId']){_0x22071d[_0xb6ff('0x6')]+=0x1;}if(_0x22071d['UserId']){_0x22071d[_0xb6ff('0x7')]+=0x1;updateWaitingCounter(_0x22071d);}}_0x22071d[_0xb6ff('0xa')]+=0x1;return _0x22071d;}function getLastMessageText(_0x2a9af4){return db[_0xb6ff('0xe')][_0xb6ff('0xf')]({'where':{'$and':[{'MailInteractionId':_0x2a9af4['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':_0xb6ff('0x10')},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':['plainBody'],'order':_0xb6ff('0x11'),'raw':!![]})[_0xb6ff('0x12')](function(_0x3c6a79){if(_0x3c6a79&&_0x3c6a79[_0xb6ff('0x13')]){_0x2a9af4['lastMsgText']=_['trimStart'](_0x3c6a79['plainBody']);}return _0x2a9af4;})['catch'](function(_0x2f0459){throw _0x2f0459;});}exports[_0xb6ff('0x14')]=function(_0x28def5,_0xcbd263,_0x2d7e0f,_0x16cee1){return new Promise(function(_0x39296e,_0x137205){return getInteractionUsers(_0x28def5['id'],_0x2d7e0f)['then'](function(_0xbdd788){_0x28def5[_0xb6ff('0x15')]=_0xbdd788;return getInteractionTags(_0x28def5[_0xb6ff('0x16')],_0xcbd263);})['then'](function(_0x43c214){_0x28def5[_0xb6ff('0x17')]=_0x43c214;delete _0x28def5[_0xb6ff('0x16')];return updateInteractionCounters(_0x28def5);})['then'](function(_0xce063a){if(_0x16cee1){return getLastMessageText(_0xce063a);}else{return _0xce063a;}})['then'](function(_0x38f7b1){_0x39296e(_0x38f7b1);})['catch'](function(_0xae2a4c){_0x137205(_0xae2a4c);});});};