3 var _ = require('lodash');
4 var util = require('util');
5 var Agent = require('../../models').User;
6 var Team = require('../../models').Team;
10 exports.index = function (req, res, next) {
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;
21 offset: page * per_page
24 _.forIn(req.query, function (value, key) {
30 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
36 attributes.forEach(function (attribute) {
39 $like: '%' + value + '%'
42 query.where.$or.push(tmp);
49 query.where[key].$like = '%' + value + '%';
54 .findAndCountAll(query)
55 .then(function (result) {
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;
61 res.status(200).send({
65 previous_page: previous_page,
66 total_pages: total_pages
70 .catch(function (err) {
71 res.status(500).send({
72 error: 'Something blew up!'
78 exports.show = function (req, res) {
80 .findById(req.params.id)
81 .then(function (agent) {
83 return res.sendStatus(404);
85 return res.send(agent);
87 .catch(function (err) {
88 return handleError(res, err);
92 // Validate the existence of an internal number
93 exports.internalValidation = function (req, res) {
97 internal: req.body.internal
100 .then(function (internals) {
102 return res.sendStatus(404);
104 return res.send(internals);
106 .catch(function (err) {
107 return handleError(res, err);
111 // Creates a new agent in the DB.
112 exports.create = function (req, res) {
115 .then(function (agent) {
122 .then(function (team) {
126 return res.status(201).send(agent);
128 .catch(function (err) {
130 return handleError(res, err);
133 .catch(function (err) {
135 return handleError(res, err);
138 .catch(function (err) {
140 return handleError(res, err);
144 // Updates an existing agent in the DB.
145 exports.update = function (req, res) {
150 .findById(req.params.id)
151 .then(function (agent) {
153 return res.sendStatus(404);
155 var updated = _.merge(agent, req.body);
159 return res.status(200).send(agent);
161 .catch(function (err) {
162 return handleError(res, err);
165 .catch(function (err) {
166 return handleError(res, err);
170 // Deletes a agent from the DB.
171 exports.destroy = function (req, res) {
173 .findById(req.params.id)
174 .then(function (agent) {
176 return res.sendStatus(404);
180 return res.sendStatus(204);
182 .catch(function (err) {
183 return handleError(res, err);
186 .catch(function (err) {
187 return handleError(res, err);
191 // Deletes a agent from the DB.
192 exports.bulkDestroy = function (req, res) {
198 individualHooks: true
201 return res.sendStatus(204);
203 .catch(function (err) {
204 return handleError(res, err);
209 * Change an agent password
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);
218 .then(function (agent) {
219 if (agent.authenticate(oldPass)) {
220 agent.password = newPass;
223 res.status(200).send(agent);
225 .catch(function (err) {
235 * Change an agent password by admin
237 exports.resetPassword = function (req, res, next) {
238 var newPass = String(req.body.newPassword);
240 .findById(req.params.id)
241 .then(function (user) {
242 user.password = newPass;
245 res.status(200).send(user);
247 .catch(function (err) {
248 return handleError(res, err);
253 exports.agentValidation = function (req, res) {
255 where[req.params.field] = req.body.value;
260 .then(function (result) {
262 return res.status(200).send({
264 value: req.body.value
267 return res.status(200).send({
269 value: req.body.value
272 .catch(function (err) {
273 return handleError(res, err);
278 function handleError(res, err) {
279 return res.status(500).send(err);