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