3 var _ = require('lodash');
4 var util = require('util');
6 var ChatQueue = require('../../models').ChatQueue;
7 var UserHasChatQueue = require('../../models').UserHasChatQueue;
9 // Get list of mailQueues
10 exports.index = function(req, res) {
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;
19 offset: page * per_page,
25 _.forIn(req.query, function(value, key) {
31 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
37 attributes.forEach(function(attribute) {
40 $like: '%' + value + '%'
43 query.where.$or.push(tmp);
50 query.where[key].$like = '%' + value + '%';
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;
61 res.status(200).send({
65 previous_page: previous_page,
66 total_pages: total_pages
70 .catch(function(err) {
71 return handleError(res, err);
75 // Get list of my chat_rooms
76 exports.me = function(req, res, next) {
83 .then(function(userHasChatQueues) {
84 return res.status(200).send(userHasChatQueues);
86 .catch(function(err) {
87 return handleError(res, err);
91 // Get a single chat_queue
92 exports.show = function(req, res) {
94 .findById(req.params.id)
95 .then(function(chat_queue) {
97 return res.sendStatus(404);
99 return res.send(chat_queue);
101 .catch(function(err) {
102 return handleError(res, err);
106 // Creates a new chat_queue in the DB.
107 exports.create = function(req, res) {
110 .then(function(chat_queue) {
111 return res.status(201).send(chat_queue);
113 .catch(function(err) {
114 return handleError(res, err);
118 // Updates an existing chat_queue in the DB.
119 exports.update = function(req, res) {
129 .then(function(chat_queue) {
131 return res.sendStatus(404);
133 var updated = _.merge(chat_queue, req.body);
136 return res.status(200).send(chat_queue);
138 .catch(function(err) {
139 return handleError(res, err);
142 .catch(function(err) {
143 return handleError(res, err);
147 // Updates an existing user_has_chatQueue in the DB.
148 exports.addAgents = function(req, res, next) {
150 .findById(req.params.id)
151 .then(function(chatQueue) {
156 .addUsers(req.body.agents)
158 return res.sendStatus(200);
160 .catch(function(err) {
164 return next(new Error('no mail queue found'));
167 .catch(function(err) {
172 exports.removeAgents = function(req, res, next) {
174 .findById(req.params.id)
175 .then(function(chatQueue) {
180 .removeUsers(req.body.agents)
182 return res.sendStatus(200);
184 .catch(function(err) {
188 return next(new Error('no mail queue found'));
191 .catch(function(err) {
196 // Deletes a chat_queue from the DB.
197 exports.destroy = function(req, res) {
204 .then(function(chat_queue) {
206 return res.sendStatus(404);
210 return res.sendStatus(204);
212 .catch(function(err) {
213 return handleError(res, err);
216 .catch(function(err) {
217 return handleError(res, err);
221 // Deletes a agent from the DB.
222 exports.bulkDestroy = function(req, res) {
228 individualHooks: true
231 return res.sendStatus(204);
233 .catch(function(err) {
234 return handleError(res, err);
238 function handleError(res, err) {
239 return res.status(500).send(err);