Built motion from commit 9d0783e6.|2.6.13
[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 _0xb417=['then','plainBody','lastMsgText','trimStart','catch','Tags','TagIds','../../mysqldb','map','split','forEach','push','waiting','talking','answered','unmanaged','abandoned','total','closed','UserId','disposition','findOne','out','-id'];(function(_0x45fdbf,_0x1ace60){var _0x35cba4=function(_0xcf87a4){while(--_0xcf87a4){_0x45fdbf['push'](_0x45fdbf['shift']());}};_0x35cba4(++_0x1ace60);}(_0xb417,0x1b7));var _0x7b41=function(_0x3dde60,_0x495bb6){_0x3dde60=_0x3dde60-0x0;var _0x24ba57=_0xb417[_0x3dde60];return _0x24ba57;};'use strict';var _=require('lodash');var db=require(_0x7b41('0x0'))['db'];function getInteractionUsers(_0x140fd4,_0x293a70){return new Promise(function(_0x4280cb,_0x5933a6){try{var _0x25581e=[];if(_0x293a70[_0x140fd4]){_0x25581e=_0x293a70[_0x140fd4][_0x7b41('0x1')](function(_0x629425){return{'id':_0x629425['id'],'fullname':_0x629425['fullname']};});}return _0x4280cb(_0x25581e);}catch(_0x15f4e3){_0x5933a6(_0x15f4e3);}});}function getInteractionTags(_0x170a10,_0x3b8513){var _0x4c247b=[];if(_0x170a10){_0x170a10[_0x7b41('0x2')](',')[_0x7b41('0x3')](function(_0x47fdcf){_0x4c247b[_0x7b41('0x4')](_0x3b8513[_0x47fdcf]);});}return _0x4c247b;}function updateWaitingCounter(_0x5a2c1a){if(_0x5a2c1a[_0x7b41('0x5')]){_0x5a2c1a[_0x7b41('0x5')]-=0x1;}}function updateInteractionCounters(_0x164814){_0x164814[_0x7b41('0x5')]=0x0;_0x164814[_0x7b41('0x6')]=0x0;_0x164814[_0x7b41('0x7')]=0x0;_0x164814[_0x7b41('0x8')]=0x0;_0x164814[_0x7b41('0x9')]=0x0;_0x164814[_0x7b41('0xa')]=0x0;if(_0x164814[_0x7b41('0xb')]){if(_0x164814['disposition']===_0x7b41('0x9')){_0x164814[_0x7b41('0x9')]+=0x1;updateWaitingCounter(_0x164814);}if(_0x164814[_0x7b41('0xc')]){_0x164814[_0x7b41('0x7')]+=0x1;if(_0x164814[_0x7b41('0x6')]){_0x164814[_0x7b41('0x6')]-=0x1;}}else{if(_0x164814[_0x7b41('0xd')]){_0x164814[_0x7b41('0x8')]+=0x1;updateWaitingCounter(_0x164814);}}}else{if(!_0x164814['UserId']){_0x164814[_0x7b41('0x5')]+=0x1;}if(_0x164814['UserId']){_0x164814[_0x7b41('0x6')]+=0x1;updateWaitingCounter(_0x164814);}}_0x164814[_0x7b41('0xa')]+=0x1;return _0x164814;}function getLastMessageText(_0x543510){return db['MailMessage'][_0x7b41('0xe')]({'where':{'$and':[{'MailInteractionId':_0x543510['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':_0x7b41('0xf')},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':['plainBody'],'order':_0x7b41('0x10'),'raw':!![]})[_0x7b41('0x11')](function(_0xb3866b){if(_0xb3866b&&_0xb3866b[_0x7b41('0x12')]){_0x543510[_0x7b41('0x13')]=_[_0x7b41('0x14')](_0xb3866b[_0x7b41('0x12')]);}return _0x543510;})[_0x7b41('0x15')](function(_0x5979ac){throw _0x5979ac;});}exports['updateInteraction']=function(_0xf1f7fa,_0x4eddb5,_0x441e31,_0x59366f){return new Promise(function(_0x45991b,_0x4b98ed){return getInteractionUsers(_0xf1f7fa['id'],_0x441e31)[_0x7b41('0x11')](function(_0x511411){_0xf1f7fa['Users']=_0x511411;return getInteractionTags(_0xf1f7fa['TagIds'],_0x4eddb5);})['then'](function(_0xfb04b8){_0xf1f7fa[_0x7b41('0x16')]=_0xfb04b8;delete _0xf1f7fa[_0x7b41('0x17')];return updateInteractionCounters(_0xf1f7fa);})[_0x7b41('0x11')](function(_0x5e0bb3){if(_0x59366f){return getLastMessageText(_0x5e0bb3);}else{return _0x5e0bb3;}})[_0x7b41('0x11')](function(_0x3cd386){_0x45991b(_0x3cd386);})['catch'](function(_0x2845ec){_0x4b98ed(_0x2845ec);});});};