5ca5afef3271a0cec8e5ef1fc59f973f3d907919
[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 _0x5427=['[CM][%s][%s]','exec','data1','xmd-contactid','ListId','data3','$or','map','routeId','destcalleridnum','firstName','phone','mobile','updatedAt','union','resolve','then','isEmpty','length','[CM][%s][%s]\x20Contact\x20found:%s','uniqueid','join','[CM][%s][%s]\x20Contact\x20not\x20found','data4','[CM][%s][%s]\x20Autocreate\x20enabled','isNil','calleridnum','calleridname','<unknown>','Unknown','destcalleridname','createContact','push','[CM][%s][%s]\x20Contact\x20created:%s','[CM][%s][%s]\x20Autocreate\x20disabled','name','format','trigger:%s','action','toLowerCase','merge','bluebird','ioredis','../../../../config/environment','../../../../config/logger','../../rpc/cmContact','redis','defaults','localhost','info'];(function(_0x4a86a0,_0xefcfc2){var _0x500eb0=function(_0x3415db){while(--_0x3415db){_0x4a86a0['push'](_0x4a86a0['shift']());}};_0x500eb0(++_0xefcfc2);}(_0x5427,0xbf));var _0x7542=function(_0x3c30be,_0x3a44df){_0x3c30be=_0x3c30be-0x0;var _0x241da5=_0x5427[_0x3c30be];return _0x241da5;};'use strict';var _=require('lodash');var BPromise=require(_0x7542('0x0'));var Redis=require(_0x7542('0x1'));var util=require('util');var config=require(_0x7542('0x2'));var logger=require(_0x7542('0x3'))('trigger');var cmContactRpc=require(_0x7542('0x4'));config[_0x7542('0x5')]=_[_0x7542('0x6')](config[_0x7542('0x5')],{'host':_0x7542('0x7'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x7542('0x5')]));function emit(_0x52c496,_0x45588e,_0x7f1d24,_0x5785f8,_0x38f9a5){logger[_0x7542('0x8')](_0x7542('0x9'),_0x52c496,_0x45588e['id'],_0x7f1d24,_0x5785f8,JSON['stringify'](_0x38f9a5));io['to'](_0x7f1d24)['emit'](_0x5785f8,_0x38f9a5);}exports[_0x7542('0xa')]=function(_0x31cbfc,_0x3a4573,_0x3fa570,_0x1f60fc){var _0x40d882,_0x38f445;var _0x14f657={};var _0x3dab2a=Number(_0x3fa570[_0x7542('0xb')]);var _0x189eaf=_0x3fa570['data2']?Boolean(Number(_0x3fa570['data2'])):![];var _0x1f3e51;if(_0x1f60fc[_0x7542('0xc')]){_0x14f657['id']=_0x1f60fc[_0x7542('0xc')];}if(!_0x14f657['id']){_0x14f657[_0x7542('0xd')]=_0x3dab2a;_0x1f3e51=_0x3fa570[_0x7542('0xe')]&&_0x3fa570[_0x7542('0xe')]!=='0'?_0x3fa570[_0x7542('0xe')]['split'](','):['phone'];_0x14f657[_0x7542('0xf')]=_[_0x7542('0x10')](_0x1f3e51,function(_0x1dc0e4){var _0x798e20={};_0x798e20[_0x1dc0e4]=_0x1f60fc[_0x7542('0x11')]?_0x1f60fc[_0x7542('0x12')]:_0x1f60fc['calleridnum'];return _0x798e20;});}var _0x52dd78=['id',_0x7542('0x13'),'lastName',_0x7542('0x14'),_0x7542('0x15'),'fax',_0x7542('0xd'),'createdAt','scheduledAt',_0x7542('0x16')];var _0x48f4d9=_[_0x7542('0x17')](_0x52dd78,_0x1f3e51);BPromise[_0x7542('0x18')]()[_0x7542('0x19')](cmContactRpc['getContacts']({'attributes':_0x48f4d9,'where':_0x14f657}))[_0x7542('0x19')](function(_0x5c4400){_0x38f445=_0x5c4400;if(!_[_0x7542('0x1a')](_0x38f445)){if(_0x38f445[_0x7542('0x1b')]===0x1){_0x40d882=_0x38f445[0x0];logger[_0x7542('0x8')](_0x7542('0x1c'),_0x1f60fc[_0x7542('0x1d')],_0x3a4573['id'],_0x40d882['id']);}else{logger[_0x7542('0x8')]('[CM][%s][%s]\x20Multiple\x20contacts\x20found:[%s]',_0x1f60fc[_0x7542('0x1d')],_0x3a4573['id'],_[_0x7542('0x10')](_0x38f445,'id')[_0x7542('0x1e')](','));}return;}else{logger[_0x7542('0x8')](_0x7542('0x1f'),_0x1f60fc[_0x7542('0x1d')],_0x3a4573['id']);var _0x1dcc2e={'ListId':_0x3dab2a};var _0x2958da=_0x3fa570[_0x7542('0x20')]&&_0x3fa570[_0x7542('0x20')]!=='0'?_0x3fa570[_0x7542('0x20')]:_0x7542('0x14');if(_0x189eaf){logger[_0x7542('0x8')](_0x7542('0x21'),_0x1f60fc[_0x7542('0x1d')],_0x3a4573['id']);_0x1dcc2e[_0x2958da]=_[_0x7542('0x22')](_0x1f60fc['routeId'])?_0x1f60fc[_0x7542('0x23')]:_0x1f60fc[_0x7542('0x12')];if(_[_0x7542('0x22')](_0x1f60fc[_0x7542('0x11')])){_0x1dcc2e[_0x7542('0x13')]=_0x1f60fc[_0x7542('0x24')]&&_0x1f60fc[_0x7542('0x24')]!==_0x7542('0x25')?_0x1f60fc['calleridname']:_0x7542('0x26');}else{_0x1dcc2e[_0x7542('0x13')]=_0x1f60fc[_0x7542('0x27')]&&_0x1f60fc[_0x7542('0x27')]!==_0x7542('0x25')?_0x1f60fc[_0x7542('0x27')]:_0x7542('0x26');}return BPromise['resolve']()[_0x7542('0x19')](cmContactRpc[_0x7542('0x28')](_0x1dcc2e))[_0x7542('0x19')](function(_0x277cc0){_0x40d882=_0x277cc0;_0x38f445[_0x7542('0x29')](_0x277cc0);if(_0x40d882){logger[_0x7542('0x8')](_0x7542('0x2a'),_0x1f60fc[_0x7542('0x1d')],_0x3a4573['id'],_0x40d882['id']);}return;});}else{logger[_0x7542('0x8')](_0x7542('0x2b'),_0x1f60fc['uniqueid'],_0x3a4573['id']);_0x1dcc2e[_0x2958da]=_[_0x7542('0x22')](_0x1f60fc[_0x7542('0x11')])?_0x1f60fc['calleridnum']:_0x1f60fc[_0x7542('0x12')];if(_[_0x7542('0x22')](_0x1f60fc[_0x7542('0x11')])){_0x1dcc2e[_0x7542('0x13')]=_0x1f60fc[_0x7542('0x24')]&&_0x1f60fc[_0x7542('0x24')]!==_0x7542('0x25')?_0x1f60fc[_0x7542('0x24')]:'Unknown';}else{_0x1dcc2e[_0x7542('0x13')]=_0x1f60fc[_0x7542('0x27')]&&_0x1f60fc['destcalleridname']!==_0x7542('0x25')?_0x1f60fc['destcalleridname']:_0x7542('0x26');}_0x40d882=_0x1dcc2e;_0x38f445[_0x7542('0x29')](_0x1dcc2e);return;}}})[_0x7542('0x19')](function(){if(_0x31cbfc){emit(_0x1f60fc[_0x7542('0x1d')],_0x3a4573,util['format']('user:%s',_0x31cbfc[_0x7542('0x2c')]),util[_0x7542('0x2d')](_0x7542('0x2e'),_0x3fa570[_0x7542('0x2f')][_0x7542('0x30')]()),{'evt':_[_0x7542('0x31')](_0x1f60fc,{'autoCreate':_0x189eaf}),'contact':_0x40d882===undefined?{}:_0x40d882,'contacts':_0x38f445[_0x7542('0x1b')]===0x0?undefined:_0x38f445});}});};