Built motion from commit 06df96e on branch develop.
[motion.git] / server / api / voice_voicemail / voice_voicemail.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var VoiceVoicemail = require('../../models').VoiceVoicemail;
5 var VoiceVoicemailMessages = require('../../models').VoiceVoicemailMessages;
6 var stream = require('stream');
7
8 // Get list of voice_voicemails
9 exports.index = function(req, res) {
10
11   var attributes = ['mailbox', 'fullname', 'email'];
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   VoiceVoicemail
52     .findAndCountAll(query)
53     .then(function(voice_voicemails) {
54       return res.status(200).send(voice_voicemails);
55     })
56     .catch(function(err) {
57       return handleError(res, err);
58     });
59 };
60
61 // Get a single voice_voicemail
62 exports.show = function(req, res) {
63   VoiceVoicemail
64     .find({
65       where: {
66         uniqueid: req.params.uniqueid
67       }
68     })
69     .then(function(voice_voicemail) {
70       if (!voice_voicemail) {
71         return res.sendStatus(404);
72       }
73       return res.send(voice_voicemail);
74     })
75     .catch(function(err) {
76       return handleError(res, err);
77     });
78 };
79
80 exports.getMessages = function(req, res) {
81   VoiceVoicemail
82     .find({
83       where: {
84         uniqueid: req.params.uniqueid
85       }
86     })
87     .then(function(voice_voicemail) {
88       VoiceVoicemailMessages
89         .findAll({
90           where: {
91             mailboxcontext: voice_voicemail.context,
92             mailboxuser: voice_voicemail.mailbox
93           }
94         })
95         .then(function(voicemailMessages) {
96           return res.status(200).send(voicemailMessages);
97         })
98         .catch(function(err) {
99           return handleError(res, err);
100         });
101     })
102     .catch(function(err) {
103       return handleError(res, err);
104     });
105 };
106
107 // validate mailbox uniqueness
108 exports.mailboxValidation = function(req, res) {
109   console.log(req.body);
110   VoiceVoicemail
111     .findAll({
112       where: {
113         mailbox: req.body.mailbox,
114         context: req.body.context
115       }
116     })
117     .then(function(voicemails) {
118       if (!voicemails) {
119         return res.sendStatus(404);
120       }
121       return res.send(voicemails);
122     })
123     .catch(function(err) {
124       return handleError(res, err);
125     });
126 };
127 // Creates a new voice_voicemail in the DB.
128 exports.create = function(req, res) {
129   VoiceVoicemail
130     .findAll({
131       where: {
132         mailbox: req.body.mailbox,
133         context: req.body.context
134       }
135     })
136     .then(function(voicemails) {
137       if (!voicemails) {
138         return res.sendStatus(404);
139       }
140       if (voicemails.length > 0) {
141         return res.status(500).send({
142           message: 'MESSAGE_EXIST_MAILBOX'
143         })
144       }
145       VoiceVoicemail
146         .create(req.body)
147         .then(function(voice_voicemail) {
148           return res.status(201).send(voice_voicemail);
149         })
150         .catch(function(err) {
151           return handleError(res, err);
152         })
153         .catch(function(err) {
154           return handleError(res, err);
155         });
156     })
157 };
158
159 // Updates an existing voice_voicemail in the DB.
160 exports.update = function(req, res) {
161   VoiceVoicemail
162     .findAll({
163       where: {
164         mailbox: req.body.mailbox,
165         context: req.body.context,
166         uniqueid: {
167           $ne: req.body.uniqueid
168         }
169       }
170     })
171     .then(function(voicemails) {
172       if (!voicemails) {
173         return res.sendStatus(404);
174       }
175       if (voicemails.length > 0) {
176         return res.status(500).send({
177           message: 'MESSAGE_EXIST_MAILBOX'
178         })
179       }
180       if (req.body.uniqueid) {
181         delete req.body.uniqueid;
182       }
183       VoiceVoicemail
184         .find({
185           where: {
186             uniqueid: req.params.uniqueid
187           }
188         })
189         .then(function(voice_voicemail) {
190           if (!voice_voicemail) {
191             return res.sendStatus(404);
192           }
193           var updated = _.merge(voice_voicemail, req.body);
194           updated.save()
195             .then(function() {
196               return res.status(200).send(voice_voicemail);
197             })
198             .catch(function(err) {
199               return handleError(res, err);
200             });
201         })
202         .catch(function(err) {
203           return handleError(res, err);
204         });
205     })
206     .catch(function(err) {
207       return handleError(res, err);
208     });
209
210 };
211
212 // Deletes a voice_voicemail from the DB.
213 exports.destroy = function(req, res) {
214   VoiceVoicemail
215     .find({
216       where: {
217         uniqueid: req.params.uniqueid
218       }
219     })
220     .then(function(voice_voicemail) {
221       if (!voice_voicemail) {
222         return res.sendStatus(404);
223       }
224       voice_voicemail.destroy()
225         .then(function() {
226           return res.sendStatus(204);
227         })
228         .catch(function(err) {
229           return handleError(res, err);
230         });
231     })
232     .catch(function(err) {
233       return handleError(res, err);
234     });
235 };
236
237 // Deletes a voice_voicemail from the DB.
238 exports.bulkDestroy = function(req, res) {
239   VoiceVoicemail
240     .destroy({
241       where: {
242         uniqueid: req.query.uniqueid
243       },
244       individualHooks: true
245     })
246     .then(function() {
247       return res.sendStatus(204);
248     })
249     .catch(function(err) {
250       return handleError(res, err);
251     });
252 };
253
254 // Deletes a voice_voicemail message from the DB.
255 exports.destroyMessage = function(req, res) {
256   VoiceVoicemailMessages
257     .findById(req.params.id)
258     .then(function(voicemailMessage) {
259       if (!voicemailMessage) {
260         return res.sendStatus(404);
261       }
262       voicemailMessage.recording = null;
263       voicemailMessage.destroy()
264         .then(function() {
265           return res.sendStatus(204);
266         })
267         .catch(function(err) {
268           return handleError(res, err);
269         });
270     })
271     .catch(function(err) {
272       return handleError(res, err);
273     });
274 };
275
276 exports.downloadMessage = function(req, res) {
277   VoiceVoicemailMessages
278     .findById(req.params.id)
279     .then(function(message) {
280       if (!message) {
281         return res.sendStatus(404);
282       }
283       var filename = message.msg_id + '.wav';
284       res.writeHead(200, "OK", {
285         "Content-Type": "audio/wav",
286         "Content-Disposition": "attachment; filename=" + filename,
287         "Content-Length": message.recording.length
288       });
289       var bufferStream = new stream.PassThrough();
290       bufferStream.end(new Buffer(message.recording));
291       bufferStream.pipe(res)
292     })
293 };
294
295
296 function handleError(res, err) {
297   return res.status(500).send(err);
298 }