3 var _ = require('lodash');
4 var util = require('util');
6 var Team = require('../../models').Team;
7 var User = require('../../models').User;
10 exports.index = function (req, res) {
12 var attributes = ['name', 'description'];
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;
23 offset: page * per_page
26 _.forIn(req.query, function (value, key) {
32 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
38 attributes.forEach(function (attribute) {
41 $like: '%' + value + '%'
44 query.where.$or.push(tmp);
51 query.where[key].$like = '%' + value + '%';
55 console.log('team', query);
58 .findAndCountAll(query)
59 .then(function (result) {
61 var total_pages = Math.ceil(result.count / per_page);
62 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;
63 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
65 res.status(200).send({
69 previous_page: previous_page,
70 total_pages: total_pages
74 .catch(function (err) {
75 res.status(500).send({
76 error: 'Something blew up!'
82 exports.show = function (req, res) {
90 .then(function (team) {
92 return res.sendStatus(404);
94 return res.send(team);
96 .catch(function (err) {
97 return handleError(res, err);
101 // Creates a new team in the DB.
102 exports.create = function (req, res) {
105 .then(function (team) {
106 return res.status(201).send(team);
108 .catch(function (err) {
109 return handleError(res, err);
113 // Updates an existing team in the DB.
114 exports.update = function (req, res) {
124 .then(function (team) {
126 return res.sendStatus(404);
128 var updated = _.merge(team, req.body);
131 return res.status(200).send(team);
133 .catch(function (err) {
134 return handleError(res, err);
137 .catch(function (err) {
138 return handleError(res, err);
142 // Deletes a team from the DB.
143 exports.destroy = function (req, res) {
150 .then(function (team) {
152 return res.sendStatus(404);
156 return res.sendStatus(204);
158 .catch(function (err) {
159 return handleError(res, err);
162 .catch(function (err) {
163 return handleError(res, err);
167 // Deletes a agent from the DB.
168 exports.bulkDestroy = function (req, res) {
174 individualHooks: true
177 return res.sendStatus(204);
179 .catch(function (err) {
180 return handleError(res, err);
184 // Updates an existing user_has_mail_queue in the DB.
185 exports.addAgents = function (req, res, next) {
187 .findById(req.params.id)
188 .then(function (team) {
193 .addUsers(req.body.agents)
195 return res.sendStatus(200);
197 .catch(function (err) {
201 return next(new Error('no mail queue found'));
204 .catch(function (err) {
209 exports.removeAgents = function (req, res, next) {
211 .findById(req.params.id)
212 .then(function (team) {
217 .removeUsers(req.body.agents)
219 return res.sendStatus(200);
221 .catch(function (err) {
225 return next(new Error('no mail queue found'));
228 .catch(function (err) {
233 function handleError(res, err) {
234 return res.status(500).send(err);