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