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