Built motion from commit 1038d87.|0.0.141
[motion.git] / server / models / chat_room.js
1 'use strict';
2
3 module.exports = function(sequelize, DataTypes) {
4
5         var ChatRoom = sequelize.define('ChatRoom', {
6                 type: DataTypes.ENUM('internal', 'external', 'group'),
7                 token: {
8                         type: DataTypes.STRING,
9                         unique: 'token'
10                 },
11                 status: {
12                         type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'ABANDON', 'UNMANAGED'),
13                         defaultValue: 'NEW'
14                 },
15                 rating: DataTypes.ENUM('good', 'bad'),
16                 ratingMessage: DataTypes.TEXT,
17                 completeReason: DataTypes.ENUM('agent', 'requester'),
18                 ChatVisitorId: {
19                         type: DataTypes.INTEGER,
20                         unique: 'token'
21                 },
22                 waiting: {
23                         type: DataTypes.BOOLEAN,
24                         defaultValue: false
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                 openReason: DataTypes.STRING,
46                 contact: DataTypes.STRING,
47                 website: DataTypes.STRING
48         }, {
49                 tableName: 'chat_rooms',
50                 associate: function(models) {
51                         ChatRoom.hasMany(models.ChatMessage);
52                         ChatRoom.belongsTo(models.ChatVisitor);
53                         ChatRoom.belongsTo(models.ChatWebsite);
54                         ChatRoom.belongsToMany(models.User, {
55                                 through: models.UserHasChatRoom
56                         });
57                         ChatRoom.addScope('online', {
58                                 where: {
59                                         $and: [{
60                                                 type: 'external'
61                                         }, {
62                                                 $or: [{
63                                                         status: 'CLOSED'
64                                                 }, {
65                                                         status: 'OPEN'
66                                                 }]
67                                         }]
68                                 }
69                         });
70                 }
71         });
72
73         return ChatRoom;
74 };