Built motion from commit 82438f7.|0.0.115
[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'),
13       defaultValue: 'NEW'
14     },
15     tags: {
16       type: DataTypes.TEXT,
17       get: function() {
18         var tags;
19         if (this.getDataValue('tags')) {
20           tags = this.getDataValue('tags').split(';');
21           tags.pop();
22         } else {
23           tags = [];
24         }
25         return tags;
26       },
27       set: function(val) {
28         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
29       }
30     },
31     waiting: {
32       type: DataTypes.BOOLEAN,
33       defaultValue: false
34     },
35     disposition: {
36       type: DataTypes.STRING
37     },
38     ParentId: {
39       type: DataTypes.INTEGER
40     },
41     voiceSource: {
42       type: DataTypes.BOOLEAN,
43       defaultValue: false
44     }
45   }, {
46     tableName: 'sms_rooms',
47     associate: function(models) {
48       // BINDING
49       SmsRoom.hasMany(models.SmsMessage);
50       SmsRoom.belongsToMany(models.User, {
51         through: 'user_has_sms_rooms'
52       });
53       SmsRoom.belongsTo(models.SmsAccount);
54       SmsRoom.addScope('default', {
55         order: [
56           ['createdAt', 'DESC']
57         ],
58         include: [{
59           model: models.SmsAccount
60         }, {
61           model: models.SmsMessage,
62           include: [{
63             model: models.User,
64             attributes: ['id', 'name', 'fullname', 'email']
65           }]
66         }, {
67           model: models.User,
68           attributes: ['id', 'name', 'fullname', 'email']
69         }]
70       });
71       SmsRoom.addScope('agent', function(id) {
72         return {
73           include: [{
74             model: models.User,
75             where: {
76               id: id
77             }
78           }]
79         }
80       });
81     }
82
83
84   });
85   return SmsRoom;
86 };