22dcafc3ba3bb84fa2640460d49da34dcea4be55
[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 _0x6fd6=['waiting','talking','unmanaged','total','closed','disposition','UserId','answered','MailMessage','findOne','out','plainBody','-id','lastMsgText','trimStart','catch','updateInteraction','Users','TagIds','Tags','then','lodash','../../mysqldb','map','fullname','split','forEach'];(function(_0x427272,_0x5a5946){var _0x41252f=function(_0x3f0ffa){while(--_0x3f0ffa){_0x427272['push'](_0x427272['shift']());}};_0x41252f(++_0x5a5946);}(_0x6fd6,0x81));var _0x66fd=function(_0x59cca4,_0x25319f){_0x59cca4=_0x59cca4-0x0;var _0x543df0=_0x6fd6[_0x59cca4];return _0x543df0;};'use strict';var _=require(_0x66fd('0x0'));var db=require(_0x66fd('0x1'))['db'];function getInteractionUsers(_0x2636aa,_0x14afd9){return new Promise(function(_0x1023d5,_0x1785e1){try{var _0x1ead8f=[];if(_0x14afd9[_0x2636aa]){_0x1ead8f=_0x14afd9[_0x2636aa][_0x66fd('0x2')](function(_0xdde160){return{'id':_0xdde160['id'],'fullname':_0xdde160[_0x66fd('0x3')]};});}return _0x1023d5(_0x1ead8f);}catch(_0x1801f1){_0x1785e1(_0x1801f1);}});}function getInteractionTags(_0x5172f4,_0x3c8118){var _0x150d26=[];if(_0x5172f4){_0x5172f4[_0x66fd('0x4')](',')[_0x66fd('0x5')](function(_0x42d509){_0x150d26['push'](_0x3c8118[_0x42d509]);});}return _0x150d26;}function updateWaitingCounter(_0x49b0da){if(_0x49b0da[_0x66fd('0x6')]){_0x49b0da[_0x66fd('0x6')]-=0x1;}}function updateInteractionCounters(_0x2fe5a3){_0x2fe5a3[_0x66fd('0x6')]=0x0;_0x2fe5a3[_0x66fd('0x7')]=0x0;_0x2fe5a3['answered']=0x0;_0x2fe5a3[_0x66fd('0x8')]=0x0;_0x2fe5a3['abandoned']=0x0;_0x2fe5a3[_0x66fd('0x9')]=0x0;if(_0x2fe5a3[_0x66fd('0xa')]){if(_0x2fe5a3[_0x66fd('0xb')]==='abandoned'){_0x2fe5a3['abandoned']+=0x1;updateWaitingCounter(_0x2fe5a3);}if(_0x2fe5a3[_0x66fd('0xc')]){_0x2fe5a3[_0x66fd('0xd')]+=0x1;if(_0x2fe5a3[_0x66fd('0x7')]){_0x2fe5a3[_0x66fd('0x7')]-=0x1;}}else{if(_0x2fe5a3['disposition']){_0x2fe5a3[_0x66fd('0x8')]+=0x1;updateWaitingCounter(_0x2fe5a3);}}}else{if(!_0x2fe5a3[_0x66fd('0xc')]){_0x2fe5a3['waiting']+=0x1;}if(_0x2fe5a3[_0x66fd('0xc')]){_0x2fe5a3['talking']+=0x1;updateWaitingCounter(_0x2fe5a3);}}_0x2fe5a3[_0x66fd('0x9')]+=0x1;return _0x2fe5a3;}function getLastMessageText(_0x40756d){return db[_0x66fd('0xe')][_0x66fd('0xf')]({'where':{'$and':[{'MailInteractionId':_0x40756d['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':_0x66fd('0x10')},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':[_0x66fd('0x11')],'order':_0x66fd('0x12'),'raw':!![]})['then'](function(_0x4ad6f7){if(_0x4ad6f7&&_0x4ad6f7['plainBody']){_0x40756d[_0x66fd('0x13')]=_[_0x66fd('0x14')](_0x4ad6f7['plainBody']);}return _0x40756d;})[_0x66fd('0x15')](function(_0x485017){throw _0x485017;});}exports[_0x66fd('0x16')]=function(_0x3eb32e,_0xb23f7a,_0x47be30,_0x52a731){return new Promise(function(_0x22b397,_0x1b4c36){return getInteractionUsers(_0x3eb32e['id'],_0x47be30)['then'](function(_0x8c9b94){_0x3eb32e[_0x66fd('0x17')]=_0x8c9b94;return getInteractionTags(_0x3eb32e[_0x66fd('0x18')],_0xb23f7a);})['then'](function(_0x2e24ee){_0x3eb32e[_0x66fd('0x19')]=_0x2e24ee;delete _0x3eb32e[_0x66fd('0x18')];return updateInteractionCounters(_0x3eb32e);})[_0x66fd('0x1a')](function(_0x24d564){if(_0x52a731){return getLastMessageText(_0x24d564);}else{return _0x24d564;}})[_0x66fd('0x1a')](function(_0x5582ea){_0x22b397(_0x5582ea);})[_0x66fd('0x15')](function(_0x31cd68){_0x1b4c36(_0x31cd68);});});};