Built motion from commit 3594e56.|0.0.120
[motion.git] / server / models / fax_room.js
1 /**
2  * Fax Room Model
3  */
4
5 const crypto = require('crypto');
6 const moment = require('moment');
7
8 module.exports = function(sequelize, DataTypes) {
9
10   var FaxRoom = sequelize.define('FaxRoom', {
11     from: DataTypes.STRING,
12     account: DataTypes.STRING,
13     faxIn: {
14       type: DataTypes.INTEGER,
15       defaultValue: 0
16     },
17     faxOut: {
18       type: DataTypes.INTEGER,
19       defaultValue: 0
20     },
21     status: {
22       type: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'CLOSED'),
23       defaultValue: 'NEW',
24       set: function(status) {
25         this.setDataValue('status', status);
26
27         switch (status) {
28           case 'NEW':
29             this.setDataValue('arrivedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
30             break;
31           case 'CLOSED':
32             this.setDataValue('closedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
33             break;
34           default:
35
36         }
37       }
38     },
39     lastEvent: {
40       type: DataTypes.ENUM('SENT', 'SENDING', 'RECEIVED', 'FAILED', 'NOTE')
41     },
42     lastEventAt: {
43       type: DataTypes.DATE
44     },
45     tags: {
46       type: DataTypes.TEXT,
47       get: function() {
48         var tags;
49         if (this.getDataValue('tags')) {
50           tags = this.getDataValue('tags').split(';');
51           tags.pop();
52         } else {
53           tags = [];
54         }
55         return tags;
56       },
57       set: function(val) {
58         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
59       }
60     },
61     waiting: {
62       type: DataTypes.BOOLEAN,
63       defaultValue: false
64     },
65     disposition: {
66       type: DataTypes.STRING
67     },
68     arrivedAt: {
69       type: DataTypes.DATE
70     },
71     closedAt: {
72       type: DataTypes.DATE
73     }
74   }, {
75     tableName: 'fax_rooms',
76     paranoid: true,
77     associate: function(models) {
78       // hasMany relations
79       FaxRoom.hasMany(models.FaxMessage);
80       FaxRoom.hasMany(models.FaxRoomStatus, {
81         plural: 'MailRoomStatuses'
82       });
83       FaxRoom.belongsTo(models.FaxAccount);
84       FaxRoom.belongsToMany(models.User, {
85         through: 'user_has_fax_rooms'
86       });
87       // SCOPES
88       FaxRoom.addScope('default', {
89         order: [
90           ['createdAt', 'DESC']
91         ],
92         include: [{
93           model: models.FaxAccount
94         }, {
95           model: models.FaxMessage,
96           include: [{
97             model: models.FaxAttachment
98           }, {
99             model: models.User,
100             attributes: ['id', 'name', 'fullname', 'email']
101           }]
102         }, {
103           model: models.User,
104           attributes: ['id', 'name', 'fullname', 'email']
105         }]
106       });
107     }
108   });
109
110   return FaxRoom;
111 };