662a803e1dfda40513e72ac039aa20b3e5006910
[motion.git] / server / api / voice_context / voice_context.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5 var sequelize = require('../../models').sequelize;
6
7 var VoiceContext = require('../../models').VoiceContext;
8 var VoiceExtension = require('../../models').VoiceExtension;
9
10 // Get list of agents
11 exports.index = function(req, res, next) {
12
13   var attributes = ['name', 'description'];
14   var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
15   var page = req.query.page ? parseInt(req.query.page, 10) : 0;
16
17   var query = {
18     where: {},
19     limit: per_page,
20     offset: page * per_page
21   };
22
23   _.forIn(req.query, function(value, key) {
24     switch (key) {
25       case 'per_page':
26       case 'page':
27         break;
28       case 'sort_by':
29         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
30         break;
31       case 'sort_order':
32         break;
33       case '$':
34         query.where.$or = [];
35         attributes.forEach(function(attribute) {
36           var tmp = {};
37           tmp[attribute] = {
38             $like: '%' + value + '%'
39           };
40
41           query.where.$or.push(tmp);
42         });
43         break;
44       default:
45         query.where[key] = {
46           $like: {}
47         };
48         query.where[key].$like = '%' + value + '%';
49     }
50   });
51
52   VoiceContext
53     .findAndCountAll(query)
54     .then(function(result) {
55
56       var total_pages = Math.ceil(result.count / per_page);
57       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;
58       var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
59
60       res.status(200).send({
61         count: result.count,
62         rows: result.rows,
63         next_page: next_page,
64         previous_page: previous_page,
65         total_pages: total_pages
66       });
67
68     })
69     .catch(function(err) {
70       res.status(500).send({
71         error: 'Something blew up!'
72       });
73     });
74 };
75
76 // Get a single voice_context
77 exports.show = function(req, res) {
78   VoiceContext
79     .findById(req.params.id)
80     .then(function(voice_context) {
81       if (!voice_context) {
82         return res.sendStatus(404);
83       }
84       return res.send(voice_context);
85     })
86     .catch(function(err) {
87       return handleError(res, err);
88     });
89 };
90
91 // Validate the existence of a context
92 exports.contextValidation = function(req, res) {
93   console.log(req.body);
94   VoiceContext
95     .findAll({
96       where: {
97         name: req.body.name
98       }
99     })
100     .then(function(voice_contexts) {
101       if (!voice_contexts) {
102         return res.sendStatus(404);
103       }
104       return res.send(voice_contexts);
105     })
106     .catch(function(err) {
107       return handleError(res, err);
108     });
109 };
110
111 // Creates a new voice_context in the DB.
112 exports.create = function(req, res) {
113   VoiceContext
114     .findAll({
115       where: {
116         name: req.body.name
117       }
118     })
119     .then(function(existingContexts) {
120       console.log(existingContexts);
121       console.log('Finding app row.....');
122       if (!existingContexts) {
123         return res.sendStatus(404);
124       }
125       if (existingContexts.length > 0) {
126         return res.status(500).send({
127           message: 'MESSAGE_EXIST_ROUTE'
128         });
129       }
130       VoiceContext
131         .create(req.body)
132         .then(function(voice_context) {
133           return res.status(201).send(voice_context);
134         })
135         .catch(function(err) {
136           return handleError(res, err);
137         });
138     })
139     .catch(function(err) {
140       return handleError(res, err);
141     });
142
143 };
144
145 // Updates an existing voice_context in the DB.
146 exports.update = function(req, res) {
147   VoiceContext
148     .findAll({
149       where: {
150         name: req.body.name,
151         id: {
152           $ne: req.body.id
153         }
154       }
155     })
156     .then(function(existingContexts) {
157       console.log(existingContexts);
158       console.log('Finding app row.....');
159       if (!existingContexts) {
160         return res.sendStatus(404);
161       }
162       if (existingContexts.length > 0) {
163         return res.status(500).send({
164           message: 'MESSAGE_EXIST_ROUTE'
165         });
166       }
167       if (req.body.id) {
168         delete req.body.id;
169       }
170       VoiceContext
171         .findById(req.params.id)
172         .then(function(voice_context) {
173           if (!voice_context) {
174             return res.sendStatus(404);
175           }
176           var updated = _.merge(voice_context, req.body);
177           updated.save()
178             .then(function() {
179               return res.status(200).send(voice_context);
180             })
181             .catch(function(err) {
182               return handleError(res, err);
183             });
184         })
185         .catch(function(err) {
186           return handleError(res, err);
187         });
188     })
189     .catch(function(err) {
190       return handleError(res, err);
191     });
192
193 };
194
195 // Deletes a voice_context from the DB.
196 exports.destroy = function(req, res) {
197   VoiceContext
198     .findById(req.params.id)
199     .then(function(voice_context) {
200       if (!voice_context) {
201         return res.sendStatus(404);
202       }
203       VoiceExtension
204         .findAll({
205           where: {
206             context: voice_context.name
207           }
208         })
209         .then(function(contextExtensions) {
210           if (contextExtensions.length > 0) {
211             return res.status(500).send({
212               message: 'MESSAGE_CONTEXT_ROUTE_ASSOCIATED'
213             });
214           }
215           voice_context.destroy()
216             .then(function() {
217               return res.sendStatus(204);
218             })
219             .catch(function(err) {
220               return handleError(res, err);
221             });
222         })
223         .catch(function(err) {
224           return handleError(res, err);
225         });
226     })
227     .catch(function(err) {
228       return handleError(res, err);
229     });
230 };
231
232 // Deletes a context from the DB.
233 exports.bulkDestroy = function(req, res) {
234   VoiceContext
235     .findAll({
236       where: {
237         id: req.query.id
238       }
239     })
240     .then(function(voice_contexts) {
241       if (!voice_contexts) {
242         return res.sendStatus(404);
243       }
244       var destroyContextsNames = _.pluck(_.pluck(voice_contexts, 'dataValues'), 'name');
245       VoiceExtension
246         .findAll({
247           where: {
248             context: destroyContextsNames
249           }
250         })
251         .then(function(contextExtensions) {
252           if (contextExtensions.length > 0) {
253             return res.status(500).send({
254               message: 'MESSAGE_CONTEXT_ROUTE_ASSOCIATED'
255             });
256           }
257           VoiceContext
258             .destroy({
259               where: {
260                 id: req.query.id
261               },
262               individualHooks: true
263             })
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     .catch(function(err) {
276       return handleError(res, err);
277     });
278 };
279
280 function handleError(res, err) {
281   return res.status(500).send(err);
282 }