Built motion from commit f782815 on branch develop.
[motion.git] / server / api / contact_manager / contact_manager.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var ManagedContact = require('../../models').ManagedContact;
5 var ManagedContactHistory = require('../../models').ManagedContactHistory;
6 var stream = require('stream');
7
8 // Get list of contacts
9 exports.index = function(req, res) {
10
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;
14
15   var query = {
16     where: {},
17     limit: per_page,
18     offset: page * per_page
19   };
20
21   _.forIn(req.query, function(value, key) {
22     switch (key) {
23       case 'per_page':
24       case 'page':
25         break;
26       case 'sort_by':
27         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
28         break;
29       case 'sort_order':
30         break;
31       case '$':
32         query.where.$or = [];
33         attributes.forEach(function(attribute) {
34           var tmp = {};
35           tmp[attribute] = {
36             $like: '%' + value + '%'
37           };
38
39           query.where.$or.push(tmp);
40         });
41         break;
42       default:
43         query.where[key] = {
44           $like: {}
45         };
46         query.where[key].$like = '%' + value + '%';
47     }
48   });
49
50
51   ManagedContact
52     .findAndCountAll(query)
53     .then(function(managed_contacts) {
54       return res.status(200).send(managed_contacts);
55     })
56     .catch(function(err) {
57       return handleError(res, err);
58     });
59 };
60
61 // Get a single managed_contact
62 exports.show = function(req, res) {
63   ManagedContact
64     .findById(req.params.id)
65     .then(function(managed_contact) {
66       if (!managed_contact) {
67         return res.sendStatus(404);
68       }
69       return res.send(managed_contact);
70     })
71     .catch(function(err) {
72       return handleError(res, err);
73     });
74 };
75
76 exports.getHistory = function(req, res) {
77   ManagedContactHistory
78     .findAll({
79       where: {
80         ManagedContactId: req.params.id,
81       }
82     })
83     .then(function(contactHistory) {
84       return res.status(200).send(contactHistory);
85     })
86     .catch(function(err) {
87       return handleError(res, err);
88     });
89 };
90
91 // validate mailbox uniqueness
92 exports.contactValidation = function(req, res) { //DA FARE!!!
93   //   // console.log(req.body);
94   //   ManagedContact
95   //     .findAll({
96   //       where: {
97   //         mailbox: req.body.mailbox,
98   //         context: req.body.context
99   //       }
100   //     })
101   //     .then(function(voicemails) {
102   //       if (!voicemails) {
103   //         return res.sendStatus(404);
104   //       }
105   //       return res.send(voicemails);
106   //     })
107   //     .catch(function(err) {
108   //       return handleError(res, err);
109   //     });
110 };
111 // Creates a new managed_contact in the DB.
112 exports.create = function(req, res) {
113   // ManagedContact
114   //   .findAll({
115   //     where: {
116   //       mailbox: req.body.mailbox,
117   //       context: req.body.context
118   //     }
119   //   })
120   //   .then(function(voicemails) {
121   //     if (!voicemails) {
122   //       return res.sendStatus(404);
123   //     }
124   //     if (voicemails.length > 0) {
125   //       return res.status(500).send({
126   //         message: 'MESSAGE_EXIST_MAILBOX'
127   //       })
128   //     }
129   //     ManagedContact
130   //       .create(req.body)
131   //       .then(function(managed_contact) {
132   //         return res.status(201).send(managed_contact);
133   //       })
134   //       .catch(function(err) {
135   //         return handleError(res, err);
136   //       })
137   //       .catch(function(err) {
138   //         return handleError(res, err);
139   //       });
140   //   })
141 };
142
143 // Updates an existing managed_contact in the DB.
144 exports.update = function(req, res) {
145   // ManagedContact
146   //   .findAll({
147   //     where: {
148   //       mailbox: req.body.mailbox,
149   //       context: req.body.context,
150   //       uniqueid: {
151   //         $ne: req.body.uniqueid
152   //       }
153   //     }
154   //   })
155   //   .then(function(voicemails) {
156   //     if (!voicemails) {
157   //       return res.sendStatus(404);
158   //     }
159   //     if (voicemails.length > 0) {
160   //       return res.status(500).send({
161   //         message: 'MESSAGE_EXIST_MAILBOX'
162   //       })
163   //     }
164   //     if (req.body.uniqueid) {
165   //       delete req.body.uniqueid;
166   //     }
167   //     ManagedContact
168   //       .find({
169   //         where: {
170   //           uniqueid: req.params.uniqueid
171   //         }
172   //       })
173   //       .then(function(managed_contact) {
174   //         if (!managed_contact) {
175   //           return res.sendStatus(404);
176   //         }
177   //         var updated = _.merge(managed_contact, req.body);
178   //         updated.save()
179   //           .then(function() {
180   //             return res.status(200).send(managed_contact);
181   //           })
182   //           .catch(function(err) {
183   //             return handleError(res, err);
184   //           });
185   //       })
186   //       .catch(function(err) {
187   //         return handleError(res, err);
188   //       });
189   //   })
190   //   .catch(function(err) {
191   //     return handleError(res, err);
192   //   });
193
194 };
195
196 // Deletes a managed_contact from the DB.
197 exports.destroy = function(req, res) {
198   ManagedContact
199     .find({
200       where: {
201         uniqueid: req.params.uniqueid
202       }
203     })
204     .then(function(managed_contact) {
205       if (!managed_contact) {
206         return res.sendStatus(404);
207       }
208       managed_contact.destroy()
209         .then(function() {
210           return res.sendStatus(204);
211         })
212         .catch(function(err) {
213           return handleError(res, err);
214         });
215     })
216     .catch(function(err) {
217       return handleError(res, err);
218     });
219 };
220
221 // Deletes a managed_contact from the DB.
222 exports.bulkDestroy = function(req, res) {
223   ManagedContact
224     .destroy({
225       where: {
226         uniqueid: req.query.uniqueid
227       },
228       individualHooks: true
229     })
230     .then(function() {
231       return res.sendStatus(204);
232     })
233     .catch(function(err) {
234       return handleError(res, err);
235     });
236 };
237
238 function handleError(res, err) {
239   return res.status(500).send(err);
240 }