Built motion from commit (unavailable).|2.5.4
[motion2.git] / server / services / ami / trigger / cmContact / index.js
1 'use strict';
2
3 var _ = require('lodash');
4 var BPromise = require('bluebird');
5 var Redis = require('ioredis');
6 var util = require('util');
7
8 var config = require('../../../../config/environment');
9 var logger = require('../../../../config/logger')('trigger');
10 var cmContactRpc = require('../../rpc/cmContact');
11
12 config.redis = _.defaults(config.redis, {
13   host: 'localhost',
14   port: 6379
15 });
16
17 var io = require('socket.io-emitter')(new Redis(config.redis));
18
19 function emit(uniqueid, trigger, room, evt, obj) {
20   logger.info('[CM][%s][%s]', uniqueid, trigger.id, room, evt, JSON.stringify(obj));
21   io.to(room).emit(evt, obj);
22 }
23
24 exports.exec = function (agent, trigger, action, evt) {
25   var contact, contacts;
26   var options = {};
27   var listId = Number(action.data1);
28   var autoCreate = action.data2 ? Boolean(Number(action.data2)) : false;
29
30   // Checking if the contact's id is defined
31   if (evt['xmd-contactid2'] && !_.isEmpty(evt['xmd-contactid2'])) options.id = evt['xmd-contactid2'];
32
33   if (!options.id) {
34     options.ListId = listId;
35     // Retrieves the fields selected for searching the contact
36     var searchFields = action.data3 && action.data3 !== '0' ? action.data3.split(',') : ['phone'];
37     options.$or = _.map(searchFields, function (field) {
38       return {
39         [field]: evt.routeId ? evt.destcalleridnum : evt.calleridnum
40       };
41     });
42   }
43
44   // Searching the contact
45   BPromise.resolve()
46     .then(
47       cmContactRpc.getContacts({
48         where: options
49       })
50     )
51     .then(function (result) {
52       contacts = result;
53       if (!_.isEmpty(contacts)) {
54         if (contacts.length === 1) {
55           contact = contacts[0];
56           logger.info('[CM][%s][%s] Contact found:%s', evt.uniqueid, trigger.id, contact.id);
57         } else {
58           logger.info('[CM][%s][%s] Multiple contacts found:[%s]', evt.uniqueid, trigger.id, _.map(contacts, 'id').join(','));
59         }
60         return;
61       } else {
62         logger.info('[CM][%s][%s] Contact not found', evt.uniqueid, trigger.id);
63         if (autoCreate) {
64           logger.info('[CM][%s][%s] Autocreate enabled', evt.uniqueid, trigger.id);
65           var creationField = action.data4 && action.data4 !== '0' ? action.data4 : 'phone';
66           var newContact = {
67             ListId: listId,
68             [creationField]: _.isNil(evt.routeId) ? evt.calleridnum : evt.destcalleridnum
69           };
70
71           if (_.isNil(evt.routeId)) {
72             newContact.firstName = evt.calleridname && evt.calleridname !== '<unknown>' ? evt.calleridname : 'Unknown';
73           } else {
74             newContact.firstName = evt.destcalleridname && evt.destcalleridname !== '<unknown>' ? evt.destcalleridname : 'Unknown';
75           }
76
77           return BPromise.resolve()
78             .then(cmContactRpc.createContact(newContact))
79             .then(function (result) {
80               contact = result;
81               if (contact) {
82                 logger.info('[CM][%s][%s] Contact created:%s', evt.uniqueid, trigger.id, contact.id);
83               }
84               return;
85             });
86         } else {
87           logger.info('[CM][%s][%s] Autocreate disabled', evt.uniqueid, trigger.id);
88           return;
89         }
90       }
91     })
92     .then(function () {
93       // Emitting the event
94       if (agent) {
95         emit(evt.uniqueid, trigger, util.format('user:%s', agent.name), util.format('trigger:%s', action.action.toLowerCase()), {
96           evt: _.merge(evt, { autoCreate: autoCreate }),
97           contact: contact,
98           contacts: !contact ? contacts : undefined
99         });
100       }
101     });
102 };