Built motion from commit b322525.|0.0.112
[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     disposition: {
32       type: DataTypes.STRING
33     },
34     ParentId: {
35       type: DataTypes.INTEGER
36     },
37     source: DataTypes.STRING
38   }, {
39     tableName: 'sms_rooms',
40     associate: function(models) {
41       // BINDING
42       SmsRoom.hasMany(models.SmsMessage);
43       SmsRoom.belongsToMany(models.User, {
44         through: 'user_has_sms_rooms'
45       });
46       SmsRoom.belongsTo(models.SmsAccount);
47       SmsRoom.addScope('default', {
48         order: [
49           ['createdAt', 'DESC']
50         ],
51         include: [{
52           model: models.SmsAccount
53         }, {
54           model: models.SmsMessage,
55           include: [{
56             model: models.User,
57             attributes: ['id', 'name', 'fullname', 'email']
58           }]
59         }, {
60           model: models.User,
61           attributes: ['id', 'name', 'fullname', 'email']
62         }]
63       });
64       SmsRoom.addScope('agent', function(id) {
65         return {
66           include: [{
67             model: models.User,
68             where: {
69               id: id
70             }
71           }]
72         }
73       });
74     }
75
76
77   });
78   return SmsRoom;
79 };