Built motion from commit 1038d87.|0.0.141
[motion.git] / server / models / fax_room.js
1 /**
2  * Fax Room Model
3  */
4
5 const moment = require('moment');
6
7 module.exports = function(sequelize, DataTypes) {
8
9   var FaxRoom = sequelize.define('FaxRoom', {
10     contact: DataTypes.STRING,
11     account: DataTypes.STRING,
12     faxIn: {
13       type: DataTypes.INTEGER,
14       defaultValue: 0
15     },
16     faxOut: {
17       type: DataTypes.INTEGER,
18       defaultValue: 0
19     },
20     status: {
21       type: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'CLOSED'),
22       defaultValue: 'NEW',
23       set: function(status) {
24         this.setDataValue('status', status);
25
26         switch (status) {
27           case 'NEW':
28             this.setDataValue('arrivedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
29             break;
30           case 'CLOSED':
31             this.setDataValue('closedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
32             break;
33           default:
34
35         }
36       }
37     },
38     lastEvent: {
39       type: DataTypes.ENUM('SENT', 'SENDING', 'RECEIVED', 'FAILED', 'NOTE')
40     },
41     lastEventAt: {
42       type: DataTypes.DATE
43     },
44     tags: {
45       type: DataTypes.TEXT,
46       get: function() {
47         var tags;
48         if (this.getDataValue('tags')) {
49           tags = this.getDataValue('tags').split(';');
50           tags.pop();
51         } else {
52           tags = [];
53         }
54         return tags;
55       },
56       set: function(val) {
57         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
58       }
59     },
60     waiting: {
61       type: DataTypes.BOOLEAN,
62       defaultValue: false
63     },
64     disposition: {
65       type: DataTypes.STRING
66     },
67     openReason: DataTypes.STRING,
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 };