b8e6f18e8fa1e53065670ad5d843ae47678ac3be
[motion.git] / server / models / sms_room.js
1 'use strict';
2
3 module.exports = function(sequelize, DataTypes) {
4   var SmsRoom = sequelize.define('SmsRoom', {
5     from: {
6       type: DataTypes.STRING,
7       validate: {
8         is: /^[0-9]+$/
9       }
10     },
11     status: {
12       type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'UNMANAGED', 'CSQUARE'),
13       defaultValue: 'NEW'
14     },
15     tags: {
16       type: DataTypes.TEXT,
17       get: function() {
18         return this.getDataValue('tags') ? this.getDataValue('tags').split(';') : [];
19       },
20       set: function(val) {
21         this.setDataValue('tags', val.join(';'));
22       }
23     }
24   }, {
25     tableName: 'sms_rooms',
26     associate: function(models) {
27       // BINDING
28       SmsRoom.hasMany(models.SmsMessage);
29       SmsRoom.belongsToMany(models.User, {
30         through: 'user_has_sms_rooms'
31       });
32       SmsRoom.belongsTo(models.SmsAccount);
33       SmsRoom.addScope('default', {
34         order: [
35           ['createdAt', 'DESC']
36         ],
37         include: [{
38           model: models.SmsAccount
39         }, {
40           model: models.SmsMessage,
41           include: [{
42             model: models.User,
43             attributes: ['id', 'name', 'fullname', 'email']
44           }]
45         }, {
46           model: models.User,
47           attributes: ['id', 'name', 'fullname', 'email']
48         }]
49       });
50       SmsRoom.addScope('agent', function(id) {
51         return {
52           include: [{
53             model: models.User,
54             where: {
55               id: id
56             }
57           }]
58         }
59       });
60     }
61
62
63   });
64   return SmsRoom;
65 };