Built motion from commit 1020cd7.|0.0.107
[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     disposition: {
36       type: DataTypes.STRING
37     }
38   }, {
39     tableName: 'fax_rooms',
40     paranoid: true,
41     associate: function(models) {
42       // hasMany relations
43       FaxRoom.hasMany(models.FaxMessage);
44       FaxRoom.belongsTo(models.FaxAccount);
45       FaxRoom.belongsToMany(models.User, {
46         through: 'user_has_fax_rooms'
47       });
48       // SCOPES
49       FaxRoom.addScope('default', {
50         order: [
51           ['createdAt', 'DESC']
52         ],
53         include: [{
54           model: models.FaxAccount
55         }, {
56           model: models.FaxMessage,
57           include: [{
58             model: models.FaxAttachment
59           }, {
60             model: models.User,
61             attributes: ['id', 'name', 'fullname', 'email']
62           }]
63         }, {
64           model: models.User,
65           attributes: ['id', 'name', 'fullname', 'email']
66         }]
67       });
68     }
69   });
70
71   return FaxRoom;
72 };