Built motion from commit b5996064.|2.6.21
[motion2.git] / server / services / ami / trigger / cmContact / index.js
index c833611..a26c462 100644 (file)
-'use strict';
-
-var _ = require('lodash');
-var BPromise = require('bluebird');
-var Redis = require('ioredis');
-var util = require('util');
-
-var config = require('../../../../config/environment');
-var logger = require('../../../../config/logger')('trigger');
-var cmContactRpc = require('../../rpc/cmContact');
-
-config.redis = _.defaults(config.redis, {
-  host: 'localhost',
-  port: 6379
-});
-
-var io = require('socket.io-emitter')(new Redis(config.redis));
-
-function emit(uniqueid, trigger, room, evt, obj) {
-  logger.info('[CM][%s][%s]', uniqueid, trigger.id, room, evt, JSON.stringify(obj));
-  io.to(room).emit(evt, obj);
-}
-
-exports.exec = function (agent, trigger, action, evt) {
-  var contact, contacts;
-  var options = {};
-  var listId = Number(action.data1);
-  var autoCreate = action.data2 ? Boolean(Number(action.data2)) : false;
-
-  // Checking if the contact's id is defined
-  if (evt['xmd-contactid2'] && !_.isEmpty(evt['xmd-contactid2'])) options.id = evt['xmd-contactid2'];
-
-  if (!options.id) {
-    options.ListId = listId;
-    // Retrieves the fields selected for searching the contact
-    var searchFields = action.data3 && action.data3 !== '0' ? action.data3.split(',') : ['phone'];
-    options.$or = _.map(searchFields, function (field) {
-      return {
-        [field]: evt.routeId ? evt.destcalleridnum : evt.calleridnum
-      };
-    });
-  }
-
-  // Searching the contact
-  BPromise.resolve()
-    .then(
-      cmContactRpc.getContacts({
-        where: options
-      })
-    )
-    .then(function (result) {
-      contacts = result;
-      if (!_.isEmpty(contacts)) {
-        if (contacts.length === 1) {
-          contact = contacts[0];
-          logger.info('[CM][%s][%s] Contact found:%s', evt.uniqueid, trigger.id, contact.id);
-        } else {
-          logger.info('[CM][%s][%s] Multiple contacts found:[%s]', evt.uniqueid, trigger.id, _.map(contacts, 'id').join(','));
-        }
-        return;
-      } else {
-        logger.info('[CM][%s][%s] Contact not found', evt.uniqueid, trigger.id);
-        if (autoCreate) {
-          logger.info('[CM][%s][%s] Autocreate enabled', evt.uniqueid, trigger.id);
-          var creationField = action.data4 && action.data4 !== '0' ? action.data4 : 'phone';
-          var newContact = {
-            ListId: listId,
-            [creationField]: _.isNil(evt.routeId) ? evt.calleridnum : evt.destcalleridnum
-          };
-
-          if (_.isNil(evt.routeId)) {
-            newContact.firstName = evt.calleridname && evt.calleridname !== '<unknown>' ? evt.calleridname : 'Unknown';
-          } else {
-            newContact.firstName = evt.destcalleridname && evt.destcalleridname !== '<unknown>' ? evt.destcalleridname : 'Unknown';
-          }
-
-          return BPromise.resolve()
-            .then(cmContactRpc.createContact(newContact))
-            .then(function (result) {
-              contact = result;
-              if (contact) {
-                logger.info('[CM][%s][%s] Contact created:%s', evt.uniqueid, trigger.id, contact.id);
-              }
-              return;
-            });
-        } else {
-          logger.info('[CM][%s][%s] Autocreate disabled', evt.uniqueid, trigger.id);
-          return;
-        }
-      }
-    })
-    .then(function () {
-      // Emitting the event
-      if (agent) {
-        emit(evt.uniqueid, trigger, util.format('user:%s', agent.name), util.format('trigger:%s', action.action.toLowerCase()), {
-          evt: _.merge(evt, { autoCreate: autoCreate }),
-          contact: contact,
-          contacts: !contact ? contacts : undefined
-        });
-      }
-    });
-};
+// *************************************************************************
+// *                                                                       *
+// * xCALLY Motion -  The Omnichannel Contact Center                       *
+// * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
+// *                                                                       *
+// *************************************************************************
+// *                                                                       *
+// * Email: info@xcally.com                                                *
+// * Website: https://www.xcally.com                                       *
+// *                                                                       *
+// *************************************************************************
+// *                                                                       *
+// * The SOFTWARE PRODUCT is protected by copyright laws and international *
+// * copyright treaties, as well as other intellectual property laws and   *
+// * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
+// *                                                                       *
+// *************************************************************************
+var _0x588e=['getContacts','then','isEmpty','length','info','[CM][%s][%s]\x20Contact\x20found:%s','uniqueid','map','join','[CM][%s][%s]\x20Contact\x20not\x20found','data4','[CM][%s][%s]\x20Autocreate\x20enabled','calleridname','Unknown','destcalleridname','<unknown>','resolve','createContact','[CM][%s][%s]\x20Contact\x20created:%s','push','user:%s','name','trigger:%s','merge','lodash','bluebird','util','../../../../config/logger','trigger','../../rpc/cmContact','redis','defaults','localhost','socket.io-emitter','stringify','emit','exec','data1','data2','isNil','data5','xmd-contactid','ListId','data3','split','phone','$or','routeId','destcalleridnum','calleridnum','firstName','lastName','mobile','fax','createdAt','scheduledAt','updatedAt'];(function(_0x4d613c,_0x55f220){var _0x301fe3=function(_0x5eec0b){while(--_0x5eec0b){_0x4d613c['push'](_0x4d613c['shift']());}};_0x301fe3(++_0x55f220);}(_0x588e,0x135));var _0xe588=function(_0x5f4b06,_0x3e5997){_0x5f4b06=_0x5f4b06-0x0;var _0x55cc05=_0x588e[_0x5f4b06];return _0x55cc05;};'use strict';var _=require(_0xe588('0x0'));var BPromise=require(_0xe588('0x1'));var Redis=require('ioredis');var util=require(_0xe588('0x2'));var config=require('../../../../config/environment');var logger=require(_0xe588('0x3'))(_0xe588('0x4'));var cmContactRpc=require(_0xe588('0x5'));config[_0xe588('0x6')]=_[_0xe588('0x7')](config['redis'],{'host':_0xe588('0x8'),'port':0x18eb});var io=require(_0xe588('0x9'))(new Redis(config[_0xe588('0x6')]));function emit(_0x25a01c,_0xe8afb7,_0x4cbb93,_0xf4c60b,_0xe6c1b0){logger['info']('[CM][%s][%s]',_0x25a01c,_0xe8afb7['id'],_0x4cbb93,_0xf4c60b,JSON[_0xe588('0xa')](_0xe6c1b0));io['to'](_0x4cbb93)[_0xe588('0xb')](_0xf4c60b,_0xe6c1b0);}exports[_0xe588('0xc')]=function(_0x9d070f,_0x14cf8a,_0x263ec8,_0x5c197c){var _0x35015a,_0xba8ad2;var _0x567d4d={};var _0xbf9bfe;var _0x2e0c6d=Number(_0x263ec8[_0xe588('0xd')]);var _0x3caa0d=_0x263ec8[_0xe588('0xe')]?Boolean(Number(_0x263ec8[_0xe588('0xe')])):![];var _0x231c4c;var _0xe6c575=![];if(!_[_0xe588('0xf')](_0x263ec8[_0xe588('0x10')])){if(_0x263ec8['data5']=='1')_0xe6c575=!![];}if(!_0xe6c575){if(_0x5c197c['xmd-contactid']){_0x567d4d['id']=_0x5c197c[_0xe588('0x11')];}}if(!_0x567d4d['id']){_0x567d4d[_0xe588('0x12')]=_0x2e0c6d;_0x231c4c=_0x263ec8[_0xe588('0x13')]&&_0x263ec8[_0xe588('0x13')]!=='0'?_0x263ec8[_0xe588('0x13')][_0xe588('0x14')](','):[_0xe588('0x15')];_0x567d4d[_0xe588('0x16')]=_['map'](_0x231c4c,function(_0x431132){var _0x3d6301={};_0x3d6301[_0x431132]=_0x5c197c[_0xe588('0x17')]?_0x5c197c[_0xe588('0x18')]:_0x5c197c[_0xe588('0x19')];return _0x3d6301;});_0xbf9bfe=_0x5c197c[_0xe588('0x17')]?_0x5c197c[_0xe588('0x18')]:_0x5c197c[_0xe588('0x19')];}var _0x397dff=['id',_0xe588('0x1a'),_0xe588('0x1b'),_0xe588('0x15'),_0xe588('0x1c'),_0xe588('0x1d'),_0xe588('0x12'),_0xe588('0x1e'),_0xe588('0x1f'),_0xe588('0x20')];var _0x94979c=_['union'](_0x397dff,_0x231c4c);BPromise['resolve']()['then'](cmContactRpc[_0xe588('0x21')]({'attributes':_0x94979c,'where':_0x567d4d,'phone':_0xbf9bfe}))[_0xe588('0x22')](function(_0x192946){_0xba8ad2=_0x192946;if(!_[_0xe588('0x23')](_0xba8ad2)){if(_0xba8ad2[_0xe588('0x24')]===0x1){_0x35015a=_0xba8ad2[0x0];logger[_0xe588('0x25')](_0xe588('0x26'),_0x5c197c[_0xe588('0x27')],_0x14cf8a['id'],_0x35015a['id']);}else{logger[_0xe588('0x25')]('[CM][%s][%s]\x20Multiple\x20contacts\x20found:[%s]',_0x5c197c[_0xe588('0x27')],_0x14cf8a['id'],_[_0xe588('0x28')](_0xba8ad2,'id')[_0xe588('0x29')](','));}return;}else{logger[_0xe588('0x25')](_0xe588('0x2a'),_0x5c197c[_0xe588('0x27')],_0x14cf8a['id']);var _0x4a7e6a={'ListId':_0x2e0c6d};var _0x36e6ad=_0x263ec8['data4']&&_0x263ec8[_0xe588('0x2b')]!=='0'?_0x263ec8[_0xe588('0x2b')]:_0xe588('0x15');if(_0x3caa0d){logger[_0xe588('0x25')](_0xe588('0x2c'),_0x5c197c[_0xe588('0x27')],_0x14cf8a['id']);_0x4a7e6a[_0x36e6ad]=_[_0xe588('0xf')](_0x5c197c['routeId'])?_0x5c197c[_0xe588('0x19')]:_0x5c197c['destcalleridnum'];if(_['isNil'](_0x5c197c[_0xe588('0x17')])){_0x4a7e6a[_0xe588('0x1a')]=_0x5c197c[_0xe588('0x2d')]&&_0x5c197c[_0xe588('0x2d')]!=='<unknown>'?_0x5c197c[_0xe588('0x2d')]:_0xe588('0x2e');}else{_0x4a7e6a[_0xe588('0x1a')]=_0x5c197c[_0xe588('0x2f')]&&_0x5c197c['destcalleridname']!==_0xe588('0x30')?_0x5c197c[_0xe588('0x2f')]:_0xe588('0x2e');}return BPromise[_0xe588('0x31')]()[_0xe588('0x22')](cmContactRpc[_0xe588('0x32')](_0x4a7e6a))[_0xe588('0x22')](function(_0x280cb5){_0x35015a=_0x280cb5;_0xba8ad2['push'](_0x280cb5);if(_0x35015a){logger[_0xe588('0x25')](_0xe588('0x33'),_0x5c197c['uniqueid'],_0x14cf8a['id'],_0x35015a['id']);}return;});}else{logger[_0xe588('0x25')]('[CM][%s][%s]\x20Autocreate\x20disabled',_0x5c197c[_0xe588('0x27')],_0x14cf8a['id']);_0x4a7e6a[_0x36e6ad]=_['isNil'](_0x5c197c[_0xe588('0x17')])?_0x5c197c[_0xe588('0x19')]:_0x5c197c[_0xe588('0x18')];if(_[_0xe588('0xf')](_0x5c197c[_0xe588('0x17')])){_0x4a7e6a[_0xe588('0x1a')]=_0x5c197c[_0xe588('0x2d')]&&_0x5c197c[_0xe588('0x2d')]!==_0xe588('0x30')?_0x5c197c[_0xe588('0x2d')]:'Unknown';}else{_0x4a7e6a[_0xe588('0x1a')]=_0x5c197c['destcalleridname']&&_0x5c197c[_0xe588('0x2f')]!=='<unknown>'?_0x5c197c[_0xe588('0x2f')]:'Unknown';}_0x35015a=_0x4a7e6a;_0xba8ad2[_0xe588('0x34')](_0x4a7e6a);return;}}})[_0xe588('0x22')](function(){if(_0x9d070f){emit(_0x5c197c[_0xe588('0x27')],_0x14cf8a,util['format'](_0xe588('0x35'),_0x9d070f[_0xe588('0x36')]),util['format'](_0xe588('0x37'),_0x263ec8['action']['toLowerCase']()),{'evt':_[_0xe588('0x38')](_0x5c197c,{'autoCreate':_0x3caa0d}),'contact':_0x35015a===undefined?{}:_0x35015a,'contacts':_0xba8ad2[_0xe588('0x24')]===0x0?undefined:_0xba8ad2});}});};
\ No newline at end of file