251443814ff3900b632573bb4da73cd73e17a941
[motion.git] / server / models / fax_room.js
1 /**
2  * Fax Room Model
3  */
4
5 var crypto = require('crypto');
6
7 module.exports = function(sequelize, DataTypes) {
8
9   var FaxRoom = sequelize.define('FaxRoom', {
10     from: DataTypes.STRING,
11     account: DataTypes.STRING,
12     status: {
13       type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'UNMANAGED'),
14       defaultValue: 'NEW'
15     },
16     lastEvent: {
17       type: DataTypes.ENUM('SENT', 'SENDING', 'RECEIVED', 'FAILED', 'NOTE')
18     },
19     tags: {
20       type: DataTypes.TEXT,
21       get: function() {
22         var tags;
23         if (this.getDataValue('tags')) {
24           tags = this.getDataValue('tags').split(';');
25           tags.pop();
26         } else {
27           tags = [];
28         }
29         return tags;
30       },
31       set: function(val) {
32         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
33       }
34     },
35     waiting: {
36       type: DataTypes.BOOLEAN,
37       defaultValue: false
38     },
39     disposition: {
40       type: DataTypes.STRING
41     }
42   }, {
43     tableName: 'fax_rooms',
44     paranoid: true,
45     associate: function(models) {
46       // hasMany relations
47       FaxRoom.hasMany(models.FaxMessage);
48       FaxRoom.belongsTo(models.FaxAccount);
49       FaxRoom.belongsToMany(models.User, {
50         through: 'user_has_fax_rooms'
51       });
52       // SCOPES
53       FaxRoom.addScope('default', {
54         order: [
55           ['createdAt', 'DESC']
56         ],
57         include: [{
58           model: models.FaxAccount
59         }, {
60           model: models.FaxMessage,
61           include: [{
62             model: models.FaxAttachment
63           }, {
64             model: models.User,
65             attributes: ['id', 'name', 'fullname', 'email']
66           }]
67         }, {
68           model: models.User,
69           attributes: ['id', 'name', 'fullname', 'email']
70         }]
71       });
72     }
73   });
74
75   return FaxRoom;
76 };