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