84ee16a863b8c2dfdf6350718602aedf8d439d75
[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 _0x3944=['$or','map','routeId','destcalleridnum','calleridnum','firstName','mobile','fax','createdAt','scheduledAt','union','resolve','then','getContacts','isEmpty','length','[CM][%s][%s]\x20Contact\x20found:%s','uniqueid','[CM][%s][%s]\x20Multiple\x20contacts\x20found:[%s]','join','[CM][%s][%s]\x20Contact\x20not\x20found','data4','[CM][%s][%s]\x20Autocreate\x20enabled','calleridname','<unknown>','destcalleridname','Unknown','createContact','push','[CM][%s][%s]\x20Contact\x20created:%s','[CM][%s][%s]\x20Autocreate\x20disabled','format','user:%s','trigger:%s','action','toLowerCase','merge','util','../../../../config/environment','../../../../config/logger','trigger','../../rpc/cmContact','socket.io-emitter','redis','info','[CM][%s][%s]','stringify','emit','exec','data1','data2','isNil','xmd-contactid','ListId','data3','split','phone'];(function(_0x1b6186,_0x1ec7f2){var _0x186121=function(_0x1748a8){while(--_0x1748a8){_0x1b6186['push'](_0x1b6186['shift']());}};_0x186121(++_0x1ec7f2);}(_0x3944,0x17b));var _0x4394=function(_0x462cc6,_0x1b2f28){_0x462cc6=_0x462cc6-0x0;var _0x2409d6=_0x3944[_0x462cc6];return _0x2409d6;};'use strict';var _=require('lodash');var BPromise=require('bluebird');var Redis=require('ioredis');var util=require(_0x4394('0x0'));var config=require(_0x4394('0x1'));var logger=require(_0x4394('0x2'))(_0x4394('0x3'));var cmContactRpc=require(_0x4394('0x4'));config['redis']=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0x4394('0x5'))(new Redis(config[_0x4394('0x6')]));function emit(_0x1d5bdc,_0x2b01ba,_0x56d32f,_0x5b0ed0,_0x1335fc){logger[_0x4394('0x7')](_0x4394('0x8'),_0x1d5bdc,_0x2b01ba['id'],_0x56d32f,_0x5b0ed0,JSON[_0x4394('0x9')](_0x1335fc));io['to'](_0x56d32f)[_0x4394('0xa')](_0x5b0ed0,_0x1335fc);}exports[_0x4394('0xb')]=function(_0x3847c0,_0x315865,_0x17d8a4,_0x3c0f1c){var _0x1a867f,_0x282bbc;var _0x56771a={};var _0x160e55;var _0x379879=Number(_0x17d8a4[_0x4394('0xc')]);var _0xd8e33e=_0x17d8a4[_0x4394('0xd')]?Boolean(Number(_0x17d8a4[_0x4394('0xd')])):![];var _0x4c86c6;var _0x310052=![];if(!_[_0x4394('0xe')](_0x17d8a4['data5'])){if(_0x17d8a4['data5']=='1')_0x310052=!![];}if(!_0x310052){if(_0x3c0f1c[_0x4394('0xf')]){_0x56771a['id']=_0x3c0f1c['xmd-contactid'];}}if(!_0x56771a['id']){_0x56771a[_0x4394('0x10')]=_0x379879;_0x4c86c6=_0x17d8a4[_0x4394('0x11')]&&_0x17d8a4[_0x4394('0x11')]!=='0'?_0x17d8a4[_0x4394('0x11')][_0x4394('0x12')](','):[_0x4394('0x13')];_0x56771a[_0x4394('0x14')]=_[_0x4394('0x15')](_0x4c86c6,function(_0x129042){var _0x5e760a={};_0x5e760a[_0x129042]=_0x3c0f1c[_0x4394('0x16')]?_0x3c0f1c[_0x4394('0x17')]:_0x3c0f1c['calleridnum'];return _0x5e760a;});_0x160e55=_0x3c0f1c['routeId']?_0x3c0f1c[_0x4394('0x17')]:_0x3c0f1c[_0x4394('0x18')];}var _0x30a949=['id',_0x4394('0x19'),'lastName',_0x4394('0x13'),_0x4394('0x1a'),_0x4394('0x1b'),_0x4394('0x10'),_0x4394('0x1c'),_0x4394('0x1d'),'updatedAt'];var _0x2d76af=_[_0x4394('0x1e')](_0x30a949,_0x4c86c6);BPromise[_0x4394('0x1f')]()[_0x4394('0x20')](cmContactRpc[_0x4394('0x21')]({'attributes':_0x2d76af,'where':_0x56771a,'phone':_0x160e55}))['then'](function(_0x12332c){_0x282bbc=_0x12332c;if(!_[_0x4394('0x22')](_0x282bbc)){if(_0x282bbc[_0x4394('0x23')]===0x1){_0x1a867f=_0x282bbc[0x0];logger['info'](_0x4394('0x24'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id'],_0x1a867f['id']);}else{logger[_0x4394('0x7')](_0x4394('0x26'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id'],_[_0x4394('0x15')](_0x282bbc,'id')[_0x4394('0x27')](','));}return;}else{logger[_0x4394('0x7')](_0x4394('0x28'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id']);var _0x399fdd={'ListId':_0x379879};var _0x8e1fb9=_0x17d8a4[_0x4394('0x29')]&&_0x17d8a4[_0x4394('0x29')]!=='0'?_0x17d8a4[_0x4394('0x29')]:_0x4394('0x13');if(_0xd8e33e){logger['info'](_0x4394('0x2a'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id']);_0x399fdd[_0x8e1fb9]=_['isNil'](_0x3c0f1c[_0x4394('0x16')])?_0x3c0f1c[_0x4394('0x18')]:_0x3c0f1c['destcalleridnum'];if(_[_0x4394('0xe')](_0x3c0f1c[_0x4394('0x16')])){_0x399fdd['firstName']=_0x3c0f1c[_0x4394('0x2b')]&&_0x3c0f1c['calleridname']!==_0x4394('0x2c')?_0x3c0f1c[_0x4394('0x2b')]:'Unknown';}else{_0x399fdd[_0x4394('0x19')]=_0x3c0f1c['destcalleridname']&&_0x3c0f1c[_0x4394('0x2d')]!==_0x4394('0x2c')?_0x3c0f1c[_0x4394('0x2d')]:_0x4394('0x2e');}return BPromise[_0x4394('0x1f')]()['then'](cmContactRpc[_0x4394('0x2f')](_0x399fdd))['then'](function(_0x2a414d){_0x1a867f=_0x2a414d;_0x282bbc[_0x4394('0x30')](_0x2a414d);if(_0x1a867f){logger[_0x4394('0x7')](_0x4394('0x31'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id'],_0x1a867f['id']);}return;});}else{logger[_0x4394('0x7')](_0x4394('0x32'),_0x3c0f1c[_0x4394('0x25')],_0x315865['id']);_0x399fdd[_0x8e1fb9]=_[_0x4394('0xe')](_0x3c0f1c[_0x4394('0x16')])?_0x3c0f1c['calleridnum']:_0x3c0f1c['destcalleridnum'];if(_['isNil'](_0x3c0f1c[_0x4394('0x16')])){_0x399fdd[_0x4394('0x19')]=_0x3c0f1c[_0x4394('0x2b')]&&_0x3c0f1c[_0x4394('0x2b')]!==_0x4394('0x2c')?_0x3c0f1c['calleridname']:_0x4394('0x2e');}else{_0x399fdd[_0x4394('0x19')]=_0x3c0f1c['destcalleridname']&&_0x3c0f1c['destcalleridname']!==_0x4394('0x2c')?_0x3c0f1c[_0x4394('0x2d')]:_0x4394('0x2e');}_0x1a867f=_0x399fdd;_0x282bbc[_0x4394('0x30')](_0x399fdd);return;}}})['then'](function(){if(_0x3847c0){emit(_0x3c0f1c[_0x4394('0x25')],_0x315865,util[_0x4394('0x33')](_0x4394('0x34'),_0x3847c0['name']),util[_0x4394('0x33')](_0x4394('0x35'),_0x17d8a4[_0x4394('0x36')][_0x4394('0x37')]()),{'evt':_[_0x4394('0x38')](_0x3c0f1c,{'autoCreate':_0xd8e33e}),'contact':_0x1a867f===undefined?{}:_0x1a867f,'contacts':_0x282bbc[_0x4394('0x23')]===0x0?undefined:_0x282bbc});}});};