Built motion from commit 4143fce.|0.0.15
[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     .findById(req.params.id)
125     .then(function (chat_queue) {
126       if (!chat_queue) {
127         return res.sendStatus(404);
128       }
129       var updated = _.merge(chat_queue, req.body);
130       updated.save()
131         .then(function () {
132           return res.status(200).send(chat_queue);
133         })
134         .catch(function (err) {
135           return handleError(res, err);
136         });
137     })
138     .catch(function (err) {
139       return handleError(res, err);
140     });
141 };
142
143 // Updates an existing user_has_chatQueue in the DB.
144 exports.addAgents = function (req, res, next) {
145   return ChatQueue
146     .findById(req.params.id)
147     .then(function (chatQueue) {
148       if (chatQueue) {
149         return chatQueue
150           .addUsers(req.body.agents, {
151             individualHooks: true
152           });
153       } else {
154         throw new Error('No mail queue found');
155       }
156     })
157     .then(function () {
158       return res.sendStatus(200);
159     })
160     .catch(function (err) {
161       return next(err);
162     });
163 };
164
165 exports.removeAgents = function (req, res, next) {
166   return ChatQueue
167     .findById(req.params.id)
168     .then(function (chatQueue) {
169       if (chatQueue) {
170         return chatQueue
171           .removeUsers(req.body.agents, {
172             individualHooks: true
173           });
174       } else {
175         throw new Error('no mail queue found');
176       }
177     })
178     .then(function () {
179       return res.sendStatus(200);
180     })
181     .catch(function (err) {
182       return next(err);
183     });
184 };
185
186 // Deletes a chat_queue from the DB.
187 exports.destroy = function (req, res) {
188   ChatQueue
189     .findById(req.params.id)
190     .then(function (chat_queue) {
191       if (!chat_queue) {
192         return res.sendStatus(404);
193       }
194       chat_queue.destroy()
195         .then(function () {
196           return res.sendStatus(204);
197         })
198         .catch(function (err) {
199           return handleError(res, err);
200         });
201     })
202     .catch(function (err) {
203       return handleError(res, err);
204     });
205 };
206
207 // Deletes a agent from the DB.
208 exports.bulkDestroy = function (req, res) {
209   ChatQueue
210     .destroy({
211       where: {
212         id: req.query.id
213       },
214       individualHooks: true
215     })
216     .then(function () {
217       return res.sendStatus(204);
218     })
219     .catch(function (err) {
220       return handleError(res, err);
221     });
222 };
223
224 function handleError(res, err) {
225   return res.status(500).send(err);
226 }