Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / chat_room / chat_room.controller.js
index f8c0799..2b76e17 100644 (file)
@@ -1 +1,462 @@
-var _0x68fc=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x6D\x64\x35","\x6D\x6F\x6D\x65\x6E\x74","\x55\x73\x65\x72","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x43\x68\x61\x74\x52\x6F\x6F\x6D","\x43\x68\x61\x74\x4D\x65\x73\x73\x61\x67\x65","\x43\x68\x61\x74\x56\x69\x73\x69\x74\x6F\x72","\x55\x73\x65\x72\x48\x61\x73\x43\x68\x61\x74\x52\x6F\x6F\x6D","\x69\x6E\x64\x65\x78","\x63\x61\x74\x63\x68","\x73\x65\x6E\x64","\x73\x74\x61\x74\x75\x73","\x74\x68\x65\x6E","\x69\x64","\x6E\x61\x6D\x65","\x66\x75\x6C\x6C\x6E\x61\x6D\x65","\x65\x6D\x61\x69\x6C","\x72\x6F\x6C\x65","\x69\x6E\x74\x65\x72\x6E\x61\x6C","\x66\x69\x6E\x64\x41\x6C\x6C","\x6D\x65","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\x67\x65\x74\x43\x68\x61\x74\x52\x6F\x6F\x6D\x73","\x75\x73\x65\x72","\x66\x69\x6E\x64\x42\x79\x49\x64","\x73\x68\x6F\x77","\x70\x61\x72\x61\x6D\x73","\x67\x65\x74\x52\x6F\x6F\x6D\x42\x79\x55\x73\x65\x72\x73","\x64\x61\x74\x61\x56\x61\x6C\x75\x65\x73","\x6D\x65\x72\x67\x65","\x67\x65\x74\x55\x73\x65\x72\x73","\x75\x73\x65\x72\x73","\x71\x75\x65\x72\x79","\x73\x65\x74\x55\x73\x65\x72\x73","\x73\x70\x72\x65\x61\x64","\x62\x61\x73\x65\x36\x34","\x73\x6F\x72\x74","\x73\x74\x72\x69\x6E\x67\x69\x66\x79","\x6F\x70\x65\x6E","\x66\x69\x6E\x64\x4F\x72\x43\x72\x65\x61\x74\x65","\x67\x65\x74\x52\x6F\x6F\x6D\x42\x79\x54\x79\x70\x65","\x74\x79\x70\x65","\x67\x65\x74\x52\x6F\x6F\x6D\x47\x72\x6F\x75\x70\x73","\x63\x72\x65\x61\x74\x65","\x75\x70\x64\x61\x74\x65","\x62\x6F\x64\x79","\x73\x61\x76\x65","\x75\x70\x64\x61\x74\x65\x55\x73\x65\x72\x73","\x64\x65\x73\x74\x72\x6F\x79"];_0x68fc[0];var _=require(_0x68fc[1]);var md5=require(_0x68fc[2]);var moment=require(_0x68fc[3]);var User=require(_0x68fc[5])[_0x68fc[4]];var ChatRoom=require(_0x68fc[5])[_0x68fc[6]];var ChatMessage=require(_0x68fc[5])[_0x68fc[7]];var ChatVisitor=require(_0x68fc[5])[_0x68fc[8]];var UserHasChatRoom=require(_0x68fc[5])[_0x68fc[9]];exports[_0x68fc[10]]=function(_0xdcf7x9,_0xdcf7xa){return ChatRoom[_0x68fc[21]]({include:[{model:User,attributes:[_0x68fc[15],_0x68fc[16],_0x68fc[17],_0x68fc[18],_0x68fc[19],_0x68fc[20]]},{model:ChatMessage},{model:ChatVisitor}]})[_0x68fc[14]](function(_0xdcf7xc){return _0xdcf7xa[_0x68fc[13]](200)[_0x68fc[12]](_0xdcf7xc)})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};exports[_0x68fc[22]]=function(_0xdcf7x9,_0xdcf7xa){return User[_0x68fc[26]](_0xdcf7x9[_0x68fc[25]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xe){return _0xdcf7xe[_0x68fc[24]]({include:[{model:ChatVisitor,attributes:[_0x68fc[15],_0x68fc[17],_0x68fc[18]]},{model:User,attributes:[_0x68fc[15],_0x68fc[16],_0x68fc[17],_0x68fc[18],_0x68fc[19],_0x68fc[20]]}]})})[_0x68fc[14]](function(_0xdcf7xd){if(!_0xdcf7xd){return _0xdcf7xa[_0x68fc[23]](404)};return _0xdcf7xa[_0x68fc[12]](_0xdcf7xd);})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};exports[_0x68fc[27]]=function(_0xdcf7x9,_0xdcf7xa){return ChatRoom[_0x68fc[26]](_0xdcf7x9[_0x68fc[28]][_0x68fc[15]],{include:[{model:ChatMessage,include:[{model:ChatVisitor,attributes:[_0x68fc[15],_0x68fc[18],_0x68fc[17]]},{model:User,attributes:[_0x68fc[15],_0x68fc[18],_0x68fc[16],_0x68fc[17]]}]},{model:User,attributes:[_0x68fc[15],_0x68fc[18],_0x68fc[16],_0x68fc[17]]}]})[_0x68fc[14]](function(_0xdcf7xf){if(!_0xdcf7xf){return _0xdcf7xa[_0x68fc[23]](404)};return _0xdcf7xa[_0x68fc[12]](_0xdcf7xf);})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};exports[_0x68fc[29]]=function(_0xdcf7x9,_0xdcf7xa,_0xdcf7x10){var _0xdcf7x11;return ChatRoom[_0x68fc[41]]({where:{token:md5(JSON[_0x68fc[39]](_0xdcf7x9[_0x68fc[34]][_0x68fc[33]][_0x68fc[38]]())).toString(_0x68fc[37])},defaults:{type:_0x68fc[20],token:md5(JSON[_0x68fc[39]](_0xdcf7x9[_0x68fc[34]][_0x68fc[33]][_0x68fc[38]]())).toString(_0x68fc[37]),status:_0x68fc[40]},include:[{model:ChatMessage,include:[{model:ChatVisitor,attributes:[_0x68fc[15],_0x68fc[18],_0x68fc[17]]},{model:User,attributes:[_0x68fc[15],_0x68fc[16],_0x68fc[17],_0x68fc[18],_0x68fc[19],_0x68fc[20]]}]},{model:User,attributes:[_0x68fc[15],_0x68fc[16],_0x68fc[17],_0x68fc[18],_0x68fc[19],_0x68fc[20]]}]})[_0x68fc[36]](function(_0xdcf7xf,_0xdcf7x13){_0xdcf7x11=_0xdcf7xf;if(_0xdcf7x13){return _0xdcf7x11[_0x68fc[35]](_0xdcf7x9[_0x68fc[34]][_0x68fc[33]])}else {return };})[_0x68fc[14]](function(){return _0xdcf7x11[_0x68fc[32]]()})[_0x68fc[14]](function(_0xdcf7x12){return _0xdcf7xa[_0x68fc[13]](200)[_0x68fc[12]](_[_0x68fc[31]](_0xdcf7x11[_0x68fc[30]],{Users:_0xdcf7x12}))})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)});};exports[_0x68fc[42]]=function(_0xdcf7x9,_0xdcf7xa){User[_0x68fc[26]](_0xdcf7x9[_0x68fc[25]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xe){_0xdcf7xe[_0x68fc[24]]({where:{type:_0xdcf7x9[_0x68fc[28]][_0x68fc[43]]},include:[{model:ChatVisitor,attributes:[_0x68fc[15],_0x68fc[17],_0x68fc[18]]}]})[_0x68fc[14]](function(_0xdcf7xd){if(!_0xdcf7xd){return _0xdcf7xa[_0x68fc[23]](404)};return _0xdcf7xa[_0x68fc[12]](_0xdcf7xd);})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};exports[_0x68fc[44]]=function(_0xdcf7x9,_0xdcf7xa){User[_0x68fc[26]](_0xdcf7x9[_0x68fc[25]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xe){_0xdcf7xe[_0x68fc[24]]({include:[{model:ChatVisitor,attributes:[_0x68fc[15],_0x68fc[17],_0x68fc[18]]},{model:User,attributes:[_0x68fc[15],_0x68fc[16],_0x68fc[17],_0x68fc[18],_0x68fc[19],_0x68fc[20]]}]})[_0x68fc[14]](function(_0xdcf7xd){if(!_0xdcf7xd){return _0xdcf7xa[_0x68fc[23]](404)};return _0xdcf7xa[_0x68fc[12]](_0xdcf7xd);})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};exports[_0x68fc[45]]=function(_0xdcf7x9,_0xdcf7xa){};exports[_0x68fc[46]]=function(_0xdcf7x9,_0xdcf7xa){if(_0xdcf7x9[_0x68fc[47]][_0x68fc[15]]){delete _0xdcf7x9[_0x68fc[47]][_0x68fc[15]]};ChatRoom[_0x68fc[26]](_0xdcf7x9[_0x68fc[28]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xf){if(!_0xdcf7xf){return _0xdcf7xa[_0x68fc[23]](404)};var _0xdcf7x14=_[_0x68fc[31]](_0xdcf7xf,_0xdcf7x9[_0x68fc[47]]);_0xdcf7x14[_0x68fc[48]]()[_0x68fc[14]](function(){return _0xdcf7xa[_0x68fc[13]](200)[_0x68fc[12]](_0xdcf7xf)})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)});})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)});};exports[_0x68fc[49]]=function(_0xdcf7x9,_0xdcf7xa){var _0xdcf7x11;if(_0xdcf7x9[_0x68fc[47]][_0x68fc[15]]){delete _0xdcf7x9[_0x68fc[47]][_0x68fc[15]]};return ChatRoom[_0x68fc[26]](_0xdcf7x9[_0x68fc[28]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xf){if(!_0xdcf7xf||!_0xdcf7x9[_0x68fc[47]][_0x68fc[33]]){return _0xdcf7xa[_0x68fc[23]](404)};return _0xdcf7xf;})[_0x68fc[14]](function(_0xdcf7xf){return _0xdcf7xf[_0x68fc[46]]({token:md5(JSON[_0x68fc[39]](_0xdcf7x9[_0x68fc[47]][_0x68fc[33]][_0x68fc[38]]())).toString(_0x68fc[37])})})[_0x68fc[14]](function(_0xdcf7xf){_0xdcf7x11=_0xdcf7xf;return _0xdcf7xf[_0x68fc[35]](_0xdcf7x9[_0x68fc[47]][_0x68fc[33]][_0x68fc[38]](),{individualHooks:true});})[_0x68fc[14]](function(){return _0xdcf7x11[_0x68fc[32]]()})[_0x68fc[14]](function(_0xdcf7x12){return _0xdcf7xa[_0x68fc[13]](200)[_0x68fc[12]](_[_0x68fc[31]](_0xdcf7x11[_0x68fc[30]],{Users:_0xdcf7x12,ChatMessages:[]}))})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)});};exports[_0x68fc[50]]=function(_0xdcf7x9,_0xdcf7xa){ChatRoom[_0x68fc[26]](_0xdcf7x9[_0x68fc[28]][_0x68fc[15]])[_0x68fc[14]](function(_0xdcf7xf){if(!_0xdcf7xf){return _0xdcf7xa[_0x68fc[23]](404)};_0xdcf7xf[_0x68fc[50]]()[_0x68fc[14]](function(){return _0xdcf7xa[_0x68fc[23]](204)})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)});})[_0x68fc[11]](function(_0xdcf7xb){return handleError(_0xdcf7xa,_0xdcf7xb)})};function handleError(_0xdcf7xa,_0xdcf7xb){return _0xdcf7xa[_0x68fc[13]](500)[_0x68fc[12]](_0xdcf7xb)}
\ No newline at end of file
+'use strict';
+
+var _ = require('lodash');
+var md5 = require('md5');
+var moment = require('moment');
+
+var User = require('../../models').User;
+var ChatRoom = require('../../models').ChatRoom;
+var ChatMessage = require('../../models').ChatMessage;
+var ChatVisitor = require('../../models').ChatVisitor;
+var UserHasChatRoom = require('../../models').UserHasChatRoom;
+
+
+// Get list of chat_rooms
+exports.index = function (req, res) {
+  return ChatRoom
+    .findAll({
+      include: [{
+        model: User,
+        attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
+      }, {
+        model: ChatMessage
+      }, {
+        model: ChatVisitor
+      }]
+    })
+    .then(function (chat_rooms) {
+      return res.status(200).send(chat_rooms);
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Get list of my chat_rooms
+exports.me = function (req, res) {
+  return User
+    .findById(req.user.id)
+    .then(function (user) {
+      return user
+        .getChatRooms({
+          include: [{
+            model: ChatVisitor,
+            attributes: ['id', 'fullname', 'email']
+          }, {
+            model: User,
+            attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
+          }]
+        });
+    })
+    .then(function (chatRooms) {
+      if (!chatRooms) {
+        return res.sendStatus(404);
+      }
+      return res.send(chatRooms);
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Get a single chatRoom
+exports.show = function (req, res) {
+  return ChatRoom
+    .findById(req.params.id, {
+      include: [{
+        model: ChatMessage,
+        include: [{
+          model: ChatVisitor,
+          attributes: ['id', 'email', 'fullname']
+        }, {
+          model: User,
+          attributes: ['id', 'email', 'name', 'fullname']
+        }]
+      }, {
+        model: User,
+        attributes: ['id', 'email', 'name', 'fullname']
+      }]
+    })
+    .then(function (chatRoom) {
+      if (!chatRoom) {
+        return res.sendStatus(404);
+      }
+      return res.send(chatRoom);
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Get a single chatRoom by users
+exports.getRoomByUsers = function (req, res, next) {
+
+  var _chatRoom;
+
+  return ChatRoom
+    .findOrCreate({
+      where: {
+        token: md5(JSON.stringify(req.query.users.sort())).toString('base64')
+      },
+      defaults: {
+        type: 'internal',
+        token: md5(JSON.stringify(req.query.users.sort())).toString('base64'),
+        status: 'open'
+      },
+      include: [{
+        model: ChatMessage,
+        include: [{
+          model: ChatVisitor,
+          attributes: ['id', 'email', 'fullname']
+        }, {
+          model: User,
+          attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
+        }]
+      }, {
+        model: User,
+        attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
+      }]
+    })
+    .spread(function (chatRoom, created) {
+      _chatRoom = chatRoom;
+      // _chatRoom.dataValues.ChatMessages = [];
+      if (created) {
+        return _chatRoom
+          .setUsers(req.query.users);
+      } else {
+        return;
+      }
+    })
+    .then(function () {
+      return _chatRoom
+        .getUsers();
+    })
+    .then(function (users) {
+      return res.status(200).send(_.merge(_chatRoom.dataValues, {
+        Users: users
+      }));
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Get a single chatRoom
+exports.getRoomByType = function (req, res) {
+  User
+    .findById(req.user.id)
+    .then(function (user) {
+      user
+        .getChatRooms({
+          where: {
+            type: req.params.type
+          },
+          include: [{
+            model: ChatVisitor,
+            attributes: ['id', 'fullname', 'email']
+          }]
+        })
+        .then(function (chatRooms) {
+          if (!chatRooms) {
+            return res.sendStatus(404);
+          }
+          return res.send(chatRooms);
+        })
+        .catch(function (err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Get a single chatRoom
+exports.getRoomGroups = function (req, res) {
+  User
+    .findById(req.user.id)
+    .then(function (user) {
+      user
+        .getChatRooms({
+          include: [{
+            model: ChatVisitor,
+            attributes: ['id', 'fullname', 'email']
+          }, {
+            model: User,
+            attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
+          }]
+        })
+        .then(function (chatRooms) {
+          if (!chatRooms) {
+            return res.sendStatus(404);
+          }
+          return res.send(chatRooms);
+        })
+        .catch(function (err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Creates a new chatRoom in the DB.
+exports.create = function (req, res) {
+  // if (req.body.to) {
+  //
+  //   var token;
+  //   var participants = {
+  //     users: {
+  //       ids: []
+  //     },
+  //     visitor: {}
+  //   };
+  //
+  //   switch (req.body.type) {
+  //     case 'internal':
+  //
+  //       if (!req.body.to.isArray) {
+  //         req.body.to = [parseInt(req.body.to, 10)];
+  //       }
+  //
+  //       participants.users.ids = req.body.to;
+  //
+  //       if (!_.contains(req.body.to, req.user.id)) {
+  //         participants.users.ids.push(req.user.id);
+  //       }
+  //
+  //       participants.users.ids.sort();
+  //
+  //       token = md5(JSON.stringify(participants)).toString(
+  //         'base64');
+  //
+  //       participants.users.fullnames = [];
+  //
+  //       User
+  //         .findAll({
+  //           where: {
+  //             id: {
+  //               $in: req.body.to
+  //             }
+  //           },
+  //           attributes: ['id', 'fullname']
+  //         })
+  //         .then(function(users) {
+  //
+  //           _.sortBy(users, 'id').forEach(function(item, index) {
+  //             participants.users.fullnames.push(item.fullname);
+  //           });
+  //
+  //           ChatRoom
+  //             .findOrCreate({
+  //               where: {
+  //                 token: token
+  //               },
+  //               defaults: _.merge({
+  //                 token: token,
+  //                 participants: JSON.stringify(participants)
+  //               }, req.body),
+  //               include: [{
+  //                 model: ChatMessage
+  //               }]
+  //             })
+  //             .spread(function(chatRoom, created) {
+  //
+  //               if (created) {
+  //                 chatRoom
+  //                   .setUsers(participants.users.ids)
+  //                   .then(function() {
+  //                     return res.status(201).send(chatRoom);
+  //                   })
+  //                   .catch(function(err) {
+  //                     return handleError(res, err);
+  //                   });
+  //               } else {
+  //                 return res.status(201).send(chatRoom);
+  //               }
+  //
+  //             })
+  //             .catch(function(err) {
+  //               return handleError(res, err);
+  //             });
+  //         })
+  //         .catch(function(err) {
+  //           return handleError(res, err);
+  //         });
+  //
+  //       break;
+  //     case 'external':
+  //
+  //       participants.visitor.id = req.body.to;
+  //       participants.users.ids.push(req.user.id);
+  //       participants.users.ids.sort();
+  //
+  //       token = md5(JSON.stringify(participants)).toString('base64');
+  //
+  //       participants.users.fullnames = [req.user.fullname];
+  //
+  //       ChatVisitor
+  //         .findById(req.body.to, {
+  //           attributes: ['id', 'fullname', 'email', 'referer']
+  //         })
+  //         .then(function(chatVisitor) {
+  //           participants.visitor.fullname = chatVisitor.fullname;
+  //           participants.visitor.referer = chatVisitor.referer;
+  //           participants.visitor.email = chatVisitor.email;
+  //
+  //           ChatRoom
+  //             .findOrCreate({
+  //               where: {
+  //                 token: token
+  //               },
+  //               defaults: _.merge({
+  //                 token: token,
+  //                 participants: JSON.stringify(participants)
+  //               }, req.body),
+  //               include: [{
+  //                 model: ChatMessage
+  //               }, {
+  //                 model: ChatVisitor
+  //               }, {
+  //                 model: User,
+  //               }]
+  //             })
+  //             .spread(function(chatRoom, created) {
+  //               if (created) {
+  //
+  //                 chatRoom
+  //                   .setUsers(participants.users.ids, {
+  //                     individualHooks: true
+  //                   })
+  //                   .then(function() {
+  //                     chatRoom
+  //                       .setChatVisitor(participants.visitor.id)
+  //                       .then(function() {
+  //                         return res.status(201).send(chatRoom);
+  //                       })
+  //                       .catch(function(err) {
+  //                         return handleError(res, err);
+  //                       });
+  //                   })
+  //                   .catch(function(err) {
+  //                     return handleError(res, err);
+  //                   });
+  //               } else {
+  //                 return res.status(201).send(chatRoom);
+  //               }
+  //
+  //             })
+  //             .catch(function(err) {
+  //               return handleError(res, err);
+  //             });
+  //         })
+  //         .catch(function(err) {
+  //           return handleError(res, err);
+  //         });
+  //
+  //       break;
+  //     default:
+  //   }
+  // } else {
+  //   return handleError(res, new Error('field "to" omitted'));
+  // }
+};
+
+// Updates an existing chatRoom in the DB.
+exports.update = function (req, res) {
+  if (req.body.id) {
+    delete req.body.id;
+  }
+
+  ChatRoom
+    .findById(req.params.id)
+    .then(function (chatRoom) {
+      if (!chatRoom) {
+        return res.sendStatus(404);
+      }
+      var updated = _.merge(chatRoom, req.body);
+      updated
+        .save()
+        .then(function () {
+          return res.status(200).send(chatRoom);
+        })
+        .catch(function (err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Updates an existing chatRoom in the DB.
+exports.updateUsers = function (req, res) {
+  var _chatRoom;
+
+  if (req.body.id) {
+    delete req.body.id;
+  }
+
+  return ChatRoom
+    .findById(req.params.id)
+    .then(function (chatRoom) {
+      if (!chatRoom || !req.body.users) {
+        return res.sendStatus(404);
+      }
+      return chatRoom;
+    })
+    .then(function (chatRoom) {
+      return chatRoom
+        .update({
+          token: md5(JSON.stringify(req.body.users.sort())).toString('base64')
+        });
+    })
+    .then(function (chatRoom) {
+      _chatRoom = chatRoom;
+      return chatRoom
+        .setUsers(req.body.users.sort(), {
+          individualHooks: true
+        });
+    })
+    .then(function () {
+      return _chatRoom
+        .getUsers();
+    })
+    .then(function (users) {
+      return res.status(200).send(_.merge(_chatRoom.dataValues, {
+        Users: users,
+        ChatMessages: []
+      }));
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+// Deletes a chatRoom from the DB.
+exports.destroy = function (req, res) {
+  ChatRoom
+    .findById(req.params.id)
+    .then(function (chatRoom) {
+      if (!chatRoom) {
+        return res.sendStatus(404);
+      }
+
+      chatRoom
+        .destroy()
+        .then(function () {
+          return res.sendStatus(204);
+        })
+        .catch(function (err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function (err) {
+      return handleError(res, err);
+    });
+};
+
+function handleError(res, err) {
+  return res.status(500).send(err);
+}