3 var _ = require('lodash');
4 var Contact = require('../../models').Contact;
5 var ContactPhone = require('../../models').ContactPhone;
6 var ContactEmail = require('../../models').ContactEmail;
7 var CustomField = require('../../models').CustomField;
8 var ReportCall = require('../../models').ReportCall;
9 var ReportIntegration = require('../../models').ReportIntegration;
10 var Service = require('../../models').Service;
11 var ReportCallHistory = require('../../models').history.ReportCallHistory;
12 var ReportMailSession = require('../../models').ReportMailSession;
13 var ReportMailSessionHistory = require('../../models').history.ReportMailSessionHistory;
14 var Tag = require('../../models').Tag;
15 var stream = require('stream');
16 var sequelize = require('../../models').sequelize;
17 var util = require('util');
19 // Get list of contacts
20 exports.index = function(req, res) {
22 var attributes = ['name', 'Service.name', 'description'];
23 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
24 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
29 offset: page * per_page,
35 _.forIn(req.query, function(value, key) {
41 if (value === 'service') {
42 _.find(query.include, {
44 }).order = util.format('name %s', req.query.sort_order || 'ASC');
46 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
53 attributes.forEach(function(attribute) {
56 $like: '%' + value + '%'
59 query.where.$or.push(tmp);
63 _.find(query.include, {
67 $like: '%' + value + '%'
75 query.where[key].$like = '%' + value + '%';
82 .findAndCountAll(query)
83 .then(function(result) {
85 var total_pages = Math.ceil(result.count / per_page);
86 var next_page = total_pages > (query.offset + 1) ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page + 1) : null;
87 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
89 res.status(200).send({
93 previous_page: previous_page,
94 total_pages: total_pages
98 .catch(function(err) {
99 res.status(500).send({
100 error: 'Something blew up!'
105 // Get a single managed_contact
106 exports.show = function(req, res) {
108 .findById(req.params.id, {
125 model: ReportCallHistory,
126 as: 'HistoryInbounds',
131 model: ReportCallHistory,
132 as: 'HistoryOutbounds',
141 model: ReportMailSession,
142 as: 'InboundMessages',
147 model: ReportMailSession,
148 as: 'OutboundMessages',
153 model: ReportMailSessionHistory,
154 as: 'HistoryInboundMessages',
159 model: ReportMailSessionHistory,
160 as: 'HistoryOutboundMessages',
172 .then(function(managed_contact) {
173 if (!managed_contact) {
174 return res.sendStatus(404);
176 return res.status(200).send(managed_contact);
178 .catch(function(err) {
180 return handleError(res, err);
184 exports.getEventContacts = function(req, res) {
186 .findById(req.params.id)
187 .then(function(event) {
188 var modelInclude = {};
189 switch (event.eventChannel) {
195 phone: event.calleridnum
210 return Contact.findAll({
211 include: [modelInclude, {
214 id: _.pluck(_.pluck(req.user.Services, 'dataValues'), 'id')
219 .then(function(contacts) {
220 return res.status(200).send(contacts);
222 .catch(function(err) {
224 return handleError(res, err);
229 // validate contact uniqueness
230 exports.contactValidation = function(req, res) {
234 '$Phones.phone$': req.body.phones,
235 '$Emails.email$': req.body.emails
242 .then(function(contacts) {
243 console.log(contacts);
244 return res.status(200).send(contacts);
246 .catch(function(err) {
248 return handleError(res, err);
252 // Creates a new managed_contact in the DB.
253 exports.create = function(req, res, next) {
256 newCustomFields = {};
258 .then(function(tags) {
260 var tagList = _.pluck(_.pluck(tags, 'dataValues'), 'name');
262 var contactTags = req.body.tags.split(',');
263 contactTags.forEach(function(elem) {
264 if (!_.includes(tags, elem)) {
270 if (newTags.length) {
271 Tag.bulkCreate(newTags, {
272 individualHooks: true
276 req.body.UserId = req.user.id;
277 var phones = _.uniq(_.pluck(req.body.Phones, 'phone'));
278 var contactPhones = [];
279 phones.forEach(function(elem) {
280 contactPhones.push(ContactPhone.findOrCreate({
289 return contactPhones;
292 .then(function(res) {
293 newPhones = _.map(res, function(elem) {
297 var contactEmails = [];
298 var emails = _.uniq(_.pluck(req.body.Emails, 'email'));
299 emails.forEach(function(elem) {
300 contactEmails.push(ContactEmail.findOrCreate({
309 return contactEmails;
312 .then(function(res) {
313 newEmails = _.map(res, function(elem) {
316 delete req.body.Phones;
317 delete req.body.Emails;
318 newCustomFields = _.clone(req.body.customFields);
319 delete req.body.customFields;
321 return Contact.create(req.body);
323 .then(function(contact) {
324 return [contact, contact.setPhones(newPhones)];
326 .spread(function(contact) {
327 return [contact, contact.setEmails(newEmails)];
329 .spread(function(contact) {
330 var customFieldsIds = _.keys(newCustomFields);
331 return [contact, CustomField.findAll({
337 .spread(function(contact, customFields) {
338 customFields.forEach(function(elem) {
339 elem.ContactHasCustomField = {
340 value: newCustomFields[String(elem.id)]
343 return [contact, contact.setCustomFields(customFields)];
345 .spread(function(contact) {
346 return res.status(201).send(contact);
348 .catch(function(err) {
354 // Updates an existing managed_contact in the DB.
355 exports.update = function(req, res, next) {
358 newCustomFields = {};
360 .then(function(tags) {
362 var tagList = _.pluck(_.pluck(tags, 'dataValues'), 'name');
364 var contactTags = req.body.tags.split(',');
365 contactTags.forEach(function(elem) {
366 if (!_.includes(tags, elem)) {
372 if (newTags.length) {
373 Tag.bulkCreate(newTags, {
374 individualHooks: true
378 var contactPhones = [];
379 var phones = _.uniq(_.pluck(req.body.Phones, 'phone'));
380 phones.forEach(function(elem) {
381 contactPhones.push(ContactPhone.findOrCreate({
390 return contactPhones;
393 .then(function(res) {
394 newPhones = _.map(res, function(elem) {
398 var contactEmails = [];
399 var emails = _.uniq(_.pluck(req.body.Emails, 'email'));
400 emails.forEach(function(elem) {
401 contactEmails.push(ContactEmail.findOrCreate({
410 return contactEmails;
413 .then(function(res) {
414 newEmails = _.map(res, function(elem) {
417 delete req.body.Phones;
418 delete req.body.Emails;
419 newCustomFields = _.clone(req.body.customFields);
420 delete req.body.customFields;
421 delete req.body.CustomFields;
422 var updateId = req.body.id;
425 return Contact.findById(updateId);
427 .then(function(contact) {
428 return [contact, contact.setPhones(newPhones)];
430 .spread(function(contact) {
431 return [contact, contact.setEmails(newEmails)];
433 .spread(function(contact) {
434 var customFieldsIds = _.keys(newCustomFields);
435 return [contact, CustomField.findAll({
441 .spread(function(contact, customFields) {
442 customFields.forEach(function(elem) {
443 elem.ContactHasCustomField = {
444 value: newCustomFields[String(elem.id)]
447 return [contact, contact.setCustomFields(customFields)];
449 .spread(function(contact) {
450 return contact.updateAttributes(req.body)
452 .then(function(contact) {
453 return res.status(200).send(contact);
455 .catch(function(err) {
457 return handleError(res, err);
461 // Deletes a managed_contact from the DB.
462 exports.destroy = function(req, res) {
464 .findById(req.params.id)
465 .then(function(managed_contact) {
466 if (!managed_contact) {
467 return res.sendStatus(404);
469 managed_contact.destroy()
471 return res.sendStatus(204);
473 .catch(function(err) {
474 return handleError(res, err);
477 .catch(function(err) {
478 return handleError(res, err);
482 // Deletes a managed_contact from the DB.
483 exports.bulkDestroy = function(req, res) {
489 individualHooks: true
492 return res.sendStatus(204);
494 .catch(function(err) {
495 return handleError(res, err);
499 function handleError(res, err) {
500 return res.status(500).send(err);