Built motion from commit 57474b6.|0.0.99
[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                 tags: {
27                         type: DataTypes.TEXT,
28                         get: function() {
29                                 return this.getDataValue('tags') ? this.getDataValue('tags').split(';').pop() : [];
30                         },
31                         set: function(val) {
32                                 this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
33                         }
34                 },
35                 disposition: {
36                         type: DataTypes.STRING
37                 },
38         }, {
39                 tableName: 'chat_rooms',
40                 associate: function(models) {
41                         ChatRoom.hasMany(models.ChatMessage);
42                         ChatRoom.belongsTo(models.ChatVisitor);
43                         ChatRoom.belongsTo(models.ChatWebsite);
44                         ChatRoom.belongsToMany(models.User, {
45                                 through: models.UserHasChatRoom
46                         });
47                         ChatRoom.addScope('online', {
48                                 where: {
49                                         $and: [{
50                                                 type: 'external'
51                                         }, {
52                                                 $or: [{
53                                                         status: 'CLOSED'
54                                                 }, {
55                                                         status: 'OPEN'
56                                                 }]
57                                         }]
58                                 }
59                         });
60                 }
61         });
62
63         return ChatRoom;
64 };