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