8e1f3b1fc06082d70a599dac43cc8fa416de20db
[motion2.git] / server / services / ami / trigger / cmContact / index.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 _0xb2ef=['isEmpty','length','[CM][%s][%s]\x20Contact\x20found:%s','uniqueid','join','[CM][%s][%s]\x20Contact\x20not\x20found','data4','isNil','calleridnum','calleridname','<unknown>','destcalleridname','Unknown','[CM][%s][%s]\x20Contact\x20created:%s','[CM][%s][%s]\x20Autocreate\x20disabled','format','trigger:%s','toLowerCase','merge','lodash','bluebird','util','../../../../config/environment','../../../../config/logger','trigger','../../rpc/cmContact','redis','socket.io-emitter','info','[CM][%s][%s]','stringify','emit','exec','data2','data5','xmd-contactid','ListId','data3','split','phone','$or','map','routeId','destcalleridnum','firstName','mobile','createdAt','scheduledAt','updatedAt','resolve','then','getContacts'];(function(_0x10d0f0,_0x1eba3d){var _0x93e308=function(_0x388548){while(--_0x388548){_0x10d0f0['push'](_0x10d0f0['shift']());}};_0x93e308(++_0x1eba3d);}(_0xb2ef,0x7b));var _0xfb2e=function(_0x1f0fad,_0xc9a4a0){_0x1f0fad=_0x1f0fad-0x0;var _0x47bb80=_0xb2ef[_0x1f0fad];return _0x47bb80;};'use strict';var _=require(_0xfb2e('0x0'));var BPromise=require(_0xfb2e('0x1'));var Redis=require('ioredis');var util=require(_0xfb2e('0x2'));var config=require(_0xfb2e('0x3'));var logger=require(_0xfb2e('0x4'))(_0xfb2e('0x5'));var cmContactRpc=require(_0xfb2e('0x6'));config[_0xfb2e('0x7')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0xfb2e('0x8'))(new Redis(config[_0xfb2e('0x7')]));function emit(_0x478335,_0x4ba089,_0x52b158,_0x8ad8ef,_0x26ffb9){logger[_0xfb2e('0x9')](_0xfb2e('0xa'),_0x478335,_0x4ba089['id'],_0x52b158,_0x8ad8ef,JSON[_0xfb2e('0xb')](_0x26ffb9));io['to'](_0x52b158)[_0xfb2e('0xc')](_0x8ad8ef,_0x26ffb9);}exports[_0xfb2e('0xd')]=function(_0x2ac6d4,_0x4eb5f9,_0x1cb099,_0x190ab7){var _0x245467,_0x3e4e2e;var _0x1552a5={};var _0x421aa2;var _0x239d12=Number(_0x1cb099['data1']);var _0x2f6901=_0x1cb099[_0xfb2e('0xe')]?Boolean(Number(_0x1cb099['data2'])):![];var _0x2100f8;var _0x5adee2=![];if(!_['isNil'](_0x1cb099[_0xfb2e('0xf')])){if(_0x1cb099['data5']=='1')_0x5adee2=!![];}if(!_0x5adee2){if(_0x190ab7['xmd-contactid']){_0x1552a5['id']=_0x190ab7[_0xfb2e('0x10')];}}if(!_0x1552a5['id']){_0x1552a5[_0xfb2e('0x11')]=_0x239d12;_0x2100f8=_0x1cb099['data3']&&_0x1cb099[_0xfb2e('0x12')]!=='0'?_0x1cb099[_0xfb2e('0x12')][_0xfb2e('0x13')](','):[_0xfb2e('0x14')];_0x1552a5[_0xfb2e('0x15')]=_[_0xfb2e('0x16')](_0x2100f8,function(_0x263614){var _0x41fa06={};_0x41fa06[_0x263614]=_0x190ab7['routeId']?_0x190ab7['destcalleridnum']:_0x190ab7['calleridnum'];return _0x41fa06;});_0x421aa2=_0x190ab7[_0xfb2e('0x17')]?_0x190ab7[_0xfb2e('0x18')]:_0x190ab7['calleridnum'];}var _0x20764c=['id',_0xfb2e('0x19'),'lastName',_0xfb2e('0x14'),_0xfb2e('0x1a'),'fax','ListId',_0xfb2e('0x1b'),_0xfb2e('0x1c'),_0xfb2e('0x1d')];var _0x11a232=_['union'](_0x20764c,_0x2100f8);BPromise[_0xfb2e('0x1e')]()[_0xfb2e('0x1f')](cmContactRpc[_0xfb2e('0x20')]({'attributes':_0x11a232,'where':_0x1552a5,'phone':_0x421aa2}))[_0xfb2e('0x1f')](function(_0x4de9e1){_0x3e4e2e=_0x4de9e1;if(!_[_0xfb2e('0x21')](_0x3e4e2e)){if(_0x3e4e2e[_0xfb2e('0x22')]===0x1){_0x245467=_0x3e4e2e[0x0];logger[_0xfb2e('0x9')](_0xfb2e('0x23'),_0x190ab7['uniqueid'],_0x4eb5f9['id'],_0x245467['id']);}else{logger['info']('[CM][%s][%s]\x20Multiple\x20contacts\x20found:[%s]',_0x190ab7[_0xfb2e('0x24')],_0x4eb5f9['id'],_[_0xfb2e('0x16')](_0x3e4e2e,'id')[_0xfb2e('0x25')](','));}return;}else{logger[_0xfb2e('0x9')](_0xfb2e('0x26'),_0x190ab7[_0xfb2e('0x24')],_0x4eb5f9['id']);var _0x214234={'ListId':_0x239d12};var _0x3a0987=_0x1cb099[_0xfb2e('0x27')]&&_0x1cb099['data4']!=='0'?_0x1cb099[_0xfb2e('0x27')]:_0xfb2e('0x14');if(_0x2f6901){logger['info']('[CM][%s][%s]\x20Autocreate\x20enabled',_0x190ab7['uniqueid'],_0x4eb5f9['id']);_0x214234[_0x3a0987]=_[_0xfb2e('0x28')](_0x190ab7['routeId'])?_0x190ab7[_0xfb2e('0x29')]:_0x190ab7[_0xfb2e('0x18')];if(_[_0xfb2e('0x28')](_0x190ab7[_0xfb2e('0x17')])){_0x214234[_0xfb2e('0x19')]=_0x190ab7[_0xfb2e('0x2a')]&&_0x190ab7['calleridname']!==_0xfb2e('0x2b')?_0x190ab7[_0xfb2e('0x2a')]:'Unknown';}else{_0x214234[_0xfb2e('0x19')]=_0x190ab7[_0xfb2e('0x2c')]&&_0x190ab7['destcalleridname']!==_0xfb2e('0x2b')?_0x190ab7[_0xfb2e('0x2c')]:_0xfb2e('0x2d');}return BPromise['resolve']()[_0xfb2e('0x1f')](cmContactRpc['createContact'](_0x214234))['then'](function(_0x196b4b){_0x245467=_0x196b4b;_0x3e4e2e['push'](_0x196b4b);if(_0x245467){logger[_0xfb2e('0x9')](_0xfb2e('0x2e'),_0x190ab7[_0xfb2e('0x24')],_0x4eb5f9['id'],_0x245467['id']);}return;});}else{logger['info'](_0xfb2e('0x2f'),_0x190ab7[_0xfb2e('0x24')],_0x4eb5f9['id']);_0x214234[_0x3a0987]=_[_0xfb2e('0x28')](_0x190ab7[_0xfb2e('0x17')])?_0x190ab7[_0xfb2e('0x29')]:_0x190ab7[_0xfb2e('0x18')];if(_[_0xfb2e('0x28')](_0x190ab7[_0xfb2e('0x17')])){_0x214234['firstName']=_0x190ab7[_0xfb2e('0x2a')]&&_0x190ab7['calleridname']!==_0xfb2e('0x2b')?_0x190ab7['calleridname']:_0xfb2e('0x2d');}else{_0x214234[_0xfb2e('0x19')]=_0x190ab7[_0xfb2e('0x2c')]&&_0x190ab7[_0xfb2e('0x2c')]!==_0xfb2e('0x2b')?_0x190ab7[_0xfb2e('0x2c')]:_0xfb2e('0x2d');}_0x245467=_0x214234;_0x3e4e2e['push'](_0x214234);return;}}})['then'](function(){if(_0x2ac6d4){emit(_0x190ab7['uniqueid'],_0x4eb5f9,util[_0xfb2e('0x30')]('user:%s',_0x2ac6d4['name']),util[_0xfb2e('0x30')](_0xfb2e('0x31'),_0x1cb099['action'][_0xfb2e('0x32')]()),{'evt':_[_0xfb2e('0x33')](_0x190ab7,{'autoCreate':_0x2f6901}),'contact':_0x245467===undefined?{}:_0x245467,'contacts':_0x3e4e2e['length']===0x0?undefined:_0x3e4e2e});}});};