3 var _ = require('lodash');
4 var util = require('util');
5 var async = require('async');
6 var Mustache = require('mustache');
8 var sequelize = require('../../models').sequelize;
9 var ChatWebsite = require('../../models').ChatWebsite;
10 var ChatApplication = require('../../models').ChatApplication;
11 var snippet = require('../../components/xchatty').snippet;
13 // Get list of chat_websites
14 exports.index = function (req, res, next) {
16 var attributes = ['name', 'address', 'description', 'remote'];
17 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
18 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
23 offset: page * per_page
26 _.forIn(req.query, function (value, key) {
32 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
38 attributes.forEach(function (attribute) {
41 $like: '%' + value + '%'
44 query.where.$or.push(tmp);
51 query.where[key].$like = '%' + value + '%';
56 .findAndCountAll(query)
57 .then(function (result) {
59 var total_pages = Math.ceil(result.count / per_page);
60 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;
61 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
63 res.status(200).send({
67 previous_page: previous_page,
68 total_pages: total_pages
72 .catch(function (err) {
73 res.status(500).send({
74 error: 'Something blew up!'
79 // Get a single chat_website
80 exports.show = function (req, res) {
82 .findById(req.params.id)
83 .then(function (chat_website) {
85 return res.sendStatus(404);
87 return res.send(chat_website);
89 .catch(function (err) {
90 return handleError(res, err);
94 // Get a snippet for chat_website
95 exports.getSnippetCode = function (req, res) {
97 .findById(req.params.id)
98 .then(function (chat_website) {
100 remote: chat_website.remote,
101 websiteId: req.params.id
103 var html = Mustache.render(snippet, configSnippet);
104 return res.send(html);
106 .catch(function (err) {
107 return handleError(res, err);
111 // Creates a new chat_website in the DB.
112 exports.create = function (req, res) {
115 .then(function (chat_website) {
116 return res.status(201).send(chat_website);
118 .catch(function (err) {
119 return handleError(res, err);
123 // Updates an existing chat_website in the DB.
124 exports.update = function (req, res) {
130 .findById(req.params.id)
131 .then(function (chat_website) {
133 return res.sendStatus(404);
138 .then(function (chat_website) {
139 return res.status(200).send(chat_website);
142 .catch(function (err) {
143 return handleError(res, err);
147 // Deletes a chat_website from the DB.
148 exports.destroy = function (req, res) {
155 .then(function (chat_website) {
157 return res.sendStatus(404);
159 chat_website.destroy()
161 return res.sendStatus(204);
163 .catch(function (err) {
164 return handleError(res, err);
167 .catch(function (err) {
168 return handleError(res, err);
172 // Sort Mail Applications
173 exports.sortApplications = function (req, res, next) {
178 id: req.body.applications
181 .then(function (chatApplications) {
182 var tmpChatApplications = chatApplications;
185 function (callback) {
189 id: req.body.applications
191 }).then(function () {
194 .catch(function (err) {
198 function (callback) {
199 var sortedApplications = [];
200 for (var i = 0; i < req.body.applications.length; i++) {
201 var tmpChatApplication = _.find(tmpChatApplications, {
202 'id': req.body.applications[i]
204 if (tmpChatApplication) {
205 tmpChatApplication.priority = i + 1;
206 sortedApplications.push(tmpChatApplication.dataValues);
211 .bulkCreate(sortedApplications)
215 .catch(function (err) {
219 function (callback) {
223 id: req.body.applications
230 .then(function (chatApplications) {
231 callback(null, chatApplications);
233 .catch(function (err) {
237 ], function (err, result) {
239 return handleError(res, err);
241 return res.status(201).send(result);
245 .catch(function (err) {
246 return handleError(res, err);
250 // Deletes a agent from the DB.
251 exports.bulkDestroy = function (req, res) {
257 individualHooks: true
260 return res.sendStatus(204);
262 .catch(function (err) {
263 return handleError(res, err);
267 function handleError(res, err) {
268 return res.status(500).send(err);