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) {
124 .findById(req.params.id)
125 .then(function (chat_queue) {
127 return res.sendStatus(404);
129 var updated = _.merge(chat_queue, req.body);
132 return res.status(200).send(chat_queue);
134 .catch(function (err) {
135 return handleError(res, err);
138 .catch(function (err) {
139 return handleError(res, err);
143 // Updates an existing user_has_chatQueue in the DB.
144 exports.addAgents = function (req, res, next) {
146 .findById(req.params.id)
147 .then(function (chatQueue) {
150 .addUsers(req.body.agents, {
151 individualHooks: true
154 throw new Error('No mail queue found');
158 return res.sendStatus(200);
160 .catch(function (err) {
165 exports.removeAgents = function (req, res, next) {
167 .findById(req.params.id)
168 .then(function (chatQueue) {
171 .removeUsers(req.body.agents, {
172 individualHooks: true
175 throw new Error('no mail queue found');
179 return res.sendStatus(200);
181 .catch(function (err) {
186 // Deletes a chat_queue from the DB.
187 exports.destroy = function (req, res) {
189 .findById(req.params.id)
190 .then(function (chat_queue) {
192 return res.sendStatus(404);
196 return res.sendStatus(204);
198 .catch(function (err) {
199 return handleError(res, err);
202 .catch(function (err) {
203 return handleError(res, err);
207 // Deletes a agent from the DB.
208 exports.bulkDestroy = function (req, res) {
214 individualHooks: true
217 return res.sendStatus(204);
219 .catch(function (err) {
220 return handleError(res, err);
224 function handleError(res, err) {
225 return res.status(500).send(err);