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