26b4bc9811b10ada73179f7767e37c3e23e47d5d
[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 _0xe485=['waiting','answered','unmanaged','abandoned','total','disposition','talking','UserId','MailMessage','findOne','out','plainBody','then','lastMsgText','trimStart','catch','updateInteraction','Users','TagIds','Tags','lodash','../../mysqldb','fullname','push'];(function(_0x2e911d,_0x58a451){var _0x543227=function(_0x2430ad){while(--_0x2430ad){_0x2e911d['push'](_0x2e911d['shift']());}};_0x543227(++_0x58a451);}(_0xe485,0x104));var _0x5e48=function(_0x1e1de2,_0x20dd89){_0x1e1de2=_0x1e1de2-0x0;var _0x859f40=_0xe485[_0x1e1de2];return _0x859f40;};'use strict';var _=require(_0x5e48('0x0'));var db=require(_0x5e48('0x1'))['db'];function getInteractionUsers(_0x17b5b7,_0x5c87de){return new Promise(function(_0x4be847,_0x57b354){try{var _0x5aea36=[];if(_0x5c87de[_0x17b5b7]){_0x5aea36=_0x5c87de[_0x17b5b7]['map'](function(_0x4e81ee){return{'id':_0x4e81ee['id'],'fullname':_0x4e81ee[_0x5e48('0x2')]};});}return _0x4be847(_0x5aea36);}catch(_0x1c16c9){_0x57b354(_0x1c16c9);}});}function getInteractionTags(_0x192b71,_0x409517){var _0x2a8690=[];if(_0x192b71){_0x192b71['split'](',')['forEach'](function(_0x392e9a){_0x2a8690[_0x5e48('0x3')](_0x409517[_0x392e9a]);});}return _0x2a8690;}function updateWaitingCounter(_0x4bb402){if(_0x4bb402[_0x5e48('0x4')]){_0x4bb402[_0x5e48('0x4')]-=0x1;}}function updateInteractionCounters(_0x3f0754){_0x3f0754['waiting']=0x0;_0x3f0754['talking']=0x0;_0x3f0754[_0x5e48('0x5')]=0x0;_0x3f0754[_0x5e48('0x6')]=0x0;_0x3f0754[_0x5e48('0x7')]=0x0;_0x3f0754[_0x5e48('0x8')]=0x0;if(_0x3f0754['closed']){if(_0x3f0754[_0x5e48('0x9')]==='abandoned'){_0x3f0754[_0x5e48('0x7')]+=0x1;updateWaitingCounter(_0x3f0754);}if(_0x3f0754['UserId']){_0x3f0754[_0x5e48('0x5')]+=0x1;if(_0x3f0754[_0x5e48('0xa')]){_0x3f0754['talking']-=0x1;}}else{if(_0x3f0754[_0x5e48('0x9')]){_0x3f0754[_0x5e48('0x6')]+=0x1;updateWaitingCounter(_0x3f0754);}}}else{if(!_0x3f0754[_0x5e48('0xb')]){_0x3f0754['waiting']+=0x1;}if(_0x3f0754['UserId']){_0x3f0754['talking']+=0x1;updateWaitingCounter(_0x3f0754);}}_0x3f0754[_0x5e48('0x8')]+=0x1;return _0x3f0754;}function getLastMessageText(_0x277559){return db[_0x5e48('0xc')][_0x5e48('0xd')]({'where':{'$and':[{'MailInteractionId':_0x277559['id']},{'$or':[{'direction':'in'},{'$and':[{'direction':_0x5e48('0xe')},{'secret':![]},{'UserId':{'$ne':null}}]}]}]},'attributes':[_0x5e48('0xf')],'order':'-id','raw':!![]})[_0x5e48('0x10')](function(_0xfce5d1){if(_0xfce5d1&&_0xfce5d1[_0x5e48('0xf')]){_0x277559[_0x5e48('0x11')]=_[_0x5e48('0x12')](_0xfce5d1[_0x5e48('0xf')]);}return _0x277559;})[_0x5e48('0x13')](function(_0x482215){throw _0x482215;});}exports[_0x5e48('0x14')]=function(_0x2d9fad,_0x230c65,_0x52bfa5,_0x30a020){return new Promise(function(_0x2ff69a,_0x14150a){return getInteractionUsers(_0x2d9fad['id'],_0x52bfa5)['then'](function(_0x5c188d){_0x2d9fad[_0x5e48('0x15')]=_0x5c188d;return getInteractionTags(_0x2d9fad[_0x5e48('0x16')],_0x230c65);})[_0x5e48('0x10')](function(_0x5c238a){_0x2d9fad[_0x5e48('0x17')]=_0x5c238a;delete _0x2d9fad[_0x5e48('0x16')];return updateInteractionCounters(_0x2d9fad);})[_0x5e48('0x10')](function(_0x58d857){if(_0x30a020){return getLastMessageText(_0x58d857);}else{return _0x58d857;}})[_0x5e48('0x10')](function(_0x4b1e4b){_0x2ff69a(_0x4b1e4b);})['catch'](function(_0xd119c8){_0x14150a(_0xd119c8);});});};