3 var _ = require('lodash');
4 var util = require('util');
5 var Agent = require('../../models').User;
6 var Team = require('../../models').Team;
9 exports.index = function(req, res, next) {
11 var attributes = ['fullname', 'name', '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;
20 offset: page * per_page
23 _.forIn(req.query, function(value, key) {
29 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
35 attributes.forEach(function(attribute) {
38 $like: '%' + value + '%'
41 query.where.$or.push(tmp);
48 query.where[key].$like = '%' + value + '%';
53 .findAndCountAll(query)
54 .then(function(result) {
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;
60 res.status(200).send({
64 previous_page: previous_page,
65 total_pages: total_pages
69 .catch(function(err) {
70 res.status(500).send({
71 error: 'Something blew up!'
77 exports.show = function(req, res) {
79 .findById(req.params.id)
80 .then(function(agent) {
82 return res.sendStatus(404);
84 return res.send(agent);
86 .catch(function(err) {
87 return handleError(res, err);
91 // Validate the existence of an internal number
92 exports.internalValidation = function(req, res) {
96 internal: req.body.internal
99 .then(function(internals) {
101 return res.sendStatus(404);
103 return res.send(internals);
105 .catch(function(err) {
106 return handleError(res, err);
110 // Creates a new agent in the DB.
111 exports.create = function(req, res) {
114 .then(function(agent) {
121 .then(function(team) {
125 return res.status(201).send(agent);
127 .catch(function(err) {
129 return handleError(res, err);
132 .catch(function(err) {
134 return handleError(res, err);
137 .catch(function(err) {
139 return handleError(res, err);
143 // Updates an existing agent in the DB.
144 exports.update = function(req, res) {
149 .findById(req.params.id)
150 .then(function(agent) {
152 return res.sendStatus(404);
154 var updated = _.merge(agent, req.body);
158 return res.status(200).send(agent);
160 .catch(function(err) {
161 return handleError(res, err);
164 .catch(function(err) {
165 return handleError(res, err);
169 // Deletes a agent from the DB.
170 exports.destroy = function(req, res) {
172 .findById(req.params.id)
173 .then(function(agent) {
175 return res.sendStatus(404);
179 return res.sendStatus(204);
181 .catch(function(err) {
182 return handleError(res, err);
185 .catch(function(err) {
186 return handleError(res, err);
190 // Deletes a agent from the DB.
191 exports.bulkDestroy = function(req, res) {
197 individualHooks: true
200 return res.sendStatus(204);
202 .catch(function(err) {
203 return handleError(res, err);
208 * Change an agent password
210 exports.changePassword = function(req, res, next) {
211 console.log(req.body);
212 var agentId = req.user.id;
213 var oldPass = String(req.body.oldPassword);
214 var newPass = String(req.body.newPassword);
217 .then(function(agent) {
218 if (agent.authenticate(oldPass)) {
219 agent.password = newPass;
222 res.status(200).send(agent);
224 .catch(function(err) {
234 * Change an agent password by admin
236 exports.resetPassword = function(req, res, next) {
237 var newPass = String(req.body.newPassword);
239 .findById(req.params.id)
240 .then(function(user) {
241 user.password = newPass;
244 res.status(200).send(user);
246 .catch(function(err) {
247 return handleError(res, err);
252 exports.agentValidation = function(req, res) {
254 where[req.params.field] = req.body.value;
259 .then(function(result) {
261 return res.status(200).send({
263 value: req.body.value
266 return res.status(200).send({
268 value: req.body.value
271 .catch(function(err) {
272 return handleError(res, err);
277 function handleError(res, err) {
278 return res.status(500).send(err);