Built motion from commit 57474b6.|0.0.99
[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         return this.getDataValue('tags') ? this.getDataValue('tags').split(';') : [];
23       },
24       set: function(val) {
25         this.setDataValue('tags', val.join(';'));
26       }
27     }
28   }, {
29     tableName: 'fax_rooms',
30     paranoid: true,
31     associate: function(models) {
32       // hasMany relations
33       FaxRoom.hasMany(models.FaxMessage);
34       FaxRoom.belongsTo(models.FaxAccount);
35       FaxRoom.belongsToMany(models.User, {
36         through: 'user_has_fax_rooms'
37       });
38       // SCOPES
39       FaxRoom.addScope('default', {
40         order: [
41           ['createdAt', 'DESC']
42         ],
43         include: [{
44           model: models.FaxAccount
45         }, {
46           model: models.FaxMessage,
47           include: [{
48             model: models.FaxAttachment
49           }, {
50             model: models.User,
51             attributes: ['id', 'name', 'fullname', 'email']
52           }]
53         }, {
54           model: models.User,
55           attributes: ['id', 'name', 'fullname', 'email']
56         }]
57       });
58     }
59   });
60
61   return FaxRoom;
62 };