Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / chat_message / chat_message.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var md5 = require('md5');
5
6 var User = require('../../models').User;
7 var ChatRoom = require('../../models').ChatRoom;
8 var ChatMessage = require('../../models').ChatMessage;
9 var ChatVisitor = require('../../models').ChatVisitor;
10
11 // Get list of chat_messages
12 exports.index = function (req, res) {
13   ChatMessage
14     .findAll()
15     .then(function (chat_messages) {
16       return res.status(200).send(chat_messages);
17     })
18     .catch(function (err) {
19       return handleError(res, err);
20     });
21 };
22
23 // Get a single chatMessage
24 exports.show = function (req, res) {
25   ChatMessage
26     .findById(req.params.id)
27     .then(function (chatMessage) {
28       if (!chatMessage) {
29         return res.sendStatus(404);
30       }
31       return res.send(chatMessage);
32     })
33     .catch(function (err) {
34       return handleError(res, err);
35     });
36 };
37
38 // Creates a new chatMessage in the DB.
39 exports.create = function (req, res, next) {
40   // if (req.body.ChatRoomId && req.body.to) {
41   //   return handleError(res, new Error('Select ChatRoomId or to'));
42   // } else if (req.body.to) {
43   //
44   //   switch (req.body.type) {
45   //     case 'internal':
46   //       User
47   //         .findOne({
48   //           where: {
49   //             $or: [{
50   //               id: {
51   //                 $like: req.body.to
52   //               }
53   //             }, {
54   //               name: req.body.to
55   //             }]
56   //           }
57   //         })
58   //         .then(function(user) {
59   //           if (user) {
60   //
61   //             var users = _.sortBy([user, req.user], 'id');
62   //             var participants = {
63   //               users: {
64   //                 ids: [users[0].id, users[1].id]
65   //               },
66   //               visitors: {
67   //                 ids: []
68   //               }
69   //             };
70   //
71   //             var token = md5(JSON.stringify(participants)).toString(
72   //               'base64');
73   //
74   //             participants.users.fullnames = [users[0].fullname, users[1].fullname];
75   //             participants.visitors.fullnames = [];
76   //
77   //             ChatRoom
78   //               .findOrCreate({
79   //                 where: {
80   //                   token: token
81   //                 },
82   //                 defaults: {
83   //                   type: req.body.type,
84   //                   token: token,
85   //                   participants: JSON.stringify(participants)
86   //                 }
87   //               })
88   //               .spread(function(chatRoom, created) {
89   //
90   //                 chatRoom
91   //                   .updateAttributes({
92   //                     participants: JSON.stringify(participants)
93   //                   });
94   //
95   //                 ChatMessage
96   //                   .create({
97   //                     body: req.body.body,
98   //                     fullname: req.user.fullname,
99   //                     email: req.user.email,
100   //                     ChatRoomId: chatRoom.id,
101   //                     userId: req.user.id
102   //                   })
103   //                   .then(function(chatMessage) {
104   //                     return res.status(201).send(chatMessage);
105   //                   })
106   //                   .catch(function(err) {
107   //                     return handleError(res, err);
108   //                   });
109   //               });
110   //
111   //           } else {
112   //             return handleError(res, new Error('User not found'));
113   //           }
114   //         })
115   //         .catch(function(err) {
116   //           return handleError(res, err);
117   //         });
118   //
119   //       break;
120   //     case 'external':
121   //       ChatVisitor
122   //         .findOne({
123   //           where: {
124   //             $or: [{
125   //               id: {
126   //                 $like: req.body.to
127   //               }
128   //             }, {
129   //               fullname: req.body.to
130   //             }]
131   //           }
132   //         })
133   //         .then(function(chatVisitor) {
134   //           if (chatVisitor) {
135   //
136   //             var participants = {
137   //               users: {
138   //                 ids: [req.user.id]
139   //               },
140   //               visitors: {
141   //                 ids: [chatVisitor.id]
142   //               }
143   //             };
144   //
145   //             var token = md5(JSON.stringify(participants)).toString(
146   //               'base64');
147   //
148   //             participants.users.fullnames = [req.user.fullname];
149   //             participants.visitors.fullnames = [chatVisitor.fullname];
150   //
151   //             ChatRoom
152   //               .findOrCreate({
153   //                 where: {
154   //                   token: token
155   //                 },
156   //                 defaults: {
157   //                   type: req.body.type,
158   //                   token: token,
159   //                   participants: JSON.stringify(participants)
160   //                 }
161   //               })
162   //               .spread(function(chatRoom, created) {
163   //
164   //                 chatRoom
165   //                   .updateAttributes({
166   //                     participants: JSON.stringify(participants)
167   //                   });
168   //
169   //                 ChatMessage
170   //                   .create({
171   //                     body: req.body.body,
172   //                     fullname: req.user.fullname,
173   //                     email: req.user.email,
174   //                     ChatRoomId: chatRoom.id,
175   //                     userId: req.user.id
176   //                   })
177   //                   .then(function(chatMessage) {
178   //                     return res.status(201).send(chatMessage);
179   //                   })
180   //                   .catch(function(err) {
181   //                     return handleError(res, err);
182   //                   });
183   //               });
184   //           }
185   //         });
186   //       break;
187   //     default:
188   //       return handleError(res, new Error('Room type unsupported'));
189   //   }
190   //
191   //
192   // } else if (req.body.ChatRoomId) {
193   //
194   //   ChatMessage
195   //     .create(_.merge({
196   //       userId: req.user.id,
197   //       email: req.user.email,
198   //       fullname: req.user.fullname
199   //     }, req.body))
200   //     .then(function(chatMessage) {
201   //       return res.status(201).send(chatMessage);
202   //     })
203   //     .catch(function(err) {
204   //       return handleError(res, err);
205   //     });
206   // } else {
207   //   return handleError(res, new Error('Select ChatRoomId or to'));
208   // }
209   ChatMessage
210     .create(_.merge(req.body, {
211       UserId: req.user.id
212     }))
213     .then(function (chatMessage) {
214       return res.status(201).send(chatMessage);
215     })
216     .catch(function (err) {
217       return handleError(res, err);
218     });
219 };
220
221 // Updates an existing chatMessage in the DB.
222 exports.update = function (req, res) {
223   if (req.body.id) {
224     delete req.body.id;
225   }
226
227   return ChatMessage
228     .findById(req.params.id)
229     .then(function (chatMessage) {
230       if (!chatMessage) {
231         return res.sendStatus(404);
232       }
233       var updated = _.merge(chatMessage, req.body);
234       return updated.save();
235     })
236     .then(function (chatMessage) {
237       return res.status(200).send(chatMessage);
238     })
239     .catch(function (err) {
240       return handleError(res, err);
241     });
242 };
243
244 // Deletes a chatMessage from the DB.
245 exports.destroy = function (req, res) {
246   ChatMessage
247     .findById(req.params.id)
248     .then(function (chatMessage) {
249       if (!chatMessage) {
250         return res.sendStatus(404);
251       }
252       chatMessage.destroy()
253         .then(function () {
254           return res.sendStatus(204);
255         })
256         .catch(function (err) {
257           return handleError(res, err);
258         });
259     })
260     .catch(function (err) {
261       return handleError(res, err);
262     });
263 };
264
265 function handleError(res, err) {
266   return res.status(500).send(err);
267 }