Built motion from commit 639a1a7f.|2.6.19
[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 _0xc0f9=['waiting','talking','answered','unmanaged','abandoned','total','closed','disposition','UserId','findOne','-id','then','plainBody','lastMsgText','catch','TagIds','Tags','lodash','../../mysqldb','fullname','push'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xc0f9,0x176));var _0x9c0f=function(_0x38a7ac,_0x45514b){_0x38a7ac=_0x38a7ac-0x0;var _0x53061a=_0xc0f9[_0x38a7ac];return _0x53061a;};'use strict';var _=require(_0x9c0f('0x0'));var db=require(_0x9c0f('0x1'))['db'];function getInteractionUsers(_0x11f4a4,_0x5f3501){return new Promise(function(_0x383d6f,_0x5562de){try{var _0x26839a=[];if(_0x5f3501[_0x11f4a4]){_0x26839a=_0x5f3501[_0x11f4a4]['map'](function(_0x3e10c9){return{'id':_0x3e10c9['id'],'fullname':_0x3e10c9[_0x9c0f('0x2')]};});}return _0x383d6f(_0x26839a);}catch(_0x14964f){_0x5562de(_0x14964f);}});}function getInteractionTags(_0x8a0167,_0x2a3932){var _0x58e7c7=[];if(_0x8a0167){_0x8a0167['split'](',')['forEach'](function(_0x50cb36){_0x58e7c7[_0x9c0f('0x3')](_0x2a3932[_0x50cb36]);});}return _0x58e7c7;}function updateWaitingCounter(_0x3bb22b){if(_0x3bb22b[_0x9c0f('0x4')]){_0x3bb22b['waiting']-=0x1;}}function updateInteractionCounters(_0x88c383){_0x88c383[_0x9c0f('0x4')]=0x0;_0x88c383[_0x9c0f('0x5')]=0x0;_0x88c383[_0x9c0f('0x6')]=0x0;_0x88c383[_0x9c0f('0x7')]=0x0;_0x88c383[_0x9c0f('0x8')]=0x0;_0x88c383[_0x9c0f('0x9')]=0x0;if(_0x88c383[_0x9c0f('0xa')]){if(_0x88c383[_0x9c0f('0xb')]==='abandoned'){_0x88c383[_0x9c0f('0x8')]+=0x1;updateWaitingCounter(_0x88c383);}if(_0x88c383[_0x9c0f('0xc')]){_0x88c383[_0x9c0f('0x6')]+=0x1;if(_0x88c383[_0x9c0f('0x5')]){_0x88c383[_0x9c0f('0x5')]-=0x1;}}else{if(_0x88c383[_0x9c0f('0xb')]){_0x88c383[_0x9c0f('0x7')]+=0x1;updateWaitingCounter(_0x88c383);}}}else{if(!_0x88c383[_0x9c0f('0xc')]){_0x88c383['waiting']+=0x1;}if(_0x88c383[_0x9c0f('0xc')]){_0x88c383[_0x9c0f('0x5')]+=0x1;updateWaitingCounter(_0x88c383);}}_0x88c383[_0x9c0f('0x9')]+=0x1;return _0x88c383;}function getLastMessageText(_0x38fc5c){return db['MailMessage'][_0x9c0f('0xd')]({'where':{'$and':[{'MailInteractionId':_0x38fc5c['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':'out'},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':['plainBody'],'order':_0x9c0f('0xe'),'raw':!![]})[_0x9c0f('0xf')](function(_0xf51cba){if(_0xf51cba&&_0xf51cba[_0x9c0f('0x10')]){_0x38fc5c[_0x9c0f('0x11')]=_['trimStart'](_0xf51cba[_0x9c0f('0x10')]);}return _0x38fc5c;})[_0x9c0f('0x12')](function(_0x358b19){throw _0x358b19;});}exports['updateInteraction']=function(_0x11df7e,_0x425b78,_0x57f696,_0x458384){return new Promise(function(_0x574f73,_0x16d803){return getInteractionUsers(_0x11df7e['id'],_0x57f696)[_0x9c0f('0xf')](function(_0xa04f2e){_0x11df7e['Users']=_0xa04f2e;return getInteractionTags(_0x11df7e[_0x9c0f('0x13')],_0x425b78);})[_0x9c0f('0xf')](function(_0x21a636){_0x11df7e[_0x9c0f('0x14')]=_0x21a636;delete _0x11df7e[_0x9c0f('0x13')];return updateInteractionCounters(_0x11df7e);})[_0x9c0f('0xf')](function(_0x50cbd3){if(_0x458384){return getLastMessageText(_0x50cbd3);}else{return _0x50cbd3;}})[_0x9c0f('0xf')](function(_0x42d2f4){_0x574f73(_0x42d2f4);})[_0x9c0f('0x12')](function(_0x4ded6e){_0x16d803(_0x4ded6e);});});};