Built motion from commit 06df96e on branch develop.
[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   ChatWebsite
129     .find({
130       where: {
131         id: req.params.id
132       }
133     })
134     .then(function(chat_website) {
135       if (!chat_website) {
136         return res.sendStatus(404);
137       }
138       chat_website.updateAttributes(req.body)
139         .then(function(chat_website) {
140           return res.status(200).send(chat_website);
141         })
142         .catch(function(err) {
143           return handleError(res, err);
144         });
145       // var updated = _.merge(chat_website, req.body);
146       // updated.save()
147       //   .then(function() {
148       //     return res.status(200).send(chat_website);
149       //   })
150       //   .catch(function(err) {
151       //     return handleError(res, err);
152       //   });
153     })
154     .catch(function(err) {
155       return handleError(res, err);
156     });
157 };
158
159 // Deletes a chat_website from the DB.
160 exports.destroy = function(req, res) {
161   ChatWebsite
162     .find({
163       where: {
164         id: req.params.id
165       }
166     })
167     .then(function(chat_website) {
168       if (!chat_website) {
169         return res.sendStatus(404);
170       }
171       chat_website.destroy()
172         .then(function() {
173           return res.sendStatus(204);
174         })
175         .catch(function(err) {
176           return handleError(res, err);
177         });
178     })
179     .catch(function(err) {
180       return handleError(res, err);
181     });
182 };
183
184 // Sort Mail Applications
185 exports.sortApplications = function(req, res, next) {
186
187   ChatApplication
188     .findAll({
189       where: {
190         id: req.body.applications
191       }
192     })
193     .then(function(chatApplications) {
194       var tmpChatApplications = chatApplications;
195
196       async.waterfall([
197         function(callback) {
198           ChatApplication
199             .destroy({
200               where: {
201                 id: req.body.applications
202               }
203             }).then(function() {
204               callback();
205             })
206             .catch(function(err) {
207               callback(err);
208             });
209         },
210         function(callback) {
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]
215             });
216             if (tmpChatApplication) {
217               tmpChatApplication.priority = i + 1;
218               sortedApplications.push(tmpChatApplication.dataValues);
219             }
220           }
221
222           ChatApplication
223             .bulkCreate(sortedApplications)
224             .then(function() {
225               callback();
226             })
227             .catch(function(err) {
228               callback(err);
229             });
230         },
231         function(callback) {
232           ChatApplication
233             .findAll({
234               where: {
235                 id: req.body.applications
236               },
237               order: 'priority',
238               include: [{
239                 all: true
240               }]
241             })
242             .then(function(chatApplications) {
243               callback(null, chatApplications);
244             })
245             .catch(function(err) {
246               callback(err);
247             });
248         }
249       ], function(err, result) {
250         if (err) {
251           return handleError(res, err);
252         } else {
253           return res.status(201).send(result);
254         }
255       });
256     })
257     .catch(function(err) {
258       return handleError(res, err);
259     });
260 };
261
262 // Deletes a agent from the DB.
263 exports.bulkDestroy = function(req, res) {
264   ChatWebsite
265     .destroy({
266       where: {
267         id: req.query.id
268       },
269       individualHooks: true
270     })
271     .then(function() {
272       return res.sendStatus(204);
273     })
274     .catch(function(err) {
275       return handleError(res, err);
276     });
277 };
278
279 function handleError(res, err) {
280   return res.status(500).send(err);
281 }