Built motion from commit b7ec679.|0.0.83
[motion.git] / server / models / chat_room.js
1 /**
2  * Chat Website Model
3  */
4
5 var crypto = require('crypto');
6
7 module.exports = function(sequelize, DataTypes) {
8
9         var ChatRoom = sequelize.define('ChatRoom', {
10                 type: DataTypes.ENUM('internal', 'external'),
11                 token: {
12                         type: DataTypes.STRING,
13                         unique: 'token'
14                 },
15                 status: {
16                         type: DataTypes.ENUM('OPEN', 'CLOSED', 'ABANDON', 'UNMANAGED'),
17                         defaultValue: 'OPEN'
18                 },
19                 rating: DataTypes.ENUM('good', 'bad'),
20                 ratingMessage: DataTypes.TEXT,
21                 completeReason: DataTypes.ENUM('agent', 'requester'),
22                 ChatVisitorId: {
23                         type: DataTypes.INTEGER,
24                         unique: 'token'
25                 }
26         }, {
27                 tableName: 'chat_rooms',
28                 associate: function(models) {
29                         ChatRoom.hasMany(models.ChatMessage);
30                         ChatRoom.belongsTo(models.ChatVisitor);
31                         ChatRoom.belongsTo(models.ChatWebsite);
32                         ChatRoom.belongsToMany(models.User, {
33                                 through: models.UserHasChatRoom
34                         });
35                         ChatRoom.addScope('online', {
36                                 where: {
37                                         $and: [{
38                                                 type: 'external'
39                                         }, {
40                                                 $or: [{
41                                                         status: 'CLOSED'
42                                                 }, {
43                                                         status: 'OPEN'
44                                                 }]
45                                         }]
46                                 }
47                         });
48                 }
49         });
50
51         return ChatRoom;
52 };