47c9a5fed50dc56426097711b90114cc2314fc97
[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     openReason: DataTypes.STRING,
69     arrivedAt: {
70       type: DataTypes.DATE
71     },
72     closedAt: {
73       type: DataTypes.DATE
74     }
75   }, {
76     tableName: 'fax_rooms',
77     paranoid: true,
78     associate: function(models) {
79       // hasMany relations
80       FaxRoom.hasMany(models.FaxMessage);
81       FaxRoom.hasMany(models.FaxRoomStatus, {
82         plural: 'MailRoomStatuses'
83       });
84       FaxRoom.belongsTo(models.FaxAccount);
85       FaxRoom.belongsToMany(models.User, {
86         through: 'user_has_fax_rooms'
87       });
88       // SCOPES
89       FaxRoom.addScope('default', {
90         order: [
91           ['createdAt', 'DESC']
92         ],
93         include: [{
94           model: models.FaxAccount
95         }, {
96           model: models.FaxMessage,
97           include: [{
98             model: models.FaxAttachment
99           }, {
100             model: models.User,
101             attributes: ['id', 'name', 'fullname', 'email']
102           }]
103         }, {
104           model: models.User,
105           attributes: ['id', 'name', 'fullname', 'email']
106         }]
107       });
108     }
109   });
110
111   return FaxRoom;
112 };