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 ReportCallHistory = require('../../models').history.ReportCallHistory;
10 var ReportMailSession = require('../../models').ReportMailSession;
11 var ReportMailSessionHistory = require('../../models').history.ReportMailSessionHistory;
12 var Tag = require('../../models').Tag;
13 var stream = require('stream');
14 var sequelize = require('../../models').sequelize;
15 var util = require('util');
17 // Get list of contacts
18 exports.index = function(req, res) {
20 var attributes = ['name', 'surname', 'description'];
21 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
22 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
27 offset: page * per_page
30 _.forIn(req.query, function(value, key) {
36 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
42 attributes.forEach(function(attribute) {
45 $like: '%' + value + '%'
48 query.where.$or.push(tmp);
55 query.where[key].$like = '%' + value + '%';
61 .findAndCountAll(query)
62 .then(function(result) {
64 var total_pages = Math.ceil(result.count / per_page);
65 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;
66 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
68 res.status(200).send({
72 previous_page: previous_page,
73 total_pages: total_pages
77 .catch(function(err) {
78 res.status(500).send({
79 error: 'Something blew up!'
84 // Get a single managed_contact
85 exports.show = function(req, res) {
87 .findById(req.params.id, {
104 model: ReportCallHistory,
105 as: 'HistoryInbounds',
110 model: ReportCallHistory,
111 as: 'HistoryOutbounds',
120 model: ReportMailSession,
121 as: 'InboundMessages',
126 model: ReportMailSession,
127 as: 'OutboundMessages',
132 model: ReportMailSessionHistory,
133 as: 'HistoryInboundMessages',
138 model: ReportMailSessionHistory,
139 as: 'HistoryOutboundMessages',
149 .then(function(managed_contact) {
150 if (!managed_contact) {
151 return res.sendStatus(404);
153 return res.send(managed_contact);
155 .catch(function(err) {
157 return handleError(res, err);
161 exports.getHistory = function(req, res) {
165 // validate contact uniqueness
166 exports.contactValidation = function(req, res) {
170 '$Phones.phone$': req.body.phones,
171 '$Emails.email$': req.body.emails
178 .then(function(contacts) {
179 console.log(contacts);
180 return res.status(200).send(contacts);
182 .catch(function(err) {
184 return handleError(res, err);
188 // Creates a new managed_contact in the DB.
189 exports.create = function(req, res, next) {
192 newCustomFields = {};
194 .then(function(tags) {
196 var tagList = _.pluck(_.pluck(tags, 'dataValues'), 'name');
198 var contactTags = req.body.tags.split(',');
199 contactTags.forEach(function(elem) {
200 if (!_.includes(tags, elem)) {
206 if (newTags.length) {
207 Tag.bulkCreate(newTags, {
208 individualHooks: true
212 req.body.UserId = req.user.id;
213 var phones = _.uniq(_.pluck(req.body.Phones, 'phone'));
214 var contactPhones = [];
215 phones.forEach(function(elem) {
216 contactPhones.push(ContactPhone.findOrCreate({
225 return contactPhones;
228 .then(function(res) {
229 newPhones = _.map(res, function(elem) {
233 var contactEmails = [];
234 var emails = _.uniq(_.pluck(req.body.Emails, 'email'));
235 emails.forEach(function(elem) {
236 contactEmails.push(ContactEmail.findOrCreate({
245 return contactEmails;
248 .then(function(res) {
249 newEmails = _.map(res, function(elem) {
252 delete req.body.Phones;
253 delete req.body.Emails;
254 newCustomFields = _.clone(req.body.customFields);
255 delete req.body.customFields;
257 return Contact.create(req.body);
259 .then(function(contact) {
260 return [contact, contact.setPhones(newPhones)];
262 .spread(function(contact) {
263 return [contact, contact.setEmails(newEmails)];
265 .spread(function(contact) {
266 var customFieldsIds = _.keys(newCustomFields);
267 return [contact, CustomField.findAll({
273 .spread(function(contact, customFields) {
274 customFields.forEach(function(elem) {
275 elem.ContactHasCustomField = {
276 value: newCustomFields[String(elem.id)]
279 return [contact, contact.setCustomFields(customFields)];
281 .spread(function(contact) {
282 return res.status(201).send(contact);
284 .catch(function(err) {
290 // Updates an existing managed_contact in the DB.
291 exports.update = function(req, res, next) {
294 newCustomFields = {};
296 .then(function(tags) {
298 var tagList = _.pluck(_.pluck(tags, 'dataValues'), 'name');
300 var contactTags = req.body.tags.split(',');
301 contactTags.forEach(function(elem) {
302 if (!_.includes(tags, elem)) {
308 if (newTags.length) {
309 Tag.bulkCreate(newTags, {
310 individualHooks: true
314 var contactPhones = [];
315 var phones = _.uniq(_.pluck(req.body.Phones, 'phone'));
316 phones.forEach(function(elem) {
317 contactPhones.push(ContactPhone.findOrCreate({
326 return contactPhones;
329 .then(function(res) {
330 newPhones = _.map(res, function(elem) {
334 var contactEmails = [];
335 var emails = _.uniq(_.pluck(req.body.Emails, 'email'));
336 emails.forEach(function(elem) {
337 contactEmails.push(ContactEmail.findOrCreate({
346 return contactEmails;
349 .then(function(res) {
350 newEmails = _.map(res, function(elem) {
353 delete req.body.Phones;
354 delete req.body.Emails;
355 newCustomFields = _.clone(req.body.customFields);
356 delete req.body.customFields;
357 delete req.body.CustomFields;
358 var updateId = req.body.id;
361 return Contact.findById(updateId);
363 .then(function(contact) {
364 return [contact, contact.setPhones(newPhones)];
366 .spread(function(contact) {
367 return [contact, contact.setEmails(newEmails)];
369 .spread(function(contact) {
370 var customFieldsIds = _.keys(newCustomFields);
371 return [contact, CustomField.findAll({
377 .spread(function(contact, customFields) {
378 customFields.forEach(function(elem) {
379 elem.ContactHasCustomField = {
380 value: newCustomFields[String(elem.id)]
383 return [contact, contact.setCustomFields(customFields)];
385 .spread(function(contact) {
386 return contact.updateAttributes(req.body)
388 .then(function(contact) {
389 return res.status(200).send(contact);
391 .catch(function(err) {
393 return handleError(res, err);
397 // // Join contacts info in the DB.
398 // exports.joinContacts = function(req, res, next) {
399 // if (req.body.tags) {
401 // .then(function(tags) {
403 // var tagList = _.pluck(_.pluck(tags, 'dataValues'), 'name');
404 // var contactTags = req.body.tags.split(',');
405 // contactTags.forEach(function(elem) {
406 // if (!_.includes(tags, elem)) {
412 // if (newTags.length) {
413 // Tag.bulkCreate(newTags, {
414 // individualHooks: true
418 // .catch(function(err) {
419 // return handleError(res, err);
422 // var updateId = req.body.id;
423 // delete req.body.id;
424 // var phones = _.map(req.body.Phones, function(elem) {
426 // phone: elem.phone,
427 // ContactId: updateId
430 // var emails = _.map(req.body.Emails, function(elem) {
432 // email: elem.email,
433 // ContactId: updateId
436 // delete req.body.Phones;
437 // delete req.body.Emails;
438 // return sequelize.transaction(function(t) {
439 // return Contact.update(req.body, {
449 // .then(function(contact) {
450 // return ContactPhone
451 // .bulkCreate(phones, {
454 // .then(function() {
455 // return ContactEmail
456 // .bulkCreate(emails, {
459 // .then(function() {
460 // return Contact.findById(updateId, {
463 // .then(function(contact) {
464 // return res.status(201).send(contact);
470 // .catch(function(err) {
476 // Deletes a managed_contact from the DB.
477 exports.destroy = function(req, res) {
479 .findById(req.params.id)
480 .then(function(managed_contact) {
481 if (!managed_contact) {
482 return res.sendStatus(404);
484 managed_contact.destroy()
486 return res.sendStatus(204);
488 .catch(function(err) {
489 return handleError(res, err);
492 .catch(function(err) {
493 return handleError(res, err);
497 // Deletes a managed_contact from the DB.
498 exports.bulkDestroy = function(req, res) {
504 individualHooks: true
507 return res.sendStatus(204);
509 .catch(function(err) {
510 return handleError(res, err);
514 function handleError(res, err) {
515 return res.status(500).send(err);