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