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']
44 .then(function (chatRooms) {
46 return res.sendStatus(404);
48 return res.send(chatRooms);
50 .catch(function (err) {
51 return handleError(res, err);
55 // Get a single chatRoom
56 exports.show = function (req, res) {
58 .findById(req.params.id, {
63 attributes: ['id', 'email', 'fullname']
66 attributes: ['id', 'email', 'name', 'fullname']
70 attributes: ['id', 'email', 'name', 'fullname']
73 .then(function (chatRoom) {
75 return res.sendStatus(404);
77 return res.send(chatRoom);
79 .catch(function (err) {
80 return handleError(res, err);
84 // Get a single chatRoom by users
85 exports.getRoomByUsers = function (req, res, next) {
92 token: md5(JSON.stringify(req.query.users.sort())).toString('base64')
96 token: md5(JSON.stringify(req.query.users.sort())).toString('base64'),
103 attributes: ['id', 'email', 'fullname']
106 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
110 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
113 .spread(function (chatRoom, created) {
114 _chatRoom = chatRoom;
115 // _chatRoom.dataValues.ChatMessages = [];
118 .setUsers(req.query.users);
127 .then(function (users) {
128 return res.status(200).send(_.merge(_chatRoom.dataValues, {
132 .catch(function (err) {
133 return handleError(res, err);
137 // Get a single chatRoom
138 exports.getRoomByType = function (req, res) {
140 .findById(req.user.id)
141 .then(function (user) {
145 type: req.params.type
149 attributes: ['id', 'fullname', 'email']
152 .then(function (chatRooms) {
154 return res.sendStatus(404);
156 return res.send(chatRooms);
158 .catch(function (err) {
159 return handleError(res, err);
162 .catch(function (err) {
163 return handleError(res, err);
167 // Get a single chatRoom
168 exports.getRoomGroups = function (req, res) {
170 .findById(req.user.id)
171 .then(function (user) {
176 attributes: ['id', 'fullname', 'email']
179 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
182 .then(function (chatRooms) {
184 return res.sendStatus(404);
186 return res.send(chatRooms);
188 .catch(function (err) {
189 return handleError(res, err);
192 .catch(function (err) {
193 return handleError(res, err);
197 // Creates a new chatRoom in the DB.
198 exports.create = function (req, res) {
199 // if (req.body.to) {
202 // var participants = {
209 // switch (req.body.type) {
212 // if (!req.body.to.isArray) {
213 // req.body.to = [parseInt(req.body.to, 10)];
216 // participants.users.ids = req.body.to;
218 // if (!_.contains(req.body.to, req.user.id)) {
219 // participants.users.ids.push(req.user.id);
222 // participants.users.ids.sort();
224 // token = md5(JSON.stringify(participants)).toString(
227 // participants.users.fullnames = [];
236 // attributes: ['id', 'fullname']
238 // .then(function(users) {
240 // _.sortBy(users, 'id').forEach(function(item, index) {
241 // participants.users.fullnames.push(item.fullname);
249 // defaults: _.merge({
251 // participants: JSON.stringify(participants)
254 // model: ChatMessage
257 // .spread(function(chatRoom, created) {
261 // .setUsers(participants.users.ids)
262 // .then(function() {
263 // return res.status(201).send(chatRoom);
265 // .catch(function(err) {
266 // return handleError(res, err);
269 // return res.status(201).send(chatRoom);
273 // .catch(function(err) {
274 // return handleError(res, err);
277 // .catch(function(err) {
278 // return handleError(res, err);
284 // participants.visitor.id = req.body.to;
285 // participants.users.ids.push(req.user.id);
286 // participants.users.ids.sort();
288 // token = md5(JSON.stringify(participants)).toString('base64');
290 // participants.users.fullnames = [req.user.fullname];
293 // .findById(req.body.to, {
294 // attributes: ['id', 'fullname', 'email', 'referer']
296 // .then(function(chatVisitor) {
297 // participants.visitor.fullname = chatVisitor.fullname;
298 // participants.visitor.referer = chatVisitor.referer;
299 // participants.visitor.email = chatVisitor.email;
306 // defaults: _.merge({
308 // participants: JSON.stringify(participants)
311 // model: ChatMessage
313 // model: ChatVisitor
318 // .spread(function(chatRoom, created) {
322 // .setUsers(participants.users.ids, {
323 // individualHooks: true
325 // .then(function() {
327 // .setChatVisitor(participants.visitor.id)
328 // .then(function() {
329 // return res.status(201).send(chatRoom);
331 // .catch(function(err) {
332 // return handleError(res, err);
335 // .catch(function(err) {
336 // return handleError(res, err);
339 // return res.status(201).send(chatRoom);
343 // .catch(function(err) {
344 // return handleError(res, err);
347 // .catch(function(err) {
348 // return handleError(res, err);
355 // return handleError(res, new Error('field "to" omitted'));
359 // Updates an existing chatRoom in the DB.
360 exports.update = function (req, res) {
366 .findById(req.params.id)
367 .then(function (chatRoom) {
369 return res.sendStatus(404);
371 var updated = _.merge(chatRoom, req.body);
375 return res.status(200).send(chatRoom);
377 .catch(function (err) {
378 return handleError(res, err);
381 .catch(function (err) {
382 return handleError(res, err);
386 // Updates an existing chatRoom in the DB.
387 exports.updateUsers = function (req, res) {
395 .findById(req.params.id)
396 .then(function (chatRoom) {
397 if (!chatRoom || !req.body.users) {
398 return res.sendStatus(404);
402 .then(function (chatRoom) {
405 token: md5(JSON.stringify(req.body.users.sort())).toString('base64')
408 .then(function (chatRoom) {
409 _chatRoom = chatRoom;
411 .setUsers(req.body.users.sort(), {
412 individualHooks: true
419 .then(function (users) {
420 return res.status(200).send(_.merge(_chatRoom.dataValues, {
425 .catch(function (err) {
426 return handleError(res, err);
430 // Deletes a chatRoom from the DB.
431 exports.destroy = function (req, res) {
433 .findById(req.params.id)
434 .then(function (chatRoom) {
436 return res.sendStatus(404);
442 return res.sendStatus(204);
444 .catch(function (err) {
445 return handleError(res, err);
448 .catch(function (err) {
449 return handleError(res, err);
453 function handleError(res, err) {
454 return res.status(500).send(err);