3 var _ = require('lodash');
4 var md5 = require('md5');
5 var moment = require('moment');
7 var User = require('../../models').User;
8 var ChatRoom = require('../../models').ChatRoom;
9 var ChatMessage = require('../../models').ChatMessage;
10 var ChatVisitor = require('../../models').ChatVisitor;
11 var UserHasChatRoom = require('../../models').UserHasChatRoom;
14 // Get list of chat_rooms
15 exports.index = function(req, res) {
18 include: [User, ChatMessage, ChatVisitor]
20 .then(function(chat_rooms) {
21 return res.status(200).send(chat_rooms);
23 .catch(function(err) {
24 return handleError(res, err);
28 // Get list of my chat_rooms
29 exports.me = function(req, res) {
31 .findById(req.user.id)
32 .then(function(user) {
37 attributes: ['id', 'fullname', 'email']
40 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
43 .then(function(chatRooms) {
45 return res.sendStatus(404);
47 return res.send(chatRooms);
49 .catch(function(err) {
50 return handleError(res, err);
53 .catch(function(err) {
54 return handleError(res, err);
58 // Get a single chatRoom
59 exports.show = function(req, res) {
61 .findById(req.params.id, {
70 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
73 attributes: ['id', 'fullname', 'email']
77 .then(function(chatRoom) {
79 return res.sendStatus(404);
81 return res.send(chatRoom);
83 .catch(function(err) {
84 return handleError(res, err);
88 // Get a single chatRoom by users
89 exports.getRoomByUsers = function(req, res, next) {
93 token: md5(JSON.stringify(req.query.users.sort())).toString('base64')
97 token: md5(JSON.stringify(req.query.users.sort())).toString('base64'),
104 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
108 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
111 .spread(function(chatRoom, created) {
114 .setUsers(req.query.users)
118 .then(function(users) {
119 return res.status(200).send(_.merge(chatRoom.dataValues, {
124 .catch(function(err) {
125 return handleError(res, err);
128 .catch(function(err) {
129 return handleError(res, err);
132 return res.status(200).send(chatRoom);
135 .catch(function(err) {
136 return handleError(res, err);
140 // Get a single chatRoom
141 exports.getRoomByType = function(req, res) {
143 .findById(req.user.id)
144 .then(function(user) {
148 type: req.params.type
152 attributes: ['id', 'fullname', 'email']
155 .then(function(chatRooms) {
157 return res.sendStatus(404);
159 return res.send(chatRooms);
161 .catch(function(err) {
162 return handleError(res, err);
165 .catch(function(err) {
166 return handleError(res, err);
170 // Get a single chatRoom
171 exports.getRoomGroups = function(req, res) {
173 .findById(req.user.id)
174 .then(function(user) {
179 attributes: ['id', 'fullname', 'email']
182 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
185 .then(function(chatRooms) {
187 return res.sendStatus(404);
189 return res.send(chatRooms);
191 .catch(function(err) {
192 return handleError(res, err);
195 .catch(function(err) {
196 return handleError(res, err);
200 // Creates a new chatRoom in the DB.
201 exports.create = function(req, res) {
202 // if (req.body.to) {
205 // var participants = {
212 // switch (req.body.type) {
215 // if (!req.body.to.isArray) {
216 // req.body.to = [parseInt(req.body.to, 10)];
219 // participants.users.ids = req.body.to;
221 // if (!_.contains(req.body.to, req.user.id)) {
222 // participants.users.ids.push(req.user.id);
225 // participants.users.ids.sort();
227 // token = md5(JSON.stringify(participants)).toString(
230 // participants.users.fullnames = [];
239 // attributes: ['id', 'fullname']
241 // .then(function(users) {
243 // _.sortBy(users, 'id').forEach(function(item, index) {
244 // participants.users.fullnames.push(item.fullname);
252 // defaults: _.merge({
254 // participants: JSON.stringify(participants)
257 // model: ChatMessage
260 // .spread(function(chatRoom, created) {
264 // .setUsers(participants.users.ids)
265 // .then(function() {
266 // return res.status(201).send(chatRoom);
268 // .catch(function(err) {
269 // return handleError(res, err);
272 // return res.status(201).send(chatRoom);
276 // .catch(function(err) {
277 // return handleError(res, err);
280 // .catch(function(err) {
281 // return handleError(res, err);
287 // participants.visitor.id = req.body.to;
288 // participants.users.ids.push(req.user.id);
289 // participants.users.ids.sort();
291 // token = md5(JSON.stringify(participants)).toString('base64');
293 // participants.users.fullnames = [req.user.fullname];
296 // .findById(req.body.to, {
297 // attributes: ['id', 'fullname', 'email', 'referer']
299 // .then(function(chatVisitor) {
300 // participants.visitor.fullname = chatVisitor.fullname;
301 // participants.visitor.referer = chatVisitor.referer;
302 // participants.visitor.email = chatVisitor.email;
309 // defaults: _.merge({
311 // participants: JSON.stringify(participants)
314 // model: ChatMessage
316 // model: ChatVisitor
321 // .spread(function(chatRoom, created) {
325 // .setUsers(participants.users.ids, {
326 // individualHooks: true
328 // .then(function() {
330 // .setChatVisitor(participants.visitor.id)
331 // .then(function() {
332 // return res.status(201).send(chatRoom);
334 // .catch(function(err) {
335 // return handleError(res, err);
338 // .catch(function(err) {
339 // return handleError(res, err);
342 // return res.status(201).send(chatRoom);
346 // .catch(function(err) {
347 // return handleError(res, err);
350 // .catch(function(err) {
351 // return handleError(res, err);
358 // return handleError(res, new Error('field "to" omitted'));
362 // Updates an existing chatRoom in the DB.
363 exports.update = function(req, res) {
369 .findById(req.params.id)
370 .then(function(chatRoom) {
372 return res.sendStatus(404);
374 var updated = _.merge(chatRoom, req.body);
378 return res.status(200).send(chatRoom);
380 .catch(function(err) {
381 return handleError(res, err);
384 .catch(function(err) {
385 return handleError(res, err);
389 // Updates an existing chatRoom in the DB.
390 exports.updateUsers = function(req, res) {
396 .findById(req.params.id)
397 .then(function(chatRoom) {
399 return res.sendStatus(404);
403 token: md5(JSON.stringify(req.body.users.sort())).toString('base64')
405 .then(function(chatRoom) {
407 .setUsers(req.body.users.sort())
411 .then(function(users) {
412 return res.status(200).send(_.merge(chatRoom.dataValues, {
417 .catch(function(err) {
418 return handleError(res, err);
421 .catch(function(err) {
422 return handleError(res, err);
425 .catch(function(err) {
426 return handleError(res, err);
429 .catch(function(err) {
430 return handleError(res, err);
434 // Deletes a chatRoom from the DB.
435 exports.destroy = function(req, res) {
437 .findById(req.params.id)
438 .then(function(chatRoom) {
440 return res.sendStatus(404);
446 return res.sendStatus(204);
448 .catch(function(err) {
449 return handleError(res, err);
452 .catch(function(err) {
453 return handleError(res, err);
457 function handleError(res, err) {
458 return res.status(500).send(err);