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