Built motion from commit 4143fce.|0.0.15
[motion.git] / server / api / chat_website / chat_website.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5 var async = require('async');
6 var Mustache = require('mustache');
7
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;
12
13 // Get list of chat_websites
14 exports.index = function (req, res, next) {
15
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;
19
20   var query = {
21     where: {},
22     limit: per_page,
23     offset: page * per_page
24   };
25
26   _.forIn(req.query, function (value, key) {
27     switch (key) {
28     case 'per_page':
29     case 'page':
30       break;
31     case 'sort_by':
32       query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
33       break;
34     case 'sort_order':
35       break;
36     case '$':
37       query.where.$or = [];
38       attributes.forEach(function (attribute) {
39         var tmp = {};
40         tmp[attribute] = {
41           $like: '%' + value + '%'
42         };
43
44         query.where.$or.push(tmp);
45       });
46       break;
47     default:
48       query.where[key] = {
49         $like: {}
50       };
51       query.where[key].$like = '%' + value + '%';
52     }
53   });
54
55   ChatWebsite
56     .findAndCountAll(query)
57     .then(function (result) {
58
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;
62
63       res.status(200).send({
64         count: result.count,
65         rows: result.rows,
66         next_page: next_page,
67         previous_page: previous_page,
68         total_pages: total_pages
69       });
70
71     })
72     .catch(function (err) {
73       res.status(500).send({
74         error: 'Something blew up!'
75       });
76     });
77 };
78
79 // Get a single chat_website
80 exports.show = function (req, res) {
81   ChatWebsite
82     .findById(req.params.id)
83     .then(function (chat_website) {
84       if (!chat_website) {
85         return res.sendStatus(404);
86       }
87       return res.send(chat_website);
88     })
89     .catch(function (err) {
90       return handleError(res, err);
91     });
92 };
93
94 // Get a snippet for chat_website
95 exports.getSnippetCode = function (req, res) {
96   ChatWebsite
97     .findById(req.params.id)
98     .then(function (chat_website) {
99       var configSnippet = {
100         remote: chat_website.remote,
101         websiteId: req.params.id
102       };
103       var html = Mustache.render(snippet, configSnippet);
104       return res.send(html);
105     })
106     .catch(function (err) {
107       return handleError(res, err);
108     });
109 };
110
111 // Creates a new chat_website in the DB.
112 exports.create = function (req, res) {
113   ChatWebsite
114     .create(req.body)
115     .then(function (chat_website) {
116       return res.status(201).send(chat_website);
117     })
118     .catch(function (err) {
119       return handleError(res, err);
120     });
121 };
122
123 // Updates an existing chat_website in the DB.
124 exports.update = function (req, res) {
125   if (req.body.id) {
126     delete req.body.id;
127   }
128
129   return ChatWebsite
130     .findById(req.params.id)
131     .then(function (chat_website) {
132       if (!chat_website) {
133         return res.sendStatus(404);
134       }
135
136       return chat_website
137         .update(req.body)
138         .then(function (chat_website) {
139           return res.status(200).send(chat_website);
140         })
141     })
142     .catch(function (err) {
143       return handleError(res, err);
144     });
145 };
146
147 // Deletes a chat_website from the DB.
148 exports.destroy = function (req, res) {
149   ChatWebsite
150     .find({
151       where: {
152         id: req.params.id
153       }
154     })
155     .then(function (chat_website) {
156       if (!chat_website) {
157         return res.sendStatus(404);
158       }
159       chat_website.destroy()
160         .then(function () {
161           return res.sendStatus(204);
162         })
163         .catch(function (err) {
164           return handleError(res, err);
165         });
166     })
167     .catch(function (err) {
168       return handleError(res, err);
169     });
170 };
171
172 // Sort Mail Applications
173 exports.sortApplications = function (req, res, next) {
174
175   ChatApplication
176     .findAll({
177       where: {
178         id: req.body.applications
179       }
180     })
181     .then(function (chatApplications) {
182       var tmpChatApplications = chatApplications;
183
184       async.waterfall([
185         function (callback) {
186           ChatApplication
187             .destroy({
188               where: {
189                 id: req.body.applications
190               }
191             }).then(function () {
192               callback();
193             })
194             .catch(function (err) {
195               callback(err);
196             });
197         },
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]
203             });
204             if (tmpChatApplication) {
205               tmpChatApplication.priority = i + 1;
206               sortedApplications.push(tmpChatApplication.dataValues);
207             }
208           }
209
210           ChatApplication
211             .bulkCreate(sortedApplications)
212             .then(function () {
213               callback();
214             })
215             .catch(function (err) {
216               callback(err);
217             });
218         },
219         function (callback) {
220           ChatApplication
221             .findAll({
222               where: {
223                 id: req.body.applications
224               },
225               order: 'priority',
226               include: [{
227                 all: true
228               }]
229             })
230             .then(function (chatApplications) {
231               callback(null, chatApplications);
232             })
233             .catch(function (err) {
234               callback(err);
235             });
236         }
237       ], function (err, result) {
238         if (err) {
239           return handleError(res, err);
240         } else {
241           return res.status(201).send(result);
242         }
243       });
244     })
245     .catch(function (err) {
246       return handleError(res, err);
247     });
248 };
249
250 // Deletes a agent from the DB.
251 exports.bulkDestroy = function (req, res) {
252   ChatWebsite
253     .destroy({
254       where: {
255         id: req.query.id
256       },
257       individualHooks: true
258     })
259     .then(function () {
260       return res.sendStatus(204);
261     })
262     .catch(function (err) {
263       return handleError(res, err);
264     });
265 };
266
267 function handleError(res, err) {
268   return res.status(500).send(err);
269 }