Built motion from commit 06df96e on branch develop.
[motion.git] / server / api / chat_queue / chat_queue.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5
6 var ChatQueue = require('../../models').ChatQueue;
7 var UserHasChatQueue = require('../../models').UserHasChatQueue;
8
9 // Get list of mailQueues
10 exports.index = function(req, res) {
11
12   var attributes = ['description', 'name', 'timeout', 'strategy'];
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     limit: per_page,
19     offset: page * per_page,
20     include: [{
21       all: true
22     }]
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   ChatQueue
55     .findAndCountAll(query)
56     .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;
60
61       res.status(200).send({
62         count: result.count,
63         rows: result.rows,
64         next_page: next_page,
65         previous_page: previous_page,
66         total_pages: total_pages
67       });
68
69     })
70     .catch(function(err) {
71       return handleError(res, err);
72     });
73 };
74
75 // Get list of my chat_rooms
76 exports.me = function(req, res, next) {
77   UserHasChatQueue
78     .findAll({
79       where: {
80         UserId: req.user.id
81       }
82     })
83     .then(function(userHasChatQueues) {
84       return res.status(200).send(userHasChatQueues);
85     })
86     .catch(function(err) {
87       return handleError(res, err);
88     });
89 };
90
91 // Get a single chat_queue
92 exports.show = function(req, res) {
93   ChatQueue
94     .findById(req.params.id)
95     .then(function(chat_queue) {
96       if (!chat_queue) {
97         return res.sendStatus(404);
98       }
99       return res.send(chat_queue);
100     })
101     .catch(function(err) {
102       return handleError(res, err);
103     });
104 };
105
106 // Creates a new chat_queue in the DB.
107 exports.create = function(req, res) {
108   ChatQueue
109     .create(req.body)
110     .then(function(chat_queue) {
111       return res.status(201).send(chat_queue);
112     })
113     .catch(function(err) {
114       return handleError(res, err);
115     });
116 };
117
118 // Updates an existing chat_queue in the DB.
119 exports.update = function(req, res) {
120   if (req.body.id) {
121     delete req.body.id;
122   }
123   ChatQueue
124     .find({
125       where: {
126         id: req.params.id
127       }
128     })
129     .then(function(chat_queue) {
130       if (!chat_queue) {
131         return res.sendStatus(404);
132       }
133       var updated = _.merge(chat_queue, req.body);
134       updated.save()
135         .then(function() {
136           return res.status(200).send(chat_queue);
137         })
138         .catch(function(err) {
139           return handleError(res, err);
140         });
141     })
142     .catch(function(err) {
143       return handleError(res, err);
144     });
145 };
146
147 // Updates an existing user_has_chatQueue in the DB.
148 exports.addAgents = function(req, res, next) {
149   ChatQueue
150     .findById(req.params.id)
151     .then(function(chatQueue) {
152       if (chatQueue) {
153         // QUEUE FOUND
154         // ADD AGENTS
155         chatQueue
156           .addUsers(req.body.agents)
157           .then(function() {
158             return res.sendStatus(200);
159           })
160           .catch(function(err) {
161             return next(err);
162           });
163       } else {
164         return next(new Error('no mail queue found'));
165       }
166     })
167     .catch(function(err) {
168       return next(err);
169     });
170 };
171
172 exports.removeAgents = function(req, res, next) {
173   ChatQueue
174     .findById(req.params.id)
175     .then(function(chatQueue) {
176       if (chatQueue) {
177         // QUEUE FOUND
178         // REMOVE AGENTS
179         chatQueue
180           .removeUsers(req.body.agents)
181           .then(function() {
182             return res.sendStatus(200);
183           })
184           .catch(function(err) {
185             return next(err);
186           });
187       } else {
188         return next(new Error('no mail queue found'));
189       }
190     })
191     .catch(function(err) {
192       return next(err);
193     });
194 };
195
196 // Deletes a chat_queue from the DB.
197 exports.destroy = function(req, res) {
198   ChatQueue
199     .find({
200       where: {
201         id: req.params.id
202       }
203     })
204     .then(function(chat_queue) {
205       if (!chat_queue) {
206         return res.sendStatus(404);
207       }
208       chat_queue.destroy()
209         .then(function() {
210           return res.sendStatus(204);
211         })
212         .catch(function(err) {
213           return handleError(res, err);
214         });
215     })
216     .catch(function(err) {
217       return handleError(res, err);
218     });
219 };
220
221 // Deletes a agent from the DB.
222 exports.bulkDestroy = function(req, res) {
223   ChatQueue
224     .destroy({
225       where: {
226         id: req.query.id
227       },
228       individualHooks: true
229     })
230     .then(function() {
231       return res.sendStatus(204);
232     })
233     .catch(function(err) {
234       return handleError(res, err);
235     });
236 };
237
238 function handleError(res, err) {
239   return res.status(500).send(err);
240 }