e423038413e5c40726015be97a0a572bb6309b3e
[motion.git] / server / api / chat_room / chat_room.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var md5 = require('md5');
5 var moment = require('moment');
6
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;
12
13
14 // Get list of chat_rooms
15 exports.index = function (req, res) {
16   return ChatRoom
17     .findAll({
18       include: [User, ChatMessage, ChatVisitor]
19     })
20     .then(function (chat_rooms) {
21       return res.status(200).send(chat_rooms);
22     })
23     .catch(function (err) {
24       return handleError(res, err);
25     });
26 };
27
28 // Get list of my chat_rooms
29 exports.me = function (req, res) {
30   return User
31     .findById(req.user.id)
32     .then(function (user) {
33       return user
34         .getChatRooms({
35           include: [{
36             model: ChatVisitor,
37             attributes: ['id', 'fullname', 'email']
38           }, {
39             model: User,
40             attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
41           }]
42         });
43     })
44     .then(function (chatRooms) {
45       if (!chatRooms) {
46         return res.sendStatus(404);
47       }
48       return res.send(chatRooms);
49     })
50     .catch(function (err) {
51       return handleError(res, err);
52     });
53 };
54
55 // Get a single chatRoom
56 exports.show = function (req, res) {
57   return ChatRoom
58     .findById(req.params.id, {
59       include: [{
60         model: ChatMessage,
61         include: [{
62           model: ChatVisitor,
63           attributes: ['id', 'email', 'fullname']
64         }, {
65           model: User,
66           attributes: ['id', 'email', 'name', 'fullname']
67         }]
68       }, {
69         model: User,
70         attributes: ['id', 'email', 'name', 'fullname']
71       }]
72     })
73     .then(function (chatRoom) {
74       if (!chatRoom) {
75         return res.sendStatus(404);
76       }
77       return res.send(chatRoom);
78     })
79     .catch(function (err) {
80       return handleError(res, err);
81     });
82 };
83
84 // Get a single chatRoom by users
85 exports.getRoomByUsers = function (req, res, next) {
86
87   var _chatRoom;
88
89   return ChatRoom
90     .findOrCreate({
91       where: {
92         token: md5(JSON.stringify(req.query.users.sort())).toString('base64')
93       },
94       defaults: {
95         type: 'internal',
96         token: md5(JSON.stringify(req.query.users.sort())).toString('base64'),
97         status: 'open'
98       },
99       include: [{
100         model: ChatMessage,
101         include: [{
102           model: ChatVisitor,
103           attributes: ['id', 'email', 'fullname']
104         }, {
105           model: User,
106           attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
107         }]
108       }, {
109         model: User,
110         attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
111       }]
112     })
113     .spread(function (chatRoom, created) {
114       _chatRoom = chatRoom;
115       // _chatRoom.dataValues.ChatMessages = [];
116       if (created) {
117         return _chatRoom
118           .setUsers(req.query.users);
119       } else {
120         return;
121       }
122     })
123     .then(function () {
124       return _chatRoom
125         .getUsers();
126     })
127     .then(function (users) {
128       return res.status(200).send(_.merge(_chatRoom.dataValues, {
129         Users: users
130       }));
131     })
132     .catch(function (err) {
133       return handleError(res, err);
134     });
135 };
136
137 // Get a single chatRoom
138 exports.getRoomByType = function (req, res) {
139   User
140     .findById(req.user.id)
141     .then(function (user) {
142       user
143         .getChatRooms({
144           where: {
145             type: req.params.type
146           },
147           include: [{
148             model: ChatVisitor,
149             attributes: ['id', 'fullname', 'email']
150           }]
151         })
152         .then(function (chatRooms) {
153           if (!chatRooms) {
154             return res.sendStatus(404);
155           }
156           return res.send(chatRooms);
157         })
158         .catch(function (err) {
159           return handleError(res, err);
160         });
161     })
162     .catch(function (err) {
163       return handleError(res, err);
164     });
165 };
166
167 // Get a single chatRoom
168 exports.getRoomGroups = function (req, res) {
169   User
170     .findById(req.user.id)
171     .then(function (user) {
172       user
173         .getChatRooms({
174           include: [{
175             model: ChatVisitor,
176             attributes: ['id', 'fullname', 'email']
177           }, {
178             model: User,
179             attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
180           }]
181         })
182         .then(function (chatRooms) {
183           if (!chatRooms) {
184             return res.sendStatus(404);
185           }
186           return res.send(chatRooms);
187         })
188         .catch(function (err) {
189           return handleError(res, err);
190         });
191     })
192     .catch(function (err) {
193       return handleError(res, err);
194     });
195 };
196
197 // Creates a new chatRoom in the DB.
198 exports.create = function (req, res) {
199   // if (req.body.to) {
200   //
201   //   var token;
202   //   var participants = {
203   //     users: {
204   //       ids: []
205   //     },
206   //     visitor: {}
207   //   };
208   //
209   //   switch (req.body.type) {
210   //     case 'internal':
211   //
212   //       if (!req.body.to.isArray) {
213   //         req.body.to = [parseInt(req.body.to, 10)];
214   //       }
215   //
216   //       participants.users.ids = req.body.to;
217   //
218   //       if (!_.contains(req.body.to, req.user.id)) {
219   //         participants.users.ids.push(req.user.id);
220   //       }
221   //
222   //       participants.users.ids.sort();
223   //
224   //       token = md5(JSON.stringify(participants)).toString(
225   //         'base64');
226   //
227   //       participants.users.fullnames = [];
228   //
229   //       User
230   //         .findAll({
231   //           where: {
232   //             id: {
233   //               $in: req.body.to
234   //             }
235   //           },
236   //           attributes: ['id', 'fullname']
237   //         })
238   //         .then(function(users) {
239   //
240   //           _.sortBy(users, 'id').forEach(function(item, index) {
241   //             participants.users.fullnames.push(item.fullname);
242   //           });
243   //
244   //           ChatRoom
245   //             .findOrCreate({
246   //               where: {
247   //                 token: token
248   //               },
249   //               defaults: _.merge({
250   //                 token: token,
251   //                 participants: JSON.stringify(participants)
252   //               }, req.body),
253   //               include: [{
254   //                 model: ChatMessage
255   //               }]
256   //             })
257   //             .spread(function(chatRoom, created) {
258   //
259   //               if (created) {
260   //                 chatRoom
261   //                   .setUsers(participants.users.ids)
262   //                   .then(function() {
263   //                     return res.status(201).send(chatRoom);
264   //                   })
265   //                   .catch(function(err) {
266   //                     return handleError(res, err);
267   //                   });
268   //               } else {
269   //                 return res.status(201).send(chatRoom);
270   //               }
271   //
272   //             })
273   //             .catch(function(err) {
274   //               return handleError(res, err);
275   //             });
276   //         })
277   //         .catch(function(err) {
278   //           return handleError(res, err);
279   //         });
280   //
281   //       break;
282   //     case 'external':
283   //
284   //       participants.visitor.id = req.body.to;
285   //       participants.users.ids.push(req.user.id);
286   //       participants.users.ids.sort();
287   //
288   //       token = md5(JSON.stringify(participants)).toString('base64');
289   //
290   //       participants.users.fullnames = [req.user.fullname];
291   //
292   //       ChatVisitor
293   //         .findById(req.body.to, {
294   //           attributes: ['id', 'fullname', 'email', 'referer']
295   //         })
296   //         .then(function(chatVisitor) {
297   //           participants.visitor.fullname = chatVisitor.fullname;
298   //           participants.visitor.referer = chatVisitor.referer;
299   //           participants.visitor.email = chatVisitor.email;
300   //
301   //           ChatRoom
302   //             .findOrCreate({
303   //               where: {
304   //                 token: token
305   //               },
306   //               defaults: _.merge({
307   //                 token: token,
308   //                 participants: JSON.stringify(participants)
309   //               }, req.body),
310   //               include: [{
311   //                 model: ChatMessage
312   //               }, {
313   //                 model: ChatVisitor
314   //               }, {
315   //                 model: User,
316   //               }]
317   //             })
318   //             .spread(function(chatRoom, created) {
319   //               if (created) {
320   //
321   //                 chatRoom
322   //                   .setUsers(participants.users.ids, {
323   //                     individualHooks: true
324   //                   })
325   //                   .then(function() {
326   //                     chatRoom
327   //                       .setChatVisitor(participants.visitor.id)
328   //                       .then(function() {
329   //                         return res.status(201).send(chatRoom);
330   //                       })
331   //                       .catch(function(err) {
332   //                         return handleError(res, err);
333   //                       });
334   //                   })
335   //                   .catch(function(err) {
336   //                     return handleError(res, err);
337   //                   });
338   //               } else {
339   //                 return res.status(201).send(chatRoom);
340   //               }
341   //
342   //             })
343   //             .catch(function(err) {
344   //               return handleError(res, err);
345   //             });
346   //         })
347   //         .catch(function(err) {
348   //           return handleError(res, err);
349   //         });
350   //
351   //       break;
352   //     default:
353   //   }
354   // } else {
355   //   return handleError(res, new Error('field "to" omitted'));
356   // }
357 };
358
359 // Updates an existing chatRoom in the DB.
360 exports.update = function (req, res) {
361   if (req.body.id) {
362     delete req.body.id;
363   }
364
365   ChatRoom
366     .findById(req.params.id)
367     .then(function (chatRoom) {
368       if (!chatRoom) {
369         return res.sendStatus(404);
370       }
371       var updated = _.merge(chatRoom, req.body);
372       updated
373         .save()
374         .then(function () {
375           return res.status(200).send(chatRoom);
376         })
377         .catch(function (err) {
378           return handleError(res, err);
379         });
380     })
381     .catch(function (err) {
382       return handleError(res, err);
383     });
384 };
385
386 // Updates an existing chatRoom in the DB.
387 exports.updateUsers = function (req, res) {
388   var _chatRoom;
389
390   if (req.body.id) {
391     delete req.body.id;
392   }
393
394   return ChatRoom
395     .findById(req.params.id)
396     .then(function (chatRoom) {
397       if (!chatRoom || !req.body.users) {
398         return res.sendStatus(404);
399       }
400       return chatRoom;
401     })
402     .then(function (chatRoom) {
403       return chatRoom
404         .update({
405           token: md5(JSON.stringify(req.body.users.sort())).toString('base64')
406         });
407     })
408     .then(function (chatRoom) {
409       _chatRoom = chatRoom;
410       return chatRoom
411         .setUsers(req.body.users.sort(), {
412           individualHooks: true
413         });
414     })
415     .then(function () {
416       return _chatRoom
417         .getUsers();
418     })
419     .then(function (users) {
420       return res.status(200).send(_.merge(_chatRoom.dataValues, {
421         Users: users,
422         ChatMessages: []
423       }));
424     })
425     .catch(function (err) {
426       return handleError(res, err);
427     });
428 };
429
430 // Deletes a chatRoom from the DB.
431 exports.destroy = function (req, res) {
432   ChatRoom
433     .findById(req.params.id)
434     .then(function (chatRoom) {
435       if (!chatRoom) {
436         return res.sendStatus(404);
437       }
438
439       chatRoom
440         .destroy()
441         .then(function () {
442           return res.sendStatus(204);
443         })
444         .catch(function (err) {
445           return handleError(res, err);
446         });
447     })
448     .catch(function (err) {
449       return handleError(res, err);
450     });
451 };
452
453 function handleError(res, err) {
454   return res.status(500).send(err);
455 }