Built motion from commit da617fac.|2.6.6
[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 _0xa0f3=['plainBody','then','lastMsgText','trimStart','catch','Users','TagIds','lodash','../../mysqldb','split','forEach','push','waiting','talking','unmanaged','abandoned','total','closed','disposition','answered','UserId','findOne'];(function(_0x196925,_0xf3f767){var _0x17384e=function(_0x4a145d){while(--_0x4a145d){_0x196925['push'](_0x196925['shift']());}};_0x17384e(++_0xf3f767);}(_0xa0f3,0x151));var _0x3a0f=function(_0x5447fd,_0x52a6b5){_0x5447fd=_0x5447fd-0x0;var _0x15b28a=_0xa0f3[_0x5447fd];return _0x15b28a;};'use strict';var _=require(_0x3a0f('0x0'));var db=require(_0x3a0f('0x1'))['db'];function getInteractionUsers(_0x140f91,_0x4f55d5){return new Promise(function(_0x1d42ff,_0x8b97f6){try{var _0x18426c=[];if(_0x4f55d5[_0x140f91]){_0x18426c=_0x4f55d5[_0x140f91]['map'](function(_0x24b906){return{'id':_0x24b906['id'],'fullname':_0x24b906['fullname']};});}return _0x1d42ff(_0x18426c);}catch(_0x290e57){_0x8b97f6(_0x290e57);}});}function getInteractionTags(_0x2d51ad,_0x428081){var _0x3e985c=[];if(_0x2d51ad){_0x2d51ad[_0x3a0f('0x2')](',')[_0x3a0f('0x3')](function(_0x338379){_0x3e985c[_0x3a0f('0x4')](_0x428081[_0x338379]);});}return _0x3e985c;}function updateWaitingCounter(_0x5dabf5){if(_0x5dabf5[_0x3a0f('0x5')]){_0x5dabf5['waiting']-=0x1;}}function updateInteractionCounters(_0x5115a8){_0x5115a8['waiting']=0x0;_0x5115a8[_0x3a0f('0x6')]=0x0;_0x5115a8['answered']=0x0;_0x5115a8[_0x3a0f('0x7')]=0x0;_0x5115a8[_0x3a0f('0x8')]=0x0;_0x5115a8[_0x3a0f('0x9')]=0x0;if(_0x5115a8[_0x3a0f('0xa')]){if(_0x5115a8[_0x3a0f('0xb')]===_0x3a0f('0x8')){_0x5115a8[_0x3a0f('0x8')]+=0x1;updateWaitingCounter(_0x5115a8);}if(_0x5115a8['UserId']){_0x5115a8[_0x3a0f('0xc')]+=0x1;if(_0x5115a8[_0x3a0f('0x6')]){_0x5115a8[_0x3a0f('0x6')]-=0x1;}}else{if(_0x5115a8[_0x3a0f('0xb')]){_0x5115a8[_0x3a0f('0x7')]+=0x1;updateWaitingCounter(_0x5115a8);}}}else{if(!_0x5115a8['UserId']){_0x5115a8[_0x3a0f('0x5')]+=0x1;}if(_0x5115a8[_0x3a0f('0xd')]){_0x5115a8[_0x3a0f('0x6')]+=0x1;updateWaitingCounter(_0x5115a8);}}_0x5115a8[_0x3a0f('0x9')]+=0x1;return _0x5115a8;}function getLastMessageText(_0x34fdac){return db['MailMessage'][_0x3a0f('0xe')]({'where':{'$and':[{'MailInteractionId':_0x34fdac['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':'out'},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':[_0x3a0f('0xf')],'order':'-id','raw':!![]})[_0x3a0f('0x10')](function(_0x53fde3){if(_0x53fde3&&_0x53fde3[_0x3a0f('0xf')]){_0x34fdac[_0x3a0f('0x11')]=_[_0x3a0f('0x12')](_0x53fde3['plainBody']);}return _0x34fdac;})[_0x3a0f('0x13')](function(_0x25725b){throw _0x25725b;});}exports['updateInteraction']=function(_0x4959ea,_0x1f25c7,_0x46f796,_0x776325){return new Promise(function(_0x370011,_0x494030){return getInteractionUsers(_0x4959ea['id'],_0x46f796)[_0x3a0f('0x10')](function(_0x32dc04){_0x4959ea[_0x3a0f('0x14')]=_0x32dc04;return getInteractionTags(_0x4959ea[_0x3a0f('0x15')],_0x1f25c7);})[_0x3a0f('0x10')](function(_0xaa8f69){_0x4959ea['Tags']=_0xaa8f69;delete _0x4959ea[_0x3a0f('0x15')];return updateInteractionCounters(_0x4959ea);})[_0x3a0f('0x10')](function(_0x187873){if(_0x776325){return getLastMessageText(_0x187873);}else{return _0x187873;}})[_0x3a0f('0x10')](function(_0x614283){_0x370011(_0x614283);})[_0x3a0f('0x13')](function(_0x50a60b){_0x494030(_0x50a60b);});});};