Built motion from commit (unavailable).|2.5.4
[motion2.git] / server / services / ami / trigger / cmContact / index.js
index 33a87e8..c833611 100644 (file)
-// *************************************************************************
-// *                                                                       *
-// * 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 _0xf4af=['emit','exec','autoCreate','data2','data1','calleridnum','routeId','firstName','calleridname','<unknown>','uniqueid','destcalleridname','destcalleridnum','phone','resolve','then','showContact','omit','xmd-contactid2','format','name','trigger:%s','action','toLowerCase','[%s][%s]\x20Contact\x20not\x20found','createContact','[%s][%s]\x20Contact\x20created:%s','merge','error','[%s][%s]\x20Error\x20creating\x20contact','[%s][%s]\x20Autocreate\x20disabled','user:%s','util','lodash','ioredis','../../../../config/environment','trigger','../../rpc/cmContact','redis','defaults','localhost','socket.io-emitter','info','[%s][%s]','stringify'];(function(_0x17a22a,_0x4d1ba7){var _0x1c1735=function(_0x1fac7d){while(--_0x1fac7d){_0x17a22a['push'](_0x17a22a['shift']());}};_0x1c1735(++_0x4d1ba7);}(_0xf4af,0x7a));var _0xff4a=function(_0xddb860,_0x344918){_0xddb860=_0xddb860-0x0;var _0x255df9=_0xf4af[_0xddb860];return _0x255df9;};'use strict';var BPromise=require('bluebird');var util=require(_0xff4a('0x0'));var _=require(_0xff4a('0x1'));var Redis=require(_0xff4a('0x2'));var config=require(_0xff4a('0x3'));var logger=require('../../../../config/logger')(_0xff4a('0x4'));var cmContactRpc=require(_0xff4a('0x5'));config[_0xff4a('0x6')]=_[_0xff4a('0x7')](config['redis'],{'host':_0xff4a('0x8'),'port':0x18eb});var io=require(_0xff4a('0x9'))(new Redis(config[_0xff4a('0x6')]));function emit(_0x559e5f,_0x4c796d,_0x1812af,_0x34f9d3){logger[_0xff4a('0xa')](_0xff4a('0xb'),_0x559e5f,'',_0x4c796d,_0x1812af,JSON[_0xff4a('0xc')](_0x34f9d3));io['to'](_0x4c796d)[_0xff4a('0xd')](_0x1812af,_0x34f9d3);}exports[_0xff4a('0xe')]=function(_0x1c36db,_0x1907cf,_0x5c6c83){_0x5c6c83[_0xff4a('0xf')]=0x0;if(_0x1907cf[_0xff4a('0x10')]&&parseInt(_0x1907cf[_0xff4a('0x10')],0xa)){var _0x1861b7={'ListId':parseInt(_0x1907cf[_0xff4a('0x11')],0xa),'phone':_0x5c6c83[_0xff4a('0x12')]};if(_['isNil'](_0x5c6c83[_0xff4a('0x13')])){_0x1861b7[_0xff4a('0x14')]=_0x5c6c83['calleridname']&&_0x5c6c83[_0xff4a('0x15')]!==_0xff4a('0x16')?_0x5c6c83[_0xff4a('0x15')]:_0x5c6c83[_0xff4a('0x12')]||_0x5c6c83[_0xff4a('0x17')];}else{_0x1861b7[_0xff4a('0x14')]=_0x5c6c83[_0xff4a('0x18')]&&_0x5c6c83['destcalleridname']!==_0xff4a('0x16')?_0x5c6c83['destcalleridname']:_0x5c6c83[_0xff4a('0x19')]||_0x5c6c83[_0xff4a('0x17')];_0x1861b7[_0xff4a('0x1a')]=_0x5c6c83[_0xff4a('0x18')]&&_0x5c6c83[_0xff4a('0x18')]!==_0xff4a('0x16')?_0x5c6c83[_0xff4a('0x18')]:_0x5c6c83[_0xff4a('0x19')]||_0x5c6c83[_0xff4a('0x17')];}BPromise[_0xff4a('0x1b')]()[_0xff4a('0x1c')](cmContactRpc[_0xff4a('0x1d')]({'where':_[_0xff4a('0x1e')](_0x1861b7,[_0xff4a('0x14')])}))[_0xff4a('0x1c')](function(_0xf3b485){if(_0xf3b485){logger[_0xff4a('0xa')]('[%s][%s]\x20Contact\x20found:%s',_0x5c6c83[_0xff4a('0x17')],'',_0xf3b485['id']);if(_0x1c36db){_0x5c6c83[_0xff4a('0x1f')]=_0xf3b485['id'];emit(_0x5c6c83['uniqueid'],util[_0xff4a('0x20')]('user:%s',_0x1c36db[_0xff4a('0x21')]),util['format'](_0xff4a('0x22'),_0x1907cf[_0xff4a('0x23')][_0xff4a('0x24')]()),_['merge'](_0x5c6c83,{'ListId':parseInt(_0x1907cf[_0xff4a('0x11')],0xa)}));}return;}else{logger[_0xff4a('0xa')](_0xff4a('0x25'),_0x5c6c83[_0xff4a('0x17')],'');return BPromise[_0xff4a('0x1b')]()[_0xff4a('0x1c')](cmContactRpc[_0xff4a('0x26')](_0x1861b7));}})[_0xff4a('0x1c')](function(_0x1544c2){if(_0x1544c2){logger[_0xff4a('0xa')](_0xff4a('0x27'),_0x5c6c83[_0xff4a('0x17')],'',_0x1544c2['id']);if(_0x1c36db){_0x5c6c83[_0xff4a('0x1f')]=_0x1544c2['id'];emit(_0x5c6c83[_0xff4a('0x17')],util[_0xff4a('0x20')]('user:%s',_0x1c36db['name']),util['format']('trigger:%s',_0x1907cf[_0xff4a('0x23')]['toLowerCase']()),_[_0xff4a('0x28')](_0x5c6c83,{'ListId':parseInt(_0x1907cf[_0xff4a('0x11')],0xa)}));}}})['catch'](function(_0x4889b6){logger[_0xff4a('0x29')](_0xff4a('0x2a'),_0x5c6c83[_0xff4a('0x17')],'',JSON['stringify'](_0x4889b6));});}else{if(_0x1c36db){logger['info'](_0xff4a('0x2b'),_0x5c6c83[_0xff4a('0x17')],'');emit(_0x5c6c83[_0xff4a('0x17')],util[_0xff4a('0x20')](_0xff4a('0x2c'),_0x1c36db['name']),util[_0xff4a('0x20')]('trigger:%s',_0x1907cf[_0xff4a('0x23')][_0xff4a('0x24')]()),_[_0xff4a('0x28')]({'ListId':parseInt(_0x1907cf[_0xff4a('0x11')],0xa)},_0x5c6c83));}}};
\ No newline at end of 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
+        });
+      }
+    });
+};