bb9f3e0c1e8918a64758bdfff72dc0f5cd6d0800
[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', 'group'),
11                 token: {
12                         type: DataTypes.STRING,
13                         unique: 'token'
14                 },
15                 status: {
16                         type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'ABANDON', 'UNMANAGED'),
17                         defaultValue: 'NEW'
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                 waiting: {
27                         type: DataTypes.BOOLEAN,
28                         defaultValue: false
29                 },
30                 tags: {
31                         type: DataTypes.TEXT,
32                         get: function() {
33                                 var tags;
34                                 if (this.getDataValue('tags')) {
35                                         tags = this.getDataValue('tags').split(';');
36                                         tags.pop();
37                                 } else {
38                                         tags = [];
39                                 }
40                                 return tags;
41                         },
42                         set: function(val) {
43                                 this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
44                         }
45                 },
46                 disposition: {
47                         type: DataTypes.STRING
48                 },
49         }, {
50                 tableName: 'chat_rooms',
51                 associate: function(models) {
52                         ChatRoom.hasMany(models.ChatMessage);
53                         ChatRoom.belongsTo(models.ChatVisitor);
54                         ChatRoom.belongsTo(models.ChatWebsite);
55                         ChatRoom.belongsToMany(models.User, {
56                                 through: models.UserHasChatRoom
57                         });
58                         ChatRoom.addScope('online', {
59                                 where: {
60                                         $and: [{
61                                                 type: 'external'
62                                         }, {
63                                                 $or: [{
64                                                         status: 'CLOSED'
65                                                 }, {
66                                                         status: 'OPEN'
67                                                 }]
68                                         }]
69                                 }
70                         });
71                 }
72         });
73
74         return ChatRoom;
75 };