3 var _ = require('lodash');
4 var ManagedContact = require('../../models').ManagedContact;
5 var ManagedContactHistory = require('../../models').ManagedContactHistory;
6 var stream = require('stream');
8 // Get list of contacts
9 exports.index = function(req, res) {
11 var attributes = ['name', 'surname', 'description'];
12 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
13 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
18 offset: page * per_page
21 _.forIn(req.query, function(value, key) {
27 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
33 attributes.forEach(function(attribute) {
36 $like: '%' + value + '%'
39 query.where.$or.push(tmp);
46 query.where[key].$like = '%' + value + '%';
52 .findAndCountAll(query)
53 .then(function(managed_contacts) {
54 return res.status(200).send(managed_contacts);
56 .catch(function(err) {
57 return handleError(res, err);
61 // Get a single managed_contact
62 exports.show = function(req, res) {
64 .findById(req.params.id)
65 .then(function(managed_contact) {
66 if (!managed_contact) {
67 return res.sendStatus(404);
69 return res.send(managed_contact);
71 .catch(function(err) {
72 return handleError(res, err);
76 exports.getHistory = function(req, res) {
80 ManagedContactId: req.params.id,
83 .then(function(contactHistory) {
84 return res.status(200).send(contactHistory);
86 .catch(function(err) {
87 return handleError(res, err);
91 // validate mailbox uniqueness
92 exports.contactValidation = function(req, res) { //DA FARE!!!
93 // // console.log(req.body);
97 // mailbox: req.body.mailbox,
98 // context: req.body.context
101 // .then(function(voicemails) {
102 // if (!voicemails) {
103 // return res.sendStatus(404);
105 // return res.send(voicemails);
107 // .catch(function(err) {
108 // return handleError(res, err);
111 // Creates a new managed_contact in the DB.
112 exports.create = function(req, res) {
116 // mailbox: req.body.mailbox,
117 // context: req.body.context
120 // .then(function(voicemails) {
121 // if (!voicemails) {
122 // return res.sendStatus(404);
124 // if (voicemails.length > 0) {
125 // return res.status(500).send({
126 // message: 'MESSAGE_EXIST_MAILBOX'
131 // .then(function(managed_contact) {
132 // return res.status(201).send(managed_contact);
134 // .catch(function(err) {
135 // return handleError(res, err);
137 // .catch(function(err) {
138 // return handleError(res, err);
143 // Updates an existing managed_contact in the DB.
144 exports.update = function(req, res) {
148 // mailbox: req.body.mailbox,
149 // context: req.body.context,
151 // $ne: req.body.uniqueid
155 // .then(function(voicemails) {
156 // if (!voicemails) {
157 // return res.sendStatus(404);
159 // if (voicemails.length > 0) {
160 // return res.status(500).send({
161 // message: 'MESSAGE_EXIST_MAILBOX'
164 // if (req.body.uniqueid) {
165 // delete req.body.uniqueid;
170 // uniqueid: req.params.uniqueid
173 // .then(function(managed_contact) {
174 // if (!managed_contact) {
175 // return res.sendStatus(404);
177 // var updated = _.merge(managed_contact, req.body);
179 // .then(function() {
180 // return res.status(200).send(managed_contact);
182 // .catch(function(err) {
183 // return handleError(res, err);
186 // .catch(function(err) {
187 // return handleError(res, err);
190 // .catch(function(err) {
191 // return handleError(res, err);
196 // Deletes a managed_contact from the DB.
197 exports.destroy = function(req, res) {
201 uniqueid: req.params.uniqueid
204 .then(function(managed_contact) {
205 if (!managed_contact) {
206 return res.sendStatus(404);
208 managed_contact.destroy()
210 return res.sendStatus(204);
212 .catch(function(err) {
213 return handleError(res, err);
216 .catch(function(err) {
217 return handleError(res, err);
221 // Deletes a managed_contact from the DB.
222 exports.bulkDestroy = function(req, res) {
226 uniqueid: req.query.uniqueid
228 individualHooks: true
231 return res.sendStatus(204);
233 .catch(function(err) {
234 return handleError(res, err);
238 function handleError(res, err) {
239 return res.status(500).send(err);