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