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) {
134 .then(function(chat_website) {
136 return res.sendStatus(404);
138 chat_website.updateAttributes(req.body)
139 .then(function(chat_website) {
140 return res.status(200).send(chat_website);
142 .catch(function(err) {
143 return handleError(res, err);
145 // var updated = _.merge(chat_website, req.body);
147 // .then(function() {
148 // return res.status(200).send(chat_website);
150 // .catch(function(err) {
151 // return handleError(res, err);
154 .catch(function(err) {
155 return handleError(res, err);
159 // Deletes a chat_website from the DB.
160 exports.destroy = function(req, res) {
167 .then(function(chat_website) {
169 return res.sendStatus(404);
171 chat_website.destroy()
173 return res.sendStatus(204);
175 .catch(function(err) {
176 return handleError(res, err);
179 .catch(function(err) {
180 return handleError(res, err);
184 // Sort Mail Applications
185 exports.sortApplications = function(req, res, next) {
190 id: req.body.applications
193 .then(function(chatApplications) {
194 var tmpChatApplications = chatApplications;
201 id: req.body.applications
206 .catch(function(err) {
211 var sortedApplications = [];
212 for (var i = 0; i < req.body.applications.length; i++) {
213 var tmpChatApplication = _.find(tmpChatApplications, {
214 'id': req.body.applications[i]
216 if (tmpChatApplication) {
217 tmpChatApplication.priority = i + 1;
218 sortedApplications.push(tmpChatApplication.dataValues);
223 .bulkCreate(sortedApplications)
227 .catch(function(err) {
235 id: req.body.applications
242 .then(function(chatApplications) {
243 callback(null, chatApplications);
245 .catch(function(err) {
249 ], function(err, result) {
251 return handleError(res, err);
253 return res.status(201).send(result);
257 .catch(function(err) {
258 return handleError(res, err);
262 // Deletes a agent from the DB.
263 exports.bulkDestroy = function(req, res) {
269 individualHooks: true
272 return res.sendStatus(204);
274 .catch(function(err) {
275 return handleError(res, err);
279 function handleError(res, err) {
280 return res.status(500).send(err);