Built motion from commit 8dae768.|0.0.121
[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                 openReason: DataTypes.STRING
50         }, {
51                 tableName: 'chat_rooms',
52                 associate: function(models) {
53                         ChatRoom.hasMany(models.ChatMessage);
54                         ChatRoom.belongsTo(models.ChatVisitor);
55                         ChatRoom.belongsTo(models.ChatWebsite);
56                         ChatRoom.belongsToMany(models.User, {
57                                 through: models.UserHasChatRoom
58                         });
59                         ChatRoom.addScope('online', {
60                                 where: {
61                                         $and: [{
62                                                 type: 'external'
63                                         }, {
64                                                 $or: [{
65                                                         status: 'CLOSED'
66                                                 }, {
67                                                         status: 'OPEN'
68                                                 }]
69                                         }]
70                                 }
71                         });
72                 }
73         });
74
75         return ChatRoom;
76 };